Backward Passes and Optimizers #
Gradient extraction and optimizer updates for eager sessions, including the CUDA paths that keep parameter mirrors and moment buffers on device.
Run reverse-mode backprop on the CUDA tape, returning device gradients for all tape entries.
This is the CUDA analogue of backwardDenseAll, but it does not download gradients back to the
host. This is primarily useful for implementing GPU-native optimizer steps.
Instances For
Run scalar-loss CUDA backprop and return gradients only for trainable parameter leaves.
seed scales the loss cotangent. Batch training uses 1 / batch.size so it can accumulate the
mean directly, without first forming a larger unnormalized sum.
The traversal is the engine's sparse CUDA sweep LibTorch.Tape.backwardSparse, which walks node
ids in reverse, runs a node's VJP only when that node has received a cotangent, retires every
activation cotangent as soon as it has been propagated, and keeps owned device buffers only for
the ids selected by retain. Here retain selects exactly the trainable parameter leaves, so the
returned map has one entry per trainable parameter and optimizer updates stay on device without
paying hash-table costs at every intermediate node. A trainable parameter that receives no
gradient is an error, as before.
Instances For
Add borrowed sample gradients into an owned accumulator without downloading them.
The accumulator owns every inserted buffer. Its caller must release it on success and failure.
Parameter leaf ids are stable across recordings by scalarTrainer.
Instances For
Run reverse-mode backprop and return a dense gradient array for all tape entries.
seed is the upstream gradient for out (like PyTorch's backward(gradient=...)).
Instances For
Extract the gradient for a particular TensorRef from a dense gradient array.
Instances For
Apply an SGD update to all parameters recorded via use.
PyTorch comparison: for p in params: p.data -= lr * p.grad.
Instances For
Check every trainable parameter, gradient, tape value, and optional Adam state before an optimizer changes a parameter. This prevents a malformed gradient map or checkpoint state from producing a partially applied update.
Instances For
Apply SGD from a sparse CUDA gradient map.
This is the path used by the CUDA trainer. It updates only parameter leaves and avoids allocating zero gradients for every forward activation in the tape.
Instances For
Run the shared Adam update after each entry point has converted and validated its options.
The decay coefficient stays delayed until the kernel call: AdamW evaluates -(lr * weightDecay)
for each parameter after the moment corrections, while Adam passes the literal zero coefficient.
Instances For
Apply Adam using an already-computed sparse CUDA gradient map.
Instances For
Apply AdamW from a sparse CUDA gradient map.
Normal training uses this sparse map so activation gradients can be released as soon as their contributions have been propagated.