### What does this PR do?
Type of change: Bug fix
The Torch ONNX example tests can exceed their 300-second deadline while
building a ResNet50 INT8 TensorRT engine at optimization level 4. Add
`--trt_builder_optimization_level` to the vision example and select
level 0 in the existing integration tests. The example and helper retain
level 4 by default. Quantization, ONNX export, residual Q/DQ assertions,
engine execution, and test timeout limits are unchanged.
Document the build-time versus inference-performance tradeoff in the
example README.
### Usage
```bash
cd examples/torch_onnx
python torch_quant_to_onnx.py \
--timm_model_name resnet50 \
--recipe timm/resnet/ptq/int8 \
--onnx_save_path resnet50.int8.onnx \
--calibration_data_size 1 --no_pretrained \
--trt_build --trt_builder_optimization_level 0
```
### Testing
Validation used the TensorRT 26.05 container, TensorRT 10.16.1.11, and
PyTorch 2.13.0, with the existing 300-second per-test deadline.
- RTX 6000 Ada: **8 passed**, covering FP8 and INT8 on ViT, Swin,
SwinV2, and ResNet50. ResNet50 INT8 passed in 96.65 seconds.
- RTX PRO 6000 Blackwell Max-Q: **20 passed, 3 existing skips**,
covering the complete test file. ResNet50 INT8 passed in 81.27 seconds;
the baseline timed out at 300 seconds.
```bash
# RTX 6000 Ada: supported FP8/INT8 cases
python -m pytest tests/examples/torch_onnx/test_torch_quant_to_onnx.py \
-k '(fp8 or int8) and not mxfp8' --cov
# RTX PRO 6000 Blackwell: complete example test file
python -m pytest tests/examples/torch_onnx/test_torch_quant_to_onnx.py --cov
```
On each GPU, levels 4 and 0 used the same exported ResNet50 INT8 graph
with TensorRT 10.16.1.11:
- RTX 6000 Ada: TensorRT-reported engine build time decreased from 122.6
seconds at level 4 to 27.7 seconds at level 0. Both builds and inference
runs succeeded.
- RTX PRO 6000 Blackwell Max-Q: the original level-4 test hit its
300-second deadline during the engine build; level 0 built that saved
graph in 10.9 seconds and completed inference successfully.
All pre-commit checks passed for the changed files. The existing
integration tests exercise the real engine build; no redundant mocked
tests were added.
### Before your PR is "*Ready for review*"
Make sure you read and follow [Contributor
guidelines](https://github.com/NVIDIA/Model-Optimizer/blob/main/CONTRIBUTING.md)
and your commits are signed (`git commit -s -S`).
Make sure you read and follow the [Security Best
Practices](https://github.com/NVIDIA/Model-Optimizer/blob/main/SECURITY.md#security-coding-practices-for-contributors)
(e.g. avoiding hardcoded `trust_remote_code=True`, `torch.load(...,
weights_only=False)`, `pickle`, etc.).
- Is this change backward compatible?: ✅
- 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 — the existing integration
tests were updated to exercise level 0; no new test cases were needed.
- Did you update
[Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?:
N/A — minor example/CI fix; library behavior and example defaults are
preserved.
- Did you get Claude approval on this PR?: N/A — not requested for this
focused change.
### Additional Information
Example timeout: [ResNet50 INT8 CI
failure](https://github.com/NVIDIA/Model-Optimizer/actions/runs/36560176214/job/109381161169).
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Added a configurable TensorRT builder optimization level for engine
builds, with a default of 4 and support for values from 0 to 5.
* Documented that level 0 can speed up builds, while lower optimization
levels may reduce inference performance.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
20 KiB
Torch Quantization to ONNX Export
This example demonstrates how to quantize PyTorch models followed by export to ONNX format. The scripts leverage the ModelOpt toolkit for quantization and ONNX export.
For vision models, the torch_quant_to_onnx.py script in this directory handles quantization and ONNX export directly.
For LLMs and VLMs, use TensorRT-Edge-LLM which provides a complete pipeline for quantizing models with ModelOpt and exporting them to optimized ONNX for deployment on edge platforms (Jetson, DRIVE).
| Section | Description | Link |
|---|---|---|
| Pre-Requisites | Required packages to use this example | Link |
| Vision Models | Quantize timm models and export to ONNX | Link |
| LLM Quantization and Export | Quantize and export LLMs/VLMs via TensorRT-Edge-LLM | Link |
| Supported Models | LLM and VLM models supported by TensorRT-Edge-LLM | Link |
| Mixed Precision | Auto mode for optimal per-layer quantization | Link |
| Resources | Extra links to relevant resources | Link |
Pre-Requisites
Docker
Please use the TensorRT docker image (e.g., nvcr.io/nvidia/tensorrt:26.02-py3) or visit our installation docs for more information.
Set the following environment variables inside the TensorRT docker.
export CUDNN_LIB_DIR=/usr/lib/x86_64-linux-gnu/
export LD_LIBRARY_PATH="${CUDNN_LIB_DIR}:${LD_LIBRARY_PATH}"
Local Installation
Install Model Optimizer with onnx dependencies using pip from PyPI and install the requirements for the example:
pip install -U "nvidia-modelopt[onnx]"
pip install -r requirements.txt
For TensorRT Compiler framework workloads:
Install the latest TensorRT from here.
Vision Models
The torch_quant_to_onnx.py script quantizes timm vision models and exports them to ONNX.
What it does
- Loads a pretrained timm torch model (default: ViT-Base).
- Quantizes the torch model to FP8, MXFP8, INT8, NVFP4, or INT4_AWQ using ModelOpt.
- For models with Conv2d layers (e.g., SwinTransformer), automatically overrides Conv2d quantization to FP8 (for MXFP8/NVFP4 modes) or INT8 (for INT4_AWQ mode) for TensorRT compatibility.
- Supports FP8 and INT8 recipes for convolutional architectures such as ResNet. Other formats are not supported for convolutional models because of limited TensorRT kernel support.
- ResNet FP8 and INT8 recipes quantize shortcut inputs immediately before residual adds.
- Exports the quantized model to ONNX.
- Postprocesses the ONNX model to be compatible with TensorRT.
- Saves the final ONNX model.
Opset 20 is used to export the torch models to ONNX.
Usage
python torch_quant_to_onnx.py \
--timm_model_name=<timm model name> \
--qformat=<fp8|mxfp8|int8|nvfp4|int4_awq|auto> \
--onnx_save_path=<path to save the exported ONNX model>
Without --recipe, --qformat selects a quantization preset. Pass a built-in recipe name or YAML
path to --recipe to use a PTQ or AutoQuantize recipe instead. The recipe is authoritative when
provided, so --qformat is ignored.
Convolutional architectures such as ResNet support only FP8 and INT8 quantization. MXFP8, NVFP4, INT4_AWQ, and AutoQuantize are not supported for these models because TensorRT does not provide the required convolution kernels.
Add --trt_build to build an engine after export. The builder optimization level defaults to 4;
use --trt_builder_optimization_level=0 for faster builds, as the example tests do. Lower levels
may reduce the resulting engine's inference performance.
Conv2d Quantization Override
TensorRT only supports FP8 and INT8 for convolution operations. When quantizing models with Conv2d layers (like SwinTransformer), the script automatically applies the following overrides:
| Qformat | Conv2d Override | Reason |
|---|---|---|
| FP8, INT8 | None (already compatible) | Native TRT support |
| MXFP8, NVFP4 | Conv2d -> FP8 | TRT Conv limitation |
| INT4_AWQ | Conv2d -> INT8 | TRT Conv limitation |
These overrides support transformer architectures that contain individual Conv2d layers; they do not make MXFP8, NVFP4, INT4_AWQ, or AutoQuantize supported for convolutional architectures.
Evaluation
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 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.
Note: TensorRT 10.11 or later is required to evaluate the MXFP8 or NVFP4 ONNX models.
python ../onnx_ptq/evaluate.py \
--onnx_path=<path to the exported ONNX model> \
--imagenet_path=<HF dataset card or local path to the ImageNet dataset> \
--engine_precision=stronglyTyped \
--model_name=<timm model name>
HF Embedding and Reranking Models
Experimental: Accuracy has not yet been validated for this example.
hf_embedding_quant_to_onnx.py quantizes an HF text-embedding or reranking
model (bidirectional Llama encoders such as
nvidia/llama-nemotron-embed-1b-v2
and
nvidia/llama-nemotron-rerank-1b-v2)
with a PTQ recipe and exports it to ONNX. Embedding models are exported with
mean pooling and L2 normalization on top of the encoder; reranking
(sequence-classification) models are exported to their relevance logits. Both
graphs take input_ids and attention_mask with dynamic batch/sequence axes.
The default recipe
(modelopt_recipes/model_type/nemotron_llama/ptq/nvfp4_output_quant_proj.yaml)
quantizes weights and activations to NVFP4 and additionally quantizes the
projection-Linear outputs. Without output-side quantization, quantized GEMMs
emit FP16 activations, so FP8/FP4 engines can use as much or more activation
memory than an unquantized FP16 engine; quantizing the projection outputs keeps
inter-layer activations in the low-precision format. An FP8 twin of the recipe
(fp8_output_quant_proj.yaml, pass it via --recipe) applies the same idea to
the FP8 preset. With TensorRT 10.16 on RTX PRO 6000 Blackwell (strongly-typed
engines, 5 dynamic-shape profiles up to 32x512), engine activation memory:
| Model | FP16 | fp8 preset |
fp8 recipe | nvfp4 preset |
nvfp4 recipe |
|---|---|---|---|---|---|
| llama-nemotron-embed-1b-v2 | 1040 MiB | 1392 MiB | 1096 MiB | 1040 MiB | 516 MiB |
| llama-nemotron-rerank-1b-v2 | 1040 MiB | 1392 MiB | 1096 MiB | 520 MiB | 331 MiB |
Usage
python hf_embedding_quant_to_onnx.py \
--model_path=nvidia/llama-nemotron-embed-1b-v2 \
--trust_remote_code \
--recipe=model_type/nemotron_llama/ptq/nvfp4_output_quant_proj \
--onnx_save_path=llama_nemotron_embed_nvfp4.onnx
# Reranking variant (auto-detected from the model architecture)
python hf_embedding_quant_to_onnx.py \
--model_path=nvidia/llama-nemotron-rerank-1b-v2 \
--trust_remote_code \
--onnx_save_path=llama_nemotron_rerank_nvfp4.onnx
Building a TensorRT engine with trtexec
NVFP4 requires a Blackwell GPU (SM100+) and TensorRT 10.11 or later. Build a strongly-typed engine with dynamic shapes (add optimization profiles matching your serving batch sizes and sequence lengths):
trtexec --onnx=llama_nemotron_embed_nvfp4.onnx \
--stronglyTyped \
--saveEngine=llama_nemotron_embed_nvfp4.plan \
--minShapes=input_ids:1x2,attention_mask:1x2 \
--optShapes=input_ids:32x128,attention_mask:32x128 \
--maxShapes=input_ids:32x512,attention_mask:32x512
The exported .onnx references a sibling weights file (<name>.onnx_data);
keep the two files in the same directory when building. To inspect the chosen
kernels and per-profile activation memory, add
--profilingVerbosity=detailed --exportLayerInfo=<path>.json --verbose.
LLM Quantization and Export with TensorRT-Edge-LLM
TensorRT-Edge-LLM provides a complete pipeline for quantizing LLMs and VLMs using NVIDIA ModelOpt and exporting them to optimized ONNX for deployment on edge platforms such as NVIDIA Jetson and DRIVE.
Overview
The pipeline follows these stages:
- Quantize (x86 host with GPU) — Reduce model precision using ModelOpt (FP8, INT4 AWQ, NVFP4)
- Export (x86 host with GPU) — Convert quantized model to ONNX
- Build (edge device) — Compile ONNX into TensorRT engines
- Inference (edge device) — Run the compiled engines
Installation
# Use the PyTorch Docker image (recommended)
docker pull nvcr.io/nvidia/pytorch:25.12-py3
docker run --gpus all -it --rm -v $(pwd):/workspace -w /workspace nvcr.io/nvidia/pytorch:25.12-py3 bash
# Clone and install TensorRT-Edge-LLM
git clone https://github.com/NVIDIA/TensorRT-Edge-LLM.git
cd TensorRT-Edge-LLM
git submodule update --init --recursive
python3 -m venv venv
source venv/bin/activate
pip3 install .
# Verify installation
tensorrt-edgellm-quantize --help
tensorrt-edgellm-export --help
System requirements:
- x86-64 Linux (Ubuntu 22.04 or 24.04 recommended)
- NVIDIA GPU with Compute Capability 8.0+ (Ampere or newer)
- CUDA 12.x or 13.x, Python 3.10+
- GPU VRAM: 16 GB for models up to 3B, 40 GB for models up to 4B, 80 GB for models up to 8B
CLI Tools
| Tool | Purpose |
|---|---|
tensorrt-edgellm-quantize |
Quantize models using ModelOpt (FP8, INT4 AWQ, NVFP4); subcommands: llm, draft |
tensorrt-edgellm-export |
Export quantized or FP16/BF16 checkpoint to ONNX; auto-detects VLM and audio components |
tensorrt-edgellm-insert-lora |
Insert LoRA patterns into existing ONNX models |
tensorrt-edgellm-process-lora |
Process LoRA adapter weights for runtime loading |
Example: Quantize and Export an LLM
# Step 1: Quantize with ModelOpt
tensorrt-edgellm-quantize llm \
--model_dir Qwen/Qwen2.5-3B-Instruct \
--quantization fp8 \
--output_dir quantized/qwen2.5-3b-fp8
# Step 2: Export to ONNX
tensorrt-edgellm-export \
quantized/qwen2.5-3b-fp8 \
onnx_models/qwen2.5-3b
Example: Quantize and Export a VLM
# Quantize with ModelOpt (handles both LLM and visual components)
tensorrt-edgellm-quantize llm \
--model_dir Qwen/Qwen2.5-VL-3B-Instruct \
--quantization fp8 \
--output_dir quantized/qwen2.5-vl-3b
# Export to ONNX (auto-detects VLM and exports LLM + visual encoder to separate subdirs)
tensorrt-edgellm-export \
quantized/qwen2.5-vl-3b \
onnx_models/qwen2.5-vl-3b
Example: EAGLE Speculative Decoding
# Quantize base model
tensorrt-edgellm-quantize llm \
--model_dir meta-llama/Llama-3.1-8B-Instruct \
--quantization fp8 \
--output_dir quantized/llama3.1-8b-base
# Export base model with EAGLE flag
tensorrt-edgellm-export \
quantized/llama3.1-8b-base \
onnx_models/llama3.1-8b/base \
--eagle-base
# Quantize EAGLE draft model
tensorrt-edgellm-quantize draft \
--base_model_dir meta-llama/Llama-3.1-8B-Instruct \
--draft_model_dir EAGLE3-LLaMA3.1-Instruct-8B \
--quantization fp8 \
--output_dir quantized/llama3.1-8b-draft
# Export draft model
tensorrt-edgellm-export \
quantized/llama3.1-8b-draft \
onnx_models/llama3.1-8b/draft
Quantization Methods
| Method | Description |
|---|---|
| FP8 | Best accuracy-to-memory balance on SM89+ hardware (Hopper, Ada) |
| INT4 AWQ | Weight-only quantization; effective for memory-constrained platforms and low-batch inference |
| NVFP4 | 4-bit format for NVIDIA Blackwell and Thor hardware; applies to both weights and activations |
| MXFP8 | Experimental; Microscaling FP8 format for SM89+ hardware |
| INT8 SmoothQuant | Experimental; INT8 weight and activation quantization with SmoothQuant |
| INT4 GPTQ | Can be loaded directly from HuggingFace Hub (no additional quantization needed) |
Supported Models
For the latest support matrix, see the TensorRT-Edge-LLM Supported Models page.
LLMs
| Model | FP16 | FP8 | INT4 | NVFP4 |
|---|---|---|---|---|
| Llama-3-8B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Llama-3.1-8B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Llama-3.2-3B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen2-0.5B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen2-1.5B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen2-7B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen2.5-0.5B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen2.5-1.5B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen2.5-3B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen2.5-7B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen3-0.6B | ✅ | ✅ | ✅ | ✅ |
| Qwen3-1.7B | ✅ | ✅ | ✅ | ✅ |
| Qwen3-4B-Instruct-2507 | ✅ | ✅ | ✅ | ✅ |
| Qwen3-8B | ✅ | ✅ | ✅ | ✅ |
| DeepSeek-R1-Distill-Qwen-1.5B | ✅ | ✅ | ✅ | ✅ |
| DeepSeek-R1-Distill-Qwen-7B | ✅ | ✅ | ✅ | ✅ |
VLMs
| Model | FP16 | FP8 | INT4 | NVFP4 |
|---|---|---|---|---|
| Qwen2-VL-2B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen2-VL-7B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen2.5-VL-3B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen2.5-VL-7B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen3-VL-2B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen3-VL-4B-Instruct | ✅ | ✅ | ✅ | ✅ |
| Qwen3-VL-8B-Instruct | ✅ | ✅ | ✅ | ✅ |
| InternVL3-1B | ✅ | ✅ | ✅ | ✅ |
| InternVL3-2B | ✅ | ✅ | ✅ | ✅ |
| Phi-4-multimodal-instruct | ✅ | ✅ | ✅ | ✅ |
Troubleshooting
- GPU out of memory: Use a larger GPU (40 GB for models up to 4B, 80 GB for models up to 8B) or try
--device cpu(limited precision support). - Calibration dataset issues: Download the dataset manually and pass the local path with
--calib_dataset ./path/to/dataset. - Accuracy degradation: Try FP8 instead of INT4/NVFP4, or increase calibration sample size.
For full documentation, see the TensorRT-Edge-LLM Developer Guide.
Mixed Precision Quantization (Auto Mode)
AutoQuantize recipes enable 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. The --qformat=auto CLI mode remains available for configuring the search with individual flags.
How it works
- Sensitivity Analysis: Computes per-layer sensitivity scores using gradient-based analysis
- Format Search: Searches across specified quantization formats for each layer
- Constraint Optimization: Finds the optimal format assignment that satisfies the effective bits constraint while minimizing accuracy loss
Key Parameters
| Parameter | Default | Description |
|---|---|---|
--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). |
--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. |
--calibration_data_size |
512 | Number of calibration samples used for both sensitivity scoring and calibration. For auto mode, labels are required for loss computation. |
Usage
python torch_quant_to_onnx.py \
--timm_model_name=vit_base_patch16_224 \
--recipe=general/auto_quantize/nvfp4_fp8_at_5p4bits \
--calibration_data_size=512 \
--evaluate \
--onnx_save_path=vit_base_patch16_224.auto_quant.onnx
The equivalent flag-based form is:
python torch_quant_to_onnx.py \
--timm_model_name=vit_base_patch16_224 \
--qformat=auto \
--auto_quantization_formats nvfp4_awq_lite fp8 \
--effective_bits=4.8 \
--num_score_steps=128 \
--calibration_data_size=512 \
--evaluate \
--onnx_save_path=vit_base_patch16_224.auto_quant.onnx
ONNX Export Supported Vision Models
| Model | FP8 | INT8 | MXFP8 | NVFP4 | INT4_AWQ | Auto |
|---|---|---|---|---|---|---|
| vit_base_patch16_224 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| swin_tiny_patch4_window7_224 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| swinv2_tiny_window8_256 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| resnet50 | ✅ | ✅ | N/A | N/A | N/A | N/A |
Resources
Technical Resources
There are many quantization schemes supported in the example scripts:
-
The FP8 format is available on the Hopper and Ada GPUs with CUDA compute capability greater than or equal to 8.9.
-
The INT4 AWQ 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.
-
The NVFP4 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.