• 文档 >
  • 编写 Dynamo ATen 降级通道
快捷方式

编写 Dynamo ATen 转换通道

转换通道的基础知识

ATen 转换通道是 Python 函数,它接收 ATen 算子的图作为输入,应用一些期望的修改,例如算子合并/融合、算子替换、子图重写、自定义算子插入或其他对 torch.fx.GraphModule 的操作,然后将修改后的图返回给调用者。这些转换通道通常会就地修改图并返回相同的输入对象。

转换通道要求

Torch-TRT 中的 ATen 转换通道函数必须满足两个要求: - 函数必须接收一个 torch.fx.GraphModule 和一个 torch 张量序列 Sequence[torch.Tensor] 作为输入,并返回转换后的 torch.fx.GraphModule - 函数必须使图保持有效且可调用的状态,包括执行任何必要的代码检查和重新编译

有关 FX 中 图操作 的信息,请参阅此链接。下面是一个转换通道的示例,它修复了输入同时也是输出的图,这对于 TRT 引擎是不允许的配置。

转换通道示例

def repair_input_as_output(gm: torch.fx.GraphModule, sample_inputs: Sequence[torch.Tensor]) -> torch.fx.GraphModule:
    """Repair scenarios where inputs are also outputs of the graph

    TRT does not allow such cases, so we insert a clone (identity) layer
    """
    modified_graph = False

    # Extract graph placeholder Tensors
    placeholders = [
        node
        for node in gm.graph.nodes
        if (
            node.op == "placeholder"
            and isinstance(node.type, type)
            and issubclass(node.type, torch.Tensor)
        )
    ]

    for placeholder in placeholders:
        # If any placeholder has any users which are direct graph outputs
        if len(placeholder.users) >= 1 and any(
            user.op == "output" for user in placeholder.users
        ):
            modified_graph = True

            # Get direct graph outputs which are direct uses of placeholders
            direct_outputs = [user for user in placeholder.users if user.op == "output"]

            # Insert clone node for placeholder to ensure
            # placeholder is not a direct output
            with gm.graph.inserting_after(placeholder):
                cloned_placeholder = gm.graph.call_function(
                    torch.ops.aten.clone.default,
                    args=(placeholder,),
                )

            # Replace placeholder as output with cloned version
            for output in direct_outputs:
                output.replace_input_with(placeholder, cloned_placeholder)

    # If the graph was modified, clean up the graph and ensure it is up-to-date
    if modified_graph:
        gm.graph.eliminate_dead_code()
        gm.graph.lint()
        gm.recompile()
        logger.debug(f"Graph after repair_input_as_output:\n{gm.graph}")

    return gm

注册转换通道

转换通道目前在 py/torch_tensorrt/dynamo/lowering/passes/__init__.py 中注册,使用 torch.fx.passes.pass_manager.PassManager 工具以期望的顺序组装通道列表。直接添加到该列表中的新通道将应用于 Torch-TensorRT torch.compile 后端中的图。目前,我们提供了一个 ATen 转换通道注册装饰器以方便使用,可以直接调用,也可以使用可选的 index 关键字参数,该参数控制转换通道在通道列表中的插入位置。

例如,要在默认位置(列表末尾)插入通道,可以使用以下代码

@_aten_lowering_pass
def my_custom_pass(gm: torch.fx.GraphModule, sample_inputs: Sequence[torch.Tensor]) -> torch.fx.GraphModule:
    ...

或者,要在通道列表的自定义索引(例如列表的开头)处插入通道,可以使用以下代码

@_aten_lowering_pass(index=0)
def my_custom_pass(gm: torch.fx.GraphModule, sample_inputs: Sequence[torch.Tensor]) -> torch.fx.GraphModule:
    ...

torch_tensorrt.dynamo.lowering.passes 中还提供了一些实用程序,用于显示当前可用的转换通道列表,将这些通道应用于任意的 torch.fx.GraphModule,以及移除特定索引处的转换通道。

# Print all lowering passes in the list
print(dump_lowering_passes())

# Apply lowering passes to a GraphModule
apply_lowering_passes(graph_module, sample_inputs)

# Remove the lowering pass at index 1
_remove_lowering_pass(index=1)

注意: 随着转换通道系统的演进,上述 API 可能会发生变化。

文档

访问全面的 PyTorch 开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源