Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Last active April 22, 2025 01:49
Show Gist options
  • Save Acephalia/7a1988fc34f6fccffa2198721351fbc1 to your computer and use it in GitHub Desktop.
Save Acephalia/7a1988fc34f6fccffa2198721351fbc1 to your computer and use it in GitHub Desktop.
Quick Guide For Fixing/Installing Python, PyTorch, CUDA, Triton, Sage Attention and Flash Attention For Local AI Image Generation

Quick Guide For Fixing/Installing Python, PyTorch, CUDA, Triton, Sage Attention and Flash Attention For Local AI Image Generation

With all the new local image & video generation tools 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.

In This Guide:

  1. Check Installed Python Versions
  2. Set Default Python Version by Changing PATH
  3. Installing VS Build Tools
  4. Check the Currently Active CUDA Version
  5. Download and Install the Correct CUDA Toolkit
  6. Change System CUDA Version in PATH
  7. Install to a VENV
  8. Check All Your Dependency Versions Easy
  9. Install PyTorch
  10. Install Triton
  11. Install SageAttention
  12. Install FlashAttention
  13. Installing A Fresh Venv
  14. For ComfyUI Portable Users
  15. Other Missing Dependencies
  16. Notes

To list all installed versions of Python on your system, open cmd and run:

py -0p

The 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:

  1. Open the Start menu, search for Environment Variables, and select Edit system environment variables.
  2. In the System Properties window, click Environment Variables.
  3. Under System variables (or User variables), find and select the Path variable, then click Edit.
  4. Move the entry for your desired Python version (for example, C:\Users\<yourname>\AppData\Local\Programs\Python\Python310\ and its Scripts subfolder) to the top of the list, above any other Python versions.
  5. Click OK to save and close all dialogs.
  6. 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 --version

5. 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

  1. Search for env in the Windows search bar.
  2. Open Edit system environment variables.
  3. In the System Properties window, click Environment Variables.
  4. Under System Variables, locate CUDA_PATH.
  5. 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\activate

You 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 attribute

9. 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-windows

For a specific version:

pip install triton-windows==3.2.0.post10

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 wheel 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 venv

To activate and start installing dependencies

your_env_name\Scripts\activate

Most projects will come with a requirements.txt to install this to your venv

pip install -r requirements.txt

14. 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

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 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment