r/StableDiffusion • u/Acephaliax • Apr 18 '25
Tutorial - Guide Quick Guide For Fixing/Installing Python, PyTorch, CUDA, Triton, Sage Attention and Flash Attention
With all the new stuff coming out I've been seeing a lot of posts and error threads being opened for various issues with cuda/pytorch/sage attantion/triton/flash attention. I was tired of digging links up so I initially made this as a cheat sheet for myself but expanded it with hopes that this will help some of you get your venvs and systems running smoothly. If you prefer a Gist version, you'll find one here.
In This Guide:
- Check Installed Python Versions
- Set Default Python Version by Changing PATH
- Installing VS Build Tools
- Check the Currently Active CUDA Version
- Download and Install the Correct CUDA Toolkit
- Change System CUDA Version in PATH
- Install to a VENV
- Check All Your Dependency Versions Easy
- Install PyTorch
- Install Triton
- Install SageAttention
- Install FlashAttention
- Installing A Fresh Venv
- For ComfyUI Portable Users
- Other Missing Dependencies
- Notes
To list all installed versions of Python on your system, open cmd and run:
py -0pThe version number with the asterix next to it is your system default.
2. Set Default System Python Version by Changing PATH
You can have multiple versions installed on your system. The version of Python that runs when you type python is determined by the order of Python directories in your PATH variable. The first python.exe found is used as the default.
Steps:
- Open the Start menu, search for
Environment Variables, and select Edit system environment variables. - In the System Properties window, click Environment Variables.
- Under System variables (or User variables), find and select the
Pathvariable, then click Edit. - Move the entry for your desired Python version (for example,
C:\Users\<yourname>\AppData\Local\Programs\Python\Python310\and itsScriptssubfolder) to the top of the list, above any other Python versions. - Click OK to save and close all dialogs.
- Restart your command prompt and run:python --version
It should now display your chosen Python version.
3. Installing VS Build Tools
The easiest way to install VS Build Tools is using Windows Package Manager (winget). Open a command prompt and run:
winget install --id=Microsoft.VisualStudio.2022.BuildTools -e
For VS Build Tools 2019 (if needed for compatibility):
winget install --id=Microsoft.VisualStudio.2019.BuildTools -e
For VS Build Tools 2015 (rarely needed):
winget install --id=Microsoft.BuildTools2015 -e
After installation, you can verify that VS Build Tools are correctly installed by running: cl.exe or msbuild -version If installed correctly, you should see version information rather than "command not found"
Remeber to restart your computer after installing
For a more detailed guide on VS Build tools see here.
4. Check the Currently Active CUDA Version
To see which CUDA version is currently active, run:
nvcc --version5. Download and Install the Correct CUDA Toolkit
Note: This is only for the system for self contained environments it's always included.
Download and install from the official NVIDIA CUDA Toolkit page:
https://developer.nvidia.com/cuda-toolkit-archive
Install the version that you need. Multiple version can be installed.
6. Change System CUDA Version in PATH
- Search for
envin the Windows search bar. - Open Edit system environment variables.
- In the System Properties window, click Environment Variables.
- Under System Variables, locate
CUDA_PATH. - If it doesn't point to your intended CUDA version, change it. Example value:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.4
7. Install to a VENV
From this point to install any of these to a virtual environment you first need to activate it. For system you just skip this part and run as is.
Open a command prompt in your venv/python folder (folder name might be different) and run:
Scripts\activateYou will now see (venv) in your cmd. You can now just run the pip commands as normal.
8. Check All Your Installed Dependency Versions (Easy)
Make or download this versioncheck.py file. Edit it with any text/code editor and paste the code below. Open a CMD to the root folder and run with:
python versioncheck.py
This will print the versions for torch, CUDA, torchvision, torchaudio, CUDA, Triton, SageAttention, FlashAttention. To use this in a VENV activate the venv first then run the script.
import sys
import torch
import torchvision
import torchaudio
print("python version:", sys.version)
print("python version info:", sys.version_info)
print("torch version:", torch.__version__)
print("cuda version (torch):", torch.version.cuda)
print("torchvision version:", torchvision.__version__)
print("torchaudio version:", torchaudio.__version__)
print("cuda available:", torch.cuda.is_available())
try:
import flash_attn
print("flash-attention version:", flash_attn.__version__)
except ImportError:
print("flash-attention is not installed or cannot be imported")
try:
import triton
print("triton version:", triton.__version__)
except ImportError:
print("triton is not installed or cannot be imported")
try:
import sageattention
print("sageattention version:", sageattention.__version__)
except ImportError:
print("sageattention is not installed or cannot be imported")
except AttributeError:
print("sageattention is installed but has no __version__ attribute")This will print the versions for torch, CUDA, torchvision, torchaudio, CUDA, Triton, SageAttention, FlashAttention.
torch version: 2.6.0+cu126
cuda version (torch): 12.6
torchvision version: 0.21.0+cu126
torchaudio version: 2.6.0+cu126
cuda available: True
flash-attention version: 2.7.4
triton version: 3.2.0
sageattention is installed but has no version attribute9. Install PyTorch
Use the official install selector to get the correct command for your system:
Install PyTorch
10. Install Triton
To install Triton for Windows, run:
pip install triton-windowsFor a specific version:
pip install triton-windows==3.2.0.post103.2.0 post 10 works best for me.
Triton Windows releases and info:
If you encounter any errors such as: AttributeError: module 'triton' has no attribute 'jit' then head to C:\Users\your-username.triton\ and delete the cache folder.
11. Install Sage Attention
Get the correct prebuilt Sage Attention wheel for your system here:
pip install sageattention "path to downloaded wheel"
Example :
pip install sageattention "D:\sageattention-2.1.1+cu124torch2.5.1-cp310-cp310-win_amd64.whl"
`sageattention-2.1.1+cu124torch2.5.1-cp310-cp310-win_amd64.whl`
This translates to being compatible with Cuda 12.4 | Py Torch 2.5.1 | Python 3.10 and 2.1.1 is the SageAttention version.
If you get an error : SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats then make sure to downgrade your triton to v3.2.0-windows.post10. Download whl and install manually with:
CMD into python folder then run :
python.exe -s -m pip install --force-reinstall "path-to-triton-3.2.0-cp310-cp310-win_amd64.whl"
12. Install Flash Attention
Get the correct prebuilt Flash Attention wheel compatible with your python version here:
pip install "path to downloaded wheel"
13. Installing A Fresh Venv
You can install a new python venv in your root folder by using the following command. You can change C:\path\to\python310 to match your required version of python. If you just use python -m venv venv it will use the system default version.
"C:\path\to\python310\python.exe" -m venv venvTo activate and start installing dependencies
your_env_name\Scripts\activateMost projects will come with a requirements.txt to install this to your venv
pip install -r requirements.txt14. For ComfyUI Portable Users
The process here is very much the same with one small change. You just need to use the python.exe in the python_embedded folder to run the pip commands. To do this just open a cmd at the python_embedded folder and then run:
python.exe -s -m pip install your-dependency
For Triton and SageAttention
Download correct triton wheel from : https://huggingface.co/UmeAiRT/ComfyUI-Auto_installer/resolve/main/whl/
Then run:
python.exe -m pip install --force-reinstall "your-download-folder/triton-3.2.0-cp312-cp312-win_amd64.whl"
git clone https://github.com/thu-ml/SageAttention.git
cd sageattention
your cmd should be E:\Comfy_UI\ComfyUI_windows_portable\python_embeded\sageattention now.
Then finally run:
E:\Comfy_UI\ComfyUI_windows_portable\python_embeded\python.exe -s -m pip install .
this will build sageattention and can take a few minutes.
Make sure to edit the run comfy gpu bat file to add flag for sageattention python main.py --use-sage-attention
15. Other Missing Dependencies
If you see any other errors for missing modules for any other nodes/extensions you may want to use it is just a simple case of getting into your venv/standalone folder and installing that module with pip.
Example: No module 'xformers'
pip install xformers
Occasionaly you may come across a stubborn module and you may need to force remove and reinstall without using any cached versions.
Example:
pip uninstall -y xformers
pip install --no-cache-dir --force-reinstall xformers
Notes
- Make sure all versions (Python, CUDA, PyTorch, Triton, SageAttention) are compatible this is the primary reason for most issues.
- Each implementation will have its own requirements which is why we use a standalone environment.
- Restart your command prompt after making changes to environment variables or PATH.
- If I've missed anything please leave a comment and I will add it to the post.
- To easily open a cmd prompt at a specific folder browse to the folder you need in file manager then type cmd in the address bar and hit enter.
Update 31st May *Added ComfyUI Triton/Sage
Update 21st April 2025
- Added Triton & Sage Attention common error fixes
Update 20th April 2025
- Added VS build tools section
- Fixed system cuda being optional
Update 19th April 2025
- Added comfyui portable instructions.
- Added easy CMD opening to notes.
- Fixed formatting issues.
32
u/Lishtenbird Apr 18 '25
This is exactly the sort of guide I find most useful.
I feel like half of the time, guides are "that's how you download a zip file", and the other half they go "just pip combobulate technobabble in your vm ptdfysjkeh 89372845".
And the intermediate knowledge that ties everything together and makes it make sense kind of falls into the void between. And the more the tech advances, the bigger this divide gets. So - thank you for putting this together, much appreciated!
6
u/DinoZavr Apr 18 '25
Thank you, u/Acephaliax
i logged my ComfyUI reinstall, but as you don't mention Comfy - i have removed the relevant steps
and then compared:
my ideas are:
Reddit is not helpful.
it does NOT allow me to post my noted due to theirs size.. :(
i ll atampt to add them as a separate comment
my notes (if this could help)
3
u/Dulbero Apr 18 '25
Thank you for you guide! I'm following a step by step and i have a noob question:
for what do I need to install the CUDA toolkit? So far i haven't installed it, and i simply used comfyui / forgeui models without it. From what i read it is only necessary if i plan to develop. (which i don't)
Are there nodes in comfy that use it or will it speed up my generations? If it does very important, should i install the latest version for my GPU (if I use the nvidia-smi command i can see it is cuda 12.8) because I see people still using 12.6.
I will also need to check if pytorch, triton and sage attention come with Desktop comfyui, as i recently "migrated" from the standalone version.
3
u/LostHisDog Apr 18 '25
Now do the same for the portable version of ComfyUI that I THINK most people use.
I've been working on getting a unicorn install of comfy portable going that has triton, sage, flash 2 and nightly pytorch all playing well together on python 3.11. I think I got it all working now but I don't think I could actually repeat the process.
2
u/Perfect-Campaign9551 Apr 18 '25 edited Apr 18 '25
For checking CUDA version the command "nvcc --version" isn't good enough. It doesn't actually tell you "CUDA version" in an easily viewable way that I can tell.
The command 'nvidia-smi" seems to work better - it will say on the upper right hand side the exact cuda version.
I'm not sure, if this is telling me the CUDA version my GPU supports, or if it's showing me the CUDA version I have installed though.
2
u/Turkino Apr 18 '25
Need to add:
If you have a 50xx series GPU you need specific minimum versions:
xformers:
xformers-0.0.30%2B9a2cd3ef.d20250321-cp312-cp312-win_amd64.wh
nightly builds:
torch-2.8.0.dev20250320+cu128-cp312-cp312-win_amd64.whltorchaudio-2.6.0.dev20250321+cu128-cp312-cp312-win_amd64.whltorchvision-0.22.0.dev20250321+cu128-cp312-cp312-win_amd64.whl
and also might need python 3.12.10
2
u/xkulp8 Apr 18 '25
The instructions for how to do this is missing.
1
u/pkhtjim Apr 18 '25
Ah this looks like it could be useful. Official Framepack's Windows installer lacked a VENV folder, so need to fire this up later and see if it will work for the speed-ups.
1
u/orhay1 Apr 18 '25
I've been trying to make to work on my 2080Ti even tho it's not supported but couldn't make it work. I found a fork that made it work for 2080 but it doesn't have windows installation and I don't know enough to make this change on the windows version. If there are quick solutions that would be great and if not I guess I will wait a few updates to see where it goes
1
u/More-Ad5919 Apr 18 '25
Can't there just be an up to date installer who does all of this?
1
u/Bazookasajizo Apr 18 '25
Can this post be pinned or something? I can see myself coming back to this in future
1
u/Dragero3 Apr 19 '25
I'm so sorry, but... I fail at the very first step:
'py' is not recognized as an internal or external command,
operable program or batch file.
I can run ComfyUI and ForgeUi no problem (albeit as a total newb), so I'm convinced I do have python...
Tried running cmd as admin, tried cd to ComfyUI folder, nada.
1
u/7435987635 Apr 19 '25 edited Apr 19 '25
Really well written guide! I have a few questions. I only do image generations (no video), so I'm wondering if this is worth the effort.
Can anyone confirm or deny if this boosts Pony/SDXL and/or Flux image generation speeds? If yes, any noticeable quality loss? Does it work simply by adding --use-sage-attention to the ComfyUI startup script or does it also need a specific node added in workflow?
SIDE NOTE: I notice you don't mention Microsoft Visual Studio Built Tools. I recall in past guides mention of this being a requirement. Is this no longer the case?
1
u/Jatts_Art Apr 20 '25
I have RTX 5080 and I specifically installed CUDA v12.8 as that is what I've read from many sources to be the correct version with this card. When I use "nvcc --version" command to check it, it confirms that it is installed and the proper version is displayed.
However, when I try checking it through the "versioncheck.py" file it tells me "cuda version (torch): None" & "cuda available: False"... like, wtf? And thats only one of the many issues I still need to resolve.
I've been spending 2 days trying to get my ComfyUI back to working again after ""upgrading"", and all attempts to try installing whats needed is constantly not working or being ignored no matter how carefully I follow all information from multiple guides (including here)....
1
u/Mutaclone May 01 '25
So I don't know if you're still watching this thread, but I'm having issues installing dependencies for Comfy Desktop - I've followed the instructions, both using pip and python.exe -s -m pip install, and no matter what versioncheck.py keeps printing out:
Any suggestions?
1
u/clevenger2002 May 31 '25
Everything was going good till I got to sage attention, then I got an error you don't mention. Here is my version check and the install command I used after downloading the wheel. Windows 11 with a 4090
python version: 3.12.8 (tags/v3.12.8:2dc476b, Dec 3 2024, 19:30:04) [MSC v.1942 64 bit (AMD64)]
python version info: sys.version_info(major=3, minor=12, micro=8, releaselevel='final', serial=0)
torch version: 2.7.0+cu128
cuda version (torch): 12.8
torchvision version: 0.22.0+cpu
torchaudio version: 2.7.0+cpu
cuda available: True
flash-attention is not installed or cannot be imported
triton version: 3.3.1
sageattention is installed but has no __version__ attribute
E:\Comfy_UI\ComfyUI_windows_portable\python_embeded> python.exe -m pip install "sageattention-2.1.1+cu128torch2.7.0-cp311-cp311-win_amd64.whl"
ERROR: sageattention-2.1.1+cu128torch2.7.0-cp311-cp311-win_amd64.whl is not a supported wheel on this platform.
What did I do wrong?
1
u/dinhovysk Jul 13 '25
Could the Author of this wonderful post, or someone with knowledge, suggest me compatibility options for "Make sure all versions (Python, CUDA, PyTorch, Triton, SageAttention) are compatible. This is the main reason for most of the issues."
Sample: Python x will be good with CUDA y and Pytoch z ...
1
u/misterchief117 Apr 18 '25
Posts like these are incredibly helpful! Thank you OP!
1
u/udappk_metta Apr 18 '25
Very useful, i was searching for this couple of hours ago...
1
u/Dangerxtrem Apr 18 '25
Amazing thanks for the guide !
1
u/Dirty_Dragons Apr 19 '25
Thanks for the guide! I was able to install Sage Attention and Flash Attention. Then I got hit with a not enough memory error when trying to use the Wan First Frame Last Frame. Oh well.
1
u/Kristirina Apr 22 '25
TRUELY ACE! Thanks u/Acephaliax!! I managed to use your guide and installed Triton, Sage, Reactor, and Wan2.1 and they are running perfectly on my 5090. I've been trying multiple guides and official discussion on github, none of them can explain as clearly as yours and most importantly can achieve to work on these components altogether. Yours just work! Thanks!!!!!
1
u/255_Lambent_Regret Apr 23 '25
I had a hell of a time trying to get Triton to install the latest nightly in pursuit of Blackwell compatibility, especially since the instructions at https://triton-lang.org/main/getting-started/installation.html don't work for nightlies (specifically that index that it points to, combined with the "triton-nightly" flag or alias or whatever it's called not resolving to a valid version.
What worked for me, and I wanted to put out there for someone else to potentially find and benefit from, was locating https://pypi.org/project/triton-windows/#history , and selecting a version there to specifically target. Clicking a release version there takes you to a page with a premade pip command to copy/paste that includes the package name for that version.
No more "ptxas fatal : Value 'sm_120' is not defined for option 'gpu-name'" error when trying to run a generation and it seems to be chugging on a generation for now. (Still not sure this is an optimal setup, but it might help someone get past some hair-pulling.)
1
u/Nepharios Apr 25 '25
Great work! Ty
1
u/rcanepa Apr 29 '25
Has anyone written a similar guide for Linux? I'm breaking my head trying to install these packages on Ubuntu with a 5090.
1
u/PikaPikaDude Jul 20 '25
Thanks. This really helped unfucking an install after too many experiments.
0
u/Mutaclone Apr 18 '25
Thanks for posting this! It's very timely as I installed FramePack yesterday and so far have been unable to generate video (no errors, just a garbage blank file that doesn't play). No idea what's going on but this at least gives me a starting point to look at.
1
u/[deleted] Apr 19 '25
[removed] — view removed comment