评价此页

入门#

创建于:2025 年 6 月 16 日 | 最后更新于:2025 年 6 月 16 日

在阅读本节之前,请确保已阅读torch.compiler

让我们从一个简单的 torch.compile 示例开始,演示如何使用 torch.compile 进行推理。此示例演示了 torch.cos()torch.sin() 功能,它们是逐点运算符的示例,因为它们对向量逐元素进行操作。此示例可能不会显示显著的性能提升,但应该有助于您直观地理解如何在自己的程序中使用 torch.compile

注意

要运行此脚本,您的计算机上至少需要有一块 GPU。如果您没有 GPU,可以删除以下代码片段中的 .to(device="cuda:0") 代码,它将在 CPU 上运行。您也可以将设备设置为 xpu:0 在 Intel® GPU 上运行。

import torch
def fn(x):
   a = torch.cos(x)
   b = torch.sin(a)
   return b
new_fn = torch.compile(fn, backend="inductor")
input_tensor = torch.randn(10000).to(device="cuda:0")
a = new_fn(input_tensor)

您可能想要使用的另一个更著名的逐点运算符是 torch.relu()。在 eager 模式下,逐点运算符不是最优的,因为每个运算符都需要从内存中读取一个张量,进行一些更改,然后将这些更改写回。Inductor 执行的最重要的优化是融合。在上面的示例中,我们可以将 2 次读取(xa)和 2 次写入(ab)转换为 1 次读取(x)和 1 次写入(b),这对于内存带宽(您可以多快地将数据发送到 GPU)而不是计算(您的 GPU 处理浮点运算的速度有多快)是瓶颈的新一代 GPU 尤其关键。

Inductor 提供的另一个主要优化是自动支持 CUDA 图。CUDA 图有助于消除从 Python 程序启动单个内核的开销,这对于新一代 GPU 尤其重要。

TorchDynamo 支持许多不同的后端,但 TorchInductor 特别通过生成 Triton 内核来工作。让我们将上面的示例保存到一个名为 example.py 的文件中。我们可以通过运行 TORCH_COMPILE_DEBUG=1 python example.py 来检查生成的 Triton 内核代码。当脚本执行时,您应该会在终端看到 DEBUG 消息。在日志接近末尾的地方,您应该会看到一个指向包含 torchinductor_<your_username> 的文件夹的路径。在该文件夹中,您可以找到 output_code.py 文件,其中包含生成的内核代码,类似于以下内容:

@pointwise(size_hints=[16384], filename=__file__, triton_meta={'signature': {'in_ptr0': '*fp32', 'out_ptr0': '*fp32', 'xnumel': 'i32'}, 'device': 0, 'constants': {}, 'mutated_arg_names': [], 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]})
@triton.jit
def triton_(in_ptr0, out_ptr0, xnumel, XBLOCK : tl.constexpr):
   xnumel = 10000
   xoffset = tl.program_id(0) * XBLOCK
   xindex = xoffset + tl.arange(0, XBLOCK)[:]
   xmask = xindex < xnumel
   x0 = xindex
   tmp0 = tl.load(in_ptr0 + (x0), xmask, other=0.0)
   tmp1 = tl.cos(tmp0)
   tmp2 = tl.sin(tmp1)
   tl.store(out_ptr0 + (x0 + tl.zeros([XBLOCK], tl.int32)), tmp2, xmask)

注意

上面的代码片段是一个示例。根据您的硬件,您可能会看到不同的生成的代码。

您可以验证 cossin 的融合确实发生了,因为 cossin 操作发生在单个 Triton 内核中,并且临时变量存储在寄存器中,访问速度非常快。

在此处详细了解 Triton 的性能:Triton。由于代码是用 Python 编写的,即使您没有编写太多 CUDA 内核,也相对容易理解。

接下来,让我们尝试一个真实的模型,例如 PyTorch hub 中的 resnet50。

import torch
model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet50', pretrained=True)
opt_model = torch.compile(model, backend="inductor")
opt_model(torch.randn(1,3,64,64))

而且这并非唯一可用的后端,您可以在 REPL 中运行 torch.compiler.list_backends() 来查看所有可用的后端。接下来可以受到 cudagraphs 的启发。

使用预训练模型#

PyTorch 用户经常利用来自 transformersTIMM 的预训练模型,其设计目标之一是 TorchDynamo 和 TorchInductor 能够直接与人们想要编写的任何模型配合使用。

让我们从 HuggingFace hub 直接下载一个预训练模型并对其进行优化。

import torch
from transformers import BertTokenizer, BertModel
# Copy pasted from here https://hugging-face.cn/bert-base-uncased
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained("bert-base-uncased").to(device="cuda:0")
model = torch.compile(model, backend="inductor") # This is the only line of code that we changed
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='pt').to(device="cuda:0")
output = model(**encoded_input)

如果您从模型和 encoded_input 中删除 to(device="cuda:0"),那么 Triton 将生成针对在 CPU 上运行进行优化的 C++ 内核。您可以检查 BERT 的 Triton 或 C++ 内核。它们比我们上面尝试过的三角学示例更复杂,但您可以类似地浏览它们,看看是否能理解 PyTorch 的工作原理。

同样,让我们尝试一个 TIMM 示例。

import timm
import torch
model = timm.create_model('resnext101_32x8d', pretrained=True, num_classes=2)
opt_model = torch.compile(model, backend="inductor")
opt_model(torch.randn(64,3,7,7))

后续步骤#

在本节中,我们回顾了一些推理示例,并对 torch.compile 的工作原理有了基本的了解。接下来可以查看以下内容: