• 文档 >
  • 使用 Cudagraphs 的 Torch Export
快捷方式

使用 Cudagraphs 进行 Torch 导出

CUDA Graphs 允许通过单个 CPU 操作启动多个 GPU 操作,从而减少启动开销并提高 GPU 利用率。Torch-TensorRT 提供了一个简单的接口来启用 CUDA graphs。此功能使用户可以轻松利用 CUDA graphs 的性能优势,而无需手动管理捕获和重放的复杂性。

../../../_images/cuda_graphs.png

本交互式脚本旨在概述在 ir=”dynamo” 路径中使用 Torch-TensorRT Cudagraphs 集成的过程。该功能在 torch.compile 路径中的工作方式也类似。

导入和模型定义

import torch
import torch_tensorrt
import torchvision.models as models

使用默认设置通过 torch_tensorrt.compile 进行编译

# We begin by defining and initializing a model
model = models.resnet18(pretrained=True).eval().to("cuda")

# Define sample inputs
inputs = torch.randn((16, 3, 224, 224)).cuda()
# Next, we compile the model using torch_tensorrt.compile
# We use the `ir="dynamo"` flag here, and `ir="torch_compile"` should
# work with cudagraphs as well.
opt = torch_tensorrt.compile(
    model,
    ir="dynamo",
    inputs=torch_tensorrt.Input(
        min_shape=(1, 3, 224, 224),
        opt_shape=(8, 3, 224, 224),
        max_shape=(16, 3, 224, 224),
        dtype=torch.float,
        name="x",
    ),
)

使用 Cudagraphs 集成进行推理

# We can enable the cudagraphs API with a context manager
with torch_tensorrt.runtime.enable_cudagraphs(opt) as cudagraphs_module:
    out_trt = cudagraphs_module(inputs)

# Alternatively, we can set the cudagraphs mode for the session
torch_tensorrt.runtime.set_cudagraphs_mode(True)
out_trt = opt(inputs)

# We can also turn off cudagraphs mode and perform inference as normal
torch_tensorrt.runtime.set_cudagraphs_mode(False)
out_trt = opt(inputs)
# If we provide new input shapes, cudagraphs will re-record the graph
inputs_2 = torch.randn((8, 3, 224, 224)).cuda()
inputs_3 = torch.randn((4, 3, 224, 224)).cuda()

with torch_tensorrt.runtime.enable_cudagraphs(opt) as cudagraphs_module:
    out_trt_2 = cudagraphs_module(inputs_2)
    out_trt_3 = cudagraphs_module(inputs_3)

在包含图中断的模块上使用 Cuda graphs

当 CUDA Graphs 应用于包含图中断的 TensorRT 模型时,每个中断都会引入额外的开销。这是因为图中断会阻止整个模型作为一个连续优化的单元执行。因此,CUDA Graphs 通常提供的一些性能优势(例如减少内核启动开销和提高执行效率)可能会减弱。

将封装的运行时模块与 CUDA Graphs 结合使用,即使存在图中断,您也可以将操作序列封装到图中,从而高效执行。如果 TensorRT 模块存在图中断,CUDA Graph 上下文管理器会返回一个 `wrapped_module`。该模块会捕获整个执行图,通过减少内核启动开销和提高性能,在后续推理中实现高效重放。

请注意,使用封装模块进行初始化涉及一个预热阶段,在此阶段模块会执行数次。此预热可确保内存分配和初始化不会被记录在 CUDA Graphs 中,这有助于维持一致的执行路径并优化性能。

../../../_images/cuda_graphs_breaks.png
class SampleModel(torch.nn.Module):
    def forward(self, x):
        return torch.relu((x + 2) * 0.5)


model = SampleModel().eval().cuda()
input = torch.randn((1, 3, 224, 224)).to("cuda")

# The 'torch_executed_ops' compiler option is used in this example to intentionally introduce graph breaks within the module.
# Note: The Dynamo backend is required for the CUDA Graph context manager to handle modules in an Ahead-Of-Time (AOT) manner.
opt_with_graph_break = torch_tensorrt.compile(
    model,
    ir="dynamo",
    inputs=[input],
    min_block_size=1,
    pass_through_build_failures=True,
    torch_executed_ops={"torch.ops.aten.mul.Tensor"},
)

如果模块存在图中断,整个子模块都将被 cuda graphs 记录和重放

with torch_tensorrt.runtime.enable_cudagraphs(
    opt_with_graph_break
) as cudagraphs_module:
    cudagraphs_module(input)

脚本总运行时间: ( 0 分 0.000 秒)

由 Sphinx-Gallery 生成的画廊

文档

访问全面的 PyTorch 开发者文档

查看文档

教程

为初学者和高级开发者提供深入的教程

查看教程

资源

查找开发资源并让您的问题得到解答

查看资源