• 文档 >
  • Torch-TensorRT (FX 前端) 用户指南
快捷方式

Torch-TensorRT (FX 前端) 用户指南

Torch-TensorRT (FX 前端) 是一款可以通过 torch.fx 将 PyTorch 模型转换为针对 Nvidia GPU 运行而优化的 TensorRT 引擎的工具。TensorRT 是 NVIDIA 开发的推理引擎,它包含各种优化,包括内核融合、图优化、低精度等。该工具是在 Python 环境中开发的,这使得研究人员和工程师可以非常方便地使用此工作流程。用户希望使用此工具有几个阶段,我们将在这里介绍它们。

> Torch-TensorRT (FX 前端) 处于 Beta 状态,目前建议与 PyTorch nightly 配合使用。

# Test an example by
$ python py/torch_tensorrt/fx/example/lower_example.py

将 PyTorch 模型转换为 TensorRT 引擎

通常,欢迎用户使用 compile() 来完成从模型到 TensorRT 引擎的转换。它是一个包装器 API,包含完成此转换所需的主要步骤。请参阅 examples/fx 下的 lower_example.py 文件中的示例用法。

def compile(
    module: nn.Module,
    input,
    max_batch_size=2048,
    max_workspace_size=33554432,
    explicit_batch_dimension=False,
    lower_precision=LowerPrecision.FP16,
    verbose_log=False,
    timing_cache_prefix="",
    save_timing_cache=False,
    cuda_graph_batch_size=-1,
    dynamic_batch=True,
) -> nn.Module:

    """
    Takes in original module, input and lowering setting, run lowering workflow to turn module
    into lowered module, or so called TRTModule.

    Args:
        module: Original module for lowering.
        input: Input for module.
        max_batch_size: Maximum batch size (must be >= 1 to be set, 0 means not set)
        max_workspace_size: Maximum size of workspace given to TensorRT.
        explicit_batch_dimension: Use explicit batch dimension in TensorRT if set True, otherwise use implicit batch dimension.
        lower_precision: lower_precision config given to TRTModule.
        verbose_log: Enable verbose log for TensorRT if set True.
        timing_cache_prefix: Timing cache file name for timing cache used by fx2trt.
        save_timing_cache: Update timing cache with current timing cache data if set to True.
        cuda_graph_batch_size: Cuda graph batch size, default to be -1.
        dynamic_batch: batch dimension (dim=0) is dynamic.
    Returns:
        A torch.nn.Module lowered by TensorRT.
    """

在本节中,我们将通过一个示例来说明 fx 路径使用的主要步骤。用户可以参考 examples/fx 中的 fx2trt_example.py 文件。

  • 步骤 1:使用 acc_tracer 跟踪模型

Acc_tracer 是从 FX tracer 继承的跟踪器。它带有参数标准化器,可以将所有参数转换为 kwargs 并传递给 TRT 转换器。

import torch_tensorrt.fx.tracer.acc_tracer.acc_tracer as acc_tracer

# Build the model which needs to be a PyTorch nn.Module.
my_pytorch_model = build_model()

# Prepare inputs to the model. Inputs have to be a List of Tensors
inputs = [Tensor, Tensor, ...]

# Trace the model with acc_tracer.
acc_mod = acc_tracer.trace(my_pytorch_model, inputs)

常见错误

符号跟踪的变量不能用作控制流的输入 这意味着模型包含动态控制流。请参阅 FX 指南 中的“动态控制流”部分。

  • 步骤 2:构建 TensorRT 引擎

TensorRT 处理批处理维度有两种不同的模式:显式批处理维度和隐式批处理维度。此模式由早期版本的 TensorRT 使用,现在已弃用,但为了向后兼容性继续得到支持。在显式批处理模式下,所有维度都是显式的,并且可以是动态的,也就是说它们的长度可以在执行时更改。许多新功能(例如动态形状和循环)仅在此模式下可用。当用户在 compile() 中设置 explicit_batch_dimension=False 时,仍然可以选择使用隐式批处理模式。我们不建议使用它,因为它在未来的 TensorRT 版本中将缺乏支持。

显式批处理是默认模式,必须为动态形状设置。对于大多数视觉任务,如果用户希望获得与隐式模式类似的效果(仅批处理维度发生变化),则可以选择在 compile() 中启用 dynamic_batch。它有一些要求:1. 输入、输出和激活的形状是固定的,除了批处理维度。2. 输入、输出和激活的批处理维度为主维度。3. 模型中的所有运算符都不会修改批处理维度(排列、转置、拆分等)或计算批处理维度(求和、softmax 等)。

对于最后一种路径的示例,如果我们有一个形状为 (batch, sequence, dimension) 的 3D 张量 t,则可以使用诸如 torch.transpose(0, 2) 之类的操作。如果这三个条件中的任何一个不满足,我们需要指定 InputTensorSpec 作为具有动态范围的输入。

import deeplearning.trt.fx2trt.converter.converters
from torch.fx.experimental.fx2trt.fx2trt import InputTensorSpec, TRTInterpreter

# InputTensorSpec is a dataclass we use to store input information.
# There're two ways we can build input_specs.
# Option 1, build it manually.
input_specs = [
  InputTensorSpec(shape=(1, 2, 3), dtype=torch.float32),
  InputTensorSpec(shape=(1, 4, 5), dtype=torch.float32),
]
# Option 2, build it using sample_inputs where user provide a sample
inputs = [
torch.rand((1,2,3), dtype=torch.float32),
torch.rand((1,4,5), dtype=torch.float32),
]
input_specs = InputTensorSpec.from_tensors(inputs)

# IMPORTANT: If dynamic shape is needed, we need to build it slightly differently.
input_specs = [
    InputTensorSpec(
        shape=(-1, 2, 3),
        dtype=torch.float32,
        # Currently we only support one set of dynamic range. User may set other dimensions but it is not promised to work for any models
        # (min_shape, optimize_target_shape, max_shape)
        # For more information refer to fx/input_tensor_spec.py
        shape_ranges = [
            ((1, 2, 3), (4, 2, 3), (100, 2, 3)),
        ],
    ),
    InputTensorSpec(shape=(1, 4, 5), dtype=torch.float32),
]

# Build a TRT interpreter. Set explicit_batch_dimension accordingly.
interpreter = TRTInterpreter(
    acc_mod, input_specs, explicit_batch_dimension=True/False
)

# The output of TRTInterpreter run() is wrapped as TRTInterpreterResult.
# The TRTInterpreterResult contains required parameter to build TRTModule,
# and other informational output from TRTInterpreter run.
class TRTInterpreterResult(NamedTuple):
    engine: Any
    input_names: Sequence[str]
    output_names: Sequence[str]
    serialized_cache: bytearray

#max_batch_size: set accordingly for maximum batch size you will use.
#max_workspace_size: set to the maximum size we can afford for temporary buffer
#lower_precision: the precision model layers are running on (TensorRT will choose the best perforamnce precision).
#sparse_weights: allow the builder to examine weights and use optimized functions when weights have suitable sparsity
#force_fp32_output: force output to be fp32
#strict_type_constraints: Usually we should set it to False unless we want to control the precision of certain layer for numeric #reasons.
#algorithm_selector: set up algorithm selection for certain layer
#timing_cache: enable timing cache for TensorRT
#profiling_verbosity: TensorRT logging level
trt_interpreter_result = interpreter.run(
    max_batch_size=64,
    max_workspace_size=1 << 25,
    sparse_weights=False,
    force_fp32_output=False,
    strict_type_constraints=False,
    algorithm_selector=None,
    timing_cache=None,
    profiling_verbosity=None,
)

常见错误

RuntimeError:当前不支持函数 xxx 的转换!- 这意味着我们不支持此 xxx 运算符。有关进一步说明,请参阅下面的“如何添加缺少的操作”部分。

  • 步骤 3:运行模型

一种方法是使用 TRTModule,它基本上是一个 PyTorch nn.Module。

from torch_tensorrt.fx import TRTModule
mod = TRTModule(
    trt_interpreter_result.engine,
    trt_interpreter_result.input_names,
    trt_interpreter_result.output_names)
# Just like all other PyTorch modules
outputs = mod(*inputs)
torch.save(mod, "trt.pt")
reload_trt_mod = torch.load("trt.pt")
reload_model_output = reload_trt_mod(*inputs)

到目前为止,我们详细解释了将 PyTorch 模型转换为 TensorRT 引擎的主要步骤。欢迎用户参考源代码以获取一些参数说明。在转换方案中,有两个重要操作。一个是 acc 跟踪器,它帮助我们将 PyTorch 模型转换为 acc 图。另一个是 FX 路径转换器,它有助于将 acc 图的操作转换为相应的 TensorRT 操作,并为其构建 TensoRT 引擎。

Acc 跟踪器

Acc 跟踪器是一个自定义 FX 符号跟踪器。与普通的 FX 符号跟踪器相比,它还做了一些额外的工作。我们主要依靠它将 PyTorch 操作或内置操作转换为 acc 操作。fx2trt 使用 acc 操作主要有两个目的

  1. 在 PyTorch 操作和内置操作中,有许多操作做类似的事情,例如 torch.add、builtin.add 和 torch.Tensor.add。使用 acc 跟踪器,我们将这三个操作规范化为一个 acc_ops.add。这有助于减少我们需要编写的转换器数量。

  2. acc 操作只有 kwargs,这使得编写转换器更容易,因为我们不需要添加额外的逻辑来在 args 和 kwargs 中查找参数。

FX2TRT

在符号跟踪之后,我们得到了 PyTorch 模型的图表示。fx2trt 利用了 fx.Interpreter 的强大功能。fx.Interpreter 逐个遍历整个图节点,并调用该节点表示的函数。fx2trt 通过为每个节点调用相应的转换器来覆盖调用函数的原始行为。每个转换器函数都会添加相应的 TensorRT 层。

下面是一个转换器函数的示例。装饰器用于将此转换器函数与其对应的节点注册。在此示例中,我们将此转换器注册到目标为 acc_ops.sigmoid 的 fx 节点。

@tensorrt_converter(acc_ops.sigmoid)
def acc_ops_sigmoid(network, target, args, kwargs, name):
    """
    network: TensorRT network. We'll be adding layers to it.

    The rest arguments are attributes of fx node.
    """
    input_val = kwargs['input']

    if not isinstance(input_val, trt.tensorrt.ITensor):
        raise RuntimeError(f'Sigmoid received input {input_val} that is not part '
                        'of the TensorRT region!')

    layer = network.add_activation(input=input_val, type=trt.ActivationType.SIGMOID)
    layer.name = name
    return layer.get_output(0)

如何添加缺少的操作

您实际上可以将其添加到任何您想要的位置,只需记住导入文件,以便在使用 acc_tracer 进行跟踪之前注册所有 acc 操作和映射器。

  • 步骤 1. 添加新的 acc 操作

TODO:需要更多地解释 acc 操作的逻辑,例如我们何时想要分解操作以及何时想要重用其他操作。

acc 跟踪器 中,如果为节点到 acc 操作注册了映射,我们会将图中的节点转换为 acc 操作。

为了使到 acc 操作的转换发生,需要满足两个条件。一个是应该定义 acc 操作函数,另一个是应该注册映射。

定义 acc 操作很简单,我们首先只需要一个函数,并通过 acc_normalizer.py 中的这个装饰器将该函数注册为 acc 操作。例如,以下代码添加了一个名为 foo() 的 acc 操作,它将两个给定的输入相加。

# NOTE: all acc ops should only take kwargs as inputs, therefore we need the "*"
# at the beginning.
@register_acc_op
def foo(*, input, other, alpha):
    return input + alpha * other

有两种方法可以注册映射。一种是 register_acc_op_mapping()。让我们注册一个从 torch.add 到我们上面创建的 foo() 的映射。我们需要为其添加装饰器 register_acc_op_mapping。

this_arg_is_optional = True

@register_acc_op_mapping(
    op_and_target=("call_function", torch.add),
    arg_replacement_tuples=[
        ("input", "input"),
        ("other", "other"),
        ("alpha", "alpha", this_arg_is_optional),
    ],
)
@register_acc_op
def foo(*, input, other, alpha=1.0):
    return input + alpha * other

op_and_target 决定了哪个节点将触发此映射。op 和 target 是 FX 节点的属性。在 acc_normalization 中,当我们看到一个节点的 op 和 target 与 op_and_target 中设置的相同,我们就会触发映射。由于我们想要从 torch.add 映射,因此 op 将是 call_function,target 将是 torch.addarg_replacement_tuples 决定了我们如何使用原始节点的 args 和 kwargs 来构造新的 acc op 节点的 kwargs。arg_replacement_tuples 中的每个元组代表一个参数映射规则。它包含两个或三个元素。第三个元素是一个布尔变量,决定了这个 kwarg 在*原始节点*中是否是可选的。只有当它是 True 时,我们才需要指定第三个元素。第一个元素是原始节点中的参数名,它将被用作 acc op 节点的参数,其名称是元组中的第二个元素。元组的顺序很重要,因为元组的位置决定了参数在原始节点 args 中的位置。我们使用这些信息将 args 从原始节点映射到 acc op 节点中的 kwargs。如果以下情况都不成立,我们就不必指定 arg_replacement_tuples。

  1. 原始节点和 acc op 节点的 kwargs 具有不同的名称。

  2. 存在可选参数。

另一种注册映射的方式是通过 register_custom_acc_mapper_fn()。这个函数旨在减少冗余的操作注册,因为它允许您使用一个函数通过某些组合映射到一个或多个现有的 acc op。在该函数中,您基本上可以做任何您想做的事情。让我们用一个例子来解释它是如何工作的。

@register_acc_op
def foo(*, input, other, alpha=1.0):
    return input + alpha * other

@register_custom_acc_mapper_fn(
    op_and_target=("call_function", torch.add),
    arg_replacement_tuples=[
        ("input", "input"),
        ("other", "other"),
        ("alpha", "alpha", this_arg_is_optional),
    ],
)
def custom_mapper(node: torch.fx.Node, _: nn.Module) -> torch.fx.Node:
    """
    `node` is original node, which is a call_function node with target
    being torch.add.
    """
    alpha = 1
    if "alpha" in node.kwargs:
        alpha = node.kwargs["alpha"]
    foo_kwargs = {"input": node["input"], "other": node["other"], "alpha": alpha}
    with node.graph.inserting_before(node):
        foo_node = node.graph.call_function(foo, kwargs=foo_kwargs)
        foo_node.meta = node.meta.copy()
        return foo_node

在自定义映射器函数中,我们构造了一个 acc op 节点并返回它。我们在这里返回的节点将接管原始节点的所有子节点 acc_normalizer.py

最后一步是为我们添加的新 acc op 或映射器函数*添加单元测试*。添加单元测试的位置在这里 test_acc_tracer.py

  • 步骤 2. 添加新的转换器

所有为 acc op 开发的转换器都在 acc_op_converter.py 中。它可以为您提供如何添加转换器的一个很好的例子。

本质上,转换器是将 acc op 映射到 TensorRT 层的映射机制。如果我们能够找到所有需要的 TensorRT 层,我们就可以开始使用 TensorRT API 为节点添加转换器。

@tensorrt_converter(acc_ops.sigmoid)
def acc_ops_sigmoid(network, target, args, kwargs, name):
    """
    network: TensorRT network. We'll be adding layers to it.

    The rest arguments are attributes of fx node.
    """
    input_val = kwargs['input']

    if not isinstance(input_val, trt.tensorrt.ITensor):
        raise RuntimeError(f'Sigmoid received input {input_val} that is not part '
                        'of the TensorRT region!')

    layer = network.add_activation(input=input_val, type=trt.ActivationType.SIGMOID)
    layer.name = name
    return layer.get_output(0)

我们需要使用 tensorrt_converter 装饰器来注册转换器。装饰器的参数是我们需要转换的 fx 节点的目标。在转换器中,我们可以在 kwargs 中找到 fx 节点的输入。例如,原始节点是 acc_ops.sigmoid,它在 acc_ops.py 中只有一个参数 "input"。我们获取输入并检查它是否是一个 TensorRT 张量。之后,我们向 TensorRT 网络添加一个 sigmoid 层并返回该层的输出。我们返回的输出将由 fx.Interpreter 传递给 acc_ops.sigmoid 的子节点。

如果我们在 TensorRT 中找不到与节点功能相同的对应层怎么办?

在这种情况下,我们需要做更多的工作。TensorRT 提供了充当自定义层的插件。*我们还没有实现这个功能。一旦启用,我们会及时更新*。

最后一步是为我们添加的新转换器添加单元测试。用户可以在 这个文件夹 中添加相应的单元测试。

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取针对初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并获得问题的解答

查看资源