Practice in Lab: Own EMA weights and copy non-parameter buffers →
Own EMA weights and copy non-parameter buffers
An exponential moving average is another owned model state, not a shallow alias of the current model. Averaging parameter tensors can stabilize evaluation, but buffers need a declared policy too. Here running statistics and counters are copied rather than averaged. This is an explicit exercise contract; other EMA implementations can make different choices. The update disables gradient recording, verifies compatible state before mutation and refuses shared storage so a supposed shadow model cannot silently modify its source.
import copy, torch
m = torch.nn.Linear(1, 1)
shadow = copy.deepcopy(m)
print(shadow is m)Review the design
Explain buffer policy, update cadence and evaluation ownership. Keep EMA state in a resumable checkpoint when continuation requires the same averaging history.
Your task
Update an independent average module in place and return it. Floating named parameters use decay*old+(1-decay)*live under no_grad. All other state entries, including running-statistic and integer buffers, copy the live value. Require matching state keys/shapes/dtypes and decay in [0,1]; reject shared tensor storage before changing anything. Live model state must remain unchanged. Update exact tied aliases once; reject inconsistent shadow aliases before mutation.
The explanation opens here once every check passes.