TorchLean API

NN.Proofs.Autograd.Training.StepAlgebra

StepAlgebra #

Pure training-step algebra for proved autograd graphs.

This file stays on the mathematical side of training:

It is not another runtime optimizer implementation. Runtime optimizers update parameter references or device buffers; the pure context equation here states their mathematical target, not a proof that those mutations implement it.

PyTorch correspondence / citations #

Seed cotangent for a scalar-loss graph.

The full context has shape list Γ ++ ss ++ [scalar]: original inputs/parameters, intermediate nodes, and the final scalar loss. Reverse-mode training seeds all non-output cotangents with zero and the scalar-loss output cotangent with 1, matching loss.backward() in PyTorch. The seed depends only on the shape lists Γ and ss, not on the graph or its inputs.

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

    Scalar-loss specialization of Graph.backprop_correct.

    The left side differentiates the graph by a forward-mode perturbation dx and pairs the result with the scalar-loss seed. The right side pairs the same perturbation with the context cotangent computed by reverse mode. This is the exact algebraic statement behind “the gradient returned by backprop is the cotangent for the scalar loss”.

    def Proofs.Autograd.Algebra.SGD.step {α : Type} [CommRing α] {Γ : List Spec.Shape} (params grads : TorchLean.TensorPack α Γ) (lr : α) :

    One pure SGD step over a typed parameter context:

    params := params - lr * grads

    This is the context-level version of the ordinary tensor update p := p - lr * g. Runtime training code may implement the same equation by mutating parameter references, materializing tensors eagerly, or launching CUDA kernels; this definition is the side-effect-free algebraic target those implementations are meant to realize.

    Instances For
      @[simp]

      The empty parameter context is unchanged by an SGD step.

      @[simp]

      An SGD step updates the head tensor by subSpec p (scaleSpec g lr) and applies the same learning rate recursively to the remaining parameters.