# TensorRT

**NVIDIA TensorRT** 是 NVIDIA 推出的一款软件开发工具包，它接收已训练好的深度学习模型，将其转换为针对特定 GPU 优化的快速程序，用于执行推理，即在训练完成后对新输入进行计算并给出预测。<sup>[1](https://docs.nvidia.com/deeplearning/tensorrt/latest/architecture/how-trt-works.html)</sup> 它的工作分为两个阶段：构建阶段，由构建器（builder）编译网络，并针对目标 GPU 上的每一层选择最快的可用内核（kernel，一种底层 GPU 函数），产出一个序列化二进制文件，称为引擎（engine）或计划文件（plan file）；以及运行时阶段，由运行时（runtime）将引擎加载到应用程序中并在 GPU 上执行。<sup>[1](https://docs.nvidia.com/deeplearning/tensorrt/latest/architecture/how-trt-works.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 用于在 NVIDIA GPU 上高性能运行深度学习推理的 SDK，包含产出序列化引擎的构建阶段和执行引擎的运行时阶段<sup>[1](https://docs.nvidia.com/deeplearning/tensorrt/latest/architecture/how-trt-works.html)</sup> |
| 硬件支持 | 计算能力 SM 7.5 及以上的 NVIDIA GPU<sup>[2](https://docs.nvidia.com/deeplearning/tensorrt/10.x.x/getting-started/support-matrix.html)</sup> |
| 精度支持 | FP32、FP16 和 INT8 执行；INT8 通过显式 Q/DQ 量化或（已弃用的）训练后校准实现<sup>[3](https://docs.nvidia.com/deeplearning/tensorrt/10.x.x/inference-library/work-quantized-types.html)</sup><sup> • </sup><sup>[4](https://docs.nvidia.com/deeplearning/frameworks/tf-trt-user-guide/index.html)</sup> |
| 引擎可移植性 | 序列化引擎不能跨 Linux 和 Windows 等平台移植；硬件兼容模式允许引擎在多种 GPU 架构上运行<sup>[2](https://docs.nvidia.com/deeplearning/tensorrt/10.x.x/getting-started/support-matrix.html)</sup> |
| ONNX 支持 | TensorRT 11.x 解析器支持 ONNX 算子集 9 至 25，ONNX 包版本为 1.20.0<sup>[5](https://docs.nvidia.com/deeplearning/tensorrt/latest/reference/onnx-opset-guide.html)</sup> |
| 当前版本 | TensorRT 11.4.0，适用于 x86 Linux 和 Windows、ARM SBSA Linux 以及 Windows on ARM<sup>[6](https://docs.nvidia.com/deeplearning/tensorrt/latest/getting-started/release-notes.html)</sup> |
| LLM 变体 | TensorRT-LLM 用于数据中心 LLM 推理；TensorRT Edge-LLM 用于汽车和机器人平台<sup>[7](https://docs.nvidia.com/tensorrt-llm/index.html?ncid=no-ncid)</sup><sup> • </sup><sup>[8](https://developer.nvidia.com/blog/accelerating-llm-and-vlm-inference-for-automotive-and-robotics-with-nvidia-tensorrt-edge-llm/)</sup> |

## 构建阶段与运行时阶段的工作方式

TensorRT API 基于类，采用工厂模式，即某些对象负责创建并拥有其他对象。`Builder` 创建 `NetworkDefinition`、`BuilderConfig` 和解析器对象，这些对象必须在构建器之前销毁；序列化引擎的生命周期可以长于构建器，而 `ExecutionContext` 由引擎创建，用于运行推理。<sup>[1](https://docs.nvidia.com/deeplearning/tensorrt/latest/architecture/how-trt-works.html)</sup> 输入尺寸（包括批大小）以及输入配置文件的优化点等配置选项可能导致不同的内核选择，而不同的内核实现使用不同顺序的浮点运算，因此重建之间的输出可能在数值上存在差异；NVIDIA 建议在每次重建后以适当的容差验证模型精度。<sup>[1](https://docs.nvidia.com/deeplearning/tensorrt/latest/architecture/how-trt-works.html)</sup>

应用程序可以显式管理内存：执行上下文可以通过应用程序提供的内存创建，使用 `ICudaEngine::createExecutionContext()` 配合 `IExecutionContext::setDeviceMemoryV2`，从而允许在非并发上下文之间共享单个暂存缓冲区，或从设有硬性上限的预保留 CUDA 内存池中分配。<sup>[1](https://docs.nvidia.com/deeplearning/tensorrt/latest/architecture/how-trt-works.html)</sup>

## 量化与精度控制

TensorRT 通过在优化过程中将张量和权重转换为 FP16 和 INT8 等较低精度来降低推理成本。<sup>[4](https://docs.nvidia.com/deeplearning/frameworks/tf-trt-user-guide/index.html)</sup> 现代做法是显式量化：模型作者在计算图中插入 `IQuantizeLayer` 和 `IDequantizeLayer` 节点，称为 Q/DQ 层。`IQuantizeLayer` 将高精度浮点张量转换为量化张量，`IDequantizeLayer` 将其转换回来。TensorRT 期望可量化层的每个输入上都有一对 Q/DQ，然后将其融合为量化操作。当 TensorRT 在 Q/DQ 模式下优化网络时，它会将自身限制在不改变网络算术正确性的优化范围内。<sup>[3](https://docs.nvidia.com/deeplearning/tensorrt/10.x.x/inference-library/work-quantized-types.html)</sup> 量化可以在训练期间应用，称为量化感知训练（QAT），也可以在后续处理步骤中应用，称为训练后量化（PTQ），使用 PyTorch、TensorFlow 和 [ONNX Runtime](https://www.edgepedia.org/zh/onnx-runtime) 等框架。<sup>[9](https://docs.nvidia.com/deeplearning/tensorrt-rtx/latest/inference-library/work-with-quantized-types.html)</sup>

**训练后校准。** 较旧的路径现已弃用，即 INT8 校准。TensorRT 通过在代表性输入数据上运行网络并收集每个激活张量的统计数据，为每个张量计算一个缩放值；实验表明，约 500 张图像足以校准 ImageNet 分类网络。<sup>[3](https://docs.nvidia.com/deeplearning/tensorrt/10.x.x/inference-library/work-quantized-types.html)</sup> 选择缩放值需要在离散化误差与截断误差之间取得平衡：离散化误差随着每个量化值表示更大范围而增大，截断误差则是数值被钳制到可表示极限时产生的误差。TensorRT 提供多种校准器：`IInt8EntropyCalibrator2` 是当前推荐的熵校准器，它优化量化张量的信息量，抑制离群值，是深度学习加速器（DLA）所必需的，推荐用于基于 CNN 的网络；以及 `IInt8MinMaxCalibrator`，它使用激活分布的完整范围，对 NLP 任务效果更好。<sup>[3](https://docs.nvidia.com/deeplearning/tensorrt/10.x.x/inference-library/work-quantized-types.html)</sup> 在同一设备上以相同输入按相同顺序进行校准是确定性的，生成的校准表可以缓存并重用；当校准发生在层融合之前时，缓存可跨设备移植，例如使用这两种校准器或 `kCALIBRATE_BEFORE_FUSION` 标志时。<sup>[3](https://docs.nvidia.com/deeplearning/tensorrt/10.x.x/inference-library/work-quantized-types.html)</sup> 对于权重，TensorRT 使用对称量化，缩放值由最大绝对值计算得出，卷积、反卷积和全连接权重按通道计算。<sup>[3](https://docs.nvidia.com/deeplearning/tensorrt/10.x.x/inference-library/work-quantized-types.html)</sup>

NVIDIA TensorRT Model Optimizer 是一个开源工具，旨在帮助 TensorRT 和 TensorRT-RTX 用户向预训练模型添加量化；对于大多数模型，这是推荐的方法，因为手动决定哪些层必须保持高精度并校准缩放值颇具挑战。<sup>[9](https://docs.nvidia.com/deeplearning/tensorrt-rtx/latest/inference-library/work-with-quantized-types.html)</sup>

## 模型导入与算子覆盖

通常的部署路径是将模型从训练框架导出为 ONNX，并使用 TensorRT ONNX 解析器导入，通过 `trtexec --onnx=...` 命令或解析器 API 进行验证。<sup>[5](https://docs.nvidia.com/deeplearning/tensorrt/latest/reference/onnx-opset-guide.html)</sup> 对于 TensorRT 11.x，解析器向后兼容至算子集 9，最高支持算子集 25。<sup>[5](https://docs.nvidia.com/deeplearning/tensorrt/latest/reference/onnx-opset-guide.html)</sup> TensorRT 支持 ONNX 数据类型，包括 DOUBLE、FLOAT32、FLOAT16、BFLOAT16、FP8、FP4、INT32、INT64、INT8、INT4、UINT8 和 BOOL；INT8、INT4、FP8 和 FP4 被视为量化类型，只能通过从更高精度浮点类型量化获得，DOUBLE 支持仅限于向下转换为 FLOAT 并钳制到 ±FLT_MAX。<sup>[10](https://github.com/onnx/onnx-tensorrt/blob/main/docs/operators.md)</sup> 内置算子不支持的网络可以通过自定义插件进行扩展。<sup>[11](https://docs.nvidia.com/deeplearning/tensorrt/latest/api/operators.html)</sup>

## 版本与产品家族

NVIDIA 于 2017 年 9 月 25 日发布 TensorRT 3，将其描述为高性能优化编译器和运行时引擎，用于将 AI 应用生产部署到超大规模数据中心、嵌入式和汽车 GPU 平台；截至该日期，已有超过 1,200 家公司开始使用 NVIDIA 的推理平台。<sup>[12](https://nvidianews.nvidia.com/news/nvidia-tensorrt-3-dramatically-accelerates-ai-inference-for-hyperscale-data-centers)</sup> 随后推出的 TensorRT 8 增加了量化感知训练支持，据 NVIDIA 公告称，训练好的模型可以在不损失精度的情况下运行 INT8 推理。<sup>[13](https://nvidianews.nvidia.com/_gallery/download_pdf/60f6ca9bed6ae50cb3fa7469/)</sup>

当前产品线是 TensorRT 11：版本 11.0.0 将强类型网络设为默认，移除了弱类型，重组了包文件名，并移除了静态库，11.4.0 是最新版本。<sup>[14](https://docs.nvidia.com/deeplearning/tensorrt/11.4.0/getting-started/release-notes-11/11.0.0.html)</sup><sup> • </sup><sup>[6](https://docs.nvidia.com/deeplearning/tensorrt/latest/getting-started/release-notes.html)</sup> 围绕核心 SDK，NVIDIA 还推出了几款专用产品。**TensorRT-LLM** 提供 Python API，用于定义大语言模型并构建 TensorRT 引擎，以及用于执行这些引擎的 Python 和 C++ 运行时。<sup>[7](https://docs.nvidia.com/tensorrt-llm/index.html?ncid=no-ncid)</sup> **TensorRT-RTX** 面向 Windows 和 RTX 渲染相关的工作负载，支持 INT4、INT8、FP4 和 FP8 低精度类型，不支持隐式量化，并将优化分为硬件无关的提前阶段和硬件相关的即时阶段。<sup>[9](https://docs.nvidia.com/deeplearning/tensorrt-rtx/latest/inference-library/work-with-quantized-types.html)</sup><sup> • </sup><sup>[15](https://developer.nvidia.com/tensorrt-rtx)</sup> **TensorRT Edge-LLM** 是一个开源 C++ 框架，用于在 NVIDIA DRIVE AGX Thor 和 [Jetson Thor](https://www.edgepedia.org/zh/jetson-thor) 嵌入式平台上进行 LLM 和视觉语言模型推理，随 JetPack 7.1 发布，具有 EAGLE-3 推测解码、NVFP4 量化支持和分块预填充等特性。<sup>[8](https://developer.nvidia.com/blog/accelerating-llm-and-vlm-inference-for-automotive-and-robotics-with-nvidia-tensorrt-edge-llm/)</sup>

## 比较研究中的性能表现

独立基准测试准确界定了 TensorRT 的优势所在。一项针对纯 PyTorch、ONNX Runtime、OpenVINO 和 TensorRT 的工业机器视觉推理比较研究发现，OpenVINO 在 CPU 上实现了最低推理时间，而 TensorRT 在 GPU 上实现了最低推理时间；然而，对于该研究中考虑的基于 Transformer 的模型，TensorRT 并未超越纯 PyTorch。<sup>[16](https://arxiv.org/abs/2607.11356)</sup> 在消费级硬件上的 LLM 推理中，一项关于 Blackwell 架构的研究报告称，[TensorRT-LLM](https://www.edgepedia.org/zh/tensorrt-llm) 堆栈中的 NVFP4 量化格式相比优化的 BF16 基线实现了 1.6 倍的吞吐量优势，即 151 tokens/s 对 92 tokens/s，但需要运行时约束，以启动延迟换取生成速度。<sup>[17](https://arxiv.org/abs/2605.00519)</sup>

## References

1. [How TensorRT Works, NVIDIA TensorRT Documentation](https://docs.nvidia.com/deeplearning/tensorrt/latest/architecture/how-trt-works.html)
2. [Support Matrix, NVIDIA TensorRT 10.x Documentation](https://docs.nvidia.com/deeplearning/tensorrt/10.x.x/getting-started/support-matrix.html)
3. [Working with Quantized Types, NVIDIA TensorRT 10.x Documentation](https://docs.nvidia.com/deeplearning/tensorrt/10.x.x/inference-library/work-quantized-types.html)
4. [Accelerating Inference in TensorFlow with TensorRT User Guide, NVIDIA Docs](https://docs.nvidia.com/deeplearning/frameworks/tf-trt-user-guide/index.html)
5. [ONNX Opset Guide, NVIDIA TensorRT Documentation](https://docs.nvidia.com/deeplearning/tensorrt/latest/reference/onnx-opset-guide.html)
6. [Release Notes, NVIDIA TensorRT Documentation](https://docs.nvidia.com/deeplearning/tensorrt/latest/getting-started/release-notes.html)
7. [NVIDIA TensorRT-LLM Documentation](https://docs.nvidia.com/tensorrt-llm/index.html?ncid=no-ncid)
8. [Accelerating LLM and VLM Inference for Automotive and Robotics with NVIDIA TensorRT Edge-LLM, NVIDIA Technical Blog](https://developer.nvidia.com/blog/accelerating-llm-and-vlm-inference-for-automotive-and-robotics-with-nvidia-tensorrt-edge-llm/)
9. [Work With Quantized Types, NVIDIA TensorRT for RTX Documentation](https://docs.nvidia.com/deeplearning/tensorrt-rtx/latest/inference-library/work-with-quantized-types.html)
10. [Supported ONNX Operators, onnx-tensorrt GitHub](https://github.com/onnx/onnx-tensorrt/blob/main/docs/operators.md)
11. [TensorRT Operators, NVIDIA TensorRT Documentation](https://docs.nvidia.com/deeplearning/tensorrt/latest/api/operators.html)
12. [NVIDIA TensorRT 3 Dramatically Accelerates AI Inference for Hyperscale Data Centers, NVIDIA Newsroom (September 25, 2017)](https://nvidianews.nvidia.com/news/nvidia-tensorrt-3-dramatically-accelerates-ai-inference-for-hyperscale-data-centers)
13. [NVIDIA Inference Breakthrough Makes Conversational AI Smarter, More Interactive From Cloud to Edge, NVIDIA Newsroom](https://nvidianews.nvidia.com/_gallery/download_pdf/60f6ca9bed6ae50cb3fa7469/)
14. [TensorRT 11.0.0 Release Notes, NVIDIA TensorRT Documentation](https://docs.nvidia.com/deeplearning/tensorrt/11.4.0/getting-started/release-notes-11/11.0.0.html)
15. [TensorRT-RTX Download, NVIDIA Developer](https://developer.nvidia.com/tensorrt-rtx)
16. [Benchmarking Edge Inference Strategies for Deep Learning Models in Industrial Machine Vision, arXiv:2607.11356](https://arxiv.org/abs/2607.11356)
17. [Silicon Showdown: Performance, Efficiency, and Ecosystem Barriers in Consumer-Grade LLM Inference, arXiv:2605.00519](https://arxiv.org/abs/2605.00519)

---
*Initially written Oct 15, 2026 · Edited: Oct 25, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Neural networks and deep learning › Deep learning software and hardware › Inference acceleration and optimization

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/tensorrt

English original: https://www.edgepedia.org/tensorrt
