|
| 1 | +# SpargeAttn - AMD ROCm on Windows Setup Guide |
| 2 | + |
| 3 | +This guide explains how to build and run SpargeAttn on Windows with AMD GPUs using ROCm. |
| 4 | + |
| 5 | +> **Note:** These steps should also work on Linux with minor modifications (use bash commands instead of PowerShell, `source venv/bin/activate` instead of `.\venv\Scripts\Activate.ps1`, and skip the Visual Studio environment setup). However, Linux support has not been tested yet and may have issues. |
| 6 | +
|
| 7 | +## Supported Hardware |
| 8 | + |
| 9 | +SpargeAttn on Windows has been tested with RDNA3/RDNA3.5 GPUs (gfx1100, gfx1101, gfx1102, gfx1103, gfx1151). |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +- Windows 10/11 |
| 14 | +- Python 3.11, 3.12, or 3.13 |
| 15 | +- Visual Studio 2022 with C++ build tools |
| 16 | +- AMD Adrenaline driver (latest recommended) |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +### 1. Install ROCm and PyTorch from TheRock |
| 21 | + |
| 22 | +Follow the instructions at [ROCm/TheRock RELEASES.md](https://github.com/ROCm/TheRock/blob/main/RELEASES.md) to install ROCm and PyTorch wheels for your GPU architecture. |
| 23 | + |
| 24 | +#### Create a Virtual Environment |
| 25 | + |
| 26 | +```powershell |
| 27 | +python -m venv venv |
| 28 | +.\venv\Scripts\Activate.ps1 |
| 29 | +``` |
| 30 | + |
| 31 | +#### Install ROCm SDK and PyTorch |
| 32 | + |
| 33 | +For **gfx1151** (AMD Strix Halo iGPU): |
| 34 | +```powershell |
| 35 | +pip install --index-url https://rocm.nightlies.amd.com/v2/gfx1151/ --pre rocm-sdk[devel] |
| 36 | +pip install --index-url https://rocm.nightlies.amd.com/v2/gfx1151/ --pre torch torchaudio torchvision |
| 37 | +``` |
| 38 | + |
| 39 | +For **gfx110X** (RX 7900 XTX, RX 7800 XT, RX 7700S, Radeon 780M): |
| 40 | +```powershell |
| 41 | +pip install --index-url https://rocm.nightlies.amd.com/v2/gfx110X-all/ --pre rocm-sdk[devel] |
| 42 | +pip install --index-url https://rocm.nightlies.amd.com/v2/gfx110X-all/ --pre torch torchaudio torchvision |
| 43 | +``` |
| 44 | + |
| 45 | +For **gfx120X** (RX 9060, RX 9070): |
| 46 | +```powershell |
| 47 | +pip install --index-url https://rocm.nightlies.amd.com/v2/gfx120X-all/ --pre rocm-sdk[devel] |
| 48 | +pip install --index-url https://rocm.nightlies.amd.com/v2/gfx120X-all/ --pre torch torchaudio torchvision |
| 49 | +``` |
| 50 | + |
| 51 | +#### Initialize ROCm SDK |
| 52 | + |
| 53 | +```powershell |
| 54 | +rocm-sdk init |
| 55 | +``` |
| 56 | + |
| 57 | +### 2. Set Environment Variables |
| 58 | + |
| 59 | +Open a PowerShell terminal and run: |
| 60 | + |
| 61 | +```powershell |
| 62 | +# Activate Visual Studio environment |
| 63 | +cmd /c '"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" >nul 2>&1 && set' | ForEach-Object { if ($_ -match '^([^=]+)=(.*)$') { [System.Environment]::SetEnvironmentVariable($matches[1], $matches[2], 'Process') } } |
| 64 | +
|
| 65 | +# Activate the virtual environment |
| 66 | +.\venv\Scripts\Activate.ps1 |
| 67 | +
|
| 68 | +# Set ROCm paths using rocm-sdk |
| 69 | +$ROCM_ROOT = (rocm-sdk path --root).Trim() |
| 70 | +$ROCM_BIN = (rocm-sdk path --bin).Trim() |
| 71 | +$env:ROCM_HOME = $ROCM_ROOT |
| 72 | +$env:PATH = "$ROCM_ROOT\lib\llvm\bin;$ROCM_BIN;$env:PATH" |
| 73 | +
|
| 74 | +# Set compiler and build settings |
| 75 | +$env:CC = "clang-cl" |
| 76 | +$env:CXX = "clang-cl" |
| 77 | +$env:DISTUTILS_USE_SDK = "1" |
| 78 | +
|
| 79 | +# Enable experimental features |
| 80 | +$env:FLASH_ATTENTION_TRITON_AMD_ENABLE = "TRUE" |
| 81 | +$env:TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL = "1" |
| 82 | +``` |
| 83 | + |
| 84 | +### 3. Build and Install SpargeAttn |
| 85 | + |
| 86 | +```powershell |
| 87 | +cd <path_to_spargeattn> |
| 88 | +pip install --no-build-isolation -v . |
| 89 | +``` |
| 90 | + |
| 91 | +## Testing |
| 92 | + |
| 93 | +### Quick Correctness Test |
| 94 | + |
| 95 | +Run this script to verify SpargeAttn is working correctly by comparing against PyTorch SDPA: |
| 96 | + |
| 97 | +```python |
| 98 | +import torch |
| 99 | +import torch.nn.functional as F |
| 100 | +from spas_sage_attn.core import spas_sage_attn_meansim_cuda |
| 101 | + |
| 102 | +device = torch.device('cuda') |
| 103 | + |
| 104 | +# Create random test tensors (use float16 for ROCm compatibility) |
| 105 | +q = torch.randn(1, 12, 2048, 128, dtype=torch.float16, device=device) |
| 106 | +k = torch.randn(1, 12, 2048, 128, dtype=torch.float16, device=device) |
| 107 | +v = torch.randn(1, 12, 2048, 128, dtype=torch.float16, device=device) |
| 108 | + |
| 109 | +# Compute reference output using PyTorch SDPA |
| 110 | +with torch.no_grad(): |
| 111 | + sdpa = F.scaled_dot_product_attention(q.float(), k.float(), v.float()).to(torch.float16) |
| 112 | + |
| 113 | +# Compute SpargeAttn output (with 100% sparsity = dense attention) |
| 114 | +sparge = spas_sage_attn_meansim_cuda( |
| 115 | + q, k, v, |
| 116 | + is_causal=False, |
| 117 | + smooth_k=False, |
| 118 | + simthreshd1=0.0, # No similarity threshold (keep all blocks) |
| 119 | + cdfthreshd=1.0, # 100% sparsity |
| 120 | + pvthreshd=0, |
| 121 | + tensor_layout='HND' |
| 122 | +) |
| 123 | + |
| 124 | +# Compare outputs using cosine similarity |
| 125 | +cos = F.cosine_similarity( |
| 126 | + sdpa.flatten().float().unsqueeze(0), |
| 127 | + sparge.flatten().float().unsqueeze(0) |
| 128 | +) |
| 129 | +print(f'Cosine similarity: {cos.item():.6f}') # Should be ~0.9999 |
| 130 | +``` |
| 131 | + |
| 132 | +Save this as `test_spargeattn.py` and run: |
| 133 | + |
| 134 | +```powershell |
| 135 | +python test_spargeattn.py |
| 136 | +``` |
| 137 | + |
| 138 | +Expected output: |
| 139 | +``` |
| 140 | +Cosine similarity: 0.999900 |
| 141 | +``` |
| 142 | + |
| 143 | +A cosine similarity above 0.999 indicates the kernel is working correctly. |
| 144 | + |
| 145 | +## Performance Notes |
| 146 | + |
| 147 | +At L=4096, D=128, bf16 vs PyTorch SDPA (with aotriton): |
| 148 | + |
| 149 | +| Sparsity | Time | Speedup vs SDPA | |
| 150 | +|----------|------|-----------------| |
| 151 | +| 100% | 33.0 ms | 0.18x | |
| 152 | +| 50% | 13.7 ms | 0.43x | |
| 153 | +| 25% | 7.4 ms | 0.79x | |
| 154 | +| **10%** | **3.2 ms** | **1.81x** | |
| 155 | +| 5% | 1.8 ms | 3.26x | |
| 156 | +| 2% | 1.0 ms | 6.07x | |
| 157 | + |
| 158 | +**Break-even point**: ~20-25% sparsity. Below that, SpargeAttn is faster than dense SDPA. |
| 159 | + |
| 160 | +## Known Issues |
| 161 | + |
| 162 | +1. **No FP8 support on RDNA3** - rocWMMA on gfx11xx doesn't support FP8, so FP16/BF16 is used for V. |
| 163 | + |
| 164 | +2. **Triton compiler warnings** - You may see `clang-cl: warning: unknown argument ignored` warnings during first run. These are harmless. |
| 165 | + |
| 166 | +## Troubleshooting |
| 167 | + |
| 168 | +### "LoadLibrary failed" or "cannot find amdhip64.dll" |
| 169 | + |
| 170 | +Make sure you ran `rocm-sdk init` after installing the ROCm SDK packages. |
| 171 | + |
| 172 | +### "LINK : fatal error LNK1104: cannot open file 'python312.lib'" |
| 173 | + |
| 174 | +Ensure Visual Studio environment is activated before building: |
| 175 | +```powershell |
| 176 | +cmd /c '"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" >nul 2>&1 && set' | ForEach-Object { if ($_ -match '^([^=]+)=(.*)$') { [System.Environment]::SetEnvironmentVariable($matches[1], $matches[2], 'Process') } } |
| 177 | +``` |
| 178 | + |
| 179 | +### "PermissionError" when compiling Triton kernels |
| 180 | + |
| 181 | +This is a known Windows issue with temp file handling. Make sure you're using the latest triton-windows package with AMD Windows support patches. PR is currently WIP - https://github.com/woct0rdho/triton-windows/pull/179 |
| 182 | + |
0 commit comments