onnx-tensorrt

onnxApache-2.0活跃维护

把 ONNX 模型直接转成 TensorRT 引擎的官方解析器,让训练好的模型能在 NVIDIA GPU 上做高性能推理部署。

ONNX-TensorRT: TensorRT backend for ONNX

release/11.4-GA发布于 2026-10-09 16:09 UTC3,246star551fork同步于 2026-11-10 17:43 UTC

怎么上手

由 README 提炼,执行前请确认
cd onnx-tensorrt && mkdir build && cd build && cmake .. -DTENSORRT_ROOT=<path_to_trt> && make -j
export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
python3 -m pip install onnx==1.21.0 && python3 setup.py install
硬件要求按 README 说明

需要 NVIDIA GPU 与 CUDA,构建默认在 /usr/local/cuda 查找 CUDA 工具包;依赖 TensorRT 11.4,README 未列出具体显卡型号与显存要求。

需要 CUDA
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

把 ONNX 模型解析并交给 TensorRT 执行的官方后端,让训练好的模型在 NVIDIA GPU 上跑推理

把 ONNX 模型直接转成 TensorRT 引擎的官方解析器,让训练好的模型能在 NVIDIA GPU 上做高性能推理部署。

核心能力

  • 解析 ONNX 模型并构建 TensorRT 引擎
  • 支持 full-dimensions 与动态 shape
  • 提供 C++ 解析库 libnvonnxparser.so
  • 提供 Python 绑定与 backend.prepare 接口
  • 可用 trtexec 或 polygraphy 快速验证模型

容易踩的坑

需先自行安装 TensorRT 11.4 及其开源库,并用 -DTENSORRT_ROOT 指定路径;CUDA 不在 /usr/local/cuda 时需加 -DCUDA_TOOLKIT_ROOT_DIR;Python 侧要求 onnx 1.21.0,版本不匹配可能出问题。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本release/11.4-GA
  1. release/11.4-GATensorRT 11.4 GA Parser Update最新正式版2026-10-09 · 2026-10-09 16:09 UTC

    For more details, see the 11.4 GA release notes.

  2. release/11.3-GATensorRT 11.3 GA Parser Update2026-09-14 · 2026-09-14 17:29 UTC
    查看更新说明

    For more details, see the 11.3 GA release notes.

    • Refactored how external weights are handled in IParserRefitter
  3. release/11.2-GATensorRT 11.2 GA Parser Update2026-08-03 · 2026-08-03 17:24 UTC
    查看更新说明

    For more details, see the 11.2 GA release notes.

    • Added IRefitterObserver class for better refitting of ONNX models.
    • Added support for the DFT operator and 5D GridSample operators.
  4. release/11.1-GATensorRT 11.1 GA Parser Update2026-06-22 · 2026-06-22 20:56 UTC
    查看更新说明

    For more details, see the 11.1 GA release notes.

    • Updated TRT_Attention to support raggedness
  5. release/11.0-GATensorRT 11.0 GA Parser Update2026-06-02 · 2026-06-02 18:00 UTC
    查看更新说明

    For more details, see the 11.0 GA release notes.

    • Added support for Swish operator
    • Added support for custom operators TRT_Attention, TRT_MoE, and TRT_KVCacheUpdate
    • Removed deprecated APIs - IParser::supportsModel and IParser::parseWithWeightDescriptors

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    TensorRT Backend For ONNX

    Parses ONNX models for execution with TensorRT.

    See also the TensorRT documentation.

    For the list of recent changes, see the changelog.

    For a list of commonly seen issues and questions, see the FAQ.

    For business inquiries, please contact [email protected]

    For press and other inquiries, please contact Hector Marinez at [email protected]

    Supported TensorRT Versions

    Development on the this branch is for the latest version of TensorRT 11.4 with full-dimensions and dynamic shape support.

    For previous versions of TensorRT, refer to their respective branches.

    Supported Operators

    Current supported ONNX operators are found in the operator support matrix.

    Installation

    Dependencies

    Building

    For building within Docker or on Windows, we recommend using the build instructions in the main TensorRT repository to build the onnx-tensorrt library.

    Once you have cloned the repository, you can build the parser libraries and executables by running:

    cd onnx-tensorrt
    mkdir build && cd build
    cmake .. -DTENSORRT_ROOT=<path_to_trt> && make -j
    # Ensure that you update your LD_LIBRARY_PATH to pick up the location of the newly built library:
    export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
    

    Note that this project has a dependency on CUDA. By default the build will look in /usr/local/cuda for the CUDA toolkit installation. If your CUDA path is different, overwrite the default path by providing -DCUDA_TOOLKIT_ROOT_DIR=<path_to_cuda_install> in the CMake command.

    To build with protobuf-lite support, add -DUSE_ONNX_LITE_PROTO=1 to the end of the cmake command.

    InstanceNormalizaiton Performance

    There are two implementations of InstanceNormalization that may perform differently depending on various parameters. By default, the parser will use the native TensorRT implementation of InstanceNorm. Users that want to benchmark using the plugin implementation of InstanceNorm can unset the parser flag kNATIVE_INSTANCENORM prior to parsing the model. Note that the plugin implementation cannot be used for building version compatible or hardware compatible engines, and attempting to do so will result in an error.

    C++ Example:

    // Unset the kNATIVE_INSTANCENORM flag to use the plugin implementation.
    parser->unsetFlag(nvonnxparser::OnnxParserFlag::kNATIVE_INSTANCENORM);
    

    Python Example:

    // Unset the NATIVE_INSTANCENORM flag to use the plugin implementation.
    parser.clear_flag(trt.OnnxParserFlag.NATIVE_INSTANCENORM)
    

    Executable Usage

    There are currently two officially supported tools for users to quickly check if an ONNX model can parse and build into a TensorRT engine from an ONNX file.

    For C++ users, there is the trtexec binary that is typically found in the <tensorrt_root_dir>/bin directory. The basic command of running an ONNX model is:

    trtexec --onnx=model.onnx

    Refer to the link or run trtexec -h for more information on CLI options.

    For Python users, there is the polygraphy tool. The basic command for running an onnx model is:

    polygraphy run model.onnx --trt

    Refer to the link or run polygraphy run -h for more information on CLI options.

    Python Modules

    Python bindings for the ONNX-TensorRT parser are packaged in the shipped .whl files.

    TensorRT 11.4 supports ONNX release 1.21.0. Install it with:

    python3 -m pip install onnx==1.21.0
    

    The ONNX-TensorRT backend can be installed by running:

    python3 setup.py install
    

    ONNX-TensorRT Python Backend Usage

    The TensorRT backend for ONNX can be used in Python as follows:

    import onnx
    import onnx_tensorrt.backend as backend
    import numpy as np
    
    model = onnx.load("/path/to/model.onnx")
    engine = backend.prepare(model, device='CUDA:1')
    input_data = np.random.random(size=(32, 3, 224, 224)).astype(np.float32)
    output_data = engine.run(input_data)[0]
    print(output_data)
    print(output_data.shape)
    

    C++ Library Usage

    The model parser library, libnvonnxparser.so, has its C++ API declared in this header:

    NvOnnxParser.h
    

    Tests

    After installation (or inside the Docker container), ONNX backend tests can be run as follows:

    Real model tests only:

    python onnx_backend_test.py OnnxBackendRealModelTest
    

    All tests:

    python onnx_backend_test.py
    

    You can use -v flag to make output more verbose.

    Pre-trained Models

    Pre-trained models in ONNX format can be found at the ONNX Model Zoo