TorchLean API

NN.Tensor.Internal.Representation.Shape

Static tensor shapes and coordinates #

This module gives the finite geometry used by every TorchLean.Tensor.Internal denotation. A shape is an outermost-first list of natural-number dimensions. Its coordinates are the corresponding iterated product of finite types:

Coord [d0, ..., dn] = Fin d0 x ... x Fin dn x PUnit.

The empty shape therefore has one coordinate, while a shape containing a zero-length axis has none. Coord.equivFin uses mathlib's finProdFinEquiv, so linear indices follow the conventional row-major order.

The representation is intentionally self-contained. Static shape expressions and their coordinate spaces are the shared foundation for semantics, lowering, and native execution.

References #

The row-major grouping convention follows einops v0.8.2, especially the reshape stages in einops.einops._reconstruct_from_shape_uncached and einops.einops._apply_recipe, pinned at commit 8e911db71f2e693a0c434b041180388c685ed06f.

@[reducible, inline]

A tensor shape, listed from the outermost axis to the innermost axis.

Instances For

    The number of scalar entries in a shape. The empty product is one.

    Instances For

      The number of axes in a shape.

      Instances For
        @[simp]

        The rank-zero shape contains one scalar entry.

        @[simp]

        Prepending an axis multiplies the number of entries by its length.

        @[simp]

        The empty shape has rank zero.

        @[simp]

        Prepending an axis increases the rank by one.

        Shape size is the ordinary product of its dimension list.

        @[simp]

        Concatenating shapes multiplies their numbers of entries.

        A shape has no entries exactly when one of its axes has length zero.

        @[reducible]

        A coordinate of a finite shape.

        Rank-zero coordinates are represented by PUnit. A zero-length dimension contributes Fin 0, making the entire coordinate type empty.

        Instances For
          @[instance_reducible]

          Coordinates of every finite shape have decidable equality.

          @[reducible]

          Coordinates of every finite shape can be enumerated.

          @[simp]

          The coordinate type has exactly as many elements as the shape specifies.

          The row-major equivalence between multidimensional coordinates and flat indices.

          For a nonempty shape, the outer coordinate is the high-order component and the tail coordinate is the low-order component.

          Instances For

            Convert a multidimensional coordinate to its row-major flat index.

            Instances For

              Recover a multidimensional coordinate from a row-major flat index.

              Instances For
                @[simp]

                Unlinearizing and then linearizing recovers the flat index.

                @[simp]

                Linearizing and then unlinearizing recovers the multidimensional coordinate.

                @[simp]

                The recursive linearization step is mathlib's row-major product index.

                theorem TorchLean.Tensor.Internal.Coord.linearize_cons_val {n : ℕ} {s : Shape} (i : Fin n) (j : Coord s) :
                ↑(linearize (i, j)) = ↑j.linearize + s.size * ↑i

                The numerical row-major index of a coordinate is its tail index plus the outer coordinate times the size of the tail shape.

                Horner-form row-major index: the result is acc * size s plus the index of the coordinate.

                Instances For
                  theorem TorchLean.Tensor.Internal.Coord.linearizeAux_eq (s : Shape) (acc : ℕ) (coordinate : Coord s) :
                  linearizeAux s acc coordinate = acc * s.size + ↑coordinate.linearize

                  The Horner accumulator adds acc * size s to the row-major index.

                  Arithmetic row-major index of a coordinate.

                  This is linearize without the equivalence structures that equivFin builds at every level, and it replaces linearize in compiled code.

                  Instances For

                    Arithmetic inverse of the row-major index: divide by the tail size and recurse on the rest.

                    Instances For

                      The arithmetic inverse is a right inverse of linearize.

                      @[csimp]

                      Compiled code uses the arithmetic inverse directly in place of unlinearize.

                      The row-major equivalence built from the arithmetic index maps. Compiled code uses it in place of equivFin, whose trans and prodCongr layers are otherwise rebuilt at every application.

                      Instances For
                        def TorchLean.Tensor.Internal.Coord.broadcast (sourceShape targetShape : Shape) :
                        List.Forall₂ (fun (sourceLength targetLength : ℕ) => sourceLength = targetLength ∨ sourceLength = 1) sourceShape targetShape → Coord targetShape → Coord sourceShape

                        Project a coordinate from a dimensionwise broadcast target to its source.

                        Each source dimension must either equal the corresponding target dimension or be a singleton. Equal dimensions retain the target coordinate; singleton dimensions select their unique coordinate. Recursion on the two shape lists keeps the proof in Prop while constructing coordinate data in Type.

                        Instances For
                          @[simp]
                          theorem TorchLean.Tensor.Internal.Coord.broadcast_self {shape : Shape} (hShape : List.Forall₂ (fun (sourceLength targetLength : ℕ) => sourceLength = targetLength ∨ sourceLength = 1) shape shape) (coordinate : Coord shape) :
                          broadcast shape shape hShape coordinate = coordinate

                          Broadcasting a coordinate between identical shapes leaves every dimension unchanged, independently of the proof used to certify compatibility.

                          A coordinate of a zero-size shape would yield an element of Fin 0.

                          A shape has a coordinate exactly when its size is positive.

                          A shape has no coordinates exactly when its size is zero.