Skip to content

fix(automodel): avoid fp32 weight load when optimizer holds master - #3156

Closed
lonexreb wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
lonexreb:fix/2865-automodel-load-dtype
Closed

lonexreb wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
lonexreb:fix/2865-automodel-load-dtype

Conversation

@lonexreb

Copy link
Copy Markdown
Contributor

What

The automodel (DTensor) backend hard-coded the model load dtype to float32 in nemo_rl/models/automodel/setup.py so the optimizer could keep fp32 master weights. When the optimizer maintains its own fp32 master — TE FusedAdam with master_weights=True (bf16 params + int16 remainder) — that fp32 load is redundant and roughly doubles both model-weight and optimizer master-weight memory.

Fixes #2865.

Change

Pick the load dtype from the optimizer config in validate_and_prepare_config:

  • master_weights=True (optimizer holds the master) → load in the compute dtype.
  • plain AdamW / no master_weights / no optimizer configured → keep the fp32 load (unchanged behavior).

Only the load dtype changes; the FSDP mixed-precision policy and everything downstream are untouched.

Impact

No behavior change for the exemplar configs (they use torch.optim.AdamW, which has no internal master → still fp32). The savings apply only to the FusedAdam+master_weights path the issue reports (~131GB → ~102GB optimizer state, ~67GB → ~36GB model params in the reporter's run).

Test plan

  • New unit tests in tests/unit/models/automodel/test_automodel_setup.py asserting the torch_dtype passed to AutoConfig.from_pretrained:
    • plain AdamW → float32
    • FusedAdam + master_weights=True → compute dtype (bf16)
    • optimizer absent → float32
  • CI (/ok to test)

Note: local test run blocked by an unrelated uv/Python mac_ver() issue on the dev box; logic validated in isolation and syntax-checked. CI will exercise the full suite.

The automodel backend always loaded model weights in float32 so the
optimizer could keep fp32 master weights. But TE FusedAdam with
master_weights=True maintains its own fp32 master internally (bf16 params
+ int16 remainder). Forcing an fp32 load in that case is redundant and
roughly doubles both model-weight and optimizer master-weight memory
(~131GB -> ~102GB optimizer state in the reporter's run).

Pick the load dtype from the optimizer config: load in the compute dtype
when the optimizer keeps its own fp32 master (master_weights=True), and
keep the fp32 load otherwise (plain AdamW, or no optimizer configured).

Fixes NVIDIA-NeMo#2865.

Signed-off-by: Shubhankar Tripathy <[email protected]>
@lonexreb
lonexreb requested review from a team as code owners July 10, 2026 15:56
@copy-pr-bot

copy-pr-bot Bot commented Jul 10, 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.

@lonexreb

Copy link
Copy Markdown
Contributor Author

/ok to test 718c17b

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-maintainers Waiting on maintainers to respond label Jul 12, 2026

@terrykong terrykong left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hemildesai could you review?

@terrykong
terrykong requested a review from hemildesai July 16, 2026 06:43
@svcnvidia-nemo-ci svcnvidia-nemo-ci added waiting-on-maintainers Waiting on maintainers to respond and removed waiting-on-maintainers Waiting on maintainers to respond labels Jul 16, 2026
@lonexreb

Copy link
Copy Markdown
Contributor Author

Gentle ping @hemildesai @terrykong — this has been green and mergeable since mid-July (all checks passing, no conflicts). Happy to address any feedback or rebase if you'd prefer it freshened before review.

@svcnvidia-nemo-ci svcnvidia-nemo-ci removed the waiting-on-maintainers Waiting on maintainers to respond label Aug 30, 2026
@lonexreb

Copy link
Copy Markdown
Contributor Author

@hemildesai @yuki-97 — gentle ping when you have bandwidth. This fixes #2865: the automodel path loads weights in fp32 even when the precision-aware optimizer already holds fp32 master weights, doubling load-time memory for no benefit. All pre-CI checks are green and the branch is mergeable against current main; it still needs a CI:* label to run the full tier. Happy to address any feedback or rebase again if it goes stale.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-maintainers Waiting on maintainers to respond label Sep 2, 2026
@lonexreb

Copy link
Copy Markdown
Contributor Author

Closing: main has since landed a more complete version of this fix in #4143 — _has_optimizer_fp32_master(optimizer_cls, optimizer_kwargs) now gates the load dtype (nemo_rl/models/automodel/setup.py:766-773), including torch.* string normalization on the optimizer kwargs and a frozen-model (no-optimizer) branch this PR didn't cover, so there is nothing left for this diff to add.

For the record on #2865: @tianyi-zhang-02's measurements there confirmed both halves of this PR's premise — ~2× params/optimizer-state/peak from the fp32 load, and the fp32 else branch being genuinely load-bearing under plain AdamW (100% of sub-0.2%-relative updates lost at bf16). Worth also closing #2888/#2622 against #4143 if a maintainer agrees they're subsumed.

@lonexreb lonexreb closed this Sep 27, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci removed the waiting-on-maintainers Waiting on maintainers to respond label Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants