Repository navigation
Description
Summary
The automodel backend hard-codes the model load dtype to fp32 in order to support master weights here.
However, the optimizer is configurable and supports both torch.optim.AdamW and TE FusedAdam. When FusedAdam is used, the fp32 load is unnecessary and causes ~2× optimizer master-weight memory (and ~2× model-weight memory), because FusedAdam already maintains its own fp32 master internally.
Details
With the TE FusedAdam optimizer, the fp32 master is kept inside the optimizer, via:
optimizer:
name: transformer_engine.pytorch.optimizers.fused_adam.FusedAdam
kwargs:
master_weights: true
store_param_remainders: truestore_param_remainders: true lets FusedAdam split the fp32 master into bf16 (high 16 bits) + int16 remainder (low 16 bits) — 16 + 16 = 32 = fp32. This depends on the parameters being bf16: once a parameter is fp32, TE detects param.dtype != bf16 and skips the remainder path, storing a separate full fp32 master instead. Ref: https://github.com/NVIDIA/TransformerEngine/blob/42b840051647eef89761a16dfdff87e82bb253ab/transformer_engine/pytorch/optimizers/fused_adam.py#L97
So when the load dtype is hard-coded to fp32, with FusedAdam:
- Model weights: bf16 → fp32 = 2× memory.
- Optimizer master:
bf16 + int16 remainder→ full fp32 = 2× memory.
Reproduction
Run a grpo training, toggling only the model load dtype (AutoConfig(torch_dtype=...) in nemo_rl/models/automodel/setup.py) between
float32 and bfloat16, and sum the optimizer.state tensor bytes after the first optimizer.step().
Observed (rank 0)
| metric | fp32 load (current) | modified to bf16 load |
|---|---|---|
master_param |
fp32 7.32 GB + int16 0.60 GB | int16 — 4.26 GB |
| optimizer state /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 |
exp_avg / exp_avg_sq are unchanged (bf16); the entire increase is the master copy.
Suggested fix
Choose the model load dtype based on the optimizer (AdamW vs FusedAdam) instead of hard-coding fp32: load fp32 only for optimizers without an internal master, and keep the compute dtype (bf16) when FusedAdam holds the master.
For reference, I have a candidate fix in: e968d90
Activity
[-]Hard-coded fp32 model load doubles memory with FusedAdam[/-][+][Automodel]: Hard-coded fp32 model weights doubles memory with FusedAdam[/+]tianyi-zhang-02 commentedon Aug 24, 2026
Picking this up, with one correction to the framing that I think changes the shape of the fix.
The fp32 load is not simply unnecessary.
setup.py:539documents why it is there: Automodel's_restore_loaded_model_dtypere-casts parameters back to the bf16 checkpoint dtype, "silently downgrading the master weights so AdamW updates underflow and the model fails to learn (e.g. grpo-nano-v2-12b reward stuck ~0.18)". NeMo-RL no-ops that function specifically to keep the fp32 load alive. So under plaintorch.optim.AdamWit is load-bearing, with a recorded production failure behind it.Your claim is narrower than "the fp32 load is unnecessary", and as far as I can tell correct: with TE FusedAdam under
master_weights: true/store_param_remainders: truethe optimizer keeps its own fp32 master, so the fp32 model load is redundant in that configuration.That makes the change a conditional — load fp32 only when the optimizer does not provide its own master — rather than a removal. Feasibility is not the issue:
validate_and_prepare_config(config: PolicyConfig, ...)has the whole policy config in scope, so the optimizer block is reachable where the dtype is chosen. The risk is that getting the condition wrong reproduces exactly the silent-underflow failure the workaround exists to prevent, and that failure is silent — the model simply does not learn. So it wants a loud guard rather than a quiet switch, and a test that asserts the AdamW path still gets fp32.Before proposing a patch I am going to measure four combinations on one card — bf16/fp32 × AdamW/FusedAdam — reporting parameter bytes, optimizer state bytes and peak, plus a short overfit on a fixed batch for each. The overfit is the part I actually care about: a configuration that saves memory and stops learning is not a win, and bf16 + AdamW should visibly fail as the control.
Will post the numbers here before opening anything. If someone is already on this, say so and I will stop :)
tianyi-zhang-02 commentedon Aug 24, 2026
Numbers, as promised — and a change of plan at the end, because I found three PRs already doing this.
Memory, measured
RTX 4090,
Qwen/Qwen2.5-0.5B, plaintorch.optim.AdamW, 20 steps on a fixed batch:Exactly 2× on parameters, 2× on optimizer state, 1.98× on peak. So the "doubles memory" in the title holds on the axis I can reach here.
Correctness — my first probe was wrong, so here is a better one
I had planned a 20-step overfit to show the fp32 load is load-bearing. It does not discriminate: bf16 + AdamW reached loss
0.0015against fp32's0.0011. That is not evidence the fp32 load is unnecessary — it is evidence the probe was bad. Overfitting a tiny batch produces large relative updates, which is exactly the regime where bf16 rounding does not bite, while the failure recorded insetup.py:539is many small updates on a large model.So I measured the mechanism directly. For a weight
pand updateu, the update is lost whenbf16(p + u) == bf16(p). Sweeping the update-to-weight ratio over 2,000,000 real weight coordinates sampled from the model:The cliff sits exactly where bf16's 8 mantissa bits put it (2⁻⁸ ≈ 0.39% relative resolution). Below roughly 0.2% relative update, every coordinate loses its update at bf16 and none does at fp32.
That is the quantitative form of the
setup.py:539comment, and it says the fp32 load is genuinely load-bearing under plain AdamW rather than defensive. Whatever the conditional ends up being, theelsebranch has to stay fp32.What I could not measure
Cases C and D — fp32 vs bf16 under TE FusedAdam — are the half that actually decides this issue, and I cannot run them here. This box has no
nvcc, andtransformer_engineon PyPI installs as a meta-package that refuses to import (Found empty transformer-engine meta package); the[pytorch]extra needs--no-build-isolation, i.e. a source build. So I have no independent number for the FusedAdam memory saving, only the AdamW axis above. Anyone with TE in CI can produce it with the same script.The change of plan
Before writing anything I checked for collisions, and there are three open PRs on this already:
avoid fp32 weight load when optimizer holds master) — @terrykong asked @hemildesai to review on 2026-07-18, silent since.load policy in compute dtype when optimizer holds fp32 master weights) — author asked for review, no maintainer response since 2026-07-14.configurable DTensor load precision) — adjacent, silent since 2026-06-03.Both #3156 and #2888 land on the same condition:
so a fourth PR would add to a pile that is not being reviewed rather than resolve anything. I am not going to open one.
There is one real difference between them worth a decision, and the numbers above bear on it. #2888 additionally re-enables the checkpoint-dtype restore on the bf16 path:
#3156 leaves that no-op unconditional. On the bf16 path the restore is harmless (there are no fp32 master weights to downgrade), so #2888's version is the more correct of the two — but it is also the line where getting the condition wrong reproduces the silent-underflow failure, and per the table above that failure is total, not partial: 100% of sub-0.2% updates, with no warning and no crash.
So the ask is a review of #3156 or #2888 rather than another patch. Happy to run any measurement that would help settle it — including the FusedAdam half if someone can point me at a way to get TE onto a machine without
nvcc.check_2865_fp32_load.pyandcheck_2865_update_underflow.pyproduced the two tables; I can post either inline if useful.tianyi-zhang-02 commentedon Aug 24, 2026
Correction to my previous comment: "This box has no
nvcc" was wrong, and I would rather not leave a false reason standing for why I could not measure the FusedAdam half.which nvcccame back empty, and I turned that into "no CUDA toolchain". It only meant "not on PATH".nvcc13.3 was there the whole time, shipped inside torch's own pip dependencies:Pointing
CUDA_HOMEat that directory (plus alib64 -> libsymlink, which the torch extension builder expects) gets pastCUDA_HOME environment variable is not setand CUDA extensions do start compiling.What actually blocks the
[vllm]extra here is narrower and worth stating precisely, since it may affect other outside contributors trying to reproduce anything on this path:deep_epneedslibibverbs-dev, which yourpyproject.tomlalready notes on the line above it. That is a system package, and I have no root on this box, so that one is genuinely out of reach.deep_epanddeep_gemmexcluded,uv sync --extra vllmcompletes and gives a workingvllm 0.25.1 / torch 2.11.0+cu130environment.transformer_enginestill is not in that environment, so I still do not have the FusedAdam number. That is now an open question about the TE build rather than a missing compiler, and I will say so when I know which.So the substance of the previous comment stands — the memory table, the underflow table, and the point that #3156 and #2888 are both sitting unreviewed. Only the stated reason for the missing C/D rows was wrong.
Two smaller things I should also not have implied. The AdamW memory numbers are from a 0.5B model on one card; the ratio is what I would carry over, not the absolute GiB. And the underflow table is a property of bf16 storage, measured on this model's weight distribution — it says when an update is lost, not how often real Adam steps land in that regime, which depends on model scale and learning rate.