2239 字
11 分钟
Windows 原生安装 Triton 完整指南:triton-windows 实测、避坑与 torch.compile 提速真相

RuntimeError: Cannot find a working triton installation——在 Windows 上跑 torch.compile,第一道墙就是 Triton 没有官方 Windows 轮子。

现在这堵墙已经能拆了:社区项目 triton-windows 一条 pip install 搞定,不需要 Visual Studio,也不需要 CUDA Toolkit。但装得上不等于提得了速——同一个模型在 RTX 3060 上,torch.compile 前后的解码速度是 24.0 对 24.0 tok/s。

本文给两样东西:能直接复制的安装路径(含便携 Python 必踩的 Python.h not found),和装之前的决策依据。

一句话答案#

Windows 上装 Triton 的正确姿势:pip install -U "triton-windows<3.3"(torch 2.6 对应 Triton 3.2)。不需要装 Visual Studio,也不需要装 CUDA Toolkit。

如果用便携版 / 嵌入式 Python,必须先把完整版 Python 的 Include 目录和 python312.lib 拷进去,否则报 Python.h not found。

为什么 Windows 装 Triton 这么麻烦#

Triton 是 PyTorch torch.compile 的默认编译后端,官方只发布 Linux / macOS 轮子。Windows 用户跑 torch.compile 时的典型报错:

RuntimeError: Cannot find a working triton installation.
Either the package is not installed or it is too old.

长期以来 Windows 用户的”官方答案”是 WSL2。但社区项目 triton-windows 已经给出成熟的原生方案:

  • 自 3.2.0.post11 起,轮子直接发布在 PyPI,pip 自动能找到,不用去 GitHub Releases 手动下 wheel;
  • 自 3.2.0.post13 起,TinyCC 编译器捆绑进轮子,不需要 MSVC;
  • CUDA 工具链同样捆绑(3.1–3.2 对应 CUDA 12.4,3.3 及以后对应 12.8),装 CUDA Toolkit 那一步可以直接跳过。

版本必须一一对应,PyTorch 每个 minor 版本只保证配一个 Triton minor 版本(下表据官方 README,2026-12-02 读取):

PyTorchtriton-windows安装命令
2.63.2.xpip install -U "triton-windows<3.3"
2.73.3.xpip install -U "triton-windows<3.4"
2.83.4.xpip install -U "triton-windows<3.5"
2.93.5.xpip install -U "triton-windows<3.6"
2.103.6.xpip install -U "triton-windows<3.7"

不锁上限的后果是:某天 pip 给你装了更新的 Triton,torch.compile 当场坏掉。用 <3.x 卡住是最省事的保护。

另外,这个仓库的开发已迁到 triton-lang/triton-windows,旧地址仍可访问,安装方式不变。

第一步:安装 triton-windows(5 分钟)#

Terminal window
# 以 torch 2.6 为例,装 triton 3.2
python -m pip install -U "triton-windows<3.3"

如果是嵌入式 Python(ComfyUI 便携包那种),不要直接调 pip,要走解释器:

Terminal window
D:\你的便携python\python.exe -m pip install -U "triton-windows<3.3"

装完检查:

Terminal window
python -c "import triton; print(triton.__version__)"
# 期望输出:3.2.0(或 3.2.0.postXX)

到这里,用完整安装版 Python 的用户已经可以用了。用便携版 / 嵌入式 Python 的会撞上第二个坑。

踩坑实录:Python.h not found#

这是本文的核心坑,中文资料几乎没有答案。

症状#

装了 triton-windows 后跑 torch.compile,报错变成:

Failed to compile. cc_cmd: ['...\\triton\\runtime\\tcc\\tcc.exe',
'...\\cuda_utils.c', ..., '-ID:\\xxx\\python\\Include']
error: include file 'Python.h' not found

同时有警告:Failed to find Python libs.

根因#

Triton 运行时要现场编译一个小 C 扩展(cuda_utils,封装显存管理驱动 API),需要 Python.h 和 python3xx.lib。

而嵌入式(embeddable)Python——各类”便携 Python 运行时”、绿色版 Python、python-3.12.x-embed-amd64.zip 的二次打包——只带运行时,不带开发文件:

Terminal window
# 标志性特征:有 Lib、有一堆 .pyd,但没有这两个路径
Test-Path D:\你的便携python\Include\Python.h # False → 必踩此坑
Test-Path D:\你的便携python\libs\python312.lib # False → 必踩此坑

PyTorch、CUDA、显卡驱动全都正常,缺的只是两个开发文件。这也解释了为什么这个错看起来像”C 编译器坏了”,实际与编译器无关。

修法:从完整版 Python 拷开发文件#

Terminal window
$SRC = "C:\Users\admin\AppData\Local\Programs\Python\Python312" # 完整安装版
$DST = "D:\你的便携python"
Copy-Item -Recurse "$SRC\Include" "$DST\Include"
New-Item -ItemType Directory -Force "$DST\libs" | Out-Null
Copy-Item "$SRC\libs\python312.lib" "$DST\libs\"

两个容易做错的细节:

  1. 版本对齐 minor:python312.lib 的 ABI 绑死 Python 次版本号(3.12 的 lib 不能给 3.13 用),但补丁版本(3.12.10 vs 3.12.14)可以混用。拷贝前两边各跑一遍 python --version 确认。
  2. 别去装 Visual Studio:TinyCC 和 CUDA 头文件都已经在轮子里,缺的只有 Python 开发文件这一项。看到”C 编译失败”就去装几个 GB 的 VS Build Tools 是误诊。

验证安装#

Terminal window
python -c "
import torch
@torch.compile
def f(x):
return torch.relu(x) @ x
x = torch.randn(256, 256, device='cuda')
f(x); torch.cuda.synchronize()
print('torch.compile CUDA OK')
"

输出 torch.compile CUDA OK 即成功。第一次调用会慢一些(Triton 正在编译 kernel),属正常现象。

实测:torch.compile 到底能提多少速#

装好 Triton 不等于提速。我在真实项目上做了对照实验。

测试环境:Windows 11 · RTX 3060 12GB · Python 3.12.10 · torch 2.6.0+cu124 · triton-windows 3.2.0.post21 · 模型为 Unlimited-SVG 多模态模型(DeepSeek-VL2 架构 MoE,hidden 1280,12 层,64 专家、每 token 激活 6 个,bf16 权重 6.2GB)。

方案解码速度(稳态)相对 eager
eager 基线24.0 tok/s1.0x
torch.compile(dynamic=True)24.0 tok/s1.0x

零提升,原因在机制上写死了:

  1. batch=1 解码是访存受限负载。每生成一个 token,都要把激活路径上的权重(MoE 路由选中的专家 + 共享参数 + 12.9 万词表的 lm_head)从显存读一遍。kernel 融合不改变”读多少字节”,只改变”分几个 kernel 读”。
  2. 分辨率换不动速度。同一模型 1024 和 2048 两种输入分辨率,解码速度几乎一样(22.1 vs 23 tok/s)——瓶颈从来不在注意力或序列长度。
  3. 编译覆盖有限。这个模型的注意力子图触发 inductor 动态形状 bug,只能 suppress_errors=True 回退 eager,编译能覆盖的面进一步缩水。

另一个反直觉的观察:torch.compile(...) 调用本身 1 秒就返回,编译实际发生在第一次前向时。想用”compile 调用耗时”判断编译快慢,会得出完全错误的结论。

决策清单:你的场景值不值得装#

动手之前先对照这张表:

你的场景建议
ComfyUI / SD 出图(扩散模型 UNet,batch 较大、计算密集)值得,triton-windows + SageAttention 有真实收益,社区成熟方案多
本地大模型聊天 / 流式生成(batch=1 自回归解码)先实测 10 分钟,大概率零收益;有效方向排序:换更高带宽的显卡 > 量化(INT8/FP8) > CUDA Graphs
训练 / 微调值得,计算密集场景编译收益稳定
只是想跑通某个依赖 Triton 的库(如 SageAttention)值得,按本文步骤装即可
便携包分发(embeddable Python)可以装,但记得补齐 Include 和 libs,并复测 40MB 体积增量

最小实测脚本(把 model.infer(...) 换成自己的生成调用):

import torch._dynamo
torch._dynamo.config.suppress_errors = True # 编不过的子图回退 eager
compiled = torch.compile(model, dynamic=True)
# 跑两轮完整生成,取第二轮稳态速度;第一轮含编译时间,必须丢弃

常见问题 FAQ#

Q: triton 和 triton-windows 能共存吗? 不能。triton-windows 提供的模块名也是 triton,装之前先 pip uninstall triton。两者互斥,装错版本时 import triton 看到的版本可能和你想的不一样。

Q: 必须装 Visual Studio 或 CUDA Toolkit 吗? 不需要。triton-windows 3.2.0.post13+ 的 PyPI 轮子自带 TinyCC 编译器和 CUDA 头文件。只有当你要从源码编译 Triton / SageAttention / flash-attn 时才需要 MSVC + nvcc。

Q: 报 “Python.h not found” 但我是完整安装版 Python? 检查是否多 Python 环境混用:python -c "import sys; print(sys.prefix)" 确认运行的解释器路径,和报错里 -I 的包含路径比对。triton 用的是当前进程的解释器路径,IDE / 虚拟环境选错解释器是常见原因。

Q: torch.compile 后第一次运行特别慢,正常吗? 正常。编译发生在首次调用,几十秒到几分钟都有可能。计时请丢弃预热轮。但如果每次都慢(每次都重新编译),检查 torch.compile 是否包在函数里被反复执行。

Q: Windows 上有没有 torch.compile 的替代方案? 有,按收益排序:ONNX Runtime / TensorRT(推理框架级)、bitsandbytes 量化(减带宽)、手动 CUDA Graphs(消 launch 开销)。对 batch=1 解码,量化减的是”每 token 要读的字节数”,机制上比编译融合更对症。

Q: WSL2 是不是更好的选择? 如果你的工作流本来就跑在 WSL2 / Linux,是的——官方 Triton、flash-attn、CUDA Graphs 全套都有。但如果目标产物是”Windows 便携包分发”(绿色软件、ComfyUI 便携版),原生 Windows + triton-windows 是唯一自洽的方案。

结论#

Windows 原生跑通 Triton 在 2026 年已经不是难题:pip install triton-windows 一条命令,外加便携 Python 补两个开发文件目录。

真正需要想清楚的是装它为了什么——计算密集的场景(扩散、训练)能拿到实打实的收益;batch=1 的大模型解码是”装得上、提不了”的典型,先用 10 分钟实测,再决定要不要为它在包里多背 40MB。

测试环境存档:Windows 11 · RTX 3060 12GB · Python 3.12.10 · torch 2.6.0+cu124 · triton-windows 3.2.0.post21 · 模型 Unlimited-SVG(DeepSeek-VL2 架构 MoE,hidden 1280,12 层,64 专家 / top-6,bf16 6.2GB)· 解码速度用两轮完整生成取稳态测量。

参考链接:


本文与 triton-windows 项目方无关联;版本数据以 2026-12-02 官方 README 为准,安装命令与实测数据均来自本机环境,你的显卡与 torch 版本可能得到不同结论。

Windows 原生安装 Triton 完整指南:triton-windows 实测、避坑与 torch.compile 提速真相
https://yunyingai.net/posts/triton-windows-native-install/
作者
云樱梦海
发布于
2026-12-02
许可协议
CC BY-NC-SA 4.0