Skip to content

Fix layerwise MoE source-weight accounting (NVBug 6954041) - #2876

Merged
meenchen merged 1 commit into
mainfrom
fix/nvbug-6954041-layerwise-source-accounting
Oct 21, 2026
Merged

meenchen merged 1 commit into
mainfrom
fix/nvbug-6954041-layerwise-source-accounting

Conversation

@meenchen

@meenchen meenchen commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Type of change: Bug fix

Fix layerwise MoE export copying already-quantized fused expert tensors back from the source checkpoint as BF16 weights. Those copies incorrectly add the routed expert container to exclude_modules, causing vLLM to select an unquantized MoE backend and fail loading w2_input_scale.

Snapshot complete source-key accounting at exporter binding, before per-layer packing removes the original fused parameters. Reuse the snapshot, including an empty result, for that source checkpoint. Only names are retained; genuinely unmodeled auxiliary weights still carry over. Whole-model export is unchanged.

Usage

No API changes. Re-export affected checkpoints from the original source using the existing layerwise recipe.

Testing

  • 186 focused CPU tests passed across model loading, reverse conversion, unified export, and layerwise calibration/resume (Transformers 5.18.0).
  • Real tiny native Qwen3.5-MoE VLM max calibration and checkpoint export with general/ptq/nvfp4_experts_only-kv_fp8_layerwise_export, compared with whole-model export, with and without an auxiliary source weight: passed. Packed expert weights/scales remain present, original fused BF16 weights are absent, and expert containers are not excluded. The auxiliary weight remains present and excluded.
  • Extended existing test_moe_export_matches with source provenance and the public export API. Both cases passed in a local one-layer CPU adaptation; both failed on the duplicate fused expert tensors when the snapshot lookup was reverted. The committed GPU test retains its original two-layer model and quantization configuration.
  • Meta-model binding smoke: 40 decoder layers, 256 experts/layer, 636 source keys; all parameters remained on meta and the retained source-key snapshot was empty. Full binding took 2.99 s and peaked at 12.48 MiB of Python-tracked allocations. This is a metadata probe, not a full-size PTQ/GPU memory benchmark.
  • Applicable pre-commit hooks and git diff --check: passed.
  • Not run: unadapted CUDA tests, full Qwen3.5-35B-A3B PTQ, or vLLM serving. GPU launcher MCP tools are unavailable in this session.

Before your PR is "Ready for review"

Contributor guidelines and security coding practices reviewed. Commit b139a70eb is signed and signed off; its GPG signature was verified before publishing.

  • Is this change backward compatible?: Yes.
  • If you copied code from any other sources or added a new PIP dependency, did you follow guidance in CONTRIBUTING.md: N/A; neither.
  • Did you write any new necessary tests?: Yes; extended the existing MoE export comparison to cover empty accounting and genuine auxiliary carry-over.
  • Did you update Changelog?: N/A; layerwise export is new in the unreleased 0.48 cycle.
  • Did you get Claude approval on this PR?: No; pending review.

Additional Information

NVBug 6954041. Release backport: 0.48.0.

Summary by CodeRabbit

  • Bug Fixes
    • Layerwise exports now preserve eligible auxiliary weights from the source checkpoint, keeping exported results consistent with baseline exports.

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: NVIDIA/Model-Optimizer/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: d374e5a9-9c71-4562-8faa-7ed0f6c21445
📥 Commits

Reviewing files that changed from the base of the PR and between f1d68fd and b139a70.

📒 Files selected for processing: 3
  • modelopt/torch/export/layerwise_export.py
  • modelopt/torch/export/unified_export_hf.py
  • tests/gpu/torch/export/test_layerwise_export.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

📝 Walkthrough

Layerwise export now captures unplaced source-checkpoint keys during binding and reuses them when a later lookup requests the same checkpoint path. The MoE export-equivalence test covers checkpoints with and without an auxiliary source weight.

Changes: Source-key snapshot handling

Layer / File(s) Summary
Capture source-checkpoint keys
modelopt/torch/export/layerwise_export.py
LayerwiseExporter.bind() stores the directory checkpoint path and its unplaced keys.
Reuse snapshots and validate exports
modelopt/torch/export/unified_export_hf.py, tests/gpu/torch/export/test_layerwise_export.py
unplaced_keys_for returns cached keys when the requested checkpoint path matches. The MoE test compares baseline and layerwise exports, including the optional auxiliary weight and quantization metadata.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: kevalmorabia97, fridah-nv

Merge Risk: ⚪ Minimal · up to b139a

No actionable issue remains; the change is mergeable after normal checks.

Pre-merge checks: Passed 6
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: fixing layerwise MoE source-weight accounting. The bug identifier is relevant and does not obscure the purpose.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 3 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Security Anti-Patterns Passed PASS. The pull request changes only two modelopt export files and one test file. The added modelopt code snapshots checkpoint keys and performs a local import; it does not add torch.load(..., weights_…
Full details: Security Anti-Patterns

Explanation

PASS. The pull request changes only two modelopt export files and one test file. The added modelopt code snapshots checkpoint keys and performs a local import; it does not add torch.load(..., weights_only=False), numpy.load(..., allow_pickle=True), hardcoded trust_remote_code=True, eval/exec on external input, or # nosec. No pyproject.toml or requirements file changed, so no dependency exception applies.

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@kevalmorabia97
kevalmorabia97 removed their request for review October 19, 2026 20:52
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-10-21 04:48 UTC

@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.94%. Comparing base (d7cbff3) to head (b139a70).
⚠️ Report is 11 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2876      +/-   ##
==========================================
+ Coverage   76.99%   77.94%   +0.94%     
==========================================
  Files         658      658              
  Lines       73950    75760    +1810     
==========================================
+ Hits        56939    59051    +2112     
+ Misses      17011    16709     -302     
Flag Coverage Δ
examples-diffusers 20.05% <0.00%> (-0.01%) ⬇️
examples-gpt-oss 13.52% <0.00%> (-0.01%) ⬇️
examples-hf_ptq 23.19% <44.44%> (-0.04%) ⬇️
examples-llm_distill 13.59% <0.00%> (-0.01%) ⬇️
examples-llm_eval 17.25% <44.44%> (+<0.01%) ⬆️
examples-llm_qat 17.49% <0.00%> (-0.01%) ⬇️
examples-llm_sparsity 15.77% <0.00%> (-0.01%) ⬇️
examples-megatron_bridge 26.40% <0.00%> (-0.18%) ⬇️
examples-specdec_bench 13.30% <0.00%> (-0.01%) ⬇️
examples-speculative_decoding 17.48% <0.00%> (-0.07%) ⬇️
examples-torch_onnx 21.41% <0.00%> (-0.01%) ⬇️
examples-torch_trt 15.24% <0.00%> (-0.01%) ⬇️
examples-vllm_serve 15.09% <0.00%> (-0.01%) ⬇️
gpu 59.43% <100.00%> (+8.27%) ⬆️
regression 15.10% <0.00%> (-0.01%) ⬇️
unit 60.41% <77.77%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cjluo-nv cjluo-nv 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.

Bot review (bedrock-claude-opus-5-5) — DM the bot to share feedback.

LGTM: the fix is small and correct. It snapshots the unplaced source keys in bind(), before per-layer packing removes the fused expert parameters, and then reuses that snapshot for the same checkpoint. Human sign-off is still needed because an existing test was edited, for a justified reason.

Needs action:

  • Confirm that the edit to test_moe_export_matches in tests/gpu/torch/export/test_layerwise_export.py is acceptable, and that the GPU CI job passes. The author only ran a CPU adaptation of this test, not the committed CUDA version. The edit is justified: the test now goes through export_hf_checkpoint with a real source checkpoint, and every original assertion is kept.

No action needed:

  • The local import in unplaced_keys_for is justified: it breaks a circular import, and export_hf_checkpoint already imports LAYERWISE_EXPORTER_ATTR the same way.
  • The snapshot is matched against str(ckpt), and every caller passes _source_checkpoint(model), so the two values always agree. A different checkpoint, or an exporter that was never bound, falls through to the existing logic, so whole-model export is unchanged.
  • read_unplaced_weights runs before finalize() detaches the exporter, so the snapshot is still reachable when it's needed.
  • Minor: unplaced_keys_for reads the exporter's private _source_key_snapshot attribute. That's acceptable at this size, but a small accessor method would make the coupling explicit.

@Edwardf0t1 Edwardf0t1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@meenchen
meenchen merged commit 4df410a into main Oct 21, 2026
58 checks passed
@meenchen
meenchen deleted the fix/nvbug-6954041-layerwise-source-accounting branch October 21, 2026 04:47
kevalmorabia97 added a commit that referenced this pull request Oct 28, 2026
#2928 #2885 #2908 #2929 #2950 #2972 #2877 #2951 #2816 #3005 #3012 #2971 #3015 #3006 #3026 #2988 #3028 #1860 #3030 #2612 #3033 #2653 #2599 #2655 #2618 #2868 #3032 #3027 #2869 #2984 #2975 #2345 #3020 #2997 (#3072)

## Cherry-picked PRs

- #2817
- #2671
- #2892
- #2876
- #2910
- #2911
- #2919
- #2904
- #2937
- #2928
- #2885
- #2908
- #2929
- #2950
- #2972
- #2877
- #2951
- #2816
- #3005
- #3012
- #2971
- #3015
- #3006
- #3026
- #2988
- #3028
- #1860
- #3030
- #2612
- #3033
- #2653
- #2599
- #2655
- #2618
- #2868
- #3032
- #3027
- #2869
- #2984
- #2975
- #2345
- #3020
- #2997

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Diffusion exports now offer more control over attention-quantizer
metadata and preserve sparse-attention settings in a format compatible
with downstream tooling.
* Model loading better handles select bundled model implementations, and
Docker guidance now covers writable CUDA-extension cache locations.
* **Bug Fixes**
* Improved reliability for model export and checkpoint saving,
quantization across devices, ONNX processing, and calibration.
* Temporary TensorRT and autotuning artifacts now use private
directories by default.
* **Documentation**
* Updated installation guidance, compatibility notes, and links;
corrected typos across guides and examples.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Chad Voegele <[email protected]>
Signed-off-by: Rohan Joshi <[email protected]>
Signed-off-by: LinCanNerd <[email protected]>
Signed-off-by: Keval Morabia <[email protected]>
Signed-off-by: weimingc <[email protected]>
Signed-off-by: Noey Yang <[email protected]>
Signed-off-by: Kai Xu <[email protected]>
Signed-off-by: Zhehao Hu <[email protected]>
Signed-off-by: noeyy-mino <[email protected]>
Signed-off-by: joe-nv <[email protected]>
Signed-off-by: yingguo-trt <[email protected]>
Signed-off-by: Shengliang Xu <[email protected]>
Signed-off-by: Chenjie Luo <[email protected]>
Signed-off-by: anmaurya001 <[email protected]>
Signed-off-by: Connor Baker <[email protected]>
Signed-off-by: wiederse <[email protected]>
Signed-off-by: Vu Dinh <[email protected]>
Signed-off-by: Pratik Gandhi <[email protected]>
Signed-off-by: Michael Rusu <[email protected]>
Signed-off-by: Michael-Rusu <[email protected]>
Signed-off-by: namgyu-youn <[email protected]>
Signed-off-by: Sylvester Kaczmarek <[email protected]>
Signed-off-by: Javier de Jesus <[email protected]>
Signed-off-by: javierdejesusda <[email protected]>
Signed-off-by: Zhiyu Cheng <[email protected]>
Signed-off-by: Juhi Mittal <[email protected]>
Signed-off-by: Dmitry Rantovov <[email protected]>
Signed-off-by: h-guo18 <[email protected]>
Co-authored-by: Chad Voegele <[email protected]>
Co-authored-by: LinCanNerd <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Co-authored-by: Keval Morabia <[email protected]>
Co-authored-by: Wei-Ming Chen <[email protected]>
Co-authored-by: noeyy-mino <[email protected]>
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: kaix-nv <[email protected]>
Co-authored-by: Zhehao Hu <[email protected]>
Co-authored-by: joe <[email protected]>
Co-authored-by: yingguo-trt <[email protected]>
Co-authored-by: Shengliang Xu <[email protected]>
Co-authored-by: Chenjie Luo <[email protected]>
Co-authored-by: anmaurya001 <[email protected]>
Co-authored-by: Connor Baker <[email protected]>
Co-authored-by: Jiao Mingyang <[email protected]>
Co-authored-by: Vu Dinh <[email protected]>
Co-authored-by: Pratik Gandhi <[email protected]>
Co-authored-by: Michael R <[email protected]>
Co-authored-by: Michael-Rusu <[email protected]>
Co-authored-by: namgyu-youn <[email protected]>
Co-authored-by: Sylvester Kaczmarek <[email protected]>
Co-authored-by: Javier De Jesus <[email protected]>
Co-authored-by: Zhiyu <[email protected]>
Co-authored-by: Juhi Mittal <[email protected]>
Co-authored-by: Dmitry Rantovov <[email protected]>
Co-authored-by: h-guo18 <[email protected]>
@rohansjoshi rohansjoshi added the cherry-pick-done Added by bot once PR is cherry-picked to the release branch label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cherry-pick-0.48.0 cherry-pick-done Added by bot once PR is cherry-picked to the release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants