TorchLean API

NN.Spec.Core.TensorReductionShape.ShapeChange

Shape Changing #

Flatten, unflatten, reshape, and small construction helpers for shape-indexed tensors.

Flatten a tensor into a 1‑D vector (length = Shape.size s).

The order is outermost‑dimension major (row‑major w.r.t. the shape tree). For proofs, the key invariant is that the output length matches Shape.size.

Why this exists: a lot of shape-changing ops are easiest to specify as "flatten, then rebuild", and this is also the bridge we use for some runtime interop where we want a plain sequence of scalars (e.g. importing weights or serializing test vectors).

Instances For

    Unflatten a 1‑D vector back into a tensor of a given shape.

    PyTorch analogy: flat.view(shape) (assuming the element count matches). This is the inverse of flattenSpec up to the ordering convention.

    Instances For

      flattenSpec / unflattenSpec round-trip lemmas #

      These are shape-transport facts: they justify treating flattenSpec/unflattenSpec like reshape/view in PyTorch, provided you keep the element count consistent.

      PyTorch references:

      Important nuance:

      If a shape has Shape.size s = 0, then it contains no scalar leaves (it has a 0-length dimension somewhere). In that case, there is essentially only one possible tensor value of shape s (up to definitional equality), because at the 0-length dimension the indexing function has domain Fin 0.

      We use this as a “vacuity” lemma to avoid needing division/modulo arithmetic when Shape.size s = 0.

      Round-trip unflatten ∘ flatten = id.

      This is the spec-layer analogue of reshape/view round-tripping in PyTorch when the element count matches.

      Round-trip flatten ∘ unflatten = id.

      This is the spec-layer analogue of flattening a reshaped/viewed tensor in PyTorch.

      Convenience corollary: the unflatten ∘ flatten round-trip in the common well-formed regime.

      def Spec.Tensor.reshapeSpec {α : Type} [Inhabited α] {s₁ s₂ : Shape} (t : Tensor α s₁) (h : s₁.size = s₂.size) :
      Tensor α s₂

      Reshape a tensor, given a proof that the number of elements matches.

      Instances For
        def Spec.Tensor.reshapeExplicitSpec {α : Type} [Inhabited α] {s₁ s₂ : Shape} (t : Tensor α s₁) (h : s₁.size = s₂.size) :
        Tensor α s₂

        Reshape with an explicit equality rewrite (sometimes easier for the elaborator).

        Instances For
          def Spec.Tensor.sequenceFin {α : Type} {s : Shape} {n : } (f : Fin nOption (Tensor α s)) :

          Given a partial function Fin n → Option (Tensor α s), build a tensor if all succeed.

          Instances For
            def Spec.Tensor.broadcastFill {α : Type} [Inhabited α] (s : Shape) :
            αTensor α s

            Build a tensor filled with a constant, without using fill (used in broadcasts).

            Instances For