---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://framesfield.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
title: "Fix ComfyUI PyTorch CUDA, xFormers & SageAttention Errors"
description: "Audit your exact Python + PyTorch + CUDA + GPU sm_arch triplet first, uninstall conflicting xformers/triton/sageattention wheels, reinstall clean PyTorch (+cu128 for RTX 50xx or +cu124/+cu128 for RTX 30/40xx) using native --use-pytorch-cross-attention, and only add version-matched triton-windows + sageattention after SDPA renders cleanly."
canonical: "https://framesfield.com/fixes/comfyui-windows-xformers-triton-and-sageattention-fix"
---

# Fix ComfyUI PyTorch CUDA, xFormers & SageAttention Errors

> Audit your exact Python + PyTorch + CUDA + GPU sm_arch triplet first, uninstall conflicting xformers/triton/sageattention wheels, reinstall clean PyTorch (+cu128 for RTX 50xx or +cu124/+cu128 for RTX 30/40xx) using native --use-pytorch-cross-attention, and only add version-matched triton-windows + sageattention after SDPA renders cleanly.

- **Canonical URL:** https://framesfield.com/fixes/comfyui-windows-xformers-triton-and-sageattention-fix
- **Category:** ComfyUI & Local GPU
- **Error Signature:** `ImportError: DLL load failed while importing _C (xformers) | AssertionError: Torch not compiled with CUDA enabled | RuntimeError: Cannot find a working triton installation | CUDA error: no kernel image is available (RTX 50xx sm_120)`
- **Last Verified:** 2026-12-01

## Observed Symptom

After installing a custom node or attempting to speed up Wan 2.2 / FLUX with xFormers, Triton, or SageAttention on Windows, ComfyUI refuses to start—throwing xformers _C DLL errors, claiming PyTorch has no CUDA support, failing Triton JIT compilation with missing Python.h, or crashing on RTX 5070/5080/5090 cards with no kernel image available.

## Technical Root Cause

Four Windows ABI collisions break GPU acceleration: (1) a custom node requirements.txt silently overwrote torch+cu124/cu128 with PyPI CPU-only torch; (2) xformers/sageattention binary wheels were compiled for a different Python+PyTorch+CUDA triplet; (3) Windows requires triton-windows plus MSVC and python_embeded include/libs headers rather than Linux triton; and (4) RTX 50-series Blackwell GPUs (sm_120) strictly require CUDA 12.8 (cu128) PyTorch builds.

## Under the Hood

The fastest way to break a healthy Windows ComfyUI Portable installation is blindly running pip install xformers, pip install sageattention, or pip install -r requirements.txt from an unvetted custom node. When a custom node lists bare torch without a CUDA index URL, pip happily replaces your working torch+cu124 or torch+cu128 wheel with PyPI default CPU-only PyTorch—instantly causing AssertionError: Torch not compiled with CUDA enabled on next launch. Similarly, precompiled xformers and sageattention .whl binaries link hard against one exact Python minor version, PyTorch version, and CUDA ABI; drifting by even a single minor PyTorch update causes ImportError: DLL load failed while importing _C.

Crucially, most ComfyUI users do NOT need xFormers at all in 2026. PyTorch 2.4+ ships built-in Scaled Dot-Product Attention (SDPA, activated automatically or via --use-pytorch-cross-attention), which natively dispatches FlashAttention-2 and Memory-Efficient Attention kernels with zero external DLLs and zero upgrade breakage. Uninstalling broken xformers/sageattention packages and restoring the clean CUDA-matched PyTorch triplet gets 95% of broken workstations rendering again in three minutes.

When you genuinely want SageAttention + torch.compile speedups for heavy Wan 2.2 or HunyuanVideo local batches on Windows, every prerequisite must match cleanly: (1) install CUDA 12.8 PyTorch (cu128) for RTX 50-series Blackwell GPUs (compute capability 12.0 / sm_120) or cu124/cu128 for RTX 30/40-series; (2) install triton-windows (never Linux triton) matched to your Python version alongside Visual Studio C++ Build Tools; and (3) copy the matching Python include/ and libs/ directories into ComfyUI_windows_portable\python_embeded\ so Triton C-extension compilation finds Python.h without error.

## Before vs. After

### Broken (Broken Windows Commands (Overwrites CUDA Torch + Installs Linux Triton))

```
# Bare pip overwrites torch+cu128 with CPU-only PyTorch & fails on Linux-only triton!
pip install torch xformers triton sageattention
python main.py --use-sage-attention   # -> DLL load failed / Torch not compiled with CUDA
```

### Verified Fix (Clean Recovery (Exact python_embeded + cu128 PyTorch + Zero-DLL Native SDPA))

```
# Run from ComfyUI_windows_portable root (for RTX 50xx Blackwell & RTX 30/40xx):
.\python_embeded\python.exe -s -m pip uninstall -y xformers triton triton-windows sageattention torch torchvision torchaudio
.\python_embeded\python.exe -s -m pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128

# Boot reliably using built-in PyTorch 2.x Flash/SDPA (zero xformers DLLs required!):
.\python_embeded\python.exe -s ComfyUI\main.py --use-pytorch-cross-attention --windows-standalone-build
```

Purging mismatched C++ extensions, restoring an explicit CUDA-indexed PyTorch wheel (cu128 for Blackwell sm_120), and defaulting to native PyTorch SDPA eliminates DLL conflicts immediately.

## Copy-Paste Fix (fix-comfyui-windows-cuda-sage.ps1)

```bash
# Run inside PowerShell from your ComfyUI_windows_portable folder (or swap $PY for venv\Scripts\python.exe)
$PY = ".\python_embeded\python.exe"

# 1. Print exact current Python, PyTorch, CUDA runtime, and GPU architecture (sm_86/89/120)
& $PY -s -c "import sys, torch; print('Py:', sys.version.split()[0], '| Torch:', torch.__version__, '| CUDA avail:', torch.cuda.is_available(), '| CUDA:', torch.version.cuda, '| GPU:', torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'NONE', '| Cap:', torch.cuda.get_device_capability(0) if torch.cuda.is_available() else 'NONE')"

# 2. CLEAN RECOVERY (fixes CPU-only torch overwrite, xformers _C DLL errors, and RTX 50xx sm_120 crashes)
& $PY -s -m pip uninstall -y xformers triton triton-windows sageattention torch torchvision torchaudio
# Use cu128 for RTX 5070/5080/5090 (Blackwell sm_120) AND modern RTX 30/40 series (requires NVIDIA Driver >= 570):
& $PY -s -m pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128

# 3. Baseline stable launch using native PyTorch SDPA (no xformers needed!)
# & $PY -s ComfyUI\main.py --use-pytorch-cross-attention --windows-standalone-build

# 4. OPTIONAL (Only after step 3 renders cleanly AND you installed MSVC C++ Build Tools + copied Python include/ & libs/ into python_embeded/):
# & $PY -s -m pip install -U "triton-windows<3.4"
# Install the EXACT sageattention .whl matching your printed Python + Torch + CUDA triplet, then launch with:
# & $PY -s ComfyUI\main.py --use-sage-attention --windows-standalone-build
```

## Step-by-Step Resolution

1. **Inspect your exact Python, PyTorch, CUDA, and GPU compute capability triplet:** Run the one-line probe using ComfyUI python_embeded\python.exe -s. If torch.__version__ ends in +cpu or lacks +cu124/+cu128, a custom node overwrote your GPU build; if your GPU is an RTX 50-series (capability 12,0), you strictly need +cu128 and NVIDIA Driver 570+.
2. **Purge broken xformers/triton/sageattention wheels and restore clean CUDA PyTorch:** Uninstall conflicting compiled attention extensions and reinstall torch, torchvision, and torchaudio explicitly from download.pytorch.org/whl/cu128. Boot ComfyUI with --use-pytorch-cross-attention to establish a fast, zero-DLL baseline.
3. **Add triton-windows and SageAttention only with exact ABI parity and Python headers:** If you need SageAttention for long Wan 2.2 video renders, copy the exact matching Python version include/ and libs/ folders into python_embeded/, install MSVC Build Tools + triton-windows, install a triplet-matched sageattention wheel, and test with --use-sage-attention.

## Verification Check (Confirm CUDA Active, Matching Architecture Kernel & Native SDPA Functional)

```bash
.\python_embeded\python.exe -s -c "import torch; assert torch.cuda.is_available(), 'NO CUDA'; x=torch.randn(1,4,64,64,device='cuda',dtype=torch.bfloat16); y=torch.nn.functional.scaled_dot_product_attention(x,x,x); print('OK:', torch.__version__, torch.cuda.get_device_name(0), torch.cuda.get_device_capability(0), y.shape)"
```

**Expected healthy output:**

```text
OK: 2.7.0+cu128 NVIDIA GeForce RTX 5080 (12, 0) torch.Size([1, 4, 64, 64])
```

## Prevention Guardrails

1. **Inspect custom node requirements.txt before installing and pin torch out:** Pass --no-deps or strip bare torch/xformers lines when installing community nodes manually so a 20-line utility script never downgrades your CUDA PyTorch runtime to CPU-only.
2. **Default to native PyTorch SDPA instead of legacy xFormers on modern GPUs:** PyTorch 2.x SDPA matches or beats xFormers speed on RTX 30/40/50 cards while surviving every ComfyUI update with zero third-party C++ DLL breakage.
3. **Keep a separate run_nvidia_sdpa.bat fallback next to run_nvidia_sage.bat:** Maintaining a clean --use-pytorch-cross-attention launch script ensures a PyTorch or driver update never stalls client deadlines while waiting for new SageAttention wheels.

## FAQs

### Do I still need xFormers in ComfyUI on an RTX 3090, 4090, or 5090?

No. PyTorch 2.x built-in Scaled Dot-Product Attention (--use-pytorch-cross-attention) already delivers FlashAttention-2 and memory-efficient attention natively without fragile xFormers DLL dependencies.

### Why does ComfyUI Portable throw "fatal error C1083: Cannot open include file: Python.h" when enabling Triton?

ComfyUI Windows Portable strips the C header (include/) and static library (libs/) folders from python_embeded/ to shrink download size. Download the identical Python minor version embeddable/installer package and copy its include/ and libs/ directories into python_embeded/.

### Why did my brand-new RTX 5080 or RTX 5090 fail with "no kernel image is available for execution"?

NVIDIA Blackwell GPUs use compute capability sm_120, which older CUDA 12.1 and 12.4 PyTorch builds do not contain compiled kernels for. Update your NVIDIA driver to 570+ and reinstall PyTorch from the cu128 wheel index.

### How much faster is SageAttention than native PyTorch SDPA on Wan 2.2 video renders?

On long 81-frame to 121-frame video diffusion passes (Wan 2.2, HunyuanVideo), properly matched SageAttention can cut per-step attention latency by roughly 25% to 40% compared to standard SDPA—for single 4-step FLUX stills, the wall-clock difference is negligible.

### Can SageAttention slightly alter output pixels or introduce subtle noise compared to SDPA?

Yes. Because SageAttention quantizes Q/K/V attention matrices to INT8/FP8 for speed, deterministic seeds render slightly differently from FP16/BF16 SDPA, and occasional custom LoRAs or edge resolutions can show faint artifacts—toggle back to --use-pytorch-cross-attention whenever diagnosing visual quality glitches.
