diffusion_models.utils.ema ========================== .. py:module:: diffusion_models.utils.ema Module Contents --------------- .. py:function:: unwrap_model(model) Return the module underneath a ``torch.compile`` wrapper (if any). ``torch.compile`` returns an ``OptimizedModule`` that shares its parameters with the original module but prefixes every state dict key with ``_orig_mod.``. The EMA, checkpoints and optimizer must always deal with the original module so that keys stay stable whether or not compilation is on. .. py:class:: ExponentialMovingAverage(model, decay = 0.999, warmup_steps = 0) Keeps an exponential moving average (EMA) of a model's weights. The EMA model is a frozen, eval-mode deep copy of the tracked model. The EMA is compile-aware: a ``torch.compile``-d model can be passed in, as both the copy and every update operate on the underlying module, and the EMA model itself is never compiled, so its keys stay free of ``_orig_mod.`` prefixes. Floating point parameters are averaged, while buffers (e.g. BatchNorm running statistics and integer counters) are copied verbatim from the tracked model, as averaging them is meaningless. :param model: The model to track. :param decay: The decay of the moving average, in ``[0, 1)``. :param warmup_steps: When positive, the effective decay at update ``n`` is ``min(decay, (1 + n) / (warmup_steps + n))``. This makes the average follow the model closely at the start of training. ``0`` disables it. .. py:attribute:: decay :value: 0.999 The target decay. .. py:attribute:: warmup_steps :value: 0 The warmup horizon, in updates. .. py:attribute:: num_updates :value: 0 The number of updates performed so far. .. py:attribute:: model The averaged model. .. py:method:: get_decay() The decay to use for the next update. .. py:method:: update(model) Update the averaged weights from the tracked model. :param model: The tracked model (after an optimizer step). May be compiled. .. py:method:: state_dict() The state dict of the averaged model. .. py:method:: load_state_dict(state_dict, num_updates = None) Load averaged weights into the EMA model.