PyTorch发布ExecuTorch-TensorRT方案
ExecuTorch-TensorRT: PyTorch Robot Policies at NVIDIA TensorRT Speed
机器人开发者必看,这套工具链解决了PyTorch模型在边缘端部署的内存痛点,无需ONNX中转即可享受TensorRT加速,赶紧在你的Jetson设备上试跑一下。
TL;DR
- With Torch-TensorRT 2.15 and ExecuTorch 1.6 you can compile a PyTorch model with TensorRT, save it as an ExecuTorch program, and run it from C++ or Python without loading torch. You stay in PyTorch the whole way, and whatever TensorRT can’t handle can still run on the GPU.
- On an NVIDIA Jetson Orin Nano it comes within 4% of the fastest TensorRT setup we measured, in both speed and memory. A 3.6-billion-parameter policy fits on the 8 GB board, where PyTorch eager mode runs out of memory.
- You compile with one pip install "torch_tensorrt[executorch]". To run, the robot needs only ExecuTorch and a small TensorRT backend, not PyTorch.
- 借助 Torch-TensorRT 2.15 和 ExecuTorch 1.6,你可以使用 TensorRT 编译 PyTorch 模型,将其保存为 ExecuTorch 程序,并在不加载 torch 的情况下从 C++ 或 Python 运行它。整个过程你始终留在 PyTorch 生态中,而任何 TensorRT 无法处理的部分仍可在 GPU 上运行。
- 在 NVIDIA Jetson Orin Nano 上,其速度和内存表现与我们测量的最快 TensorRT 配置相差不到 4%。一个拥有 36 亿参数的策略模型可以适配在 8 GB 的板卡上,而在该板卡上 PyTorch 的 eager 模式会因内存不足而无法运行。
- 你只需通过 pip install "torch_tensorrt[executorch]" 即可完成编译。运行时,机器人仅需 ExecuTorch 和一个小型 TensorRT 后端,无需 PyTorch。
We talked about this work at PyTorch Conference 2026: Deploying Robot Policies Across Many Targets with ExecuTorch.
我们在 PyTorch Conference 2026 上讨论了这项工作:《使用 ExecuTorch 将机器人策略部署到多种目标平台》。
https://pytorch.org/wp-content/uploads/2026/11/et-trt.mp4
GR00T 1.7 (3B) on a Jetson Orin Nano 8GB: about 200 ms per action chunk.
Jetson Orin Nano 8GB 上的 GR00T 1.7 (3B):每个动作块约需 200 毫秒。
Why is it hard to run a PyTorch model with TensorRT on a robot?
为什么在机器人上运行带有 TensorRT 的 PyTorch 模型很困难?
A robot runs its models on its own computer, like NVIDIA Jetson AGX Thor or a custom board, and they have to finish on time. A policy, the model that decides what the robot does next, returns a short plan at a time, called an action chunk, say 50 actions. If the next chunk isn’t ready when the robot finishes the current one, the robot stops and waits. And everything shares the same memory: the CPU, the GPU, the cameras and every model. Offloading to a server adds a network round trip, which can easily take 100 ms, over a link the robot can’t always count on.
机器人在其自身的计算机(如 NVIDIA Jetson AGX Thor 或定制主板)上运行模型,且必须按时完成。策略(即决定机器人下一步行为的模型)每次返回一个短计划,称为动作块(action chunk),例如 50 个动作。如果当前动作块执行完毕时下一个动作块尚未准备好,机器人就会停止并等待。所有组件共享同一内存:CPU、GPU、摄像头以及各个模型。卸载到服务器会增加网络往返时间,轻松达到 100 毫秒,而机器人所依赖的网络链路并不总是可靠。
On NVIDIA hardware the fastest way to run a model is TensorRT, NVIDIA’s inference compiler. Until now, getting a PyTorch model into TensorRT meant one of four things:
在 NVIDIA 硬件上,运行模型最快的方式是 TensorRT,即 NVIDIA 的推理编译器。在此之前,将 PyTorch 模型导入 TensorRT 意味着以下四种方式之一:
- Go through ONNX. We call this ONNX-TensorRT in the tables. Export the model to an ONNX file and build a TensorRT engine from it. It’s fast and well proven, but your model has to get through two converters you don’t own: PyTorch’s ONNX exporter and TensorRT’s ONNX parser. If either one doesn’t know an op, the build fails, and the usual way out is writing a TensorRT plugin in C++.
- Write it by hand. Rebuild the model layer by layer with TensorRT’s network definition API. tensorrtx does this for many popular models, and early TensorRT-LLM did it for language models. You get full control, but every new model is new work, and checking the result against PyTorch is on you.
- Use TensorRT Model Connect. NVIDIA’s TensorRT Model Connect, in public preview, builds TensorRT engines straight from supported Hugging Face checkpoints, with no ONNX step, and runs them from C++ without PyTorch. It covers the models on its supported list.
- Use Torch-TensorRT inside PyTorch. Torch-TensorRT converts PyTorch ops to TensorRT directly, no ONNX file involved. But what it normally produces, an ExportedProgram or an AOTInductor .pt2 package, needs libtorch to run. That’s more than 1 GB of libraries, and a process that loads it holds hundreds of MB that inference never uses.
- 通过 ONNX。我们在表格中称此为 ONNX-TensorRT。将模型导出为 ONNX 文件,并据此构建 TensorRT 引擎。这种方法速度快且经过充分验证,但你的模型必须经过两个你不拥有的转换器:PyTorch 的 ONNX 导出器和 TensorRT 的 ONNX 解析器。如果其中任何一个不认识某个算子,构建就会失败,通常的解决办法是用 C++ 编写 TensorRT 插件。
- 手动编写。使用 TensorRT 的网络定义 API 逐层重建模型。tensorrtx 为许多流行模型执行此操作,早期的 TensorRT-LLM 也为语言模型执行此操作。你可以获得完全的控制权,但每个新模型都需要新的工作,并且你需要自行检查结果与 PyTorch 的一致性。
- 使用 TensorRT Model Connect。NVIDIA 的 TensorRT Model Connect(目前处于公开预览阶段)直接从支持的 Hugging Face 检查点构建 TensorRT 引擎,无需经过 ONNX 步骤,并可在 C++ 中运行而无需 PyTorch。它涵盖了其支持列表中的模型。
- 在 PyTorch 内部使用 Torch-TensorRT。Torch-TensorRT 将 PyTorch 算子直接转换为 TensorRT,不涉及 ONNX 文件。但它通常生成的 ExportedProgram 或 AOTInductor .pt2 包需要 libtorch 才能运行。这需要超过 1 GB 的库文件,且加载它的进程会占用数百 MB 内存,而这些内存在推理过程中从未被使用。
The ONNX route and the ExecuTorch route.
ONNX 路线和 ExecuTorch 路线。
Today we’re adding a new way: Torch-TensorRT 2.15 compiles the model with TensorRT and saves it as an ExecuTorch 1.6 program that runs without libtorch. It skips ONNX and captures the model with PyTorch’s own torch.export. If TensorRT can’t convert part of the model, preprocessing included, you can send that part to ExecuTorch’s CUDA backend, which runs it on the GPU in the same program on the same CUDA stream instead of failing the build. The TensorRT parts are ordinary TensorRT engines, so the model runs about as fast as it does when you call TensorRT directly.
今天我们要介绍一种新方法:Torch-TensorRT 2.15 使用 TensorRT 编译模型,并将其保存为 ExecuTorch 1.6 程序,该程序无需 libtorch 即可运行。它跳过 ONNX,并使用 PyTorch 自带的 torch.export 捕获模型。如果 TensorRT 无法转换模型的某部分(包括预处理),你可以将该部分发送给 ExecuTorch 的 CUDA 后端,使其在同一程序的同一 CUDA 流上在 GPU 上运行,而不是导致构建失败。TensorRT 部分是普通的 TensorRT 引擎,因此模型的运行速度与你直接调用 TensorRT 时大致相同。
How it works
工作原理
ExecuTorch is PyTorch’s solution for running models on edge devices. You compile a model ahead of time into a program, and a small runtime runs it without libtorch, from C++ or Python. Hardware support comes from backends: besides TensorRT there are CUDA, Intel OpenVINO, Qualcomm QNN, Apple Core ML and MLX, Arm, Vulkan, and XNNPACK for CPUs. Torch-TensorRT could already save ExecuTorch programs with output_format="executorch". What’s new in Torch-TensorRT 2.15 and ExecuTorch 1.6 is that the workflow is production ready: one install, prebuilt C++ libraries, a Python runtime that doesn’t need PyTorch, and a few features for small boards. We call it ExecuTorch-TensorRT.
ExecuTorch 是 PyTorch 用于在边缘设备上运行模型的解决方案。你预先将模型编译为一个程序,然后由一个小型运行时在没有 libtorch 的情况下从 C++ 或 Python 运行它。硬件支持来自后端:除了 TensorRT,还有 CUDA、Intel OpenVINO、Qualcomm QNN、Apple Core ML 和 MLX、Arm、Vulkan 以及用于 CPU 的 XNNPACK。Torch-TensorRT 已经可以使用 output_format="executorch" 保存 ExecuTorch 程序。Torch-TensorRT 2.15 和 ExecuTorch 1.6 的新之处在于工作流已准备好投入生产环境:一次安装、预构建的 C++ 库、不需要 PyTorch 的 Python 运行时,以及一些面向小型开发板的功能。我们称之为 ExecuTorch-TensorRT。
Take a toy model that needs two backends:
以一个需要两个后端的玩具模型为例:
class Toy(torch.nn.Module):
def forward(self, x):
return torch.cos(torch.erfinv(torch.tanh(x)))The toy model, split across two backends.
跨两个后端划分的玩具模型。
- Capture and split. torch.export turns the model into a graph of PyTorch ops. TensorRT claims every part it can convert. It has no converter for erfinv, so the ExecuTorch CUDA backend takes that part instead. The compiler tells you which ops didn’t go to TensorRT, and why.
- Compile and pack. TensorRT builds an engine for each part it claimed, and the CUDA backend compiles its part with AOTInductor. The engines go into a .pte file, and the CUDA backend’s weights go into a .ptd data file next to it.
- Load and run. On the robot, the ExecuTorch runtime hands each part to its backend. Inputs, outputs and the tensors passed between the two backends can stay on the GPU, with no round trip through the CPU. If you set use_cuda_graphs=True, each engine’s GPU work is recorded once as a CUDA graph and replayed with a single launch, which makes every call cheaper.
- 捕获与划分。torch.export 将模型转换为 PyTorch 算子的计算图。TensorRT 声明它能转换的所有部分。由于 erfinv 没有转换器,因此该部分交由 ExecuTorch CUDA 后端处理。编译器会告诉你哪些算子未进入 TensorRT 及其原因。
- 编译与打包。TensorRT 为其声明的每个部分构建引擎,CUDA 后端使用 AOTInductor 编译其部分。这些引擎被放入 .pte 文件中,CUDA 后端的权重则被放入旁边的 .ptd 数据文件中。
- 加载并运行。在机器人上,ExecuTorch 运行时将每个部分交给其后端处理。输入、输出以及两个后端之间传递的张量可以保留在 GPU 上,无需往返 CPU。如果设置 use_cuda_graphs=True,则每个引擎的 GPU 工作会被记录一次为 CUDA 图,并通过单次启动进行重放,从而降低每次调用的开销。
If your model has a custom op that neither TensorRT nor the CUDA backend can compile, you still don’t need a TensorRT plugin in C++. Write the op as a Triton kernel and register it with torch.library.triton_op. The CUDA backend compiles it into the program like any other part of the model, and the robot runs it without Triton or PyTorch installed. ExecuTorch’s own CUDA kernels for attention, mixture-of-experts layers and 4-bit matmuls are written this way.
如果你的模型包含一个 TensorRT 或 CUDA 后端都无法编译的自定义算子,你仍然不需要编写 C++ 的 TensorRT 插件。将该算子写为 Triton 内核,并使用 torch.library.triton_op 进行注册。CUDA 后端会像编译模型其他部分一样将其编译到程序中,且机器人在未安装 Triton 或 PyTorch 的情况下也能运行它。ExecuTorch 自身用于注意力机制、混合专家(MoE)层和 4-bit 矩阵乘法的 CUDA 内核就是以这种方式编写的。
Getting started
入门指南
Install
安装
pip install "torch_tensorrt[executorch]"That gets you PyTorch, Torch-TensorRT, ExecuTorch and a small TensorRT backend for ExecuTorch. You need PyTorch only to compile. The compiled program runs without it; see Run, in Python, without torch for the smaller install for the robot. The wheels are built for CUDA 13.2 on Linux, x86-64 and Arm64, which on an NVIDIA Jetson means JetPack 7.2 or newer. On Arm64 the first install also pulls about 2.6 GB of TensorRT libraries from NVIDIA, so give it a few minutes. It can’t reuse the TensorRT that ships with NVIDIA JetPack, because Torch-TensorRT 2.15 needs TensorRT 11.4 and JetPack 7.2 comes with TensorRT 10.
这将为你安装 PyTorch、Torch-TensorRT、ExecuTorch 以及一个用于 ExecuTorch 的小型 TensorRT 后端。你仅在编译时需要 PyTorch;编译后的程序无需它即可运行;有关机器人的更小安装方案,请参阅“Run, in Python, without torch”。这些 wheel 包针对 Linux 上的 x86-64 和 Arm64 架构的 CUDA 13.2 构建,在 NVIDIA Jetson 上对应 JetPack 7.2 或更高版本。在 Arm64 上,首次安装还会从 NVIDIA 拉取约 2.6 GB 的 TensorRT 库,因此请预留几分钟时间。它无法复用随 NVIDIA JetPack 提供的 TensorRT,因为 Torch-TensorRT 2.15 需要 TensorRT 11.4,而 JetPack 7.2 自带的是 TensorRT 10。
Export, in Python
导出,在 Python 中
import torch
import torch_tensorrt
from executorch.backends.cuda.cuda_backend import CudaBackend
from executorch.backends.cuda.cuda_partitioner import CudaPartitioner
with torch.no_grad():
model = MyPolicy().eval().cuda()
example = (torch.randn(1, 3, 224, 224, device="cuda"),)
exported = torch.export.export(model, example)
trt_model = torch_tensorrt.dynamo.compile(
exported, arg_inputs=example, min_block_size=1, truncate_double=True
)
torch_tensorrt.save(
trt_model,
"policy.pte",
output_format="executorch",
# Everything TensorRT does not take goes to the CUDA backend.
partitioners=[CudaPartitioner([CudaBackend.generate_method_name_compile_spec("forward")])],
)The CUDA backend needs a CUDA toolkit (nvcc) at export time, and it writes aoti_cuda_blob.ptd next to the .pte. Give each export its own folder and ship both files. If your model has several parts, say a vision encoder and an action head, you can put them all in one program, one method per part, with torch_tensorrt.executorch.export(). It gives you back a program you can still edit before saving.
CUDA 后端在导出时需要 CUDA 工具包(nvcc),并在 .pte 文件旁边生成 aoti_cuda_blob.ptd。为每次导出分配独立的文件夹,并一起分发这两个文件。如果你的模型有多个部分,例如视觉编码器和动作头,你可以将它们全部放入一个程序中,每个部分对应一个方法,使用 torch_tensorrt.executorch.export()。它会返回一个你在保存之前仍可编辑的程序。
Run, in Python, without torch
运行,在 Python 中,无需 torch
In ExecuTorch 1.6 the Python runtime no longer needs the torch library. NumPy arrays in, NumPy arrays out:
在 ExecuTorch 1.6 中,Python 运行时不再需要 torch 库。输入 NumPy 数组,输出 NumPy 数组:
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力