EMA state: define which tensors move and which clock advances
Specify parameter averaging, buffer policy, update cadence, warmup state, and checkpoint behavior for diffusion-model evaluation.
On this page
The question for today
What you’ll understand
- Calculate a fixed-decay parameter EMA and its update-scale memory.
- Distinguish parameters, buffers, optimizer state, and EMA counters.
- Preserve EMA semantics across accumulation, skipped updates, evaluation, and resume.
An exponential moving average stores another model state for evaluation or sampling; it is not the optimizer itself. The same decay number can represent different smoothing when update cadence, batch size, or warmup changes. Define the tracked tensors and clock explicitly, then save enough state to reproduce the evaluated model rather than assuming an EMA weight file tells the whole training story.
Start with one fixed-decay recurrence
This teaching rule updates an averaged parameter after each successful optimizer step using the new parameter value. Its decay d multiplies the old average and one minus d multiplies the current parameter. Initialize the average explicitly, commonly from the starting parameters, and keep averaging outside gradient tracking. The optimizer continues to update its live parameters and moments; the EMA does not replace those moments. Variable-decay warmup is another algorithm and needs its own recurrence and saved counter.
Diffusers 0.35.1's EMAModel tracks an explicit iterable of parameters and has update-count-dependent decay logic; its default behavior is not simply this fixed-decay hand rule from the first call. PyTorch 2.14's AveragedModel supports configurable averaging and buffer behavior with separate semantics. Inspect the actual helper rather than infer its full state from an option named ema. Document which artifact supplies sampling weights and whether evaluation temporarily swaps parameters or uses a separate model copy.
Cadence and buffers are part of the evaluated model
Gradient accumulation creates several microbatches per optimizer update. Averaging after every microbatch, after every attempted update, or only after a successful update produces different clocks, especially when mixed-precision overflow skips a step. Pick and test one rule. A decay expressed per update also changes its example-scale memory when global batch size changes. Record update count, effective batch, and any warmup schedule before comparing smoothing between runs. A half-life in optimizer updates is not automatically a half-life in training examples.
Model buffers such as running statistics or counters are not the same as trainable parameters. Decide whether each is copied, averaged when meaningful, recomputed, or excluded under an explicitly supported policy. Integer counters should not accidentally receive floating interpolation. Many diffusion architectures avoid batch-running statistics, but that does not justify ignoring every registered buffer. Also verify device, dtype, distributed/sharded state handling, and condition-encoder versions. An averaged parameter list alone may not reconstruct a complete usable model.
Resume and evaluate without corrupting live training state
Save live model parameters, optimizer and scaler state, EMA shadow tensors, counters, decay configuration, relevant buffers, and random/data-loader state under the desired resume contract. Check that resuming reproduces the next EMA update as well as the next optimizer update. If sampling swaps in averaged weights, restore the live training weights before the next optimizer step. Compare live and EMA checkpoints with the same data, prompts, sampler, and seed portfolio; an apparent improvement can otherwise come from a changed inference protocol.
The Markov lab exactly matches one restricted constant-input averaging family. Reset, set p=0.10 and q=0, predict state-one mass 0.19 after two Steps, then Step twice. Separately Reset, set p=0.20 and q=0, predict mass 0.36 after two Steps, then Step twice. Starting from zero, this equals averaging a constant value one with decays 0.9 and 0.8 respectively. The instrument has no changing model parameters, buffers, optimizer, or checkpoint state, so the match does not extend to a full EMA training run.
Work it through
Apply two fixed-decay updates
A scalar EMA starts at zero. Successful optimizer updates produce theta_1=2 and theta_2=4. Use the explicitly fixed decay d=0.9, with one averaging update after each successful step.
After the first update, the average is 0.9×0+0.1×2=0.2.
After the second, it is 0.9×0.2+0.1×4=0.58.
The original-state coefficient after two updates is 0.9²=0.81. For constant decay 0.99, the old-state half-life would be ln(0.5)/ln(0.99)≈68.97 averaging updates, a different unit from microbatches or examples.
The averaged parameter is 0.58 under this declared clock and initialization. A warmup helper can intentionally produce another result.
The living notebook
Explore the model
Interactive lab · markov
A constant-input EMA has an exact two-state analogy
Reset, set p=0.10 and q=0, predict state-one probability 0.19 after two Steps, then Step twice. Separately Reset, set p=0.20 and q=0, predict 0.36 after two Steps, then Step twice. This starts in state zero and matches only an EMA toward constant value one; there are no parameters, buffers, or optimizer updates.
- P(state 1) at step 0
- 0%
- Stationary P(state 1)
- 60%
- Chain behavior
- Monotone convergence
Exact finite-step probabilities for a two-state chain starting in state 0, not sampled trajectories. Playback advances two transitions per second. When p = q = 0 every distribution is stationary; when p = q = 1 the chain alternates forever. A single zero transition creates an absorbing state. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forDecay and update count jointly determine smoothing; a matching toy recurrence does not define complete model state.
Pause & reason
Check your understanding
Your turn
Try it yourself
An average starts at zero and receives the constant value one for three fixed-decay updates with d=0.8. Compute the result and the remaining coefficient on its initial state.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Use one minus d cubed for the new-value mass.
Compare with the solution
The initial-state coefficient is 0.8³=0.512, so the average is 1−0.512=0.488. These are three averaging updates under the stated rule; treating three microbatches as three updates would be a different clock if the optimizer updates only once.
Keep the idea
Take it with you
- EMA requires a recurrence, initialization, and explicit update clock.
- Parameters and buffers need separately declared policies.
- Resume and evaluation must preserve both averaged and live training state.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.