TorchLean API

NN.Proofs.Autograd.Tape.Nodes.Shape

Shape #

Additional analytic (HasFDerivAt) tape nodes for shape permutations.

These nodes are linear/isometric and are useful for models that do explicit reshaping and dimension permutations (e.g. Multi-Head Attention head splitting/combining).

theorem Proofs.Autograd.TapeNodes.ShapeOps.inner_castVec_left {n m : } (h : n = m) (x : Vec n) (y : Vec m) :
inner (castVec h x) y = inner x (castVec y)

Move castVec across the left argument of an inner product.

theorem Proofs.Autograd.TapeNodes.ShapeOps.castVec_proof_irrel {n m : } (h₁ h₂ : n = m) (v : Vec n) :
castVec h₁ v = castVec h₂ v

castVec is proof-irrelevant in its equality argument.

reshape is linear: on vectors it is just a type cast along Spec.Shape.size equality. We implement it as a Node to keep the DAG theorem applicable.

noncomputable def Proofs.Autograd.TapeNodes.ShapeOps.reshape {Γ : List Spec.Shape} {s₁ s₂ : Spec.Shape} (idx : Idx Γ s₁) (h : s₁.size = s₂.size) :
Node Γ s₂

reshape node: reinterpret the same underlying coordinates as a different shape.

This is only definable when Spec.Shape.size s₁ = Spec.Shape.size s₂; at the vector level it is a cast.

PyTorch analogue: view/reshape operations that do not change the total number of elements. https://pytorch.org/docs/stable/tensor_view.html

Instances For
    noncomputable def Proofs.Autograd.TapeNodes.ShapeOps.reshapeFderiv {Γ : List Spec.Shape} {s₁ s₂ : Spec.Shape} (idx : Idx Γ s₁) (h : s₁.size = s₂.size) :

    NodeFDerivCorrect for reshape (it is linear/isometric).

    Instances For

      flatten is a specialization of reshape to the canonical vector shape (.dim (Spec.Shape.size s) .scalar).

      flatten node: specialization of reshape to the canonical vector shape (.dim (Spec.Shape.size s) .scalar).

      PyTorch analogue: flatten when applied to a contiguous tensor. https://pytorch.org/docs/stable/generated/torch.flatten.html

      Instances For

        NodeFDerivCorrect for flatten.

        Instances For
          noncomputable def Proofs.Autograd.TapeNodes.ShapeOps.reindexVec {n m : } (e : Fin n Fin m) :
          Vec nVec m

          Reindex a vector along a Fin equivalence (coordinate permutation/renaming).

          Instances For
            noncomputable def Proofs.Autograd.TapeNodes.ShapeOps.reindexLin {n m : } (e : Fin n Fin m) :

            The linear map induced by reindexVec.

            Instances For

              Move reindexVec across the left argument of an inner product.

              def Proofs.Autograd.TapeNodes.ShapeOps.swapAdjacentEquiv (outer inner tail : ) :
              Fin (outer * (inner * tail)) Fin (inner * (outer * tail))

              Coordinate equivalence induced by exchanging two adjacent blocks.

              Instances For
                def Proofs.Autograd.TapeNodes.ShapeOps.mapOuterEquiv (leading : ) {source target : } (e : Fin source Fin target) :
                Fin (leading * source) Fin (leading * target)

                Lift a coordinate equivalence pointwise under a leading axis.

                Instances For
                  noncomputable def Proofs.Autograd.TapeNodes.ShapeOps.reindex {Γ : List Spec.Shape} {source target : Spec.Shape} (idx : Idx Γ source) (e : Fin source.size Fin target.size) :
                  Node Γ target

                  Reindex a tensor node by any coordinate equivalence. This is the proof-level primitive behind arbitrary axis permutations; its JVP uses the same permutation and its VJP uses the inverse.

                  Instances For
                    noncomputable def Proofs.Autograd.TapeNodes.ShapeOps.reindexFDeriv {Γ : List Spec.Shape} {source target : Spec.Shape} (idx : Idx Γ source) (e : Fin source.size Fin target.size) :

                    NodeFDerivCorrect for an arbitrary coordinate reindexing.

                    Instances For