TorchLean API

NN.Proofs.Autograd.Tape.Algebra.Soundness

Algebraic tape soundness #

Tape-style (SSA/DAG) reverse-mode soundness (algebraic, backend-generic).

This is a backend-generic analogue of the tensor-tape soundness layer: it proves the global reverse-mode accumulation algorithm is sound assuming only commutative semiring laws.

This file lives under NN/Proofs/Autograd/Tape/Algebra/ because it is reused by both proof-only and runtime-link developments that target exact backends (e.g. ℚ).

PyTorch correspondence / citations #

This corresponds to the high-level structure of PyTorch’s reverse-mode engine, but stated over an arbitrary commutative semiring so we can reuse it for exact backends. https://pytorch.org/docs/stable/autograd.html

Dot product over contexts: sum of per-entry tensor dots.

This is the algebraic analogue of Spec.dotList: it uses TensorAlgebra.dot for the backend α.

Instances For

    dotList commutes with casting the left context along a shape-list equality.

    dotList is linear in its right argument with respect to TorchLean.TensorPack.add.

    Dot respects appending: dot of two snoced contexts splits into prefix + last entry.

    Dotting with the all-zero context on the right yields 0.

    Sparse context with a single nonzero entry at idx (all other tensors are 0).

    Instances For

      single is adjoint to getIdx with respect to dotList.

      Informally: ⟪dx, single idx v⟫ = ⟪getIdx dx idx, v⟫.

      theorem Proofs.Autograd.Algebra.Contributions.lookup_single {α : Type} [TorchLean.Storage α] [Zero α] {Γ : List Spec.Shape} {shape : Spec.Shape} (idx : Idx Γ shape) (value : TorchLean.Tensor α shape) :

      A single-parent dense VJP agrees with one exception to the uniform zero reader.

      One parent contribution, including the original zero entries at every other position.

      Instances For

        The original programs of a node after saving its inputs for one execution.

        Instances For

          Saved node programs with an optional certified compact backward implementation.

          Instances For

            Executable node payload (no derivative-correctness proof).

            Δ is an extra non-differentiable environment threaded through evaluation (e.g. parameters, auxiliary data). The VJP returns gradients only for the differentiable context Γ.

            Instances For
              def Proofs.Autograd.Algebra.NodeData.ofLocal {α Δ P : Type} [TorchLean.Storage α] {Γ : List Spec.Shape} {τ : Spec.Shape} (capture : TensorLookup α Γ → P) (forward : P → Δ → TorchLean.Tensor α τ) (jvp : P → P → Δ → TorchLean.Tensor α τ) (vjp : P → Δ → TorchLean.Tensor α τ → TorchLean.TensorPack α Γ) (validate : P → Δ → Except String Unit := fun (x : P) (x_1 : Δ) => Except.ok ()) :
              NodeData α Δ Γ τ

              Build a node whose runtime preparation captures a fixed collection of tensors. The same scalar programs define the pack interface and the prepared interface, so their agreement is definitional.

              Instances For
                def Proofs.Autograd.Algebra.NodeData.ofLocalCompact {α Δ P : Type} [TorchLean.Storage α] {Γ : List Spec.Shape} {τ : Spec.Shape} (capture : TensorLookup α Γ → P) (forward : P → Δ → TorchLean.Tensor α τ) (jvp : P → P → Δ → TorchLean.Tensor α τ) (vjp : P → Δ → TorchLean.Tensor α τ → Contributions α Γ) (validate : P → Δ → Except String Unit := fun (x : P) (x_1 : Δ) => Except.ok ()) :
                NodeData α Δ Γ τ

                Local scalar programs with a compact VJP whose dense adapter remains the public VJP.

                Instances For
                  def Proofs.Autograd.Algebra.NodeData.prepare {α Δ : Type} [TorchLean.Storage α] {Γ : List Spec.Shape} {τ : Spec.Shape} (node : NodeData α Δ Γ τ) (ctx : TensorContext α Γ) (data : Δ) :
                  PreparedNode α Γ τ

                  Prepare a node from array storage, using the pack adapter for arbitrary custom closures.

                  Instances For
                    @[simp]
                    theorem Proofs.Autograd.Algebra.NodeData.prepare_ofPack {α Δ : Type} [TorchLean.Storage α] {Γ : List Spec.Shape} {τ : Spec.Shape} (node : NodeData α Δ Γ τ) (ctx : TorchLean.TensorPack α Γ) (data : Δ) :
                    (node.prepare (TensorContext.ofPack ctx) data).toPreparedPrograms = { value := fun (x : Unit) => node.forward ctx data, vjp := node.vjp ctx data, validate := fun (x : Unit) => node.validate ctx data }

                    Preparation preserves the original forward, validation, and dense backward programs.

                    @[simp]
                    theorem Proofs.Autograd.Algebra.NodeData.prepare_value_ofPack {α Δ : Type} [TorchLean.Storage α] {Γ : List Spec.Shape} {τ : Spec.Shape} (node : NodeData α Δ Γ τ) (ctx : TorchLean.TensorPack α Γ) (data : Δ) :
                    (node.prepare (TensorContext.ofPack ctx) data).value = fun (x : Unit) => node.forward ctx data
                    @[simp]
                    theorem Proofs.Autograd.Algebra.NodeData.prepare_vjp_ofPack {α Δ : Type} [TorchLean.Storage α] {Γ : List Spec.Shape} {τ : Spec.Shape} (node : NodeData α Δ Γ τ) (ctx : TorchLean.TensorPack α Γ) (data : Δ) :
                    (node.prepare (TensorContext.ofPack ctx) data).vjp = node.vjp ctx data
                    @[simp]
                    theorem Proofs.Autograd.Algebra.NodeData.prepare_validate_ofPack {α Δ : Type} [TorchLean.Storage α] {Γ : List Spec.Shape} {τ : Spec.Shape} (node : NodeData α Δ Γ τ) (ctx : TorchLean.TensorPack α Γ) (data : Δ) :
                    (node.prepare (TensorContext.ofPack ctx) data).validate = fun (x : Unit) => node.validate ctx data

                    Proof-carrying node: NodeData plus the local adjointness law.

                    The field correct is the algebraic version of the standard JVP/VJP inner-product law.

                    Instances For

                      Executable-only graph: a snoc-list of NodeData.

                      Instances For
                        def Proofs.Autograd.Algebra.GraphData.eval {α : Type} [TorchLean.Storage α] {Δ : Type} {Γ ss : List Spec.Shape} (g : GraphData α Δ Γ ss) (x : TorchLean.TensorPack α Γ) (d : Δ) :

                        Evaluate a GraphData on an input context x, producing the full context Γ ++ ss.

                        Instances For
                          def Proofs.Autograd.Algebra.GraphData.evalArray {α : Type} [TorchLean.Storage α] {Δ : Type} {Γ ss : List Spec.Shape} (g : GraphData α Δ Γ ss) (x : TorchLean.TensorPack α Γ) (d : Δ) :
                          TensorContext α (Γ ++ ss)

                          Evaluate using indexed storage and save only each node's local input tensors.

                          Instances For
                            theorem Proofs.Autograd.Algebra.GraphData.evalArray_eq {α : Type} [TorchLean.Storage α] {Δ : Type} {Γ ss : List Spec.Shape} (g : GraphData α Δ Γ ss) (x : TorchLean.TensorPack α Γ) (d : Δ) :

                            Indexed forward execution computes the original full typed context.

                            def Proofs.Autograd.Algebra.GraphData.evalWithArray {α : Type} [TorchLean.Storage α] {Δ : Type} {Γ ss : List Spec.Shape} (g : GraphData α Δ Γ ss) (x : TorchLean.TensorPack α Γ) (d : Δ) :

                            Public typed forward result, decoded once after array execution.

                            Instances For
                              @[csimp]

                              Compile the public evaluator to the proved array execution path.

                              def Proofs.Autograd.Algebra.GraphData.jvpCtx {α : Type} [TorchLean.Storage α] {Δ : Type} {Γ ss : List Spec.Shape} (g : GraphData α Δ Γ ss) (x dx : TorchLean.TensorPack α Γ) (d : Δ) :

                              Compute the JVP of eval, producing a tangent context of shape Γ ++ ss.

                              Instances For
                                def Proofs.Autograd.Algebra.GraphData.backpropCtx {α : Type} [TorchLean.Storage α] {Δ : Type} {Γ : List Spec.Shape} [Add α] {ss : List Spec.Shape} (g : GraphData α Δ Γ ss) (x : TorchLean.TensorPack α Γ) (d : Δ) (seed : TorchLean.TensorPack α (Γ ++ ss)) :

                                Reverse-mode accumulation on contexts (VJP), given a seed cotangent for Γ ++ ss.

                                Instances For

                                  A proof-carrying tape/SSA graph.

                                  Nodes are appended in topological order and may reference any previously computed value (fan-out and sharing are allowed). This mirrors the structure of PyTorch’s dynamic autograd graph, but with shape-typed contexts.

                                  Instances For
                                    def Proofs.Autograd.Algebra.Graph.toData {α : Type} [TorchLean.Storage α] [CommSemiring α] {Δ : Type} {Γ ss : List Spec.Shape} :
                                    Graph Δ Γ ss → GraphData α Δ Γ ss

                                    Forget local correctness proofs, yielding an executable GraphData.

                                    Instances For
                                      def Proofs.Autograd.Algebra.Graph.eval {α : Type} [TorchLean.Storage α] [CommSemiring α] {Δ : Type} {Γ ss : List Spec.Shape} (g : Graph Δ Γ ss) (x : TorchLean.TensorPack α Γ) (d : Δ) :

                                      Evaluate a proof-carrying graph through its executable representation.

                                      Instances For
                                        def Proofs.Autograd.Algebra.Graph.jvpCtx {α : Type} [TorchLean.Storage α] [CommSemiring α] {Δ : Type} {Γ ss : List Spec.Shape} (g : Graph Δ Γ ss) (x dx : TorchLean.TensorPack α Γ) (d : Δ) :

                                        Propagate tangents with the same evaluator used by the executable graph.

                                        Instances For
                                          def Proofs.Autograd.Algebra.Graph.backpropCtx {α : Type} [TorchLean.Storage α] [CommSemiring α] {Δ : Type} {Γ ss : List Spec.Shape} (g : Graph Δ Γ ss) (x : TorchLean.TensorPack α Γ) (d : Δ) (seed : TorchLean.TensorPack α (Γ ++ ss)) :

                                          Accumulate cotangents with the executable graph's reverse pass.

                                          Instances For
                                            theorem Proofs.Autograd.Algebra.Graph.backprop_correct {α : Type} [TorchLean.Storage α] [CommSemiring α] {Δ : Type} {Γ ss : List Spec.Shape} (g : Graph Δ Γ ss) (x dx : TorchLean.TensorPack α Γ) (d : Δ) (seed : TorchLean.TensorPack α (Γ ++ ss)) :
                                            TensorPack.dotList (g.jvpCtx x dx d) seed = TensorPack.dotList dx (g.backpropCtx x d seed)

                                            Global tape soundness (algebraic form).

                                            Assuming each node satisfies its local adjointness law, backpropCtx is the adjoint of jvpCtx with respect to TensorPack.dotList.