Shortcuts

如何在Windows CPU上使用TorchInductor

创建于:2024年10月1日 | 最后更新:2024年10月22日 | 最后验证:2024年10月1日

作者: Zhaoqiong Zheng, Xu, Han

TorchInductor 是一个编译器后端,它将由 TorchDynamo 生成的 FX 图转换为高度优化的 C++/Triton 内核。 本教程将指导您在 Windows CPU 上使用 TorchInductor 的过程。

What you will learn
  • 如何使用PyTorch编译和执行Python函数,针对Windows CPU进行优化

  • 使用C++/Triton内核进行TorchInductor优化的基础知识。

Prerequisites
  • PyTorch v2.5 或更高版本

  • 微软Visual C++ (MSVC)

  • 适用于 Windows 的 Miniforge

安装所需软件

首先,让我们安装所需的软件。TorchInductor 优化需要 C++ 编译器。 在这个例子中,我们将使用 Microsoft Visual C++ (MSVC)。

  1. 下载并安装 MSVC

  2. 在安装过程中,在工作负载表的桌面和移动部分选择使用C++进行桌面开发。然后安装软件。

注意

我们推荐使用C++编译器ClangIntel Compiler。 请查看替代编译器以获得更好的性能

  1. 下载并安装 Miniforge3-Windows-x86_64.exe

设置环境

  1. 通过cmd.exe打开命令行环境。

  2. 使用以下命令激活 MSVC

    "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvars64.bat"
    
  3. 使用以下命令激活 conda

    "C:/ProgramData/miniforge3/Scripts/activate.bat"
    
  4. 创建并激活一个自定义的conda环境:

    conda create -n inductor_cpu_windows python=3.10 -y
    conda activate inductor_cpu_windows
    
  5. 安装 PyTorch 2.5 或更高版本。

在Windows CPU上使用TorchInductor

这里有一个简单的例子来演示如何使用 TorchInductor:

import torch
def foo(x, y):
    a = torch.sin(x)
    b = torch.cos(y)
    return a + b
opt_foo1 = torch.compile(foo)
print(opt_foo1(torch.randn(10, 10), torch.randn(10, 10)))

以下是此代码可能返回的示例输出:

tensor([[-3.9074e-02,  1.3994e+00,  1.3894e+00,  3.2630e-01,  8.3060e-01,
        1.1833e+00,  1.4016e+00,  7.1905e-01,  9.0637e-01, -1.3648e+00],
        [ 1.3728e+00,  7.2863e-01,  8.6888e-01, -6.5442e-01,  5.6790e-01,
        5.2025e-01, -1.2647e+00,  1.2684e+00, -1.2483e+00, -7.2845e-01],
        [-6.7747e-01,  1.2028e+00,  1.1431e+00,  2.7196e-02,  5.5304e-01,
        6.1945e-01,  4.6654e-01, -3.7376e-01,  9.3644e-01,  1.3600e+00],
        [-1.0157e-01,  7.7200e-02,  1.0146e+00,  8.8175e-02, -1.4057e+00,
        8.8119e-01,  6.2853e-01,  3.2773e-01,  8.5082e-01,  8.4615e-01],
        [ 1.4140e+00,  1.2130e+00, -2.0762e-01,  3.3914e-01,  4.1122e-01,
        8.6895e-01,  5.8852e-01,  9.3310e-01,  1.4101e+00,  9.8318e-01],
        [ 1.2355e+00,  7.9290e-02,  1.3707e+00,  1.3754e+00,  1.3768e+00,
        9.8970e-01,  1.1171e+00, -5.9944e-01,  1.2553e+00,  1.3394e+00],
        [-1.3428e+00,  1.8400e-01,  1.1756e+00, -3.0654e-01,  9.7973e-01,
        1.4019e+00,  1.1886e+00, -1.9194e-01,  1.3632e+00,  1.1811e+00],
        [-7.1615e-01,  4.6622e-01,  1.2089e+00,  9.2011e-01,  1.0659e+00,
        9.0892e-01,  1.1932e+00,  1.3888e+00,  1.3898e+00,  1.3218e+00],
        [ 1.4139e+00, -1.4000e-01,  9.1192e-01,  3.0175e-01, -9.6432e-01,
        -1.0498e+00,  1.4115e+00, -9.3212e-01, -9.0964e-01,  1.0127e+00],
        [ 5.7244e-04,  1.2799e+00,  1.3595e+00,  1.0907e+00,  3.7191e-01,
        1.4062e+00,  1.3672e+00,  6.8502e-02,  8.5216e-01,  8.6046e-01]])

使用替代编译器以获得更好的性能

为了在Windows电感器上提高性能,您可以使用Intel编译器或LLVM编译器。然而,它们依赖于Microsoft Visual C++(MSVC)的运行时库。因此,您的第一步应该是安装MSVC。

英特尔编译器

  1. 下载并安装Intel Compiler的Windows版本。

  2. 使用CXX环境变量设置Windows Inductor编译器 set CXX=icx-cl.

英特尔还提供了一份全面的逐步指南,并附有性能数据。请查看Intel® oneAPI DPC++/C++ Compiler Boosts PyTorch* Inductor Performance on Windows* for CPU Devices

LLVM 编译器

  1. 下载并安装LLVM Compiler并选择win64版本。

  2. 使用CXX环境变量设置Windows Inductor编译器 set CXX=clang-cl

结论

在本教程中,我们学习了如何在Windows CPU上使用PyTorch的Inductor。此外,我们还讨论了使用Intel编译器和LLVM编译器进一步优化性能的方法。

优云智算