CPU gradient maps #
The reverse sweeps themselves live with the tape in NN.Runtime.Autograd.Engine.Tape. These
helpers turn a CPU sweep's result into the sparse map that training code reads.
def
Runtime.Autograd.Tape.denseToHashMap
{α : Type}
[TorchLean.Storage α]
(grads : Array (Option (Spec.SomeTensor α)))
:
Convert the optional dense gradient array returned by backwardDense into a sparse HashMap.
Only entries that are present (some (some g)) are kept. The result records exactly the nodes
reached by reverse-mode propagation.
Instances For
def
Runtime.Autograd.Tape.backward
{α : Type}
[TorchLean.Storage α]
[Add α]
(t : Tape (Spec.SomeTensor α))
(outId : ℕ)
(seed : Spec.SomeTensor α)
:
Result (Std.HashMap ℕ (Spec.SomeTensor α))
Reverse-mode backpropagation returning a HashMap of only the nodes that received gradients.
This is the sparse public form of backwardDense: it computes dense gradients first, then drops
nodes that did not receive a gradient.
Instances For
def
Runtime.Autograd.Tape.backwardScalar
{α : Type}
[TorchLean.Storage α]
[Add α]
[One α]
(t : Tape (Spec.SomeTensor α))
(outId : ℕ)
:
Result (Std.HashMap ℕ (Spec.SomeTensor α))
Backpropagate from a scalar output with seed gradient 1.
PyTorch analogy: loss.backward() when loss is a scalar.