Skip to content

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

Open

Description

@jQizhang

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: true

store_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

  1. added
    bug
    on Jun 17, 2026
  2. changed the title [-]Hard-coded fp32 model load doubles memory with FusedAdam[/-] [+][Automodel]: Hard-coded fp32 model weights doubles memory with FusedAdam[/+] on Jun 17, 2026
  3. tianyi-zhang-02 commented on Aug 24, 2026

    @tianyi-zhang-02
    Contributor

    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:539 documents why it is there: Automodel's _restore_loaded_model_dtype re-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 plain torch.optim.AdamW it 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: true the 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 :)

  4. tianyi-zhang-02 commented on Aug 24, 2026

    @tianyi-zhang-02
    Contributor

    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, plain torch.optim.AdamW, 20 steps on a fixed batch:

    case params optimizer state peak allocated
    bf16 load 0.920 GiB 1.840 GiB 4.697 GiB
    fp32 load 1.840 GiB 3.681 GiB 9.310 GiB

    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.0015 against fp32's 0.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 in setup.py:539 is many small updates on a large model.

    So I measured the mechanism directly. For a weight p and update u, the update is lost when bf16(p + u) == bf16(p). Sweeping the update-to-weight ratio over 2,000,000 real weight coordinates sampled from the model:

    update / |w| lost at bf16 lost at fp32
    1e-1 0.0% 0.0%
    1e-2 0.0% 0.0%
    1e-3 100.0% 0.0%
    1e-4 … 1e-7 100.0% 0.0%

    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:539 comment, and it says the fp32 load is genuinely load-bearing under plain AdamW rather than defensive. Whatever the conditional ends up being, the else branch 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, and transformer_engine on 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:

    Both #3156 and #2888 land on the same condition:

    load_dtype = dtype if optimizer_kwargs.get("master_weights", False) else torch.float32

    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:

    if not _optimizer_holds_master_weights(config):
        _disable_automodel_checkpoint_dtype_restore()

    #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.py and check_2865_update_underflow.py produced the two tables; I can post either inline if useful.

  5. tianyi-zhang-02 commented on Aug 24, 2026

    @tianyi-zhang-02
    Contributor

    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 nvcc came back empty, and I turned that into "no CUDA toolchain". It only meant "not on PATH". nvcc 13.3 was there the whole time, shipped inside torch's own pip dependencies:

    .../site-packages/nvidia/cu13/bin/nvcc      # Cuda compilation tools, release 13.3, V13.3.73
    .../site-packages/nvidia/cu13/include/cuda_runtime.h
    

    Pointing CUDA_HOME at that directory (plus a lib64 -> lib symlink, which the torch extension builder expects) gets past CUDA_HOME environment variable is not set and 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_ep needs libibverbs-dev, which your pyproject.toml already 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.
    • With deep_ep and deep_gemm excluded, uv sync --extra vllm completes and gives a working vllm 0.25.1 / torch 2.11.0+cu130 environment.
    • transformer_engine still 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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Memorybug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions