Skip to content

安装 triton-windows

在一些加速方案中,需要使用该软件,比如 torch.compile,用于文生图加速/文生视频加速等。

GPU 要求

  1. sm >= 80 的 Nvidia GPUs,例如 RTX 30xx 及以上
  2. 如果需要在 fp8 (float8) 模型上使用 triton 加速,需要 sm >= 89 的 Nvidia GPUs,例如 RTX 40xx 及以上

安装 Pytorch

  1. triton 3.1.0 需要 torch >= 2.4.0
  2. triton 3.2.0 需要 torch >= 2.6.0

进入 pytorch 官网,如下图所示进行安装

img.png

安装 CUDA

CUDA 需要 12.x,安装见 安装 NVIDIA drivers/CUDA/cuDNN

安装 VisualStudio

VisualStudio 的安装见 安装 Visual Studio

安装完成之后,配置 cl.exe 所在的文件到环境变量 Path 中,例如:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\bin\Hostx64\x64

安装 triton

下载页 选择适配自己 python 版本的 whl,例如,python3.12 下载 triton-3.1.0-cp312-cp312-win_amd64.whl;python3.10 下载 triton-3.1.0-cp310-cp310-win_amd64.whl

下载之后,进行安装,如果是在普通的 python 环境下,执行:

shell
pip install triton-3.1.0-cp312-cp312-win_amd64.whl

如果是在 python_embeded 内嵌包环境下(例如 ComfyUI_windows_portable),在 python_embeded 内嵌包路径下执行:

shell
.\python_embeded\python.exe -m pip install triton-3.1.0-cp312-cp312-win_amd64.whl

(可选)针对内嵌版的 python 包

以 ComfyUI_windows_portable 为例,对于使用 python3.12,可下载 https://github.com/woct0rdho/triton-windows/releases/download/v3.0.0-windows.post1/python_3.12.7_include_libs.zip ,解压后将其中的 libs 和 include 文件夹拷贝到 python_embeded 文件夹下。

验证 triton 是否安装成功

验证 triton 是否安装成功。编写 python 文件 test_triton.py:

python
import torch
import triton
import triton.language as tl

@triton.jit
def add_kernel(x_ptr, y_ptr, output_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
    pid = tl.program_id(axis=0)
    block_start = pid * BLOCK_SIZE
    offsets = block_start + tl.arange(0, BLOCK_SIZE)
    mask = offsets < n_elements
    x = tl.load(x_ptr + offsets, mask=mask)
    y = tl.load(y_ptr + offsets, mask=mask)
    output = x + y
    tl.store(output_ptr + offsets, output, mask=mask)

def add(x: torch.Tensor, y: torch.Tensor):
    output = torch.empty_like(x)
    n_elements = output.numel()
    grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]),)
    add_kernel[grid](x, y, output, n_elements, BLOCK_SIZE=1024)
    return output

a = torch.rand(3, device="cuda")
b = a + a
b_compiled = add(a, a)
print(b_compiled - b)
print("install triton success")

以内嵌包为例,之后执行如下命令,

shell
.\python_embeded\python.exe test_triton.py

输出 install triton success 则表示成功!

ComfyUI triton-windows 简化安装脚本

百度网盘 下载安装包。

text
1. 前提:安装好 VisualStudio
2. 解压下载的压缩包,将 triton-3.1.0-cp312-cp312-win_amd64.whl / test_triton.py / install_triton_wheel.bat 三个文件拷贝到 ComfyUI_windows_portable\ 文件夹下
3. 双击 install_triton_wheel.bat
4. 解压 python_3.12.7_include_libs.zip,将解压后 libs 和 include 文件夹拷贝到 \ComfyUI_windows_portable\python_embeded\ 下
5. 启动 ComfyUI