Skip to content

Chapter 20: Reverse-Mode Tensor Autograd, Checkpointing & Optimizers

"Differentiation is a compiler pass over tape records; memory is the bottleneck we trade compute against."


20.1 Reverse-Mode Dynamic Tape & Vector-Jacobian Products (VJP)

RCRE pairs a micrograd-style scalar DAG engine (RCRE_ScalarVal) with a cache-blocked Tensor Tape (RCRE_TensorTape, src/autograd/tensor_autograd.c). During the forward pass, each operator allocates its output in a rewindable RCRE_Arena while pushing a compact backward closure onto a heap-grown node array—so rcre_arena_temp_end() rewinds scratch activations per batch without corrupting the tape graph.

Exact VJP for Spatial Convolution & Attention

For scaled dot-product attention \(\mathbf{O} = \mathbf{P}\mathbf{V}\) where \(\mathbf{P} = \text{softmax}\left(\frac{\mathbf{Q}\mathbf{K}^T}{\sqrt{D}}\right)\):

\[\frac{\partial \mathcal{L}}{\partial \mathbf{V}} = \mathbf{P}^T \frac{\partial \mathcal{L}}{\partial \mathbf{O}}, \qquad \frac{\partial \mathcal{L}}{\partial \mathbf{P}} = \frac{\partial \mathcal{L}}{\partial \mathbf{O}} \mathbf{V}^T\]
\[\left(\frac{\partial \mathcal{L}}{\partial \mathbf{S}}\right)_i = \frac{1}{\sqrt{D}}\,\mathbf{P}_i \odot \left(\left(\frac{\partial \mathcal{L}}{\partial \mathbf{P}}\right)_i - \sum_j P_{ij}\left(\frac{\partial \mathcal{L}}{\partial \mathbf{P}}\right)_{ij}\right)\]

rcre_autograd_attention() and rcre_autograd_conv2d() compute exact analytical gradients \(\nabla_{\mathbf{Q}}, \nabla_{\mathbf{K}}, \nabla_{\mathbf{V}}\) and \(\nabla_{\mathbf{X}}, \nabla_{\mathbf{W}}\) directly in C23.


20.2 Memory-Frugal Optimizers: Lion & Factored Adafactor

Standard AdamW stores \(2\times\) parameter count in FP32 optimizer states (\(m_t, v_t\)). RCRE v2.0 (src/nn/optim.c) adds two memory-saving alternatives:

  1. Lion (EvoLved Sign Momentum, Chen et al. 2023): Halves optimizer state by maintaining only the first moment \(m_t\) and updating parameters via the sign of a momentum interpolation: $\(\mathbf{c}_t = \beta_1 \mathbf{m}_{t-1} + (1 - \beta_1)\mathbf{g}_t, \qquad \theta_t \leftarrow \theta_{t-1} - \eta\big(\text{sign}(\mathbf{c}_t) + \lambda \theta_{t-1}\big)\)$
  2. Adafactor (Shazeer & Stern 2018): Replaces an \([R \times C]\) second-moment matrix \(V\) with rank-1 row and column factors \(\mathbf{r} \in \mathbb{R}^R\) and \(\mathbf{c} \in \mathbb{R}^C\): $\(\hat{V}_{ij} = \frac{r_i c_j}{\bar{r}}\)$ shrinking variance memory from \(\mathcal{O}(RC)\) down to \(\mathcal{O}(R + C)\).

20.3 Gradient Checkpointing (BF16 + Block Recomputation)

Storing every intermediate layer activation across an \(L\)-layer Transformer scales memory as \(\mathcal{O}(L \cdot T \cdot D)\). In rcre_llama_train_step_f32(..., grad_checkpoint=true), RCRE saves only block boundary inputs across layers and recomputes intra-block RMSNorm, projection, and SwiGLU states on the fly during the backward sweep inside a single shared \(\mathcal{O}(T \cdot D)\) buffer.


20.4 Chapter Project: Attention VJP & Lion Sign-Momentum Convergence

Companion code in examples/ch20/main.c (make ch20) verifies row-wise Softmax Attention VJP analytical gradients against central finite differences and runs Lion sign-momentum minimization under ASan and UBSan.