ema
ema
¶
Exponential moving average callback.
EMACallback
¶
Bases: Callback
Exponential Moving Average callback for model weights.
Maintains a moving average of model weights during training. The EMA model is used for validation and can provide more stable predictions.
Based on @crowsonkb's notes on EMA Warmup: If gamma=1 and power=1, implements a simple average. gamma=1, power=2/3 are good values for models you plan to train for a million or more steps (reaches decay factor 0.999 at 31.6K steps, 0.9999 at 1M steps), gamma=1, power=3/4 for models you plan to train for less (reaches decay factor 0.999 at 10K steps, 0.9999 at 215.4k steps).
Initialize EMA callback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
power
|
float
|
Exponential factor of EMA warmup (default: 0.75 for shorter training) |
0.75
|
update_after_step
|
int
|
Start EMA updates after this many steps |
0
|
inv_gamma
|
float
|
Inverse multiplicative factor of EMA warmup |
1.0
|
min_value
|
float
|
Minimum EMA decay rate |
0.0
|
max_value
|
float
|
Maximum EMA decay rate |
0.9999
|
Source code in src/versatil/training/callbacks/ema.py
on_fit_start
¶
Create EMA model copy at start of training.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer
|
Trainer
|
Lightning trainer |
required |
pl_module
|
LightningModule
|
Lightning module (LightningPolicy) |
required |
Source code in src/versatil/training/callbacks/ema.py
on_train_batch_end
¶
Update EMA model after each training batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer
|
Trainer
|
Lightning trainer |
required |
pl_module
|
LightningModule
|
Lightning module |
required |
outputs
|
Tensor | dict[str, Any] | None
|
Training step outputs |
required |
batch
|
Any
|
Current batch |
required |
batch_idx
|
int
|
Batch index |
required |
Source code in src/versatil/training/callbacks/ema.py
on_save_checkpoint
¶
Inject EMA weights into the checkpoint, stashing the raw weights.
The checkpoint's state_dict intentionally holds the EMA weights so
every saved checkpoint is deployment-ready. The raw fast weights are
kept under a separate key so resumed training does not restart from
the average with optimizer moments that belong to the raw weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer
|
Trainer
|
Lightning trainer |
required |
pl_module
|
LightningModule
|
Lightning module |
required |
checkpoint
|
dict
|
Checkpoint dictionary being saved |
required |
Source code in src/versatil/training/callbacks/ema.py
on_load_checkpoint
¶
Restore raw training weights and queue the EMA state for rebuild.
Lightning loads state_dict (the EMA weights) into the module
before this hook runs, so the raw weights are written back onto the
policy here and the EMA weights are stashed until on_fit_start
recreates the EMA model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer
|
Trainer
|
Lightning trainer |
required |
pl_module
|
LightningModule
|
Lightning module |
required |
checkpoint
|
dict
|
Loaded checkpoint dictionary |
required |
Source code in src/versatil/training/callbacks/ema.py
on_validation_start
¶
Temporarily replace policy with EMA model for validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer
|
Trainer
|
Lightning trainer |
required |
pl_module
|
LightningModule
|
Lightning module |
required |
Source code in src/versatil/training/callbacks/ema.py
on_validation_end
¶
Restore original policy after validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer
|
Trainer
|
Lightning trainer |
required |
pl_module
|
LightningModule
|
Lightning module |
required |