Where the batch dimension goes, and when you want it kept
shape▲△△A layer has and sees a batch , producing adjoints . Give the shape of and say where the 64 went. Then give the shape of the object a per-sample gradient method needs instead, and its size in fp32.
Hint
A gradient has the shape of the thing it differentiates. has no batch dimension, so the answer is forced — the question is only which operation removes it.
Solution
The shape. is , the shape of . The batch is contracted away by the product of (I.6.4):
The 64 is the inner dimension. Nobody writes a sum over the batch because the matrix product already is one — it adds the 64 per-example outer products together as it goes.
What is lost. The sum is not invertible. Once formed, cannot be taken apart into the 64 contributions that made it, so any method needing individual examples must intervene before the contraction rather than after it.
The per-sample object. That is the stack of outer products, one per example:
values, bytes in fp32 — MB for one layer’s gradient at batch 64, against bytes for the reduced version. Sixty-four times the memory, which is the batch size, and that is the whole cost of the method.
Who needs it. Differentially private training clips each example’s gradient to a fixed norm before averaging, which is meaningless once they are summed. Influence functions and per-example importance weighting want the same object. All of them pay the same memory, and the usual response is to recompute per-sample gradients layer by layer rather than hold them all at once.