Skip to content

fix: load policy in compute dtype when optimizer holds fp32 master weights - #2888

Open
Doondi-Ashlesh wants to merge 2 commits into
NVIDIA-NeMo:mainfrom
Doondi-Ashlesh:fix/automodel-optimizer-aware-load-dtype
Open

Doondi-Ashlesh wants to merge 2 commits into
NVIDIA-NeMo:mainfrom
Doondi-Ashlesh:fix/automodel-optimizer-aware-load-dtype

Conversation

@Doondi-Ashlesh

@Doondi-Ashlesh Doondi-Ashlesh commented Jun 22, 2026 •

Copy link
Copy Markdown

What does this PR do ?

Avoid doubling model- and optimizer-state memory on the AutoModel backend by loading the policy in fp32 only when the optimizer has no internal master, instead of always loading fp32.

Issues

Closes #2865

Details

The AutoModel backend always loaded the policy with torch_dtype=float32 to preserve optimizer master weights. When the optimizer already keeps its own fp32 master (TE FusedAdam with master_weights, stored compactly as bf16 params plus an int16 remainder), the fp32 load duplicates that master and roughly doubles both model- and optimizer-state memory.

This PR selects the load dtype based on the optimizer:

  • Optimizers that hold their own master (FusedAdam with master_weights): load in the compute dtype (e.g. bf16) and let the optimizer own the master.
  • Masterless optimizers (e.g. torch.optim.AdamW): keep the fp32 load so updates do not underflow.

The Automodel checkpoint-dtype-restore workaround is now applied only on the fp32 path, where it is actually needed.

Reported memory impact from the issue (FusedAdam recipe):

Metric fp32 load compute-dtype load
Master params 7.32 GB 4.26 GB
Optimizer state per rank 16.43 GB 12.77 GB
Optimizer state all ranks 131.5 GB 102.2 GB
Model params all ranks 66.6 GB 35.7 GB

Usage

No config or API change. Existing recipes that use FusedAdam with master_weights: true automatically load in the compute dtype; AdamW recipes are unchanged.

Additional Information

  • New unit tests in tests/unit/models/automodel/test_automodel_setup.py cover the optimizer detection helper, the dtype selection in validate_and_prepare_config, and the restore gating in setup_model_and_optimizer.
  • The unit tests require torch and nemo_automodel, so please trigger CI to run the full suite. The AdamW path is unchanged, so existing behavior and tests are preserved.

Always loading the policy in fp32 to preserve optimizer master weights
duplicates the master and roughly doubles model- and optimizer-state
memory when the optimizer already maintains its own fp32 master (e.g.
Transformer Engine FusedAdam with master_weights, which stores it
compactly as bf16 params plus an int16 remainder).

Select the load dtype based on the optimizer: load in the compute dtype
when the optimizer holds its own master, and keep fp32 only for
masterless optimizers (e.g. torch.optim.AdamW) so their updates do not
underflow. The Automodel checkpoint-dtype-restore workaround is now
applied only on the fp32 path, where it is actually needed.

Add unit tests for the optimizer detection helper, the dtype selection
in validate_and_prepare_config, and the restore gating in
setup_model_and_optimizer.

Closes NVIDIA-NeMo#2865

Signed-off-by: Doondi Ashlesh <[email protected]>
@Doondi-Ashlesh
Doondi-Ashlesh requested review from a team as code owners June 22, 2026 21:36
@copy-pr-bot

copy-pr-bot Bot commented Jun 22, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-maintainers Waiting on maintainers to respond label Jun 25, 2026
@Doondi-Ashlesh

Copy link
Copy Markdown
Author

Hi, checking in on this PR, can a maintainer please review it. Happy to make further changes if required. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-request waiting-on-maintainers Waiting on maintainers to respond

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Automodel]: Hard-coded fp32 model weights doubles memory with FusedAdam

2 participants