TorchLean API

NN.Proofs.Autograd.Tape.Algebra.Gradients

Gradient references carry the version of their last materialization. A uniform contribution updates one shared history; selected parent gradients replay their pending updates before receiving the node's complete local contribution. All scalar additions retain their dense evaluation order.

def Proofs.Autograd.Algebra.Deferred.addUniform {α : Type} [TorchLean.Storage α] [Add α] {shape : Spec.Shape} (uniform : α) (value : TorchLean.Tensor α shape) :

Add a uniform tensor on the right, exactly as in dense context accumulation.

Instances For
    def Proofs.Autograd.Algebra.Deferred.materializePack {α : Type} [TorchLean.Storage α] [Add α] [DecidableEq α] (history : History α) (clock : ℕ) (epochs : Array ℕ) {shapes : List Spec.Shape} :
    TorchLean.TensorPack α shapes → ℕ → TorchLean.TensorPack α shapes

    Materialize a typed boundary in one traversal, with constant-time version lookup.

    Instances For
      theorem Proofs.Autograd.Algebra.Deferred.getIdx_materializePack {α : Type} [TorchLean.Storage α] [Add α] [DecidableEq α] (history : History α) (clock : ℕ) (epochs : Array ℕ) {shapes : List Spec.Shape} (xs : TorchLean.TensorPack α shapes) (offset : ℕ) {shape : Spec.Shape} (idx : Idx shapes shape) :
      getIdx (materializePack history clock epochs xs offset) idx = applyRecentStable addUniform history (clock - epochs[offset + ↑idx.i]?.getD 0) (getIdx xs idx)
      theorem Proofs.Autograd.Algebra.Deferred.materializePack_snoc {α : Type} [TorchLean.Storage α] [Add α] [DecidableEq α] (history : History α) (clock : ℕ) (epochs : Array ℕ) {shapes : List Spec.Shape} (xs : TorchLean.TensorPack α shapes) (offset : ℕ) {shape : Spec.Shape} (value : TorchLean.Tensor α shape) :
      materializePack history clock epochs (xs.snoc value) offset = (materializePack history clock epochs xs offset).snoc (applyRecentStable addUniform history (clock - epochs[offset + shapes.length]?.getD 0) value)

      Array gradients and a shared chronological history of deferred uniform additions.

      • stored : TensorContext α shapes

        Most recently materialized tensor at each active position.

      • epochs : Array ℕ

        History version already applied to each tensor. Finalized suffix entries may remain here.

      • clock : ℕ

        Number of uniform additions issued so far.

      • history : Deferred.History α

        Consecutive identical contributions share one run.

      • covered : self.stored.values.size ≤ self.epochs.size

        Every active tensor has a version slot.

      • bounded (i : ℕ) (bound : i < self.epochs.size) : self.epochs[i] ≤ self.clock

        No tensor version refers to an update that has not happened.

      Instances For

        Start with a materialized seed and no deferred additions.

        Instances For

          Read one gradient, replaying precisely the updates since its saved version.

          Instances For

            Materialize only when crossing back into the public typed pack interface.

            Instances For

              Defer one uniform contribution to the active prefix.

              Instances For
                theorem Proofs.Autograd.Algebra.GradientContext.lookup_addUniform {α : Type} [TorchLean.Storage α] {shapes : List Spec.Shape} [Add α] [DecidableEq α] (ctx : GradientContext α shapes) (uniform : α) {shape : Spec.Shape} (idx : Idx shapes shape) :
                (ctx.addUniform uniform).lookup.read idx = Deferred.addUniform uniform (ctx.lookup.read idx)
                def Proofs.Autograd.Algebra.GradientContext.set {α : Type} [TorchLean.Storage α] {shapes : List Spec.Shape} (ctx : GradientContext α shapes) {shape : Spec.Shape} (idx : Idx shapes shape) (value : TorchLean.Tensor α shape) :
                GradientContext α shapes

                Save an already materialized gradient and its current history version.

                Instances For
                  @[simp]
                  theorem Proofs.Autograd.Algebra.GradientContext.lookup_set {α : Type} [TorchLean.Storage α] {shapes : List Spec.Shape} [Add α] [DecidableEq α] (ctx : GradientContext α shapes) {shape : Spec.Shape} (idx : Idx shapes shape) (value : TorchLean.Tensor α shape) :
                  (ctx.set idx value).lookup = ctx.lookup.set idx value
                  @[reducible, inline]

                  A materialized update contains no reference to the full previous gradient context.

                  Instances For
                    def Proofs.Autograd.Algebra.GradientContext.snapshot {α : Type} [TorchLean.Storage α] {shapes : List Spec.Shape} (indices : List (SomeIdx shapes)) (source : TensorLookup α shapes) :
                    List (Update α shapes)

                    Compute every exceptional result before modifying the gradient array.

                    Instances For

                      Install materialized exceptions in their existing order. Duplicate writes are identical.

                      Instances For
                        theorem Proofs.Autograd.Algebra.GradientContext.lookup_setMany_snapshot {α : Type} [TorchLean.Storage α] {shapes : List Spec.Shape} [Add α] [DecidableEq α] (ctx : GradientContext α shapes) (indices : List (SomeIdx shapes)) (source : TensorLookup α shapes) {shape : Spec.Shape} (other : Idx shapes shape) :
                        (ctx.setMany (snapshot indices source)).lookup.read other = if ↑other.i ∈ List.map (fun (entry : SomeIdx shapes) => ↑entry.snd.i) indices then source.read other else ctx.lookup.read other
                        def Proofs.Autograd.Algebra.GradientContext.addContributions {α : Type} [TorchLean.Storage α] {shapes : List Spec.Shape} [Add α] [DecidableEq α] (ctx : GradientContext α shapes) (contribution : Contributions α shapes) :
                        GradientContext α shapes

                        Accumulate a node's complete local contribution, preserving each dense addition.

                        Instances For
                          theorem Proofs.Autograd.Algebra.GradientContext.lookup_addContributions {α : Type} [TorchLean.Storage α] {shapes : List Spec.Shape} [Add α] [DecidableEq α] (ctx : GradientContext α shapes) (contribution : Contributions α shapes) :
                          (ctx.addContributions contribution).lookup = ctx.lookup.add contribution.lookup
                          theorem Proofs.Autograd.Algebra.GradientContext.toPack_addContributions {α : Type} [TorchLean.Storage α] {shapes : List Spec.Shape} [Add α] [DecidableEq α] (ctx : GradientContext α shapes) (contribution : Contributions α shapes) :
                          (ctx.addContributions contribution).toPack = ctx.toPack.add (contribution.dense ())

                          Compact accumulation equals full pack addition without an additive-identity assumption.

                          def Proofs.Autograd.Algebra.GradientContext.addDense {α : Type} [TorchLean.Storage α] {shapes : List Spec.Shape} [Add α] [DecidableEq α] (ctx : GradientContext α shapes) (contribution : TorchLean.TensorPack α shapes) :
                          GradientContext α shapes

                          Custom dense VJPs remain supported through an exact materializing adapter.

                          Instances For
                            @[simp]
                            theorem Proofs.Autograd.Algebra.GradientContext.toPack_addDense {α : Type} [TorchLean.Storage α] {shapes : List Spec.Shape} [Add α] [DecidableEq α] (ctx : GradientContext α shapes) (contribution : TorchLean.TensorPack α shapes) :
                            (ctx.addDense contribution).toPack = ctx.toPack.add contribution
                            def Proofs.Autograd.Algebra.GradientContext.cast {α : Type} [TorchLean.Storage α] {shapes other : List Spec.Shape} (same : shapes = other) (ctx : GradientContext α shapes) :

                            Transport the typed active prefix without touching gradient storage.

                            Instances For
                              @[simp]
                              theorem Proofs.Autograd.Algebra.GradientContext.toPack_cast {α : Type} [TorchLean.Storage α] {shapes : List Spec.Shape} [Add α] [DecidableEq α] {other : List Spec.Shape} (same : shapes = other) (ctx : GradientContext α shapes) :
                              def Proofs.Autograd.Algebra.GradientContext.pop {α : Type} [TorchLean.Storage α] {shapes : List Spec.Shape} [Add α] [DecidableEq α] {shape : Spec.Shape} (ctx : GradientContext α (shapes ++ [shape])) :

                              Finalize one output gradient and remove its slot from the active prefix.

                              Instances For
                                theorem Proofs.Autograd.Algebra.GradientContext.toPack_pop {α : Type} [TorchLean.Storage α] {shapes : List Spec.Shape} [Add α] [DecidableEq α] {shape : Spec.Shape} (ctx : GradientContext α (shapes ++ [shape])) :
                                (ctx.pop.1.toPack, ctx.pop.2) = ctx.toPack.unsnoc

                                Removing a deferred gradient context agrees with removing the fully materialized pack.