01-kv-cache-autoregressive-generation cached results forward in time (reuse past K/V instead of recomputing them) to save compute at the cost of memory. Gradient checkpointing makes the exact opposite trade, on the training side of the same network: a standard backward pass needs every layer's intermediate activation kept in memory (there are num_layers + 1 of them, all alive at once), which becomes the actual memory bottleneck for very deep networks — gradient checkpointing throws those intermediates away after the forward pass and recomputes them, on demand, during backward, trading extra compute for far less peak memory.
Implement linear_backward, forward_full/backward_full (the standard, memory-heavy approach), forward_checkpointed/backward_checkpointed (which keeps nothing but the original input, recomputing everything else when backward needs it), and count_stored_activations, quantifying the memory difference.
forward_full returns (final_output, activations, pre_activations), storing every intermediate value.forward_checkpointed returns only final_output — nothing else is kept.backward_checkpointed must produce results numerically identical to backward_full on the same computation — checkpointing changes when things are computed, never what the answer is.count_stored_activations(num_layers, use_checkpointing) returns num_layers + 1 without checkpointing, 1 with it.Open one at a time. Each gives away a little more than the last.
linear_backward is the same chain-rule reasoning 03-mse-gradient's Theory already built up, applied to a general Linear layer instead of directly to a loss: grad_input = grad_output @ weight, grad_weight = grad_output.T @ input, grad_bias = grad_output.sum(axis=0).
backward_checkpointed doesn't need any new backward-pass logic at all — it just calls forward_full first (to regenerate the activations that were never stored), then hands the result straight to backward_full, unmodified.
No results yet
Run your code to check it against the examples, or submit to run every test.