php 開源企業(yè)網(wǎng)站百度網(wǎng)盤網(wǎng)頁版入口
目錄
- 背景
- 解決方法
- 方法一:(治標(biāo)不治本)
- 方法二:(triton-windows)
- - 安裝 MSVC 和 Windows SDK
- - vcredist 安裝
- - whl 安裝
- - 驗(yàn)證
背景
triton 目前官方只有Linux 版本,若未安裝,則會(huì)出現(xiàn)報(bào)錯(cuò):
ModuleNotFoundError: No module named 'triton'
在 Windows 系統(tǒng)中,如果直接用 pip install triton
來安裝,則會(huì)報(bào)錯(cuò):
ERROR: Could not find a version that satisfies the requirement triton (from versions: none)
ERROR: No matching distribution found for triton
解決方法
方法一:(治標(biāo)不治本)
有大神強(qiáng)行在Windows平臺上編譯了 triton 的whl,參考博客【window平臺安裝 triton】【Python|Windows 系統(tǒng)安裝 triton 的方法】,在下載路徑下 pip install 安裝 whl 文件即可。
即直接去 HuggingFace 上下載 triton 的 Windows 包:https://hf-mirror.com/madbuda/triton-windows-builds。
在此給出各個(gè)版本的下載鏈接:
- 【triton 2.0.0 (Python 3.10)】
- 【triton 2.1.0 (Python 3.10)】【triton 2.1.0 (Python 3.11)】
- 【triton 3.0.0(Python 3.10)】【triton 3.0.0(Python 3.11)】【triton 3.0.0(Python 3.12)】
但是,實(shí)測上述安裝包里面 triton 核心的 triton.jit
和 torch.compile
等功能均無法像Linux下正常運(yùn)行,上述安裝包只是從形式上完成編譯。
方法二:(triton-windows)
主要參考大佬的工作:triton-windows。
環(huán)境要求:
torch >= 2.4.0
;CUDA >=12
;- 安裝
MSVC
和Windows SDK
; - 環(huán)境需要有
msvcp140.dll
和vcruntime140.dll
。如果 - 然后就可以安裝他編譯的 whl,實(shí)現(xiàn)真正的功能。
- 安裝 MSVC 和 Windows SDK
參考博客:【W(wǎng)indows 如何僅安裝 MSVC 而不安裝 Visual Studio】
-
下載 Visual Studio Installer,下載地址為:https://aka.ms/vs/17/release/vs_BuildTools.exe。
-
運(yùn)行下載的exe,然后安裝單個(gè)組件:
注意只需要下載單個(gè)組件,無需安裝Visual Studio。 -
修改環(huán)境變量,請參考博客:【W(wǎng)indows 如何僅安裝 MSVC 而不安裝 Visual Studio】
位置為:系統(tǒng)高級設(shè)置-環(huán)境變量-系統(tǒng)變量
。注意修改版本號為你自己的版本- 選擇 Path ,添加:
C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.40.33807\bin\Hostx64\x64
C:\Program Files (x86)\Windows Kits\10\bin\10.0.20348.0\x64
- 添加 LIB,添加3條:
C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.40.33807\lib\x64; C:\Program Files (x86)\Windows Kits\10\Lib\10.0.20348.0\ucrt\x64; C:\Program Files (x86)\Windows Kits\10\Lib\10.0.20348.0\um\x64
- 添加 INCLUDE,添加6條:
C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.40.33807\include; C:\Program Files (x86)\Windows Kits\10\Include\10.0.20348.0\ucrt; C:\Program Files (x86)\Windows Kits\10\Include\10.0.20348.0\um; C:\Program Files (x86)\Windows Kits\10\Include\10.0.20348.0\winrt; C:\Program Files (x86)\Windows Kits\10\Include\10.0.20348.0\cppwinrt; C:\Program Files (x86)\Windows Kits\10\Include\10.0.20348.0\shared
- 選擇 Path ,添加:
-
驗(yàn)證 MSVC 和 Windows SDK 安裝成功
命令行里輸入cl
,輸出Microsoft (R) C/C++ Optimizing Compiler ...
即可。
- vcredist 安裝
vcredist 是必需的(也稱為“Visual C++ Redistributable for Visual Studio 2015-2022”,msvcp140.dll
,vcruntime140.dll
)。如果沒有,可以從 https://aka.ms/vs/17/release/vc_redist.x64.exe 中安裝。
- whl 安裝
前期環(huán)境都配置無誤后,直接下載 whl 安裝:
pip install https://github.com/woct0rdho/triton-windows/releases/download/v3.1.0-windows.post5/triton-3.1.0-cp310-cp310-win_amd64.whl
也可手動(dòng)下載下來然后在下載路徑下安裝:
pip install triton-3.1.0-cp310-cp310-win_amd64.whl
- 驗(yàn)證
驗(yàn)證腳本為:
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_SIZEoffsets = block_start + tl.arange(0, BLOCK_SIZE)mask = offsets < n_elementsx = tl.load(x_ptr + offsets, mask=mask)y = tl.load(y_ptr + offsets, mask=mask)output = x + ytl.store(output_ptr + offsets, output, mask=mask)def add(x: torch.Tensor, y: torch.Tensor):output = torch.empty_like(x)assert x.is_cuda and y.is_cuda and output.is_cudan_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 outputa = torch.rand(3, device="cuda")
b = a + a
b_compiled = add(a, a)
print(b_compiled - b)
print("If you see tensor([0., 0., 0.], device='cuda:0'), then it works")
不報(bào)錯(cuò)即說明配置成功。