Repository navigation
fix: load policy in compute dtype when optimizer holds fp32 master weights - #2888
Open
Doondi-Ashlesh wants to merge 2 commits into
Open
Doondi-Ashlesh wants to merge 2 commits into
Doondi-Ashlesh wants to merge 2 commits into
Conversation
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]>
Author
|
Hi, checking in on this PR, can a maintainer please review it. Happy to make further changes if required. Thanks! |
1 of 2 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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=float32to preserve optimizer master weights. When the optimizer already keeps its own fp32 master (TE FusedAdam withmaster_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:
master_weights): load in the compute dtype (e.g. bf16) and let the optimizer own the master.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):
Usage
No config or API change. Existing recipes that use FusedAdam with
master_weights: trueautomatically load in the compute dtype; AdamW recipes are unchanged.Additional Information
tests/unit/models/automodel/test_automodel_setup.pycover the optimizer detection helper, the dtype selection invalidate_and_prepare_config, and the restore gating insetup_model_and_optimizer.torchandnemo_automodel, so please trigger CI to run the full suite. The AdamW path is unchanged, so existing behavior and tests are preserved.