TorchLean API

NN.Tensor.Internal.Check.Pack

Concrete pack and unpack checking #

The packing pattern has a fixed leading region, one *, and a fixed trailing region. Inputs may have different ranks because * captures an arbitrary middle shape. A checked plan records the common fixed shapes and proves that every component decomposes as

leadingShape ++ starShape ++ trailingShape.

The same CheckedPack certificate serves both directions. checkPack rejects an empty input family and obtains the fixed shapes from its first tensor. checkUnpack obtains them from the packed tensor, resolves requested star shapes, and therefore also represents the strict empty-metadata case when the packed axis has length zero.

Requested unpack dimensions are integers so -1 can denote one inferred dimension. Every other dimension must be nonnegative. Resolution rejects multiple inferred dimensions, non-divisible residual lengths, underdetermined zero products, and metadata whose segment lengths do not exactly partition the packed axis.

The number of fixed axes surrounding * in a packing pattern.

Instances For

    The middle shape captured by * in one component.

    Checked components have enough dimensions for both fixed regions. The subtraction therefore removes exactly those regions.

    Instances For

      A pack/unpack shape plan with exactly the invariants used by semantics.

      leadingShape and trailingShape are explicit because strict unpacking may produce an empty component family, from which the fixed dimensions cannot be recovered. Component star shapes, segment lengths, metadata, offsets, and the packed output shape remain deterministic derived values.

      Instances For

        The middle shape represented by one checked component.

        Instances For

          Pack metadata in component order.

          Instances For

            Flattened lengths of all packed middle regions.

            Instances For

              The length of the concatenated packed axis.

              Instances For

                Shape of the packed tensor.

                Instances For
                  @[simp]

                  Pack metadata has one entry for every input component.

                  @[simp]

                  Segment lengths have one entry for every input component.

                  Match each input component with its metadata segment.

                  The two lists have equal length by construction. Naming this equivalence keeps the dependent component-to-segment transport identical in checking, semantics, and lowering.

                  Instances For
                    @[simp]
                    theorem TorchLean.Tensor.Internal.Check.CheckedPack.componentSegmentEquiv_val (checked : CheckedPack) (component : Fin checked.inputShapes.length) :
                    (checked.componentSegmentEquiv component) = component

                    Matching a component with its metadata segment preserves its position in the component list.

                    @[simp]
                    theorem TorchLean.Tensor.Internal.Check.CheckedPack.metadata_get (checked : CheckedPack) (component : Fin checked.metadata.length) :
                    checked.metadata[component] = checked.starShape (Fin.cast component)

                    Metadata at a component index is its checked star shape.

                    @[simp]
                    theorem TorchLean.Tensor.Internal.Check.CheckedPack.segmentLengths_get (checked : CheckedPack) (segment : Fin checked.segmentLengths.length) :
                    checked.segmentLengths[segment] = (checked.starShape (Fin.cast segment)).size

                    A segment length is the row-major size of its metadata shape.

                    @[simp]
                    theorem TorchLean.Tensor.Internal.Check.CheckedPack.segmentLength_eq_star_size (checked : CheckedPack) (component : Fin checked.inputShapes.length) :
                    checked.segmentLengths[component] = (checked.starShape component).size

                    The segment selected by an input component has the row-major size of that component's star region.

                    Looking up the metadata segment selected for a component yields the size of that component's star region.

                    theorem TorchLean.Tensor.Internal.Check.CheckedPack.metadata_eq_ofFn (checked : CheckedPack) :
                    checked.metadata = List.ofFn fun (component : Fin checked.inputShapes.length) => checked.starShape component

                    Pack metadata is exactly the checked star-shape family in input order.

                    This extensional form is useful when clients consume metadata through finite component indices rather than list lookup.

                    theorem TorchLean.Tensor.Internal.Check.CheckedPack.input_size_eq_flattened (checked : CheckedPack) (component : Fin checked.inputShapes.length) :
                    (checked.inputShapes.get component).size = (checked.leadingShape ++ (checked.starShape component).size :: checked.trailingShape).size

                    Flattening the star region into one axis preserves every component's number of scalar entries.

                    theorem TorchLean.Tensor.Internal.Check.CheckedPack.input_size_eq_segment (checked : CheckedPack) (component : Fin checked.inputShapes.length) :
                    (checked.inputShapes.get component).size = (checked.leadingShape ++ checked.segmentLengths.get (checked.componentSegmentEquiv component) :: checked.trailingShape).size

                    Reshaping one input component to its checked segment shape preserves the number of entries.

                    Validate rank and fixed-prefix/suffix agreement for every component in a pack family.

                    Instances For

                      Bundle validated component decompositions into the dependent CheckedPack certificate consumed by semantics and lowering.

                      Instances For

                        Check a nonempty family of tensor shapes for packing.

                        The first component determines fixed leading and trailing dimensions. Every other component must agree at those positions, while its star region may have any finite shape, including an empty shape or zero dimensions.

                        Instances For
                          @[reducible, inline]

                          Integer star shapes accepted by the public unpack checker.

                          Instances For

                            Compute one fully specified requested star-shape size.

                            Instances For

                              Multiply the known factors of a requested shape, treating -1 as unknown.

                              Instances For

                                Count inferred -1 dimensions across the complete unpack request.

                                Instances For

                                  Replace the unique inferred dimension by its resolved natural-number value.

                                  Instances For

                                    Find the first unpack dimension smaller than the permitted sentinel -1.

                                    Instances For

                                      Resolve requested unpack metadata against one concrete packed-axis length.

                                      The result contains only natural-number shapes. Success guarantees computationally that their products sum to the packed-axis length; the subsequent checked plan turns that equality into the segment partition used by semantics.

                                      Instances For

                                        Check strict unpack metadata against a concrete packed tensor shape.

                                        Unlike the reference implementation's backend-deferred failures, every segment bound and reshape size is validated before tensor execution.

                                        Instances For