What it costs when the sink is not a scalar
complexity▲▲△This chapter assumes a scalar loss. Price what happens when that assumption is dropped: give the cost of a backward pass for a vector output of width , for per-example gradients across a batch, and for a full Jacobian — then say why the assumption is not the restriction it appears to be.
Hint
Reverse mode fills one row of the Jacobian per traversal. How many rows does each of these three objects have?
Solution
The model. layers of width at batch , as in I.6.B05. One forward pass is GFLOP and one backward traversal is twice that.
| sink | traversals | GFLOP | vs scalar |
|---|---|---|---|
| a scalar loss | 1 | ||
| a 10-vector output | 10 | ||
| per-example gradients, batch 32 | 32 | ||
| full Jacobian of 1024 outputs | 1024 |
The pattern. Cost is for traversals, so it is linear in and the ratios approach from below — the single shared forward pass is amortised across them. At the ratio is rather than for exactly that reason.
Per-example gradients are the same problem. A batch of 32 losses is a 32-vector sink, so 32 traversals. This is why the memory answer in I.6.X05 has a matching arithmetic answer: per-sample methods cost in both resources, and neither cost is avoidable by clever bookkeeping — the object genuinely has times as many numbers in it.
Why the assumption is not a restriction. A loss is a reduction. Any vector-valued objective is summed or averaged before differentiation, and that reduction is one more node in the graph — the sink where starts. Summing 32 per-example losses costs 31 additions, which against a GFLOP forward pass is a share of : free, to nine decimal places.
So the scalar assumption costs nothing whenever a single number is what is wanted, and training is that case by construction. It bites only when the individual outputs are wanted separately — and then what is being asked for is a Jacobian, and the price above is the honest price of one.
The one to remember. Multi-task learning with losses does not cost traversals, as long as the tasks are combined into one weighted sum before the backward pass. Wanting the gradients separately — to inspect their conflict, to normalise their scales — is what costs traversals, and the difference between those two situations is a modelling decision with a large and easily overlooked price attached.