Skip to content

Implement canonical ModelOpt quantized HF export through Megatron Bridge #4804

Open

Description

@kevalmorabia97

Parent PoR

Part of #5773.

Goal

Wire Megatron-Bridge topology reconstruction and HF naming to ModelOpt functional export so quantized Megatron models can be emitted in the canonical ModelOpt HF schema for native vLLM deployment and refit.

Megatron-Bridge scope

  • Reconstruct logical HF weights across TP, ETP, EP, and PP.
  • Apply fused QKV, gate/up, and expert topology transforms while carrying opaque ModelOpt export state.
  • Call ModelOpt-owned packing and config APIs and expose the resulting canonical tensors and sidecars.
  • Validate offline/functional parity, distributed topology transforms, and native vLLM load/refit behavior.
  • Reject unsupported schemas, topology transforms, and runtime fallbacks explicitly.

Out of scope for Megatron-Bridge

  • Quantization format tables, scale formulas, packing rules, sidecar semantics, or HF quantization-config construction.
  • vLLM kernel or custom quantization-method ownership.
  • Generic handling of arbitrary vendor quantized checkpoint schemas.

Vendor-specific source checkpoint import remains an explicit model adapter concern as described in #5773.

Timeline

TBD. Implementation steps and cross-repository dependencies will be linked as the APIs are finalized.

Activity

  1. added
    featureNew capabilities, enhancements, or enablement work
    area:quantQuantization (PTQ, QAT, FP8 recipes)
    PoRPlan of record item for roadmap and release tracking
    on Jul 10, 2026
  2. added
    trackingTracking issue for an ongoing project with smaller steps
    on Jul 10, 2026
  3. added
    area:ckptCheckpoint conversion, loading, export, and save paths
    on Aug 24, 2026
  4. changed the title [-]Natively support quantized megatron to HF export for vLLM deployment (retires ModelOpt custom logic)[/-] [+]Implement canonical ModelOpt quantized HF export through Megatron Bridge[/+] on Aug 24, 2026
  5. removed
    trackingTracking issue for an ongoing project with smaller steps
    PoRPlan of record item for roadmap and release tracking
    on Aug 24, 2026
  6. added
    trackingTracking issue for an ongoing project with smaller steps
    on Aug 25, 2026
  7. kevalmorabia97 commented on Sep 30, 2026

    @kevalmorabia97
    ContributorAuthor

    Re-evaluated this against current main of both repos (2026-09-30) and the open PRs, with the goal of fully deprecating ModelOpt's custom Megatron↔HF logic (unified_export_megatron.py, plugins/mcore_*.py, plugins/megatron_importer.py) in favor of the bridge. Below is the current state and the proposed split of remaining work.

    Where things stand

    Decisions

    1. Only the inline config.json quantization_config is needed; drop hf_quant_config.json.
    2. The fused MLA down-projection is fixed at quantization time in ModelOpt, not in the bridge.
      • vLLM (fused_qkv_a_proj) and TRT-LLM (fused_a) fuse q_a_proj + kv_a_proj_with_mqa into one GEMM.
      • In MCore these are two linears with separate quantizers, so their NVFP4 weight_scale_2 differ.
      • vLLM only warns and uses the max without requantizing (modelopt.py#L1975-L1986). TRT-LLM requantizes, and asserts equal input_scale (modeling_deepseekv3.py#L471).
      • ModelOpt will unify the pair's amax after calibration, so QAT numerics match what gets deployed. The bridge only needs a generic check (see below). Longer term, MCore's mla_down_proj_fusion gives the pair a single quantizer natively.
    3. Drop ModelOpt's HF→Megatron import path (import_mcore_gpt_from_hf / megatron_importer.py). BF16 HF→Megatron import is already covered by AutoBridge. The remaining users are Megatron-LM's examples/post_training/modelopt (convert_model.py, quantize.py, prune.py; export.py uses the exporter). Those examples get migrated to AutoBridge or retired together with the exporter.
    4. Testing-first. Implementation is expected to be the small part. Every PR lands with Megatron-Bridge CI tests that let us merge confidently (see "Test plan").

    Test plan (Megatron-Bridge CI, with every PR)

    • Parallelism parity against the current ModelOpt export.

      • Quantize a small model per architecture once, then export the same Megatron checkpoint two ways:

        • reference: ModelOpt's PP-only export_mcore_gpt_to_hf (TP=1, PP=1/2);
        • new: bridge native export at TP2, PP2, EP2 and ETP2 (fits the existing 2-GPU H100 / GB200 runners), with larger combinations such as TP2×EP2×PP2 in the nightly tier.
      • Assert that everything vLLM reads is identical:

        • every tensor, bitwise (names, dtypes, shapes, values);
        • config.json, including quantization_config (quant_algo, quantized_layers, exclude list, KV scheme);
        • generation config, tokenizer and processor config.

        How tensors are split across safetensors files may differ. Any intentional difference (e.g. MLA scale unification) is listed explicitly and treated as having no baseline.

      • Also export a checkpoint quantized at TP/EP > 1, so amax resharding on load is covered.

      • Once the ModelOpt exporter is removed, the bridge TP1/PP1 export becomes the reference for parallelism invariance, plus small checked-in golden hashes.

    • Format × architecture matrix: NVFP4, W4A16 NVFP4, FP8 (per-tensor / per-channel / 128×128 block), MXFP8, mixed precision, KV FP8.

    • Structural checks on every export:

      • fused sibling groups (q/k/v, gate/up, q_a/kv_a) share weight_scale_2 / input_scale;
      • exclude_modules covers exactly the unquantized linears;
      • no source-checkpoint quantization_config or *_scale_inv leaks through.
    • vLLM runs only where there is no identical baseline. If the bridge export is identical to a vLLM-validated checkpoint from the ModelOpt exporter, a vLLM run adds no information. vLLM (Blackwell for NVFP4) is needed only for:

      • architectures and formats the ModelOpt exporter doesn't cover, e.g. Qwen3-VL-MoE, Gemma3-VL, MXFP8, FP8 per-channel;
      • intentional differences from the old export.

      Even there it is a one-time load plus known-answer prompts, not a full eval.

    • Pilot: Qwen3.6-35B-A3B W4A4 NVFP4 from the ModelOpt tutorial (examples/megatron_bridge/tutorials/Qwen3.6-35B-A3B). One vLLM load-and-generate, only to confirm the parity harness compares everything vLLM reads.

    Remaining work

    ModelOpt

    • Land feat(export): add functional quantized weight export NVIDIA/Model-Optimizer#2251.
    • KV-cache export: k_scale / v_scale from k_bmm / v_bmm quantizers, plus kv_cache_quant_algo. It is hard-set to NONE today.
    • MLA q_down / kv_down amax unification: static NVFP4 via SharedWeightGlobalAmaxState patterns; dynamic NVFP4 / FP8 per-tensor via a post-calibration step.
    • Find a home for the other users of unified_export_megatron.py before deleting it:
      • vLLM fake-quant export (export_mcore_gpt_to_hf_vllm_fq, used by Megatron-LM export.py): port to the bridge's BF16 export of folded weights plus a quantizer-state sidecar;
      • IQ formats: move to the functional API;
      • EAGLE draft-head export (export_extra_modules): decide separately.
    • Switch examples/megatron_bridge and the Megatron-LM post_training/modelopt examples to the bridge. Deprecate export_mcore_gpt_to_hf / import_mcore_gpt_from_hf for one release, then remove them together with the mcore_* rule tables.

    MBridge, generic (details in #5812)

    MBridge, per bridge

    Needed Bridges
    Nothing (standard mappings only) Llama, Mistral/Ministral3 (LM), Gemma 1–3, Qwen2/3/3-MoE, Qwen2.5-VL (LM), OLMoE, MiniMax, Ernie-4.5; MLA models (DeepSeek V2/V3, Kimi K2, GLM-4.7-Flash, GLM-5) once ModelOpt unifies the pair
    Row-split / re-grouping mapping support GDN (Qwen3.5/3.6, Qwen3-Next), MambaInProjMapping (Nemotron-H, Falcon-H1), Nemotron-H MTP flattening, QKVG / ConcatenatedQKV / asymmetric QKV (Step, MiMo, Bailing, …)
    Per-HF-output unquantized override (MCore fuses these into one quantized linear, so a recipe can't exclude them) Qwen3.5/3.6 GDN in_proj_a / in_proj_b (inside the fused in_proj)
    Transposed / fused 3D experts Qwen3-VL-MoE (per-expert names), GPT-OSS, Llama4 (no bridge on main yet)
    Bypass source-format requantization during ModelOpt export DeepSeek V4, Kimi K3, Kimi K2.5-VL
    Keep BF16 in the recipe shared_expert_gate, routers, Gemma4 per-layer-embedding projections, vision towers, MTP (Qwen3.5)

    cc @cuichenx @mxinO

  8. kevalmorabia97 commented on Sep 30, 2026

    @kevalmorabia97
    ContributorAuthor

    Execution plan: pilot, then remaining PRs

    This breaks the checklist above into 8 PRs. Every PR lands with its CI tests from the test plan: bitwise parity against ModelOpt's PP-only export at TP2 / PP2 / EP2 / ETP2, plus structural checks. Implementation is expected to be quick. Most of the time goes into tests, so each PR can be merged with confidence.

    Pilot: Qwen3.6-35B-A3B W4A4 NVFP4 (PRs 1–4, target ~2 weeks)

    The reference is the checkpoint from ModelOpt's tutorial (examples/megatron_bridge/tutorials/Qwen3.6-35B-A3B). It is mixed precision:

    • NVFP4 W4A4 on routed and shared experts and lm_head;
    • FP8 on full attention and on the GatedDeltaNet (GDN) projections;
    • FP8 KV cache;
    • BF16 for MTP, routers, conv1d, GDN in_proj_a/b and the vision tower.

    That makes it a good first target: it covers grouped experts, mixed precision, the attention output gate, GDN and a VLM wrapper.

    # Repo PR Depends on
    1 Model-Optimizer NVIDIA/Model-Optimizer#2251: functional quantized-weight export API –
    2 Model-Optimizer KV-cache export (k_scale / v_scale from k_bmm / v_bmm quantizers, plus kv_cache_quant_algo) and MLA linear_q_down_proj / linear_kv_down_proj amax unification after calibration (static NVFP4 via SharedWeightGlobalAmaxState patterns; dynamic NVFP4 / FP8 per-tensor via a post-calibration step) 1
    3 Megatron-Bridge Quantized export core: #5812 rebased onto main (incl. #6074 with_megatron_names), plus:
    • offline save API (packed tensors plus the inline quantization_config; drops source quant metadata);
    • parity harness and fused-sibling scale check;
    • exclude_modules from disabled quantizers;
    • switch examples/quantization/export.py to the bridge.
    Parity tests on small Llama / Qwen3 / Qwen3-MoE models (NVFP4, FP8, mixed)
    1
    4 Megatron-Bridge Pilot: GDN mapping support (GDNLinearMappingSeparate), per-HF-output unquantized override for in_proj_a/b, KV-cache config. Parity tests on a small Qwen3.5/3.6-MoE model 2, 3

    Pilot exit criteria:

    • The Qwen3.6-35B-A3B checkpoint exported through the bridge at EP=8 is identical to the ModelOpt PP-only export. That means every tensor bitwise, plus config.json / quantization_config, the tokenizer and the processor config.
    • One vLLM load-and-generate on Blackwell, only to confirm the harness compares everything vLLM reads.

    Remaining PRs

    # Repo PR Depends on
    5 Megatron-Bridge Architecture coverage, each family with its own parity tests:
    • row-split / re-grouping mappings, by deriving row indices from each mapping's own megatron_to_hf: MambaInProjMapping, Nemotron-H MTP flattening, QKVG / ConcatenatedQKV / asymmetric QKV (Nemotron-H +MTP);
    • MLA models (DeepSeek-style, GLM-5);
    • per-expert canonical names for transpose_on_export experts (Qwen3-VL-MoE);
    • GPT-OSS fused 3D experts. Split this into its own PR if checking vLLM's ModelOpt NVFP4 GPT-OSS support delays it.
    2, 3
    6 Model-Optimizer Migration + deprecation:
    • switch examples/megatron_bridge and its tutorials to the bridge;
    • port the other users of unified_export_megatron.py: vLLM fake-quant export via the bridge's BF16 export (folded weights plus quantizer-state sidecar), IQ formats via the functional API;
    • deprecation warnings on export_mcore_gpt_to_hf / import_mcore_gpt_from_hf.
    4, 5
    7 NVIDIA/Megatron-LM Move examples/post_training/modelopt (convert_model.py, quantize.py, prune.py, export.py) to AutoBridge, or retire them 6
    8 Model-Optimizer Remove unified_export_megatron.py, the plugins/mcore_*.py rule tables and megatron_importer.py one release after the deprecation 6, 7

    vLLM testing: needed only where no identical ModelOpt-exported baseline exists. That means architectures and formats the old exporter doesn't cover (e.g. Qwen3-VL-MoE, Gemma3-VL, MXFP8, FP8 per-channel) and intentional differences (e.g. MLA scale unification). Even there it is a one-time load plus known-answer prompts.

    Out of scope for this deprecation:

    • EAGLE draft-head export (export_extra_modules): decided separately;
    • source-format requantization bypass for DeepSeek V4 / Kimi K3 / Kimi K2.5-VL: the ModelOpt exporter doesn't support these either;
    • Llama4: no bridge on main.

    Rough timeline: pilot ~2 weeks; everything through PR 7 ~5–6 engineer-weeks. The calendar depends mostly on reviews and releases:

    cc @cuichenx @mxinO

  9. kevalmorabia97 commented on Oct 14, 2026

    @kevalmorabia97
    ContributorAuthor

    Status 2026-10-14

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

Metadata

Metadata

Assignees

Labels

area:ckptCheckpoint conversion, loading, export, and save pathsarea:quantQuantization (PTQ, QAT, FP8 recipes)featureNew capabilities, enhancements, or enablement worktrackingTracking issue for an ongoing project with smaller steps

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions