Md. Asif Uddin
    Problem I.7.B05

    Adam's state, shape by shape, and what its division buys

    shape▲▲△

    Exact shapes; algebra exact.

    STATEMENT

    Give the shape of every tensor Adam holds for a concrete model, then establish the property those shapes exist to provide: that the step taken in each coordinate is unchanged when the loss is multiplied by a constant.

    GIVEN

    The network of I.5.B03 — widths (784,512,256,128,10)(784, 512, 256, 128, 10), four affine layers, 567 434567\,434 parameters. Adam as in (I.7.6), with β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999, ε=10−8\varepsilon = 10^{-8}.

    FIND

    The shape of mm and vv for each parameter tensor and the totals; then, for a loss rescaled to cLc\mathcal{L}, the effect on gg, mm, vv and the update.

    STRATEGY

    Every moment estimate is an average of things with the shape of the gradient, and a gradient has the shape of its parameter — so the shapes follow without any new argument. The scale question is answered by tracking a factor of cc through (I.7.6) and seeing where it cancels.

    SOLUTION

    Step 1 — the shapes. mm and vv are elementwise running averages of gg and g2g^2, so each has exactly the shape of the parameter it belongs to:

    tensorshapeentriesmmvv
    W(1)\mat{W}^{(1)}784×512784 \times 512401 408401\,408samesame
    b(1)\vec{b}^{(1)}1×5121 \times 512512512samesame
    W(2)\mat{W}^{(2)}512×256512 \times 256131 072131\,072samesame
    b(2)\vec{b}^{(2)}1×2561 \times 256256256samesame
    W(3)\mat{W}^{(3)}256×128256 \times 12832 76832\,768samesame
    b(3)\vec{b}^{(3)}1×1281 \times 128128128samesame
    W(4)\mat{W}^{(4)}128×10128 \times 101 2801\,280samesame
    b(4)\vec{b}^{(4)}1×101 \times 101010samesame
    total567 434567\,434567 434567\,434567 434567\,434

    Two extra copies, 1 134 8681\,134\,868 values, 4.544.54 MB in fp32 against the model’s own 2.272.27 MB — the 2×2\times of I.7.B04, on a concrete model.

    Nothing is per-layer or per-tensor: the state is per scalar parameter. That is what “adaptive” means here, and it is the whole reason the state is this large.

    Step 2 — rescale the loss. Replace L\mathcal{L} by cLc\mathcal{L} for a constant c>0c > 0. Gradients are linear, so every gtg_t becomes cgtcg_t. Both moments are linear in their inputs:

    mt=β1mt−1+(1−β1)cgt⟹cmt,vt=β2vt−1+(1−β2)c2gt2⟹c2vtm_t = \beta_1 m_{t-1} + (1-\beta_1)cg_t \Longrightarrow cm_t, \qquad v_t = \beta_2 v_{t-1} + (1-\beta_2)c^2g_t^2 \Longrightarrow c^2v_t

    The first moment picks up cc and the second picks up c2c^2, because one averages the gradient and the other its square.

    Step 3 — the cancellation. Bias correction divides both by constants, so the factors survive it: m^t→cm^t\hat{m}_t \to c\hat{m}_t and v^t→c2v^t\hat{v}_t \to c^2\hat{v}_t. The step is

    c m^tc2v^t+ε=c m^tcv^t+ε\frac{c\,\hat{m}_t}{\sqrt{c^2\hat{v}_t} + \varepsilon} = \frac{c\,\hat{m}_t}{c\sqrt{\hat{v}_t} + \varepsilon}

    For ε\varepsilon negligible against cv^tc\sqrt{\hat{v}_t} the cc cancels and the update is unchanged. Adam’s step is invariant to rescaling the loss, and by the same argument to rescaling any individual coordinate’s gradient.

    Step 4 — what plain descent does instead. Its update is −ηcgt-\eta cg_t, which scales with cc directly. Doubling the loss doubles every step, so a rate tuned for one loss scale is wrong for another — and since a sum over a batch and a mean over a batch differ by a factor of BB, that is not a hypothetical.

    Step 5 — where the invariance fails. ε\varepsilon is an additive constant and does not scale, so the cancellation is approximate. When cc is small enough that cv^≲εc\sqrt{\hat{v}} \lesssim \varepsilon, the denominator is dominated by ε\varepsilon and the step becomes ≈cm^/ε\approx c\hat{m}/\varepsilon — linear in cc again, like plain descent. So the invariance holds over a wide range and breaks at the bottom of it, which is one reason ε\varepsilon is a number worth knowing rather than a formality: it sets where the adaptive behaviour stops.

    Answer

    mm and vv each have the shape of the parameters, tensor for tensor: 567 434567\,434 entries apiece, 1 134 8681\,134\,868 values of state, 4.544.54 MB in fp32 against 2.272.27 MB of weights.

    Under L→cL\mathcal{L} \to c\mathcal{L}: g→cgg \to cg, m→cmm \to cm, v→c2vv \to c^2v, and the update m^/(v^+ε)\hat{m}/(\sqrt{\hat{v}} + \varepsilon) is unchanged while cv^≫εc\sqrt{\hat{v}} \gg \varepsilon. Plain descent’s update scales with cc.

    Check — sanity

    The state has no shape of its own. Every entry of mm and vv corresponds to exactly one scalar parameter, so a memory estimate that treats optimiser state as a per-layer overhead is wrong by orders of magnitude.

    The exponents on cc are 1 and 2. They must be: mm averages gg and vv averages g2g^2. Getting cc and cc would mean the square had been dropped, and the cancellation would then leave a stray factor rather than none.

    The step’s magnitude is about η\eta. With m^≈v^\hat{m} \approx \sqrt{\hat{v}} for a steady gradient, the ratio is near 1 and each coordinate moves by roughly η\eta per step regardless of its gradient’s size. That is the property that makes a single η\eta serviceable across parameters of very different scales, and it is also why Adam’s η\eta is typically 10−310^{-3} where SGD’s might be 10−110^{-1} — they are measured in different things.

    Where this breaks

    Scale invariance is a property of the step, not of training. Rescaling the loss leaves Adam’s trajectory unchanged but not anything coupled to the loss value: gradient clipping by norm triggers at different points, a fixed convergence threshold on the loss means something different, and in mixed precision the gradient scaler exists precisely because the raw magnitude decides whether fp16 overflows or flushes to zero. “Adam does not care about the loss scale” is true of one equation and false of the system around it.

    The invariance is also per-coordinate, which is a double-edged property. A parameter whose gradient is genuinely tiny because it barely matters is stepped just as far as one whose gradient is large, since only the ratio survives. That is the intended behaviour and it is also why Adam can keep moving parameters that plain descent would leave alone — sometimes usefully, sometimes as an argument for decoupled weight decay, which I.7.X04 takes up.

    Finally, ε\varepsilon sits outside the square root in (I.7.6) and some implementations place it inside. The two differ, and the difference shows up exactly in the small-gradient regime where the invariance is already breaking down. It is not a detail worth arguing about often, but it is a real difference between two things both called Adam.

    Variation

    Work out what happens under a rescaling of the parameters rather than the loss — replace θ\theta by aθa\theta with the model reparameterised to compensate. Is Adam invariant to that too? The answer is different from Step 3’s, and it is the reason normalisation layers still matter when Adam is in use.

    Draws on