|
| 1 | +# Torch Quantization to ONNX Export |
| 2 | + |
| 3 | +This example demonstrates how to quantize PyTorch models (vision and LLM) followed by export to ONNX format. The scripts leverage the ModelOpt toolkit for both quantization and ONNX export. |
| 4 | + |
| 5 | +<div align="center"> |
| 6 | + |
| 7 | +| **Section** | **Description** | **Link** | |
| 8 | +| :------------: | :------------: | :------------: | |
| 9 | +| Pre-Requisites | Required packages to use this example | [Link](#pre-requisites) | |
| 10 | +| Vision Models | Quantize timm models and export to ONNX | [Link](#vision-models) | |
| 11 | +| LLM Export | Export LLMs to quantized ONNX | [Link](#llm-export) | |
| 12 | +| Mixed Precision | Auto mode for optimal per-layer quantization | [Link](#mixed-precision-quantization-auto-mode) | |
| 13 | +| Support Matrix | View the ONNX export supported LLM models | [Link](#onnx-export-supported-llm-models) | |
| 14 | +| Resources | Extra links to relevant resources | [Link](#resources) | |
| 15 | + |
| 16 | +</div> |
| 17 | + |
| 18 | +## Pre-Requisites |
| 19 | + |
| 20 | +### Docker |
| 21 | + |
| 22 | +Please use the TensorRT docker image (e.g., `nvcr.io/nvidia/tensorrt:25.08-py3`) or visit our [installation docs](https://nvidia.github.io/Model-Optimizer/getting_started/2_installation.html) for more information. |
| 23 | + |
| 24 | +Set the following environment variables inside the TensorRT docker. |
| 25 | + |
| 26 | +```bash |
| 27 | +export CUDNN_LIB_DIR=/usr/lib/x86_64-linux-gnu/ |
| 28 | +export LD_LIBRARY_PATH="${CUDNN_LIB_DIR}:${LD_LIBRARY_PATH}" |
| 29 | +``` |
| 30 | + |
| 31 | +### Local Installation |
| 32 | + |
| 33 | +Install Model Optimizer with `onnx` dependencies using `pip` from [PyPI](https://pypi.org/project/nvidia-modelopt/) and install the requirements for the example: |
| 34 | + |
| 35 | +```bash |
| 36 | +pip install -U "nvidia-modelopt[onnx]" |
| 37 | +pip install -r requirements.txt |
| 38 | +``` |
| 39 | + |
| 40 | +For TensorRT Compiler framework workloads: |
| 41 | + |
| 42 | +Install the latest [TensorRT](https://developer.nvidia.com/tensorrt) from [here](https://developer.nvidia.com/tensorrt/download). |
| 43 | + |
| 44 | +## Vision Models |
| 45 | + |
| 46 | +The `torch_quant_to_onnx.py` script quantizes [timm](https://github.com/huggingface/pytorch-image-models) vision models and exports them to ONNX. |
| 47 | + |
| 48 | +### What it does |
| 49 | + |
| 50 | +- Loads a pretrained timm torch model (default: ViT-Base). |
| 51 | +- Quantizes the torch model to FP8, MXFP8, INT8, NVFP4, or INT4_AWQ using ModelOpt. |
| 52 | +- Exports the quantized model to ONNX. |
| 53 | +- Postprocesses the ONNX model to be compatible with TensorRT. |
| 54 | +- Saves the final ONNX model. |
| 55 | + |
| 56 | +> *Opset 20 is used to export the torch models to ONNX.* |
| 57 | +
|
| 58 | +### Usage |
| 59 | + |
| 60 | +```bash |
| 61 | +python torch_quant_to_onnx.py \ |
| 62 | + --timm_model_name=vit_base_patch16_224 \ |
| 63 | + --quantize_mode=<fp8|mxfp8|int8|nvfp4|int4_awq> \ |
| 64 | + --onnx_save_path=<path to save the exported ONNX model> |
| 65 | +``` |
| 66 | + |
| 67 | +### Evaluation |
| 68 | + |
| 69 | +If the input model is of type image classification, use the following script to evaluate it. The script automatically downloads and uses the [ILSVRC/imagenet-1k](https://huggingface.co/datasets/ILSVRC/imagenet-1k) dataset from Hugging Face. This gated repository requires authentication via Hugging Face access token. See <https://huggingface.co/docs/hub/en/security-tokens> for details. |
| 70 | + |
| 71 | +> *Note: TensorRT 10.11 or later is required to evaluate the MXFP8 or NVFP4 ONNX models.* |
| 72 | +
|
| 73 | +```bash |
| 74 | +python ../onnx_ptq/evaluate.py \ |
| 75 | + --onnx_path=<path to the exported ONNX model> \ |
| 76 | + --imagenet_path=<HF dataset card or local path to the ImageNet dataset> \ |
| 77 | + --engine_precision=stronglyTyped \ |
| 78 | + --model_name=vit_base_patch16_224 |
| 79 | +``` |
| 80 | + |
| 81 | +## LLM Export |
| 82 | + |
| 83 | +The `llm_export.py` script exports LLM models to ONNX with optional quantization. |
| 84 | + |
| 85 | +### What it does |
| 86 | + |
| 87 | +- Loads a HuggingFace LLM model (local path or model name). |
| 88 | +- Optionally quantizes the model to FP8, INT4_AWQ, or NVFP4. |
| 89 | +- Exports the model to ONNX format. |
| 90 | +- Post-processes the ONNX graph for TensorRT compatibility. |
| 91 | + |
| 92 | +### Usage |
| 93 | + |
| 94 | +```bash |
| 95 | +python llm_export.py \ |
| 96 | + --hf_model_path=<HuggingFace model name or local path> \ |
| 97 | + --dtype=<fp16|fp8|int4_awq|nvfp4> \ |
| 98 | + --output_dir=<directory to save ONNX model> |
| 99 | +``` |
| 100 | + |
| 101 | +### Examples |
| 102 | + |
| 103 | +Export Qwen2 to FP16 ONNX: |
| 104 | + |
| 105 | +```bash |
| 106 | +python llm_export.py \ |
| 107 | + --hf_model_path=Qwen/Qwen2-0.5B-Instruct \ |
| 108 | + --dtype=fp16 \ |
| 109 | + --output_dir=./qwen2_fp16 |
| 110 | +``` |
| 111 | + |
| 112 | +Export Qwen2 to FP8 ONNX with quantization: |
| 113 | + |
| 114 | +```bash |
| 115 | +python llm_export.py \ |
| 116 | + --hf_model_path=Qwen/Qwen2-0.5B-Instruct \ |
| 117 | + --dtype=fp8 \ |
| 118 | + --output_dir=./qwen2_fp8 |
| 119 | +``` |
| 120 | + |
| 121 | +Export to NVFP4 with custom calibration: |
| 122 | + |
| 123 | +```bash |
| 124 | +python llm_export.py \ |
| 125 | + --hf_model_path=Qwen/Qwen3-0.6B \ |
| 126 | + --dtype=nvfp4 \ |
| 127 | + --calib_size=512 \ |
| 128 | + --output_dir=./qwen3_nvfp4 |
| 129 | +``` |
| 130 | + |
| 131 | +### Key Parameters |
| 132 | + |
| 133 | +| Parameter | Description | |
| 134 | +| :--- | :--- | |
| 135 | +| `--hf_model_path` | HuggingFace model name (e.g., `Qwen/Qwen2-0.5B-Instruct`) or local model path | |
| 136 | +| `--dtype` | Export precision: `fp16`, `fp8`, `int4_awq`, or `nvfp4` | |
| 137 | +| `--output_dir` | Directory to save the exported ONNX model | |
| 138 | +| `--calib_size` | Number of calibration samples for quantization (default: 512) | |
| 139 | +| `--lm_head` | Precision of lm_head layer (default: `fp16`) | |
| 140 | +| `--save_original` | Save the raw ONNX before post-processing | |
| 141 | +| `--trust_remote_code` | Trust remote code when loading from HuggingFace Hub | |
| 142 | + |
| 143 | +## Mixed Precision Quantization (Auto Mode) |
| 144 | + |
| 145 | +The `auto` mode enables mixed precision quantization by searching for the optimal quantization format per layer. This approach balances model accuracy and compression by assigning different precision formats (e.g., NVFP4, FP8) to different layers based on their sensitivity. |
| 146 | + |
| 147 | +### How it works |
| 148 | + |
| 149 | +1. **Sensitivity Analysis**: Computes per-layer sensitivity scores using gradient-based analysis |
| 150 | +2. **Format Search**: Searches across specified quantization formats for each layer |
| 151 | +3. **Constraint Optimization**: Finds the optimal format assignment that satisfies the effective bits constraint while minimizing accuracy loss |
| 152 | + |
| 153 | +### Key Parameters |
| 154 | + |
| 155 | +| Parameter | Default | Description | |
| 156 | +| :--- | :---: | :--- | |
| 157 | +| `--effective_bits` | 4.8 | Target average bits per weight across the model. Lower values = more compression but potentially lower accuracy. The search algorithm finds the optimal per-layer format assignment that meets this constraint while minimizing accuracy loss. For example, 4.8 means an average of 4.8 bits per weight (mix of FP4 and FP8 layers). | |
| 158 | +| `--num_score_steps` | 128 | Number of forward/backward passes used to compute per-layer sensitivity scores via gradient-based analysis. Higher values provide more accurate sensitivity estimates but increase search time. Recommended range: 64-256. | |
| 159 | +| `--calibration_data_size` | 512 | Number of calibration samples used for both sensitivity scoring and calibration. For auto mode, labels are required for loss computation. | |
| 160 | + |
| 161 | +### Usage |
| 162 | + |
| 163 | +```bash |
| 164 | +python torch_quant_to_onnx.py \ |
| 165 | + --timm_model_name=vit_base_patch16_224 \ |
| 166 | + --quantize_mode=auto \ |
| 167 | + --auto_quantization_formats NVFP4_AWQ_LITE_CFG FP8_DEFAULT_CFG \ |
| 168 | + --effective_bits=4.8 \ |
| 169 | + --num_score_steps=128 \ |
| 170 | + --calibration_data_size=512 \ |
| 171 | + --evaluate \ |
| 172 | + --onnx_save_path=vit_base_patch16_224.auto_quant.onnx |
| 173 | +``` |
| 174 | + |
| 175 | +### Results (ViT-Base) |
| 176 | + |
| 177 | +| | Top-1 accuracy (torch) | Top-5 accuracy (torch) | |
| 178 | +| :--- | :---: | :---: | |
| 179 | +| Torch autocast (FP16) | 85.11% | 97.53% | |
| 180 | +| NVFP4 Quantized | 84.558% | 97.36% | |
| 181 | +| Auto Quantized (FP8 + NVFP4, 4.78 effective bits) | 84.726% | 97.434% | |
| 182 | + |
| 183 | +## ONNX Export Supported LLM Models |
| 184 | + |
| 185 | +| Model | FP16 | INT4 | FP8 | NVFP4 | |
| 186 | +| :---: | :---: | :---: | :---: | :---: | |
| 187 | +| [Llama-3-8B-Instruct](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct) | ✅ | ✅ | ✅ | ✅ | |
| 188 | +| [Llama3.1-8B](https://huggingface.co/meta-llama/Llama-3.1-8B) | ✅ | ✅ | ✅ | ✅ | |
| 189 | +| [Llama3.2-3B](https://huggingface.co/meta-llama/Llama-3.2-3B) | ✅ | ✅ | ✅ | ✅ | |
| 190 | +| [Qwen2-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2-0.5B-Instruct) | ✅ | ✅ | ✅ | ✅ | |
| 191 | +| [Qwen2-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2-1.5B-Instruct) | ✅ | ✅ | ✅ | ✅ | |
| 192 | +| [Qwen2-7B-Instruct](https://huggingface.co/Qwen/Qwen2-7B-Instruct) | ✅ | ✅ | ✅ | ✅ | |
| 193 | +| [Qwen2.5-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct) | ✅ | ✅ | ✅ | ✅ | |
| 194 | +| [Qwen2.5-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct) | ✅ | ✅ | ✅ | ✅ | |
| 195 | +| [Qwen2.5-3B-Instruct](https://huggingface.co/Qwen/Qwen2.5-3B-Instruct) | ✅ | ✅ | ✅ | ✅ | |
| 196 | +| [Qwen2.5-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-7B-Instruct) | ✅ | ✅ | ✅ | ✅ | |
| 197 | + |
| 198 | +## Resources |
| 199 | + |
| 200 | +- 📅 [Roadmap](https://github.com/NVIDIA/Model-Optimizer/issues/146) |
| 201 | +- 📖 [Documentation](https://nvidia.github.io/Model-Optimizer) |
| 202 | +- 🎯 [Benchmarks](../benchmark.md) |
| 203 | +- 💡 [Release Notes](https://nvidia.github.io/Model-Optimizer/reference/0_changelog.html) |
| 204 | +- 🐛 [File a bug](https://github.com/NVIDIA/Model-Optimizer/issues/new?template=1_bug_report.md) |
| 205 | +- ✨ [File a Feature Request](https://github.com/NVIDIA/Model-Optimizer/issues/new?template=2_feature_request.md) |
| 206 | + |
| 207 | +### Technical Resources |
| 208 | + |
| 209 | +There are many quantization schemes supported in the example scripts: |
| 210 | + |
| 211 | +1. The [FP8 format](https://developer.nvidia.com/blog/nvidia-arm-and-intel-publish-fp8-specification-for-standardization-as-an-interchange-format-for-ai/) is available on the Hopper and Ada GPUs with [CUDA compute capability](https://developer.nvidia.com/cuda-gpus) greater than or equal to 8.9. |
| 212 | + |
| 213 | +1. The [INT4 AWQ](https://arxiv.org/abs/2306.00978) is an INT4 weight only quantization and calibration method. INT4 AWQ is particularly effective for low batch inference where inference latency is dominated by weight loading time rather than the computation time itself. For low batch inference, INT4 AWQ could give lower latency than FP8/INT8 and lower accuracy degradation than INT8. |
| 214 | + |
| 215 | +1. The [NVFP4](https://blogs.nvidia.com/blog/generative-ai-studio-ces-geforce-rtx-50-series/) is one of the new FP4 formats supported by NVIDIA Blackwell GPU and demonstrates good accuracy compared with other 4-bit alternatives. NVFP4 can be applied to both model weights as well as activations, providing the potential for both a significant increase in math throughput and reductions in memory footprint and memory bandwidth usage compared to the FP8 data format on Blackwell. |
0 commit comments