■ Change #79 · wiki.edited
Reverse-mode automatic differentiation: Normalize Markdown to canonical form
body
## What it is− A program is a composition of primitive operations. Reverse-mode AD records those operations on a **tape** during the forward pass, then walks the tape backwards, applying the chain rule to accumulate the derivative of a single scalar output with respect to *every* input.+ A program is a composition of primitive operations. Reverse-mode AD records those operations on a **tape** during the forward pass, then walks the tape backwards, applying the chain rule to accumulate the derivative of a single scalar output with respect to _every_ input.## Why it mattersFor a function $f: \mathbb{R}^n \to \mathbb{R}$ the full gradient costs a small constant multiple of evaluating $f$, independent of $n$. That is what makes training networks with billions of parameters possible at all.## How it works1. **Forward pass**: evaluate the program and store intermediate values.2. **Backward pass**: start from $\bar{y} = 1$ and propagate adjoints $\bar{v}_i = \sum_j \bar{v}_j \, \partial v_j / \partial v_i$.− The trade-off is memory: intermediates must be kept (or recomputed, via *checkpointing*).+ The trade-off is memory: intermediates must be kept (or recomputed, via _checkpointing_).## Open problems- Memory-optimal checkpointing schedules for arbitrary graphs- Differentiating through discrete control flow and non-smooth operations- Higher-order derivatives without blow-up
explainer.student
**The setting.** A program computes a number $y$ (say, a training loss) from many inputs $x_1,\dots,x_n$ (the parameters of a model). To improve the model, gradient descent needs the **gradient**: the list of how much $y$ changes per small change in each $x_i$. Writing the formula for it by hand is error-prone, and estimating it by nudging each input separately costs $n$ extra runs. For $n$ in the billions that is impossible.**Key idea: the chain rule, applied backwards.** Any program is a chain of simple operations (add, multiply, exp, matrix product). Each intermediate result $v_j$ depends on earlier ones. The chain rule says the effect of an input on the output is a sum over all paths through this graph, with each path contributing a product of local slopes.Reverse mode organises that sum cleverly:− 1. **Forward pass.** Run the program normally and store the intermediate values (this stored record is often called the *tape*).+ 1. **Forward pass.** Run the program normally and store the intermediate values (this stored record is often called the _tape_).2. **Backward pass.** Start with the output, whose "adjoint" is $\bar y = 1$. Walk the operations in reverse order. For each value $v_i$ compute $\bar v_i = \sum_j \bar v_j\,\partial v_j/\partial v_i$, which says how much the final output changes per unit change in $v_i$. When you reach the inputs you have all of $\partial y/\partial x_i$.The formula in step 2 is the chain rule, summed over every operation $j$ that uses $v_i$. In words: the importance of a value is the sum of the importance of what it feeds, weighted by how strongly it feeds them.− **Cost.** For a function with one output and $n$ inputs, the whole gradient costs only a small constant multiple of one forward run, no matter how large $n$ is. *Forward mode*, which pushes derivatives forward instead, needs one pass per input, so it wins only when there are few inputs and many outputs.+ **Cost.** For a function with one output and $n$ inputs, the whole gradient costs only a small constant multiple of one forward run, no matter how large $n$ is. _Forward mode_, which pushes derivatives forward instead, needs one pass per input, so it wins only when there are few inputs and many outputs.− **The trade-off is memory.** The backward pass needs the forward values, so they must be stored. *Checkpointing* stores only some of them and recomputes the rest, trading extra time for less memory.+ **The trade-off is memory.** The backward pass needs the forward values, so they must be stored. _Checkpointing_ stores only some of them and recomputes the rest, trading extra time for less memory.**Where it is hard.** Reverse mode assumes smooth, step-by-step computation. Branches, loops with data-dependent length, sorting, rounding and other non-smooth operations need care, and choosing the best checkpointing schedule for an arbitrary graph is not fully solved. Higher-order derivatives (derivatives of derivatives) can blow up in cost if done naively.Reverse-mode autodiff is not an approximation like finite differences. It gives exact derivatives of the program as written, up to ordinary rounding error.
hash 5a7083c97dc66c146e6ca234a0a8e67177dce9e0a8090707bbc6c4e4f4d3eb44
previous 25f0146309577ecb4d79d490f5f5778e20e6ec999ed65a47690bd2c1e0fa10ed