Techniques to Imporve TensorRT Model Inference Speed

Description

Ways to improve Model Inference Speed

We aim to enhance the inference speed of our object detection model without compromising accuracy.

Current Setup:

Model Task: Object Detection (3 Classes)

Hardware: NVIDIA Jetson (likely AGX Orin)

Input: Single image/frame inference

Model Versions:

PyTorch Version:

Model Size: 88 MB

Inference Latency: ~420 ms per frame

TensorRT Version (Quantized):

Model Size: 90 MB on FP16

Inference Latency: ~200 ms per frame

Objectives:

  • Further reduce latency to process high throughput (preferably below 100 ms/ per frame inference)

  • Maintain or minimally impact model accuracy on INT8 quantized version.

Environment

TensorRT Version: 10.3.0.30
GPU Type: Tegra
Nvidia Driver Version: 540.4.0
CUDA Version: 12.6.68
CUDNN Version: 9.3.0.75
Operating System + Version: Ubuntu 22.04
Python Version (if applicable): 3.10.12
Baremetal or Container (if container which image + tag): ultralytics/ultralytics:latest-jetson-jetpack6

Please ensure you follow best practices outlined in our documentation.

Hi @Arumugamdeepakkumar — going from 420 ms (PyTorch) to 200 ms (TensorRT FP16) is already a solid win. Here are the next levers to pull on Jetson AGX Orin without sacrificing much accuracy.

**1. INT8 quantization**

This is usually the biggest next step. On Jetson, INT8 can be 1.5–2x faster than FP16 with minimal accuracy loss for object detection.

- Use TensorRT’s calibration with a few hundred representative images.

- If accuracy drops, try per-channel quantization or partial INT8 (keep sensitive layers in FP16).

- On Jetson Orin, INT8 uses the DPX/Tensor Cores effectively.

**2. Reduce input resolution**

If your model is running at 640x640, try 512x512 or 416x416. For 3-class detection, you can often drop resolution significantly with only a small mAP hit. Measure mAP@50 on your actual dataset, not just COCO.

**3. Prune the backbone**

If you’re using YOLOv8/YOLO11 or similar:

- Switch to a smaller variant (YOLOv8n instead of YOLOv8s/m).

- Use neural architecture search or structured pruning tools like Torch-Pruning.

- A smaller backbone in FP16 often beats a larger backbone in INT8.

**4. Optimize the TensorRT build**

```bash

/usr/src/tensorrt/bin/trtexec \

–onnx=model.onnx \

–fp16 --int8 \

–workspace=4096 \

–minShapes=images:1x3x416x416 \

–optShapes=images:1x3x416x416 \

–maxShapes=images:1x3x416x416 \

–saveEngine=model.plan

```

Use static shapes for single-image inference — dynamic shapes add overhead.

**5. CUDA Graphs**

For fixed-shape, single-image inference, capture a CUDA graph around the `enqueueV3` call. This removes per-launch CPU overhead and can give another 10–30% depending on model size.

**6. Pipeline preprocessing on GPU**

If you’re still doing resize/letterbox/normalize on CPU, move it to CUDA with NPP or a custom kernel. On Jetson, CPU→GPU copies are often a hidden bottleneck.

**7. Power mode**

Make sure you’re in MAXN mode:

```bash

sudo nvpmodel -m 0

sudo jetson_clocks

```

**Realistic target**

For a 3-class detector on AGX Orin, I’d expect:

- FP16 + 416x416: ~80–120 ms

- INT8 + 416x416: ~50–80 ms

- INT8 + 320x320: ~30–50 ms

Your actual numbers depend heavily on backbone choice. If you share which model architecture you’re using, I can give more specific advice.

Your jump from 420 ms (PyTorch) to 200 ms (TensorRT FP16) is solid. Here’s how to push toward your <100 ms target on Jetson AGX Orin:

1. INT8 Quantization (1.5–2x speedup)
This is the biggest next lever. Use TensorRT’s calibration with a few hundred representative images. If accuracy drops, try per-channel quantization or partial INT8 (keep sensitive layers in FP16). On Jetson Orin, INT8 uses the DPX/Tensor Cores effectively.

2. Reduce Input Resolution
If running at 640×640, try 512×512 or 416×416. For 3-class detection, you can often drop resolution significantly with only a small mAP hit. Measure mAP@50 on your actual dataset.

3. Prune the Backbone
If using YOLOv8/YOLO11:

  • Switch to a smaller variant (YOLOv8n instead of YOLOv8s/m)
  • Use structured pruning tools like Torch-Pruning
  • A smaller backbone in FP16 often beats a larger backbone in INT8

4. Optimize the TensorRT Build
Use static shapes for single-image inference—dynamic shapes add overhead:

/usr/src/tensorrt/bin/trtexec --onnx=model.onnx --fp16 --int8 --workspace=4096 --minShapes=images:1x3x416x416 --optShapes=images:1x3x416x416 --maxShapes=images:1x3x416x416 --saveEngine=model.plan

5. CUDA Graphs
For fixed-shape, single-image inference, capture a CUDA graph around the enqueueV3 call. This removes per-launch CPU overhead and can give another 10–30% depending on model size.

6. Pipeline Preprocessing on GPU
If still doing resize/letterbox/normalize on CPU, move it to CUDA with NPP or a custom kernel. CPU→GPU copies are often a hidden bottleneck on Jetson.

7. Power Mode
Ensure you’re in MAXN mode:

sudo nvpmodel -m 0
sudo jetson_clocks

Realistic Targets for 3-Class Detector on AGX Orin:

  • FP16 + 416×416: ~80–120 ms
  • INT8 + 416×416: ~50–80 ms
  • INT8 + 320×320: ~30–50 ms

Your actual numbers depend heavily on backbone choice. Start with INT8 + resolution reduction, then layer in CUDA graphs and GPU preprocessing.