跳到正文
实时
PyTorch Blog· the Torch-TensorRT team at NVIDIA and the PyTorch team at Meta·· 9 天前精选72

ExecuTorch-TensorRT 发布:PyTorch 机器人策略以 TensorRT 速度运行

ExecuTorch-TensorRT: PyTorch Robot Policies at NVIDIA TensorRT Speed

要点速览

Torch-TensorRT 2.15 与 ExecuTorch 1.6 支持将 PyTorch 模型编译成 TensorRT 引擎并保存为 ExecuTorch 程序,运行时无需加载 torch,TensorRT 无法转换的部分交给 CUDA 后端在同一 CUDA 流上执行。

Why it matters

官方给出 Orin Nano 与 Thor 上四种机器人策略的逐项时延和内存对比,可据此判断边缘部署的取舍。

正文 · AI 翻译

简而言之

  • 使用 Torch-TensorRT 2.15 和 ExecuTorch 1.6,您可以编译 PyTorch 模型并将其作为 ExecuTorch 程序保存,然后从 C++ 或 Python 中运行它,而无需加载 torch。您始终保持在 PyTorch 中,并且无论 TensorRT 无法处理的任何内容仍然可以在 GPU 上运行。
  • 在 NVIDIA Jetson Orin Nano 上,它的速度和内存都与我们测量的最快 TensorRT 设置相差不到 4%。一个 36 亿参数的策略可以适应 8 GB 的板卡,而 PyTorch 激活模式会耗尽内存。
  • 您使用一个 pip install "torch_tensorrt[executorch]" 进行编译。要运行,机器人只需要 ExecuTorch 和一个小型 TensorRT 后端,不需要 PyTorch。

我们在 2026 年 PyTorch 大会上讨论了这项工作:使用 ExecuTorch 在多个目标上部署机器人策略。

GR00T 1.7 (3B) 在 Jetson Orin Nano 8GB 上:每个动作块大约 200 毫秒。

为什么在机器人上运行 PyTorch 模型与 TensorRT 很困难?

机器人在其自己的计算机上运行模型,例如 NVIDIA Jetson AGX Thor 或自定义板卡,并且它们必须在规定时间内完成。策略,即决定机器人下一步做什么的模型,每次返回一个简短的计划,称为动作块,例如 50 个动作。如果下一个块在机器人完成当前块时未准备好,机器人将停止并等待。并且所有内容共享同一内存:CPU、GPU、摄像头和每个模型。将任务卸载到服务器会增加网络往返时间,这可能会轻松地花费 100 毫秒,而机器人无法始终依赖该链接。

在 NVIDIA 硬件上,运行模型的最快方式是 TensorRT,这是 NVIDIA 的推理编译器。到目前为止,将 PyTorch 模型转换为 TensorRT 有四种方法:

  • 通过 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 的内存,而推理从未使用。

ONNX 路径和 ExecuTorch 路径。

今天我们添加了一种新方法:Torch-TensorRT 2.15 使用 TensorRT 编译模型并将其保存为 ExecuTorch 1.6 程序,该程序无需 libtorch 即可运行。它跳过 ONNX 并使用 PyTorch 自身的 torch.export 捕获模型。如果 TensorRT 无法转换模型的部分(包括预处理部分),您可以将该部分发送到 ExecuTorch 的 CUDA 后端,该后端在相同的程序和相同的 CUDA 流中在 GPU 上运行它,而不是失败构建。TensorRT 部分是普通的 TensorRT 引擎,因此模型的运行速度与您直接调用 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。

取一个需要两个后端的玩具模型:

class Toy(torch.nn.Module):
    def forward(self, x):
        return torch.cos(torch.erfinv(torch.tanh(x)))

跨越两个后端的玩具模型。

  1. 捕获和分割。 torch.export 将模型转换为 PyTorch 操作的图。TensorRT 声称它可以转换每一部分。它没有 erfinv 的转换器,因此 ExecuTorch 的 CUDA 后端 接管了该部分。编译器会告诉您哪些操作没有进入 TensorRT,以及原因。
  2. 编译和打包。 TensorRT 为其声称的每一部分构建引擎,CUDA 后端使用 AOTInductor 编译其部分。引擎进入 .pte 文件,CUDA 后端的权重进入其旁边的 .ptd 数据文件。
  3. 加载和运行。 在机器人上,ExecuTorch 运行时将每一部分交给其后端。输入、输出以及两个后端之间传递的张量可以保留在 GPU 上,无需通过 CPU 进行往返。如果您设置 use_cuda_graphs=True,每个引擎的 GPU 工作将记录一次作为 CUDA 图,并使用单次启动重放,这使得每次调用都更便宜。

如果您的模型有一个 TensorRT 和 CUDA 后端都无法编译的自定义操作,您仍然不需要 C++ 中的 TensorRT 插件。将操作作为 Triton 内核编写并使用 torch.library.triton_op 进行注册。CUDA 后端将其像模型的其他部分一样编译到程序中,机器人无需安装 Triton 或 PyTorch 即可运行它。ExecuTorch 自己的用于注意力、专家混合层和 4 位 matmuls 的 CUDA 内核就是以这种方式编写的。

入门指南

安装

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.

Export, in 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.

Run, in Python, without torch

In ExecuTorch 1.6 the Python runtime no longer needs the torch library. NumPy arrays in, NumPy arrays out:

import numpy as np
import torch_tensorrt_executorch_runtime  # registers the TensorRT backend
from executorch.runtime import Runtime

program = Runtime.get().load_program("policy.pte", data_path="aoti_cuda_blob.ptd")
policy = program.load_method("forward")
actions = np.asarray(policy.execute([np.zeros((1, 3, 224, 224), np.float32)])[0])

This matters on a small board. On the Orin Nano, just importing PyTorch takes about 2.2 s and 550 MB. For a robot that only runs models, this installs just the runtime, the TensorRT backend, and the TensorRT and CUDA libraries they need, about 4.6 GB instead of 11 GB for the full install:

pip install --no-deps executorch==1.6.0 torch-tensorrt-executorch-runtime==0.2.1 \
  nvidia-cuda-runtime==13.2.86 tensorrt-cu13-libs==11.4.0.106 numpy

Run, in C++

#include <executorch/extension/module/module.h>
#include <executorch/extension/tensor/tensor.h>

using namespace executorch::aten;
using namespace executorch::extension;

Module policy("policy.pte", "aoti_cuda_blob.ptd");
std::vector<float> pixels(1 * 3 * 224 * 224);  // filled from the camera
auto observation = make_tensor_ptr({1, 3, 224, 224}, pixels);
auto actions = policy.forward(observation);

To build the app, link the CMake packages that come with the pip install. The Torch-TensorRT ExecuTorch guide has a complete CMakeLists.txt.

If the input is already on the GPU, a camera frame for example, you can skip the copies to and from the CPU. Export with device inputs and outputs (see the CUDA backend docs) and pass the GPU pointer directly:

// A camera pipeline already wrote the image to GPU memory.
float* gpu_pixels = camera.gpu_frame();

// The policy writes its actions to GPU memory that you own.
float* gpu_actions = nullptr;
cudaMalloc(&gpu_actions, 50 * 6 * sizeof(float));  // 50 actions, 6 joints each
auto actions = from_blob(gpu_actions, {1, 50, 6}, ScalarType::Float, DeviceType::CUDA);
policy.set_output(*actions);

policy.forward(from_blob(gpu_pixels, {1, 3, 224, 224}, ScalarType::Float, DeviceType::CUDA));

Performance

We took four robot policies from LeRobot, Hugging Face’s robot-learning library: ACT, SmolVLA, pi0.5 and GR00T 1.7. Each one ran five ways on a Jetson Orin Nano Super (8 GB, about $250), with the same trained weights, the same inputs, and nothing quantized.

Time per action chunk on the Orin Nano, milliseconds, lower is better:

Policy ExecuTorch-TensorRT ONNX-TensorRT Torch-TensorRT torch.compile PyTorch eager
ACT (52M) 26 25 26 42 48
SmolVLA (450M) 120 121 120 159 765
pi0.5 (3.6B) 467 452 out of memory out of memory out of memory
GR00T 1.7 (3B, fine-tuned) 215 208 215 out of memory out of memory

Memory on the Orin Nano: what the policy’s process holds while it runs, CPU and GPU together, MiB, lower is better:

Policy ExecuTorch-TensorRT ONNX-TensorRT Torch-TensorRT torch.compile PyTorch eager
ACT 407 415 1083 1642 1435
SmolVLA 1146 1178 2615 4125 3095
pi0.5 5538 5429 out of memory out of memory out of memory
GR00T 1.7 5301 5272 6464 out of memory out of memory

Time: median of three runs of 100 chunks, after 20 warm-up chunks. Memory: median of three runs, over the last 30 s of 120 s; files the process only reads, such as the program files, are not counted, and a range means the runs differed by more than 5%. The TensorRT columns and torch.compile replay CUDA graphs. Maximum power, GPU clock locked. Bold is best in the row. Differences of a few milliseconds are within run-to-run noise. Measured with the released Torch-TensorRT 2.15.0 and ExecuTorch 1.6.0. Benchmark method and full tables.

A few things stand out:

  • 在 Orin Nano 上,ExecuTorch-TensorRT 在每个策略上都在 ONNX-TensorRT 的 4% 以内,并且比 PyTorch 懒加载模式快 6.4 倍。
  • 内存与 ONNX-TensorRT 大致相同,在两个较小的策略上更低。Torch-TensorRT 列表在 PyTorch 内运行相同的 TensorRT 引擎。这增加了 0.7 到 1.4 GB 的开销,pi0.5 完全不适用。
  • pi0.5,具有 36 亿个参数,在 8 GB Orin Nano 上处理 50 个动作的块大约需要半秒,而 PyTorch 懒加载模式会耗尽内存。

在 NVIDIA Jetson AGX Thor 开发板上,高端机器人主板资源限制不是问题,但内存效率仍然允许更多工作负载在设备上运行。同样,编译路径最终具有相似的性能。

Thor 上每个动作块的时间,毫秒,越低越好:

策略 ExecuTorch-TensorRT ONNX-TensorRT Torch-TensorRT torch.compile PyTorch 懒加载
ACT 3.9 3.4 3.5 6.3 9.9
SmolVLA 29.6 29.5 29.5 39.9 165.1
pi0.5 80.5 86.1 78.2 90.6 218.9
GR00T 1.7 59.7 58.4 59.5 65.3 94.3

Thor 上的内存,MiB,越低越好:

策略 ExecuTorch-TensorRT ONNX-TensorRT Torch-TensorRT torch.compile PyTorch 懒加载
ACT 443 440 1000 2066 1793
SmolVLA 1185 1205 2701 4479 到 5203 3344 到 3568
pi0.5 5617 5495 7326 7905 7421
GR00T 1.7 5314 5261 6525 7520 7042

与 Orin Nano 上的方法相同。

它不仅限于机器人策略。我们在 A100 上运行了一个具有 260 亿个参数的 Gemma 4 语言模型,权重为 4 位。TensorRT 处理了其中的一部分,CUDA 后端处理了其余部分,输出标记与单独使用 CUDA 后端完全相同。

在小板上运行大型策略

为了获得最佳性能,TensorRT 引擎是为一种类型的 GPU 构建的,因此你需要在运行它的相同类型的板上构建它。构建需要整个模型在内存中,并且在 8 GB Orin Nano 上没有足够的空间一次性构建一个 36 亿个参数的策略。因此我们将 pi0.5 和 GR00T 分割成一系列较小的程序。每个程序都是在其自己的进程中从其自己的权重构建的,因此完整的检查点永远不会同时出现在内存中。这两个策略都通过重复一个“去噪”步骤来执行动作,因此该步骤是一个程序,对于 pi0.5 运行十次,对于 GR00T 运行四次。程序在 GPU 上相互传递张量。

在运行时,TensorRT 后端通过 ExecuTorch 的共享 GPU 内存分配器加载其引擎,因此没有额外的副本。没有它,pi0.5 的第二个引擎在 Orin Nano 上根本无法加载。引擎也可以大于 2 GB,并且可以与 .pte 旁边的 .ptd 文件一起存在,就像 ExecuTorch 中的其他大型数据一样。在独立 GPU 上,权重流 也运行那些不适合 GPU 内存中的引擎。

一个 GPU 上运行多个策略

机器人很少只运行一个模型。一个大型策略计划下一步动作,一个感知模型查看每个摄像头帧,一个小型控制器必须以固定的、高速率运行。它们都共享一个 GPU 的内存和计算。

内存是较容易的部分。如果一个进程加载同一个程序两次,比如每个臂一个策略,Torch-TensorRT 2.15 仅在 GPU 内存中保留其引擎一次。在 Orin Nano 上,第二次加载只用了 76 MiB 而不是 805 MiB。还有一个选项允许许多引擎共享每个 GPU 的一个工作缓冲区。

计算更难。一个大型模型可以填满每个 流式多处理器 (SM),而其他所有内容都必须等待。

一个 CUDA 绿色上下文 为模型提供一组固定的 SM。ExecuTorch 程序中的每个 GPU 后端都在你选择的 CUDA 流 上运行,因此你为每个绿色上下文创建一个流,并在其自己的线程上运行每个策略。它必须是每个策略一个线程,因为 forward() 等待其 GPU 工作完成:

#include <executorch/extension/cuda/caller_stream.h>

using namespace executorch::extension::cuda;

// One thread per policy. Each thread has its own green context and stream.
void run_policy(Module& policy, cudaStream_t stream) {
  CallerStreamGuard guard(stream);  // this thread's GPU work runs on this policy's SMs
  while (running) {
    policy.forward(next_input(policy));
  }
}

辅助工具在 executorch::extension::cuda 中,并与 TensorRT 和 CUDA 后端一起工作。一些限制:

  • 内存带宽和其他 GPU 资源仍然共享,所以延迟会变短但不会消失。
  • Jetsons 的 SM 很少(Orin Nano 上有 8 个,AGX Thor 上有 20 个),所以你只能以几种方式分割它们。
  • Torch-TensorRT 为整个 GPU 构建每个引擎。在绿色上下文中 TensorRT 会发出警告,并且使用需要比上下文更多的 SM 的内核的引擎可能会失败,所以给每个绿色上下文足够的 SM 以供其引擎使用。

接下来是什么

  • 为大型策略进行端到端编译。NVIDIA 正在努力在不手动分割的情况下编译大型模型,所以 Jetson Orin Nano 可以将 pi0.5 和 GR00T 作为单个程序构建,具有更低的延迟和更简单的流程。
  • LeRobot。我们正在与 Hugging Face LeRobot 团队合作,为 LeRobot 策略开发 ExecuTorch 导出,所以导出的策略比在 PyTorch 急性模式下或 torch.compile 中运行得更快,并且不需要 PyTorch 即可运行。你可以在 这个跟踪问题 中跟踪哪些策略导出并在每个 GPU 上运行,该跟踪问题使用比上述表格更新的构建版本。

了解更多

你想运行哪个模型,在哪个板上,以及今天它在哪出问题?在 Torch-TensorRT 或 ExecuTorch 上的问题中告诉我们。

文章 ExecuTorch-TensorRT: PyTorch 机器人策略在 NVIDIA TensorRT 速度下 首次出现在 PyTorch。

来源: PyTorch Blog · pytorch.org