Skip to content

Support Mamba direct conv1d quant export#1716

Draft
mxinO wants to merge 2 commits into
mainfrom
mxin/fix-export-megatron
Draft

Support Mamba direct conv1d quant export#1716
mxinO wants to merge 2 commits into
mainfrom
mxin/fix-export-megatron

Conversation

@mxinO

@mxinO mxinO commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Type of change: bug fix

Megatron now stores Mamba conv1d as direct conv1d_weight and conv1d_bias parameters instead of an nn.Conv1d child. ModelOpt export still needs to emit HF conv1d.weight and conv1d.bias keys, and quantization needs the direct weight to participate in the normal weight-only calibration path.

The quantizer follows the existing per-parameter naming contract, so conv1d_weight pairs with conv1d_weight_weight_quantizer. The export adapter exposes that internal pair as a standard weight/weight_quantizer view for the existing name remapping and quantized export logic.

Usage

# Add a code snippet demonstrating how to use this

Testing

Before your PR is "Ready for review"

Make sure you read and follow Contributor guidelines and your commits are signed (git commit -s -S).

Make sure you read and follow the Security Best Practices (e.g. avoiding hardcoded trust_remote_code=True, torch.load(..., weights_only=False), pickle, etc.).

  • Is this change backward compatible?: ✅ / ❌ / N/A
  • If you copied code from any other sources or added a new PIP dependency, did you follow guidance in CONTRIBUTING.md: ✅ / ❌ / N/A
  • Did you write any new necessary tests?: ✅ / ❌ / N/A
  • Did you update Changelog?: ✅ / ❌ / N/A
  • Did you get Claude approval on this PR?: ✅ / ❌ / N/A

Additional Information

Megatron now stores Mamba conv1d as direct conv1d_weight and conv1d_bias parameters instead of an nn.Conv1d child. ModelOpt export still needs to emit HF conv1d.weight and conv1d.bias keys, and quantization needs the direct weight to participate in the normal weight-only calibration path.

The quantizer follows the existing per-parameter naming contract, so conv1d_weight pairs with conv1d_weight_weight_quantizer. The export adapter exposes that internal pair as a standard weight/weight_quantizer view for the existing name remapping and quantized export logic.

Constraint: New Megatron Mamba no longer exposes layer.mixer.conv1d.

Rejected: Add a Mamba-specific calibration iterator | standard per-weight quantizer naming keeps generic discovery working.

Rejected: Change shared weight discovery helpers | unnecessary once the quantizer follows existing naming convention.

Confidence: high

Scope-risk: moderate

Tested: python -m compileall on touched files

Tested: git diff --check

Tested: uvx ruff@0.12.11 check on touched files

Tested: uvx ruff@0.12.11 format --check on touched files

Tested: Slurm smoke 215464 passed in container, including export remap, generic weight_attr_names discovery, max_calibrate amax, and quant/export context invocation

Not-tested: Full end-to-end HF artifact export/load for a real Nano3 checkpoint
Signed-off-by: Meng Xin <mxin@nvidia.com>
@copy-pr-bot

copy-pr-bot Bot commented Jun 14, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@coderabbitai

coderabbitai Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: f530833e-fe38-4e26-8d8e-4f6cf8a33da0

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch mxin/fix-export-megatron

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

@github-actions

github-actions Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://NVIDIA.github.io/Model-Optimizer/pr-preview/pr-1716/

Built to branch gh-pages at 2026-06-14 10:19 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@codecov

codecov Bot commented Jun 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 11.11111% with 56 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.05%. Comparing base (9f37fe1) to head (fc15b2f).

Files with missing lines Patch % Lines
modelopt/torch/quantization/plugins/megatron.py 2.38% 41 Missing ⚠️
modelopt/torch/export/unified_export_megatron.py 16.66% 15 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1716      +/-   ##
==========================================
- Coverage   77.12%   77.05%   -0.08%     
==========================================
  Files         511      511              
  Lines       56236    56297      +61     
==========================================
+ Hits        43374    43380       +6     
- Misses      12862    12917      +55     
Flag Coverage Δ
unit 54.34% <11.11%> (-0.05%) ⬇️

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.

New Megatron stores Mamba conv1d as direct parameters. ModelOpt adds the
direct Conv1d weight quantizer as a dynamic-module attribute, so MCore's
native Mamba sharded-state path did not reliably preserve the scalar amax
that NVFP4 export needs after checkpoint reload.

The Mamba quant module now saves the direct Conv1d quantizer amax in
distributed checkpoints, recomputing the weight max from conv1d_weight when
reload has dropped calibrator statistics. The Megatron exporter also keeps
direct Mamba Conv1d in dtype when its kernel dimension cannot be packed by
the configured block quantization format.

Constraint: Nano3 uses dynamic block-scale NVFP4; export still requires a calibrated scalar amax.
Rejected: Let export tolerate missing amax | missing amax means checkpoint save/restore dropped quantizer state.
Rejected: Pack direct Conv1d NVFP4 with kernel size 4 and block size 16 | packed block format requires divisibility.
Confidence: medium
Scope-risk: moderate
Tested: py_compile on touched files; ruff check/format on touched files; file-scoped pre-commit hooks; Nano3 PTQ/train/export smoke job 215752.
Not-tested: full ModelOpt GPU test suite.
Signed-off-by: Meng Xin <mxin@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant