Skip to content

fix(automodel): merge LoRA into refit weights under activation checkpointing - #4810

Merged
yuki-97 merged 5 commits into
NVIDIA-NeMo:mainfrom
yuekaizhang:fix-lora-merge-under-activation-checkpointing
Oct 22, 2026
Merged

yuki-97 merged 5 commits into
NVIDIA-NeMo:mainfrom
yuekaizhang:fix-lora-merge-under-activation-checkpointing

Conversation

@yuekaizhang

@yuekaizhang yuekaizhang commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do ?

Fixes the silent LoRA refit failure on the Automodel (DTensor) backend when
activation_checkpointing: true (#4470): the adapters trained on the policy were never merged
into the weights sent to the inference engine, so vLLM kept sampling from the base model for the
whole run while lora_A/lora_B kept moving. Reported on Nemotron 3.5 Lightning (#4470),
Nemotron 3.5 Super (#4259) and Nemotron 3.5 Super VL (a partner run: train reward flat at ~0.45,
~0.83 with the fix). Every Automodel LoRA + activation-checkpointing recipe is affected, including
grpo-qwen3-8B-base-1n8g-automodel-lora in the nightlies.

Root cause. With activation checkpointing Automodel wraps every decoder block in torch's
CheckpointWrapper. state_dict() strips the wrapper's _checkpoint_wrapped_module. component
from its keys, named_modules() keeps it, and _module_params_generator built its module map
from named_modules():

source name
named_modules() layers.0._checkpoint_wrapped_module.q_proj
state_dict() layers.0.q_proj.weight

_maybe_merge_lora_weight therefore found no LinearLoRA for any weight inside a block and
returned the bare base tensor; only LoRA modules outside the wrapper (e.g. lm_head) were merged.

Fix.

  • Key the module map by Automodel's nemo_automodel.shared.parameter_names.canonical_parameter_fqn(),
    the same state-dict spelling Automodel's own checkpoint and optimizer code use, so NeMo RL follows
    Automodel's naming contract instead of hard-coding wrapper prefixes.
  • Raise if a LinearLoRA still has no matching state_dict weight, so this class of failure is loud
    instead of a slowly growing gen_kl_error.
  • Merge in fp32 and round once, so the refit tensor equals an offline fp32 merge of the same adapter
    (the bf16 two-step merge could differ by one ulp per element).

Tests. test_merges_lora_inside_activation_checkpoint_wrapper fails on main (the refit tensor
equals the base weight); test_raises_when_lora_module_has_no_state_dict_weight covers the guard;
test_bf16_lora_merge_matches_offline_fp32_merge covers the fp32 merge.

Nightly. grpo-qwen3-8B-base-1n8g-automodel-lora before/after, 2 x 4 GB200 GPUs, 20 steps,
same base commit, only this fix differing:

without fix with fix
gen_kl_error steps 1-20 flat 0.0010 until step 16, then 0.0013 / 0.0014 / 0.0015 / 0.0015 0.0010-0.0011 throughout
validation accuracy @0 / 10 / 20 0.289 / 0.328 / 0.242 0.289 / 0.313 / 0.387
max train reward 0.37 0.44
image Reward and the 256-sample validation stay within noise over 20 steps; the monotonic `gen_kl_error` climb (adapter drifting away from the frozen generator) is the only metric that separates the two, and the previous `< 0.002` bound passed both. The nightly now bounds the final and the maximum `gen_kl_error` at 0.0013. These numbers come from GB200; the H100 nightly may need a small adjustment after its first run.

A longer 100-step run of the same recipe on 1 x 4 GB200 GPUs shows the failure is not a small
drift: without the fix the generator never receives the adapter, so the trained policy walks
away from it until the run collapses, while the fixed run keeps learning.

without fix with fix
gen_kl_error @10 / 50 / 80 / 100 0.0012 / 0.064 / 2.12 / 27.0 0.0011 / 0.0015 / 0.0014 / 0.0014
validation accuracy @0 / 50 / 100 0.328 / 0.320 / 0.332 0.328 / 0.543 / 0.633
mean train reward, steps 1-20 / 41-60 / 81-100 0.295 / 0.307 / 0.313 0.307 / 0.542 / 0.578

Related. Consolidates #3953 (anubhutivyas: prefix strip + guard + fp32 merge) and #4259
(zhiyuanma-agents: prefix strip incl. _orig_mod, fp32 merge, tests), both written against the old
dtensor_policy_worker_v2.py and no longer applying to main, plus the local patch from
sniu@salesforce (co-authored here) that first used canonical_parameter_fqn. _orig_mod.
(torch.compile) is not handled: NeMo RL's Automodel path does not compile the policy, and
OptimizedModule does not strip the prefix from state_dict() keys the way CheckpointWrapper
does; if that changes it belongs in Automodel's canonical_parameter_fqn.

Usage

No user-facing change. Any Automodel recipe with automodel_cfg.lora_cfg.enabled: true and
automodel_cfg.activation_checkpointing: true now refits the merged weights.

yuekaizhang and others added 2 commits October 19, 2026 23:37
…ointing

With activation_checkpointing: true Automodel wraps every decoder block in
torch's CheckpointWrapper. state_dict() strips the wrapper's
"_checkpoint_wrapped_module." component from its keys but named_modules()
keeps it, so the module map that the refit exporter built from
named_modules() never matched a per-layer state_dict weight name.
_maybe_merge_lora_weight then found no LinearLoRA for those weights and the
refit streamed bare base weights to the inference engine: generation kept
sampling the base model for the whole run while training updated the
adapters (only LoRA modules outside the wrapper, e.g. lm_head, were merged).

Key the module map by Automodel's canonical_parameter_fqn(), the same
state-dict spelling its checkpoint and optimizer code use, so the exporter
follows Automodel's naming contract instead of hard-coding wrapper prefixes.
Raise if a LinearLoRA still has no matching state_dict weight rather than
silently dropping its update. Merge in fp32 and round once so the refit
tensor equals an offline fp32 merge of the same adapter (the bf16 two-step
merge could differ by one ulp per element).

Tests: the checkpoint-wrapper case fails on main (refit tensor equals the
base weight); the guard and the fp32 merge each have a test.

Co-authored-by: sniu <[email protected]>
Signed-off-by: Yuekai <[email protected]>
… catches a broken refit

Before/after runs of grpo-qwen3-8B-base-1n8g-automodel-lora (2 x 4 GB200 GPUs,
20 steps) with and without the LoRA refit fix: with the adapters merged into
the refit weights gen_kl_error stays at 0.0010-0.0011 for all 20 steps; with
the inference engine stuck on the base weights it climbs to 0.0013-0.0015 over
steps 17-20, while max reward (0.44 vs 0.37) and the 256-sample validation
accuracy stay within noise. The previous 0.002 bound passed both runs, so the
nightly never caught the bug. Bound the final and maximum gen_kl_error at
0.0013 instead.

Signed-off-by: Yuekai <[email protected]>
@yuekaizhang

Copy link
Copy Markdown
Contributor Author

/ok to test 9c12c91

@copy-pr-bot

copy-pr-bot Bot commented Oct 20, 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.

@yuekaizhang yuekaizhang added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Oct 20, 2026
@yuekaizhang

Copy link
Copy Markdown
Contributor Author

/ok to test bd46301

@yuki-97
yuki-97 requested a review from RayenTian October 20, 2026 08:49
@RayenTian

Copy link
Copy Markdown
Contributor

Thanks a lot for adding this @yuekaizhang ! Left some comments.

@yuekaizhang

Copy link
Copy Markdown
Contributor Author

/ok to test f107761

@yuekaizhang

Copy link
Copy Markdown
Contributor Author

/ok to test 633cdf1

@yuki-97
yuki-97 merged commit 688880b into NVIDIA-NeMo:main Oct 22, 2026
94 checks passed
rahul-sarvam added a commit to rahul-sarvam/RL that referenced this pull request Oct 31, 2026
Merge Automodel's grouped expert adapters into gate_and_up_projs and
down_projs before HF conversion during merged refitting, and exclude the
bare adapter parameters from both streamed weights and refit metadata.

Preserve canonical module names under activation checkpointing and extend
the reachability guard from NVIDIA-NeMo#4810 to both grouped expert base weights.
Compute the per-expert A @ B delta in fp32 and cast once, matching the
dense LoRA merge without modifying training parameters in place.

Document explicit expert targeting and lora_refit_mode: merged. Cover
gated/non-gated GroupedExpertsLoRA and GroupedExpertsDeepEPLoRA layouts,
checkpoint wrappers, missing weights, metadata, precision, and two-rank
CPU DTensor gathering. The 33 expert cases and 33 existing LoRA/refit
regressions pass against Automodel b916107a on CPU.

Signed-off-by: Rahul A R <[email protected]>
soluwalana pushed a commit to soluwalana/RL that referenced this pull request Oct 31, 2026
Merge Automodel's grouped expert adapters into gate_and_up_projs and
down_projs before HF conversion during merged refitting, and exclude the
bare adapter parameters from both streamed weights and refit metadata.

Preserve canonical module names under activation checkpointing and extend
the reachability guard from NVIDIA-NeMo#4810 to both grouped expert base weights.
Compute the per-expert A @ B delta in fp32 and cast once, matching the
dense LoRA merge without modifying training parameters in place.

Document explicit expert targeting and lora_refit_mode: merged. Cover
gated/non-gated GroupedExpertsLoRA and GroupedExpertsDeepEPLoRA layouts,
checkpoint wrappers, missing weights, metadata, precision, and two-rank
CPU DTensor gathering. The 33 expert cases and 33 existing LoRA/refit
regressions pass against Automodel b916107a on CPU.

Signed-off-by: Rahul A R <[email protected]>
soluwalana pushed a commit to soluwalana/RL that referenced this pull request Oct 31, 2026
Merge Automodel's grouped expert adapters into gate_and_up_projs and
down_projs before HF conversion during merged refitting, and exclude the
bare adapter parameters from both streamed weights and refit metadata.

Preserve canonical module names under activation checkpointing and extend
the reachability guard from NVIDIA-NeMo#4810 to both grouped expert base weights.
Compute the per-expert A @ B delta in fp32 and cast once, matching the
dense LoRA merge without modifying training parameters in place.

Document explicit expert targeting and lora_refit_mode: merged. Cover
gated/non-gated GroupedExpertsLoRA and GroupedExpertsDeepEPLoRA layouts,
checkpoint wrappers, missing weights, metadata, precision, and two-rank
CPU DTensor gathering. The 33 expert cases and 33 existing LoRA/refit
regressions pass against Automodel b916107a on CPU.

Signed-off-by: Rahul A R <[email protected]>
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants