Hitesh Sahu
Hitesh SahuHitesh Sahu
  1. Home
  2. ›
  3. posts
  4. ›
  5. …

  6. ›
  7. 2 2 TensorRT

Loading ⏳
Fetching content, this won’t take long…


💡 Did you know?

🤯 Your stomach gets a new lining every 3–4 days.

🍪 This website uses cookies

No personal data is stored on our servers however third party tools Google Analytics cookies to measure traffic and improve your website experience. Learn more

Loading ⏳
Fetching content, this won’t take long…


💡 Did you know?

🍯 Honey never spoils — archaeologists found 3,000-year-old jars still edible.
AI-Infrastructure

    AI-AgenticAI

    AI-DeepLearning

    AI-GenAI

    AI-Infrastructure
    • NVIDIA AI Infrastructure and Operations Fundamentals


    • AI Infra Computing : GPU, DPU, Virtualization, DGX Systems


    • AI Programming Model


    • Pinned Memory (Page-Locked Memory) in CUDA and GPU Computing


    • RAPIDS and GPU Accelerated Data Science: cuDF, cuML, CUDA, NCCL and Distributed AI Pipelines


    • NVIDIA DCGM: GPU Health, Diagnostics, and Prometheus Metrics


    • NVIDIA Base Command Manager: Provisioning and Operating GPU Clusters


    • Slurm: The HPC Workload Manager Behind AI Training Clusters


    • TensorRT and High-Performance AI Inference: CUDA, ONNX, TensorRT-LLM and GPU Optimization


    • NCCL and Distributed GPU Communication: CUDA, AllReduce, Multi-GPU and AI Cluster Networking


    • ONNX (Open Neural Network Exchange): Portable AI Models, TensorRT and Cross-Framework Inference


    • LangChain and AI Agent Orchestration: RAG, LLM Workflows, Vector Databases and Tool Calling


    • NVIDIA NeMo and Enterprise AI Platforms: Distributed LLM Training, RAG and TensorRT-LLM


    • Megatron-LM and Distributed LLM Training: Tensor Parallelism, NCCL and Trillion-Scale AI Models


    • NVIDIA Triton Inference Server: TensorRT-LLM, GPU Serving and Production AI Inference


    • NVIDIA Riva: Real-Time Conversational AI with ASR, NLP and Text-to-Speech


    • NVIDIA NGC Catalog: GPU Optimized Containers, AI Models and Enterprise AI Infrastructure


    • AI Infra Networking: GPU Clusters, InfiniBand, RoCE, and DPU Integration


    • AI Infra Storage: NVMe, Parallel File Systems, Object Storage, and GPUDirect Storage


    • AI/ML Operations


    • AI-Infrastructure Index


    AI-Machine-Learning

    AI-Math

    AWS

    Azure

    kubernetes

    Management

    Programming

    Terraform

    Z_Appendix

Cover Image for TensorRT and High-Performance AI Inference: CUDA, ONNX, TensorRT-LLM and GPU Optimization
AI-Infrastructure

TensorRT and High-Performance AI Inference: CUDA, ONNX, TensorRT-LLM and GPU Optimization

Comprehensive overview of NVIDIA TensorRT covering ONNX model optimization, CUDA kernel fusion, FP16 and INT8 inference, TensorRT-LLM, GPU memory optimization, Triton Inference Server integration, and production-scale AI inference pipelines on NVIDIA GPUs.

NVIDIA
TensorRT
TensorRT-LLM
CUDA
ONNX
GPU Inference
← Previous

RAPIDS and GPU Accelerated Data Science: cuDF, cuML, CUDA, NCCL and Distributed AI Pipelines

Next →

NCCL and Distributed GPU Communication: CUDA, AllReduce, Multi-GPU and AI Cluster Networking

🖲 TensorRT

NVIDIA TensorRT is a C++ library that facilitates high-performance inference on NVIDIA graphics processing units (GPUs).

TensorRT takes a trained network and produces a highly optimized runtime engine that performs inference for that network.

What is TensorRT?

NVIDIA’s high-performance deep learning inference SDK designed to optimize and accelerate trained AI models on NVIDIA GPUs.

It takes trained models from frameworks such as PyTorch, TensorFlow, and ONNX, and optimizes them for high-performance deployment with support for mixed precision (FP32/FP16/BF16/FP8/INT8), dynamic shapes, and specialized optimizations for transformers and large language models (LLMs).

It is mainly used for:

  • Low-latency inference
  • High-throughput AI serving
  • Real-time AI applications
  • LLM inference optimization
  • Edge AI deployments

TensorRT takes trained models from frameworks like PyTorch or TensorFlow and converts them into highly optimized GPU execution engines.

Why TensorRT Is Fast

TensorRT improves performance through:

  • Kernel fusion
  • Reduced precision inference
  • GPU-specific tuning
  • Optimized memory reuse
  • Parallel CUDA execution
  • Reduced data movement
  • Efficient batching

This can often produce:

  • 2x–10x faster inference
  • lower latency
  • lower GPU memory usage

compared to standard framework inference.

TensorRT Architecture

flowchart TD

    A["Trained Model 🎛<br/>PyTorch / TensorFlow"]
        --> B["ONNX Export"]

    B --> C["TensorRT Optimizer<br/><br/>• Layer Fusion<br/>• Quantization<br/>• Kernel Tuning"]

    C --> D["TensorRT Engine 🖲" ]

    D --> E["CUDA Runtime 📟"]

    E --> F["NVIDIA GPU 🧮" ]

TensorRT vs PyTorch Inference

FeaturePyTorchTensorRT
Ease of useEasierMore optimization setup
Training supportYesNo
Inference speedGoodExcellent
GPU optimizationGeneralHighly optimized
Production deploymentModerateExcellent
LatencyHigherLower

Running TensorRT

TensorRT is available as standalone Docker Image

# Pull the latest TensorRT Docker image
docker pull nvcr.io/nvidia/tensorrt:26.04-py3

# Run a container with TensorRT
docker run --gpus all -it --rm nvcr.io/nvidia/tensorrt:26.04-py3

Common TensorRT Use Cases

  • LLM serving
  • Real-time computer vision
  • Autonomous driving
  • Recommendation systems
  • Speech AI
  • Video analytics
  • Edge AI devices
  • Robotics
  • Medical imaging

How TensorRT Works Under the Hood

TensorRT Ecosystem

ComponentPurpose
CUDAGPU compute platform
cuDNNDeep learning kernels
TensorRTInference optimization
Triton ServerModel serving
TensorRT-LLMLLM optimization
NCCLMulti-GPU communication

1. Model Import

TensorRT typically imports models using ONNX.

torch.onnx.export(model, sample_input, "model.onnx")

Supported sources:

  • PyTorch
  • TensorFlow
  • ONNX
  • Hugging Face Transformers
  • TensorFlow-TRT integration

2. Graph Optimization

TensorRT analyzes the neural network computation graph and applies optimizations such as:

  • Layer fusion
  • Kernel auto-tuning
  • Precision calibration
  • Memory optimization
  • Tensor layout optimization

Example:

    Conv + BatchNorm + ReLU
            ↓
    Single fused GPU kernel

This reduces:

  • GPU memory reads/writes
  • Kernel launch overhead
  • Latency

Precision Optimization

TensorRT supports multiple precision modes:

PrecisionDescription
FP32Standard floating point
FP16Half precision for faster inference
INT8Quantized inference for maximum speed
FP8Newer ultra-efficient precision on modern GPUs

Lower precision:

  • reduces memory usage
  • increases throughput
  • improves latency

FP32 vs FP16 vs INT8

ModeSpeedAccuracyMemory Usage
FP32SlowestHighestHighest
FP16FasterVery closeLower
INT8FastestSlight drop possibleLowest

Example:


config.set_flag(trt.BuilderFlag.FP16)

3. CUDA Kernel Selection

TensorRT benchmarks multiple CUDA kernels internally and selects the fastest implementation for the target GPU.

This is called:

Kernel Auto-Tuning

Different GPUs may produce different optimized engines.

4. Engine Generation

TensorRT builds a serialized inference engine.


serialized_engine = engine.serialize()

This engine contains:

  • optimized kernels
  • memory plans
  • execution graphs
  • scheduling strategies

The engine is GPU-specific.

5. Runtime Execution

Inference executes directly on the GPU with minimal CPU overhead.


context.execute_v2(bindings)

TensorRT optimizes:

  • memory reuse
  • asynchronous execution
  • CUDA stream utilization
  • batching

TensorRT + LLM Inference

TensorRT is heavily used for LLM acceleration.

NVIDIA provides:

  • TensorRT-LLM (its predecessor, FasterTransformer, has been folded into and superseded by TensorRT-LLM)
  • Triton Inference Server integration

Optimizations for LLMs include:

  • KV cache optimization
  • Attention kernel fusion
  • Paged attention
  • Tensor parallelism
  • Continuous batching

TensorRT LLM Example

1. Convert ONNX model to TensorRT engine

trtexec --onnx=model.onnx --fp16 --saveEngine=model.engine

2. Python inference example

import tensorrt as trt

logger = trt.Logger(trt.Logger.WARNING)
runtime = trt.Runtime(logger)

with open("model.engine", "rb") as f:
    engine = runtime.deserialize_cuda_engine(f.read())

context = engine.create_execution_context()

Related Posts

  • NVIDIA NIM: Optimized Inference Microservices — NIM profiles are pre-compiled TRT-LLM engines; this post explains what's inside those profiles
  • NVIDIA Triton Inference Server — Triton loads TensorRT engines as its TensorRT-LLM backend for LLM serving
  • ONNX: Portable AI Models — ONNX is the standard interchange format used to import models into TensorRT for compilation
  • Optimizing AI Inference at Scale — where TRT-LLM optimizations (FP8, kernel fusion, in-flight batching) fit in the full inference optimization stack
  • Training a Neural Network — the trained neural network produced by backpropagation + gradient descent is the input to TensorRT; TRT optimizes the inference side of this training pipeline
Hitesh Sahu
Written by Hitesh Sahu, a passionate developer and blogger.

Tue May 19 2026

Share This on

← Previous

RAPIDS and GPU Accelerated Data Science: cuDF, cuML, CUDA, NCCL and Distributed AI Pipelines

Next →

NCCL and Distributed GPU Communication: CUDA, AllReduce, Multi-GPU and AI Cluster Networking

AI-Infrastructure/2-2-TensorRT
Let's work together
hiteshkrsahu@gmail.com
Munich 🥨, Germany 🇩🇪, EU
Playstore
Hitesh Sahu's apps on Google Play Store
Need Help?
Let's Connect
Navigation
  Home/About
  Skills
  Work/Projects
  Lab/Experiments
  Contribution
  Awards
  Art/Sketches
  Thoughts
  Contact
Links
  Sitemap
  Legal Notice
  Privacy Policy

Made with

NextJS logo

NextJS by

hitesh Sahu

| © 2026 All rights reserved.