### What does this PR do? Type of change: Bug fix **Problem:** `export_mcore_gpt_to_hf` writes a quantization config only for FP8, FP8 block-wise, NVFP4, W4A16 NVFP4 / MXFP4 and the GGML formats. Weights in any other format are still packed and written with their scales, but with no `quantization_config` and no `hf_quant_config.json`, and nothing warns that the checkpoint can't be loaded correctly. On a tiny Llama this happens with: - INT4 weight-only and INT4 AWQ, also after `disable_pre_quant_scale_and_resmooth(..., delete_pre_quant_scale=True)`, as reported in #3681; - MXFP8, MXFP4, W4A8 MXFP4-FP8 and W4A8 NVFP4-FP8; - a mixed model whose first quantized layer uses one of those formats. Related problems affect mixed models whose leading format is mapped: - INT4 layers get a per-layer config that claims a `pre_quant_scale`, which is never written. - Per-channel (INT8, FP8 per-channel) QKV layers, and 2-D blockwise FP8 on QKV, gate/up or GatedDeltaNet / KDA `in_proj` layers, crash during row slicing on their stage only, and the other stages hang. - Grouped experts whose quantizers differ are packed under a single recorded format. Under pipeline parallelism only the last stage's format was checked, because that stage writes the config. So a last stage holding no quantized layer, or an unmapped one, dropped the config for every stage. `examples/megatron_bridge/export_quantized_megatron_to_hf.py` reaches this exporter through `--modelopt_megatron_exporter` and through its fallback. **Fix:** - Before packing or writing anything, `save_pretrained` gathers from every rank the formats of the weights it exports. The scan covers only the children the export writes (`embedding`, `decoder`, `output_layer`, `mtp`), so drafts and a distillation teacher are left out. Within them it follows `_get_state_dict`'s selection: a tied output layer, the MTP stage's embedding, an MTP `eh_proj` the rules don't export, and KDA's raw `conv1d` and mHC's `mapping_proj` are skipped, and each grouped-expert quantizer is resolved as packing resolves it. - An export whose config-writing rank (the last pipeline stage's first rank) holds no quantized layer while other ranks do is refused on every rank: its config couldn't describe the other ranks' weights. A single-stage export keeps the writer's own format. - Every rank raises `NotImplementedError` in these cases: - no config is mapped for that format; - any exported layer uses a format whose per-layer config would promise a `pre_quant_scale` (INT4 / W4A8 AWQ, NVFP4 AWQ / SVDQuant), or has no usable schema (real blockwise FP8, `fp8_pb_real`, which was previously passed through as a top-level format); - a QKV layer has per-channel scales (by label, or by a multi-element weight amax); - a row-split layer (QKV, gated `linear_fc1`, GatedDeltaNet / KDA `in_proj`) uses blocks spanning several rows (2-D block scales, e.g. FP8 2-D blockwise); - an INT8-labelled weight carries block scales (MXINT8), which packs as neither INT8 layout; - a grouped module's experts, or the local experts that packed rules (Llama 4, GPT-OSS) stack into one tensor, differ in format or block size, or mix enabled and disabled. - The error names the format and points to HF PTQ export, or to Megatron-Bridge for the formats it supports. - If format detection fails on one rank, every rank raises instead of hanging. - #3681's base-model `pre_quant_scale` refusal, merged from main, shares the same single cross-rank exchange and takes precedence over format-detection errors. The exchange uses `modelopt.torch.utils.distributed.allgather` (tensor bytes loaded with `weights_only`), now backend-aware so CPU / Gloo groups work too. - No new formats are mapped. Mixed models such as FP8 + MXFP8 still export the same `MIXED_PRECISION` config. **Why a separate scan.** The preflight mirrors `_get_state_dict`'s module selection rather than reusing the export rules or `get_quantized_weight_export_spec`: - The refusal has to happen on every rank before any rank packs. The rule dispatch packs as it walks and runs expert-parallel collectives inside it, so a rank failing mid-walk would strand its peers, and a dry run would pack everything twice. - `get_quantized_weight_export_spec` (the functional spec Bridge and HF export use) has its own allowlist, which differs from what this exporter packs. Reusing it would refuse formats this exporter writes today and accept some it can't. - Each skip in the scan names the rule it follows (tied output, MTP embedding and `eh_proj`, KDA `conv1d`, mHC `mapping_proj`, drafts), and the tests cover them. ### Modules touched - `modelopt/torch/export`: the fix itself, in `unified_export_megatron.py`. - `modelopt/torch/utils/distributed.py`: `allgather` always put its tensors on CUDA and padded with an uninitialized buffer. The export preflight's cross-rank exchange needs it to pick the device from the group's backend configuration, so CPU / Gloo groups work too, and it now pads with zeros. ### Testing All runs used `nvcr.io/nvidia/nemo:26.10` with 2 local GPUs. - `tests/gpu_megatron/torch/export/test_unified_export_megatron.py`: - New `llama` + `MXFP8_DEFAULT_CFG` case in `test_unified_export_megatron`: export refuses and writes nothing. - New `nemotron_h` + `NVFP4_EXPERTS_ONLY_CFG` row: the real TEGroupedLinear experts alone produce both configs and are the only packed weights. - The Medusa rows now quantize only the draft heads and expect a BF16 export with no `hf_quant_config.json`. - The Llama PP=2 MTP test now quantizes only the MTP `eh_proj`, which Llama's rules don't export, and expects a BF16 export. - New PP=2 test `test_unified_export_megatron_pp2_quant_config_spans_stages`: - W4A16 NVFP4 on the first stage and an unquantized last stage: refused on both ranks, nothing written. - MXFP8 on the first stage behind a mapped W4A16 NVFP4 writer: exported with a `MIXED_PRECISION` config (inline and sidecar, including their per-layer entries) and both layers' packed weights and scales. - INT4 on the first stage with a mapped writer stage: refused on both ranks. - INT8 on the first stage (per-channel QKV) with a mapped writer stage: refused on both ranks. - Per-channel FP8 QKV with `axis=(0,)`, which resolves to plain `fp8`, with a mapped writer stage: refused on both ranks. - FP8 2-D blockwise on the first stage with a mapped writer stage: refused on both ranks. - A Qwen3.5 GatedDeltaNet model with FP8 2-D blockwise only on stage 0's `in_proj`: refused on both ranks. - Real blockwise FP8 on stage 0's `linear_fc2` behind a mapped writer: refused on both ranks. - MXINT8 only on stage 0's `linear_fc2`: refused on both ranks. - MXFP6 on one stage: detection fails there, and both ranks raise. - Every case runs under `bounded_collectives`, which now also bounds `dist.all_gather` (used by the preflight's `allgather`), so a rank left waiting fails in seconds. - New fast test `test_export_format_scan_follows_packing`. It uses TEGroupedLinear's numbered-weight layout with uniform experts, mixed experts, and a disabled first expert. The test checks that the scan resolves each expert as packing does, flags mixed groups, and ignores a quantized tied output layer, KDA's raw `conv1d`, and 2-D scales on Mamba's `in_proj`. - New fast test `test_qkv_per_channel_scales_are_found_by_the_scale_layout`: calibrated per-channel FP8 on QKV is refused; `axis=()`, a constant scalar amax, and static NVFP4 with one-row blocks (`{-2: 1}` or `{-2: None}`) are accepted and pack through the real `_qkv_slicing`. - New `test_retained_pre_quant_scale_refused_behind_a_supported_layer`: #3681's refusal still wins when the format scan raises on a GGML layer that kept its scale. - `test_mcore_vllm_export_packed` (vLLM fake-quant) now also exports a stack with one weight-only NVFP4 expert among BF16 ones. - Every new or changed case fails with the fix reverted. - Whole file plus `test_vllm_fakequant_megatron_export.py`, at536ba8fa9aafter merging main (#3681, then #3662/#3663/#3677/#3693): 137 passed and 1 failed. The failure is `test_glm5_next_export_matches_source`: the stock 26.10 Megatron-Bridge can't load `glm5_next` (CI overlays a newer Bridge), and it fails before export. `test_qkv_slicing_gqa_tp2` failed once atf5b54f9264on a pre-existing race (both ranks write the same `config.json`) and passed on reruns. - `tests/unit/torch/export/test_unified_export_megatron.py`: the preflight agrees across two Gloo ranks without CUDA (fails with the CUDA-only `allgather`). `tests/gpu/torch/utils/test_distributed.py` passes, including new `allgather` cases over a CUDA-only `cuda:nccl` group and over a CPU-only group initialized without a backend. - I ran a one-off export sweep on main and with this change. It covered the FP8, NVFP4, W4A16 NVFP4, INT4, INT8, MX and W4A8 configs, mixed FP8 + MXFP8 / INT4, and per-stage PP=2 combinations. - I did not run TP>1 or EP>1. ### Before your PR is "*Ready for review*" - Is this change backward compatible?: ✅ (formats that used to export without a valid config now raise instead) - 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?: ✅ - Did you update [Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?: ✅ - Did you get Claude approval on this PR?: ❌ (review in progress) ### Additional Information - Tracked in OMNIML-6311. Adding export support for more formats belongs to Megatron-Bridge's export (OMNIML-5980). - Single-format INT8 (weight-only and SmoothQuant) and FP8 per-channel exports used to crash with a reshape error in `_qkv_slicing` before writing anything. They now get the same refusal on every rank. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Megatron-to-Hugging Face exports now detect unsupported quantization formats and layouts before writing, helping prevent invalid or incomplete exports. * Export validation now accounts for quantized layers across pipeline stages, including cases where the stage writing the configuration has no quantized layers. * Distributed gathering now supports CPU process groups as well as CUDA-backed groups. * **Documentation** * For unsupported export formats, use Hugging Face export or Megatron-Bridge where supported. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Keval Morabia <28916987+kevalmorabia97@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
NVIDIA Model Optimizer (referred to as Model Optimizer, or ModelOpt) is a library comprising state-of-the-art model optimization techniques including quantization, pruning, Neural Architecture Search (NAS), distillation, speculative decoding and sparsity to accelerate models.
[Input] Model Optimizer currently supports inputs of a Hugging Face, PyTorch or ONNX model.
[Optimize] Model Optimizer provides Python APIs for users to easily compose the above model optimization techniques and export an optimized quantized checkpoint. Model Optimizer is also integrated with NVIDIA Megatron-Bridge, Megatron-LM and Hugging Face Accelerate for training required inference optimization techniques.
[Export for deployment] Seamlessly integrated within the NVIDIA AI software ecosystem, the quantized checkpoint generated from Model Optimizer is ready for deployment in downstream inference frameworks like SGLang, TensorRT-LLM, TensorRT, or vLLM. The unified Hugging Face export API now supports both transformers and diffusers models.
Latest News
- [2026/11/17] BLOG: Training Standalone Speculative Decoding Drafts for Fast Inference on Groq 3 LPX: Three ModelOpt-trained standalone drafts for Qwen3.8-27B; at unbounded draft length, offline scoring gives the 4B a 59% higher acceptance length than the live MTP baseline.
- [2026/10/28] BLOG: GLM-5.3-Flash at 2-3 Bits per Expert Weight: IQ2_S PTQ Recipes and QAD: IQ2_S experts with NVFP4 down projections match the FP8 checkpoint without training, at less than half its size; QAD recovers long-context accuracy on the all-IQ2_S model.
- [2026/10/15] NVIDIA Nemotron 3.5 Super VL 120B-A12B NVFP4 checkpoint: Quantized with Model Optimizer using post-training quantization (PTQ) and quantization-aware distillation (QAD). For details, see the Quantization section (Section 4) of the technical report.
- [2026/10/08] BLOG: AutoQuant KV: Automatic Mixed-Precision KV Cache Quantization: Per-layer KV format search, GEMM composition, and a Qwen3-8B accuracy case study.
- [2026/09/16] BLOG: Scale Learning: Training NVFP4 Block Scales to Recover Accuracy
- [2026/09/16] End-to-end W4A4 NVFP4 + QAD tutorial for Qwen3.6-35B-A3B: NVFP4 W4A4 PTQ plus quantization-aware distillation, reaching up to 1.30x vLLM throughput over BF16 and 3.1x smaller checkpoints while recovering the accuracy W4A4 costs.
- [2026/09/09] BLOG: Improving NVFP4 Accuracy with Local-Hessian Weight Scales
- [2026/08/24] BLOG: AutoQuantize: A Fast Automatic Mixed-Precision Assignment
- [2026/08/17] BLOG: Developing Nemotron 3.5 Lightning NVFP4 with QAD Using NVIDIA Model Optimizer: Learn how quantization-aware distillation recovers accuracy from aggressive NVFP4 quantization while reducing model size and increasing throughput.
- [2026/06/26] BLOG: Creating the NVIDIA Nemotron 3 Ultra NVFP4 Checkpoint with NVIDIA Model Optimizer: How we quantized Nemotron 3 Ultra (550B) to NVFP4 with Model Optimizer — up to 5.9× higher decode-heavy inference throughput than GLM-5.1 754B FP4 while matching BF16 accuracy. NVFP4 Checkpoint on Hugging Face.
- [2026/05/27] End-to-end Optimization tutorial for Nemotron-3-Nano-30B-A3B: Pruning + two-phase distillation + FP8 quantization achieving 2.6× vLLM throughput and 2.6× memory reduction.
- [2026/05/13] Puzzletron: A new algorithm for heterogeneous pruning & NAS of LLM and VLM models.
- [2026/04/15] Customer story: Domyn compresses Colosseum-355B → 260B using ModelOpt's Minitron pruning + distillation
- [2026/03/17] Customer story: Bielik.AI builds Bielik Minitron 7B (33% smaller, 50% faster, 90% quality retained) using ModelOpt's Minitron pruning + distillation
- [2026/03/11] Model Optimizer quantized Nemotron-3-Super checkpoints are available on Hugging Face for download: FP8, NVFP4. Learn more in the Nemotron 3 Super release blog. Check out how to quantize Nemotron 3 models for deployment acceleration here
- [2026/03/11] NeMo Megatron Bridge now supports Nemotron-3-Super quantization (PTQ and QAT) and export workflows using the Model Optimizer library. See the Quantization (PTQ and QAT) guide for FP8/NVFP4 quantization and HF export instructions.
- [2025/12/11] BLOG: Top 5 AI Model Optimization Techniques for Faster, Smarter Inference
- [2025/12/08] NVIDIA TensorRT Model Optimizer is now officially rebranded as NVIDIA Model Optimizer.
- [2025/10/07] BLOG: Pruning and Distilling LLMs Using NVIDIA Model Optimizer
- [2025/09/17] BLOG: An Introduction to Speculative Decoding for Reducing Latency in AI Inference
- [2025/09/11] BLOG: How Quantization Aware Training Enables Low-Precision Accuracy Recovery
- [2025/08/29] BLOG: Fine-Tuning gpt-oss for Accuracy and Performance with Quantization Aware Training
- [2025/08/01] BLOG: Optimizing LLMs for Performance and Accuracy with Post-Training Quantization
- [2025/06/24] BLOG: Introducing NVFP4 for Efficient and Accurate Low-Precision Inference
- [2025/05/14] NVIDIA TensorRT Unlocks FP4 Image Generation for NVIDIA Blackwell GeForce RTX 50 Series GPUs
- [2025/04/21] Adobe optimized deployment using Model-Optimizer + TensorRT leading to a 60% reduction in diffusion latency, a 40% reduction in total cost of ownership
- [2025/04/05] NVIDIA Accelerates Inference on Meta Llama 4 Scout and Maverick. Check out how to quantize Llama4 for deployment acceleration here
- [2025/03/18] World's Fastest DeepSeek-R1 Inference with Blackwell FP4 & Increasing Image Generation Efficiency on Blackwell
- [2025/02/25] Model Optimizer quantized NVFP4 models available on Hugging Face for download: DeepSeek-R1-FP4, Llama-3.3-70B-Instruct-FP4, Llama-3.1-405B-Instruct-FP4
- [2025/01/28] Model Optimizer has added support for NVFP4. Check out an example of NVFP4 PTQ here.
- [2025/01/28] Model Optimizer is now open source!
Previous News
- [2024/10/23] Model Optimizer quantized FP8 Llama-3.1 Instruct models available on Hugging Face for download: 8B, 70B, 405B.
- [2024/09/10] Post-Training Quantization of LLMs with NVIDIA NeMo and Model Optimizer.
- [2024/08/28] Boosting Llama 3.1 405B Performance up to 44% with Model Optimizer on NVIDIA H200 GPUs
- [2024/08/28] Up to 1.9X Higher Llama 3.1 Performance with Medusa
- [2024/08/15] New features in recent releases: Cache Diffusion, QLoRA workflow with NVIDIA NeMo, and more. Check out our blog for details.
- [2024/06/03] Model Optimizer now has an experimental feature to deploy to vLLM as part of our effort to support popular deployment frameworks. Check out the workflow here
- [2024/05/08] Announcement: Model Optimizer Now Formally Available to Further Accelerate GenAI Inference Performance
- [2024/03/27] Model Optimizer supercharges TensorRT-LLM to set MLPerf LLM inference records
- [2024/03/18] GTC Session: Optimize Generative AI Inference with Quantization in TensorRT-LLM and TensorRT
- [2024/03/07] Model Optimizer's 8-bit Post-Training Quantization enables TensorRT to accelerate Stable Diffusion to nearly 2x faster
- [2024/02/01] Speed up inference with Model Optimizer quantization techniques in TRT-LLM
Install
To install stable release packages for Model Optimizer with pip from PyPI:
pip install -U nvidia-modelopt[all]
Model Optimizer will download and install additional third-party open source software projects. Review the license terms of these open source projects before use.
To install from source in editable mode with all development dependencies or to use the latest features, run:
# Clone the Model Optimizer repository
git clone git@github.com:NVIDIA/Model-Optimizer.git
cd Model-Optimizer
pip install -e .[dev]
You can also directly use NVIDIA container images, which have Model Optimizer pre-installed:
nvcr.io/nvidia/pytorch:<version>-py3nvcr.io/nvidia/nemo:<version>nvcr.io/nvidia/tensorrt-llm/release:<version>
Before pulling and using the container images, please review their respective license terms. Make sure to upgrade Model Optimizer to the latest version as described above. Visit our installation guide for more fine-grained control on installed dependencies or for alternative docker images and environment variables to setup.
Techniques
| Technique | Description | Getting started | Examples |
|---|---|---|---|
| Post Training Quantization | Compress model size by 2x-4x, speeding up inference while preserving model quality! | [Start here] | [HF LLMs / VLMs] [Megatron-Bridge LLMs / VLMs] [Diffusers] [ONNX] [Windows] |
| Quantization Aware Training / Distillation | Refine accuracy of quantized models even further with a few training steps! | [Start here] | [Hugging Face] [Megatron-Bridge] |
| Pruning | Reduce your model parameters or memory footprint and accelerate inference by removing unnecessary weights! | [Start here] | [General] [Megatron-Bridge] |
| Distillation | Reduce deployment model size by teaching small models to behave like larger models! | [Start here] | [Hugging Face] [Megatron-Bridge] [Megatron-LM] |
| Speculative Decoding | Train draft modules to predict extra tokens during inference! | [Start here] | [Hugging Face] [Megatron-LM] |
| Sparsity | Efficiently compress your model by storing only its non-zero parameter values and their locations | [Start here] | [Hugging Face] |
Pre-Quantized Checkpoints
- Ready-to-deploy checkpoints [🤗 Hugging Face - Nvidia Model Optimizer Collection]
- Deployable on TensorRT-LLM, vLLM and SGLang
- More models coming soon!
Resources
Model Support Matrix
| Model Type | Support Matrix |
|---|---|
| LLM / VLM Quantization | View Support Matrix |
| Diffusers Quantization | View Support Matrix |
| ONNX Quantization | View Support Matrix |
| Windows Quantization | View Support Matrix |
| Quantization Aware Training | View Support Matrix |
| Pruning | View Support Matrix |
| Distillation | View Support Matrix |
| Speculative Decoding | View Support Matrix |
Deprecation Policy
Model Optimizer follows a structured approach to managing deprecated features:
- Communication: Deprecation notices are documented in the Changelog. Deprecated items include source code statements indicating deprecation timing, with runtime warnings issued upon use.
- Migration Period: Since Model Optimizer is still pre-1.0, we provide a 1-release (~1-month) migration period after deprecation. During this window, deprecated features continue functioning while issuing warnings.
- Scope: The policy addresses both complete deprecations (entire APIs removed) and partial ones (specific parameters removed while methods remain).
- Removal: Following the migration period, deprecated elements are removed in alignment with semantic versioning standards, potentially including breaking changes in minor version updates while Model Optimizer remains in 0.x.
Citation
If you use NVIDIA Model Optimizer in your research, please cite it as follows:
@misc{nvidia-modelopt,
author = {{NVIDIA Corporation}},
title = {{NVIDIA Model Optimizer}},
howpublished = {\url{https://github.com/NVIDIA/Model-Optimizer}},
year = {2024--2026},
note = {GitHub repository}
}
Contributing
Model Optimizer is now open source! We welcome any feedback, feature requests and PRs. Please read our Contributing guidelines for details on how to contribute to this project.
AI Agents
ModelOpt's agent skills can be installed from this repository and used in any workspace.
Claude Code
claude plugin marketplace add https://github.com/NVIDIA/Model-Optimizer.git
claude plugin install modelopt@modelopt
Codex
codex plugin marketplace add https://github.com/NVIDIA/Model-Optimizer.git
Then open /plugins, select the modelopt marketplace, and install modelopt.
Contributors can also use the skills directly from a checkout. See the
agent tooling notes.
Top Contributors
Happy optimizing!
