Skip to content

fix(automodel): preserve native FP32 A_log during refit - #5456

Open
sharonyu-115 wants to merge 3 commits into
NVIDIA-NeMo:mainfrom
sharonyu-115:fix/automodel-refit-a-log-fp32
Open

sharonyu-115 wants to merge 3 commits into
NVIDIA-NeMo:mainfrom
sharonyu-115:fix/automodel-refit-a-log-fp32

Conversation

@sharonyu-115

@sharonyu-115 sharonyu-115 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What and why

Split out of #4963 at review request (comment): this change is independent of Automodel single-controller training and affects the existing refit path.

During Automodel refit, _refit_tensor_dtype casts floating-point tensors to the refit dtype (bf16) before they are streamed to the inference engine. SSM A_log parameters are stored as FP32 in the checkpoint and stay FP32 in the inference model, so the payload was downcast to bf16 and then copied back into FP32 storage, losing precision on the way.

This PR keeps source-FP32 A_log tensors in FP32 in the refit payload and in the refit metadata, the same way e_score_correction_bias is already handled. A unit test, test_refit_ssm_a_log_precision_and_metadata, covers the payload dtype and the metadata.

The change is by @yuekaizhang; it is cherry-picked unchanged from #4963 and rebased on current main (f35d1c2). The only rebase-time edit is in the new unit test: the object.__new__ worker fixture now sets pipeline = None and _weight_exporter = None, which prepare_refit_info reads since #4960 / #4963 landed (same as the neighbouring fixtures on main).

Issues

None

Convergence / perf / functionality proof

V1 Stack validation - H100, 2 x 8 GPUs, 200 steps (W&B)

Same recipe (grpo-qwen3.5-35ba3b-2n8g-automodel-ep16, stock 2 x 8 layout, seed 42) repeated on H100 with checkpoint-resume across chained 4-hour jobs. "Before" is main at e77523f, "after" is e77523f plus this change (07cbc78). Before reached step 192 and after reached step 196 before the job chain ran out, so the table covers the 192 steps both runs completed.

W&B project: https://wandb.ai/nv-welcome/nemo-rl-pr5456-alog-fp32 (runs before, after).

Metric (steps 1-192) Before mean Before max After mean After max After lower on x% of steps Welch p
train/gen_kl_error 0.000859 0.000938 0.000816 0.000916 85% 8.4e-23
train/token_mult_prob_error 1.03186 2.999 1.01789 1.090 69% 0.20
train/policy_kl_error 0.01481 1.982 0.00133 0.074 80% 0.21
train/sampling_importance_ratio 0.999995 1.00010 1.000002 1.00012 47% 0.05
train/reward 0.5928 0.8027 0.5963 0.8008 44% 0.66
train/loss 0.01094 0.0555 0.01226 0.0679 51% 0.49
train/grad_norm 0.1048 0.313 0.1081 0.648 47% 0.42
train/num_masked_seqs_by_logprob_error 0 0 0 0 - -
timing/train/total_step_time (s) 373.8 915 367.4 659 63% 0.53

Last 50 common steps (143-192): gen_kl_error before 0.000856 vs after 0.000786; token_mult_prob_error before 1.0313 vs after 1.0172; reward before 0.617 vs after 0.621.

Generation/training mismatch metrics (blue = before, orange = after):

mismatch metrics

Reward, loss, grad norm:

training metrics

Reading:

  • train/gen_kl_error is lower with the fix on 85% of steps (mean 0.000859 vs 0.000816). Both arms are a single seed and per-step values are autocorrelated, so the Welch p column is indicative only; the 85%-of-steps figure and the last-50-step means are the summaries to rely on. The two runs overlap for the first ~20 steps and then separate, with the gap growing over training. This is expected from the checkpoint: all 960 FP32 A_log values in Qwen3.5-35B-A3B-Base are exactly bf16-representable (low 16 bits zero), so the bf16 payload is lossless at step 0 and only loses precision once optimizer updates move A_log off the bf16 grid.
  • The before run has two large mismatch spikes (step 123: token_mult_prob_error 3.0, policy_kl_error 1.98; step 158: 1.65 / 0.63). The after run's worst step is 70 (1.09 / 0.074). This is a single seed per arm and the trajectories diverge after the first ~20 steps, so the spike count is suggestive rather than conclusive; the per-step gen_kl_error shift is the robust signal.
  • Reward, loss, grad norm, sampling importance ratio and step time are statistically indistinguishable (Welch p > 0.4 except importance ratio at 0.05, whose mean difference is 7e-6). No sequences were masked by logprob error in either run.

Unit test on the rebased branch (test_refit_ssm_a_log_precision_and_metadata, 3 parametrizations, plus the other -k refit tests in test_automodel_worker.py) run in the CI nightly container: 11 passed, 0 failed (job 23190116, rl.nightly.sqsh 2026-11-28).

GB200, 4 x 4 GPUs, 17 steps (TensorBoard)

Original matched before/after run of grpo-qwen3.5-35ba3b-2n8g-automodel-ep16 (non-single-controller path) on Qwen3.5-35B-A3B-Base, whose checkpoint has 30 FP32 A_log tensors of shape (32,). "Before" is main at e77523f; "after" is the same commit plus this change. Same recipe, seed and overrides on both sides.

Layout: 4 nodes x 4 GB200 GPUs instead of the recipe's 2 x 8, with cluster.num_nodes=4 cluster.gpus_per_node=4 policy.generation.vllm_cfg.tensor_parallel_size=4, W&B off, and policy.automodel_cfg.env_vars.PYTORCH_CUDA_ALLOC_CONF=expandable_segments:False. Both jobs hit the 150-minute limit before step 20, so the table compares the first 17 steps, which both runs completed. There are no W&B links; the numbers come from the TensorBoard scalars.

Metric (steps 1-17) Before, mean Before, max After, mean After, max
train/gen_kl_error 0.000869 0.000927 0.000869 0.000918
train/token_mult_prob_error 1.01821 1.01986 1.01832 1.02040
train/policy_kl_error 0.00101 0.00277 0.00105 0.00314
train/sampling_importance_ratio 0.99999 1.00004 1.00000 1.00009
train/reward 0.5476 0.6895 0.5478 0.6895

Per-step train/gen_kl_error:

before 0.00086 0.00085 0.00090 0.00086 0.00086 0.00086 0.00089 0.00090 0.00087 0.00078 0.00084 0.00093 0.00088 0.00088 0.00091 0.00088 0.00083
after  0.00086 0.00087 0.00091 0.00085 0.00085 0.00084 0.00089 0.00089 0.00087 0.00078 0.00085 0.00089 0.00089 0.00087 0.00092 0.00089 0.00084

Per-step train/token_mult_prob_error:

before 1.01808 1.01774 1.01861 1.01828 1.01818 1.01747 1.01855 1.01887 1.01840 1.01611 1.01770 1.01873 1.01836 1.01986 1.01884 1.01824 1.01762
after  1.01804 1.01792 1.01873 1.01842 1.01801 1.01732 1.01855 1.01863 1.01836 1.01624 1.01795 1.01888 1.01856 1.02040 1.01909 1.01852 1.01779

Reading: over 17 steps the two runs are within step-to-step noise. This matches the checkpoint fact above: at step 0 the bf16 payload is lossless, so a short run cannot show a difference. The longer H100 run above is where the gen_kl_error separation becomes visible.

Needs upstream changes

None

PR commands and CI
  • CI label, exactly one:
    • CI:Lfast: the default for most PRs. Runs a fast test subset.
    • CI:L1: dependency or container changes (pyproject.toml, uv.lock, Dockerfile) or broad core changes. Builds a new image and runs unit and functional tests.
    • CI:docs: docs-only changes. Runs doc tests only.
  • /ok to test <full-commit-sha>: start CI for that commit. Post again after each push.
  • /internal-ci test-case=<job>: run NVIDIA's internal nemo-ci pipeline for the PR head; eligible teams only.
  • /review: automated review comments. Add mode=strict for a stricter review, reset=true for a fresh full review.
  • /resolve-conflict: Autobot merges main into this PR and resolves the conflicts. PRs from forks must allow edits by maintainers.
  • Locally: /review-pr-team <pr-number> for a team review before you ask for human review.
  • Contributor guidelines

@sharonyu-115 sharonyu-115 added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Nov 26, 2026
@copy-pr-bot

copy-pr-bot Bot commented Nov 26, 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.

@sharonyu-115
sharonyu-115 force-pushed the fix/automodel-refit-a-log-fp32 branch from 07cbc78 to 176ac81 Compare November 30, 2026 09:21
@sharonyu-115

Copy link
Copy Markdown
Contributor Author

/ok to test 176ac81

@svcnvidia-nemo-ci svcnvidia-nemo-ci added complexity:high Large or cross-cutting change. Set by Autobot PR classify (skills/pr-classify) convergence-risk:high Likely to change training results. Set by Autobot PR classify (skills/pr-classify) proof:functionality Needs proof that it works. Set by Autobot PR classify (skills/pr-classify) proof:convergence Needs convergence proof (W&B runs). Set by Autobot PR classify (skills/pr-classify) labels Nov 30, 2026

@sharonyu-115 sharonyu-115 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for splitting this out of #4963 with its own evidence. The change keeps source-FP32 A_log tensors FP32 in the Automodel refit payload and metadata, the same way e_score_correction_bias is handled, so the inference engine's fp32 A_log storage no longer receives a bf16-rounded value every refit.

What the review checked against pinned source (vLLM v0.33.0, SGLang 3003d70f, Automodel b916107a, transformers 5.16.1):

  • The premise holds. vLLM and SGLang allocate A_log fp32 and load it with a dtype-casting copy (weight_utils.py#L1543). Mamba2 engines store A fp32 from HF A_log via -exp(x.float()) (mamba_mixer2.py#L481-L496), so Nemotron-H style models benefit too.
  • Metadata and payload agree on every path. Each transport sizes receive buffers from per-tensor metadata dtype (IPC vllm_backend.py#L2038-L2045, collective packed_tensor.py#L256). The pipeline-parallel exporter takes its manifest dtypes from the same generator (automodel_policy_worker.py#L671-L678, pipeline_parallel.py#L515-L520), and the draft branch applies the same function.
  • The name domain is right. The predicate sees HF-adapted names, and every transformers 5.16.1 SSM model spells the leaf A_log.
  • The name-list shape is the right design. C2 explains why a blanket "preserve source fp32" rule or importing Automodel's list would both be wrong.
  • The new test reaches both call sites. Both literals change under bf16 rounding (0.123456 -> 0.12353515625, 0.876543 -> 0.875), the fp64 case exercises the == float32 half of the guard, and the automodel mark routes it to the L0_Unit_Tests_Automodel_Policy shards where the skipif cannot fire silently. Not executed here (no GPU/torch on the review node); the body reports 11 passed in the nightly container.
  • A checkpoint fact in favour of the change: all 960 fp32 A_log values in Qwen3.5-35B-A3B-Base are exactly bf16-representable, which explains why the effect appears only after training moves them.

Inline comments:

  1. (C1) Restore the GB200 17-step A/B next to the H100 run and re-save the body with LF endings so the pr-description check can pass.
  2. (C2) Docstring nit: name the owner of the fp32 set and the rule for adding to it.

FYI, no action in this PR:

  • Kimi-K3 (KDA) keeps dt_bias and the conv1d weights fp32 in both Automodel (kimi_k3/state_dict_adapter.py#L36-L44) and vLLM (kimi_gdn_linear_attn.py#L237-L239, #L265-L267). A leaf-name rule cannot express that (the conv leaf is weight). NeMo-RL has no Kimi recipe today, so this is a note for when one lands.
  • Follow-up idea: the fp32-preserve set has no test in a lane that can import the engine, so engine-side changes would drift silently. A vllm-lane check of the layer constructors' fp32 parameters against this set would close that.

Linters: pinned ruff v0.9.9 (check, isort, format) pass on both files; pre-commit itself could not run on the review node. CI: L0/L1 jobs were pending at review time; pr-description is red for the CRLF reason in C1.

Process note: lanes ran as named teammates (rl-expert, comment-reviewer, test-agent, bug-finder, design-reviewer, devil-advocate, comment-editor) with peer messaging; this build has no shared task list, so the leader brokered hand-offs.

nemo-rl-review: kind=self harness=claude-code/2.1.319 model=claude-fable-5-1 effort=medium review-pr-team=v3 reviewed=176ac81

…tend it

Append to the _refit_tensor_dtype docstring that the preserved-name set
mirrors which parameters the inference engines (vLLM, SGLang) allocate as
FP32, why it cannot be derived from the trainer's source dtype (all trainer
params are FP32 master weights), and the rule for adding a leaf.

Addresses review comment C2 on NVIDIA-NeMo#5456.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Shuang Yu <[email protected]>
@sharonyu-115

Copy link
Copy Markdown
Contributor Author

/ok to test 1778ce7

@sharonyu-115

Copy link
Copy Markdown
Contributor Author

/ok to test ccca9ce

@sharonyu-115
sharonyu-115 requested a review from yuki-97 November 30, 2026 13:36
@sharonyu-115

Copy link
Copy Markdown
Contributor Author

Hi @yuki-97 would you please help review this PR? Thank you very much!

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

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) complexity:high Large or cross-cutting change. Set by Autobot PR classify (skills/pr-classify) convergence-risk:high Likely to change training results. Set by Autobot PR classify (skills/pr-classify) proof:convergence Needs convergence proof (W&B runs). Set by Autobot PR classify (skills/pr-classify) proof:functionality Needs proof that it works. Set by Autobot PR classify (skills/pr-classify)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants