Exclude frozen parameters from EMA state when freeze_filter is set
Summary
When a TrainConfig sets a freeze_filter and keeps EMA enabled (ema_decay defaults to 0.99), scripts/train.py keeps an EMA copy of the entire parameter tree, including frozen parameters. Frozen parameters never change, so their EMA entries always equal the originals. They take up GPU memory and get recomputed every step without changing anything.
trainable_filter is already applied to the gradients and the optimizer state. It just isn't applied to the EMA state.
Where
The optimizer state is built from trainable params only, but the EMA state gets the full tree (scripts/train.py:111-113):
opt_state=tx.init(params.filter(config.trainable_filter)),
ema_decay=config.ema_decay,
ema_params=None if config.ema_decay is None else params,and every step updates the whole tree (scripts/train.py:172-173):
ema_params=jax.tree.map(
lambda old, new: state.ema_decay * old + (1 - state.ema_decay) * new, state.ema_params, new_params
),Measured
I ran the real init_train_state + train_step on debug_pi05, with an action-expert-only freeze filter and ema_decay=0.99, for 300 steps (1× L40S, JAX 0.5.3, flax 0.10.2):
freeze_filter=nnx.Any(
nnx.All(nnx_utils.PathRegex(".*llm.*"), nnx.Not(nnx_utils.PathRegex(".*llm.*_1.*"))),
nnx_utils.PathRegex(".*img.*"),
)- The EMA state holds 429,146,480 frozen bf16 params (~0.86 GB) even on the debug config, because SigLIP is full size there.
- At steps 1, 10, 100 and 300, 0 of those values differed from the frozen params, so the copy is exactly redundant.
- EMA on the trainable params behaves normally.
For a full pi0.5 model with the PaliGemma backbone and SigLIP frozen (~2.92B frozen params in bf16), that's roughly 5.9 GB of GPU memory. It also adds about 17.5 GB of memory reads and writes per step for no effect, though that's a minor speed cost next to the memory.
Why shipped configs don't hit this
Only two shipped configs set a freeze_filter (pi0_libero_low_mem_finetune, pi0_fast_libero_low_mem_finetune), and both set ema_decay=None (config.py:697, config.py:741). Any custom partial-freeze config that keeps the default EMA is affected.
Proposed change
- Keep
ema_paramsonly for trainable params (params.filter(config.trainable_filter)) and update it with the filtered new params. - When saving, merge the frozen params from
state.paramsback in_split_params(checkpoints.py:145), so saved checkpoints keep the same structure and values as today. - When restoring, filter the loaded EMA params back down to trainable params.
Training results and checkpoints stay identical; only memory use goes down. I'm happy to open a PR for this.
Source: Physical-Intelligence/openpi