ComfyUI icon indicating copy to clipboard operation
ComfyUI copied to clipboard

sageattention 2.2.0 result invalid value encountered in cast

Open magicwang1111 opened this issue 1 year ago • 16 comments

Custom Node Testing

Expected Behavior

wan2.1 480p basic workflow when use sage attention result in black image,use weight_dtype is default. use --disable-xformers --force-upcast-attention doesn't help,still output black image. ################## CUDA_VISIBLE_DEVICES=0 python main.py --listen --port 7870 --disable-metadata --use-sage-attention --disable-all-custom-nodes #################

Image

Actual Behavior

Image

Steps to Reproduce

image_to_video_wan_example.json

use this workflow,and CUDA_VISIBLE_DEVICES=0 python main.py --listen --port 7870 --disable-metadata --use-sage-attention --disable-all-custom-nodes

Debug Logs

got prompt
Using xformers attention in VAE
Using xformers attention in VAE
VAE load device: cuda:0, offload device: cpu, dtype: torch.bfloat16
Requested to load CLIPVisionModelProjection
loaded completely 20941.7375 1208.09814453125 True
Using scaled fp8: fp8 matrix mult: False, scale input: False
CLIP/text encoder model load device: cuda:0, offload device: cpu, current: cpu, dtype: torch.float16
Requested to load WanTEModel
loaded completely 19631.5119140625 6419.477203369141 True
Requested to load WanVAE
loaded completely 11031.234706878662 242.02829551696777 True
model weight dtype torch.float8_e4m3fn, manual cast: torch.float16
model_type FLOW
Requested to load WAN21
loaded completely 19763.37561607666 15636.317199707031 True
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [01:32<00:00,  4.63s/it]
Requested to load WanVAE
loaded completely 2471.309841156006 242.02829551696777 True
/data/wangxi/ComfyUI/comfy_extras/nodes_images.py:134: RuntimeWarning: invalid value encountered in cast
  img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
Prompt executed in 103.21 seconds

Other

No response

magicwang1111 avatar Jul 28 '25 09:07 magicwang1111

Hey, what hardware are you running on, as well as pytorch version and OS?

Kosinkadink avatar Aug 01 '25 00:08 Kosinkadink

Hey, what hardware are you running on, as well as pytorch version and OS? i use linux,

PRETTY_NAME="Ubuntu 22.04.4 LTS" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04.4 LTS (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian

(base) wangxi@4090-algo-1:~$

(comfyui) wangxi@4090-algo-1:~$ conda list torch

packages in environment at /data/wangxi/miniconda3/envs/comfyui:

Name Version Build Channel

came-pytorch 0.1.3 pypi_0 pypi lion-pytorch 0.2.3 pypi_0 pypi open-clip-torch 2.31.0 pypi_0 pypi pytorch-lightning 2.5.1.post0 pypi_0 pypi torch 2.7.0+cu128 pypi_0 pypi torchaudio 2.7.0+cu128 pypi_0 pypi torchdiffeq 0.2.5 pypi_0 pypi torchmetrics 1.7.1 pypi_0 pypi torchsde 0.2.6 pypi_0 pypi torchvision 0.22.0+cu128 pypi_0 pypi

(comfyui) wangxi@4090-algo-1:~$ nvidia-smi Fri Aug 1 15:23:32 2025 +-----------------------------------------------------------------------------------------+ | NVIDIA-SMI 575.57.08 Driver Version: 575.57.08 CUDA Version: 12.9 | |-----------------------------------------+------------------------+----------------------+ | GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |=========================================+========================+======================|

magicwang1111 avatar Aug 01 '25 06:08 magicwang1111

I'm having the exact same issue (using an RTX 5090). It appears that Sage doesn't work with either FP8 models or FP8 dtype:

I'll provide as much info below as possible but please ask for anything else. Just to help I started with a clean python_embeded folder and did the following before testing:

.\python_embeded\python.exe -m pip install -U "triton-windows<3.4"

.\python_embeded\python.exe C:\APPS\AI\test_triton.py
tensor([0., 0., 0.], device='cuda:0')
If you see tensor([0., 0., 0.], device='cuda:0'), then it works

.\python_embeded\python.exe -m pip install C:\APPS\AI\sageattention-2.2.0+cu128torch2.7.1-cp312-cp312-win_amd64.whl

.\python_embeded\python.exe C:\APPS\AI\test_sageattn.py
q (4, 32, 64, 128) cuda:0 torch.float16
sage vs math: mean_rtol=0.0866 max_rtol=2 mean_atol=0.0791 max_atol=0.0791
The above (except max_rtol) should be < 0.05 (on RTX 20xx/30xx) or < 0.1 (on RTX 40xx/50xx)

I now have these installed:

sageattention              2.2.0+cu128torch2.7.1
torch                      2.7.1+cu128
torchaudio                 2.7.1+cu128
torchsde                   0.2.6
torchvision                0.22.1+cu128
triton-windows             3.3.1.post19

This is the command .bat I'm running although I've also tried it without the fast accumilation. I've also tried using the KJ Patch Nodes instead of the command line and get the same result:

.\python_embeded\python.exe -s ComfyUI\main.py --windows-standalone-build --fast fp16_accumulation --use-sage-attention
pause

Here's the log: https://pastebin.com/9wyZ187H

Here's the workflow, which is literally the basic ComfyUI Template and all I'm doing is switching to the FP8 model. Note I get the same problem if I use the 14B FP16 model but turn on FP8 weight dtype:

https://pastebin.com/fJeWturu

slikvik55 avatar Aug 02 '25 09:08 slikvik55

Just to consolidate the troubleshooting:

Here's the standard ComfyUI template: https://pastebin.com/fJeWturu

If you enable Sage at start-up and using:

  • "wan2.1_i2v_720p_14B_fp8_e4m3fn" model = black output.
  • "wan2.1_i2v_720p_14B_fp16" (with dtype e4m3fn) = black output.
  • "Wan2_1-I2V-14B-720p_fp8_e4m3fn_scaled_KJ" = works!!

Here's Kijai's sample template: https://pastebin.com/1MC24GQn

If you enable Sage in the Model Node and using:

  • "wan2.1_i2v_720p_14B_fp8_e4m3fn model" = works!
  • "wan2.1_i2v_720p_14B_fp16 (with dtype e4m3fn)" = works!
  • "Wan2_1-I2V-14B-720p_fp8_e4m3fn_scaled_KJ" = works!

This implies that the native nodes either have an issue with my GPU (5090), triton (3.3.1) sage (2.2) or non-scaled FP8 models when using Sage!

slikvik55 avatar Aug 02 '25 13:08 slikvik55

It seems not a fp8 problem. I switched all related models to bf16, and this error persists.

Image

fengwang avatar Aug 07 '25 14:08 fengwang

GGUF also gives black result at step 4

kanarch66 avatar Aug 08 '25 13:08 kanarch66

There are five possible solutions / workarounds in this thread:

  • https://github.com/comfyanonymous/ComfyUI/issues/9184#issuecomment-3159909662

If that isn't of any help, please do ping back.

webfiltered avatar Aug 08 '25 14:08 webfiltered

@webfiltered

I get the same issue with qwen image:

Requested to load QwenImage
loaded completely 21318.258980926512 19483.948791503906 True
100%|██████████| 4/4 [00:17<00:00,  4.30s/it]
Requested to load WanVAE
0 models unloaded.
loaded partially 128.0 127.9998779296875 0
D:\AITools\StabilityMatrixData\Packages\ComfyUI\nodes.py:1590: RuntimeWarning: invalid value encountered in cast
  img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
Prompt executed in 34.50 seconds

RTX3090, sage attention 2.2.0, windows.

The other thread has some workarounds, but no actual solution. I'd rather not remove sage attention. I did try the KJ patch node with no result. I can't remove it because my system has other issues with flash attention.

phazei avatar Aug 10 '25 08:08 phazei

Ok, I have an update.

Yesterday I did try the workaround using Patch Sage Attention and I tried all the options. At the time I got a black screen, or it crashed comfy. sageattn_qk_int8_pv_fp16_cuda gave me a weird CUDA error I don't recall, maybe something about something missing.

Since then I've updated Triton-windows from 3.3.1 => v3.4.0-windows.post20 and SageAttention from 2.2.0-windows.post0 => 2.2.0-windows.post1. (edit: I was already on Pytorch 2.8)

Today I just again tried the Patch Sage Attention node with sageattn_qk_int8_pv_fp16_cuda and I do not get a black screen.

Without the --use-sage-attention flag it takes 5.4s/it, with the sageattn_qk_int8_pv_fp16_cuda I get 4.5s/it. Additionally, I am able to use TorchCompile when using fp8_e5m2_scaled, and that brings it to 3.9s/it

So, something still broken, but with the Patch Sage Attention node I can keep the --use-sage-attention flag and it still works.

phazei avatar Aug 11 '25 06:08 phazei

2.2.0-windows.post1

only compatible with pytorch 2.8 no?

ronbere avatar Aug 11 '25 09:08 ronbere

same problem

zeng121 avatar Aug 25 '25 07:08 zeng121

Ok, I have an update.

Yesterday I did try the workaround using Patch Sage Attention and I tried all the options. At the time I got a black screen, or it crashed comfy. sageattn_qk_int8_pv_fp16_cuda gave me a weird CUDA error I don't recall, maybe something about something missing.

Since then I've updated Triton-windows from 3.3.1 => v3.4.0-windows.post20 and SageAttention from 2.2.0-windows.post0 => 2.2.0-windows.post1. (edit: I was already on Pytorch 2.8)

Today I just again tried the Patch Sage Attention node with sageattn_qk_int8_pv_fp16_cuda and I do not get a black screen.

Without the --use-sage-attention flag it takes 5.4s/it, with the sageattn_qk_int8_pv_fp16_cuda I get 4.5s/it. Additionally, I am able to use TorchCompile when using fp8_e5m2_scaled, and that brings it to 3.9s/it

So, something still broken, but with the Patch Sage Attention node I can keep the --use-sage-attention flag and it still works.

Yes this is only solution until they fix it. Thanks

sinand99 avatar Aug 25 '25 16:08 sinand99

Ok, I have an update. Yesterday I did try the workaround using Patch Sage Attention and I tried all the options. At the time I got a black screen, or it crashed comfy. sageattn_qk_int8_pv_fp16_cuda gave me a weird CUDA error I don't recall, maybe something about something missing. Since then I've updated Triton-windows from 3.3.1 => v3.4.0-windows.post20 and SageAttention from 2.2.0-windows.post0 => 2.2.0-windows.post1. (edit: I was already on Pytorch 2.8) Today I just again tried the Patch Sage Attention node with sageattn_qk_int8_pv_fp16_cuda and I do not get a black screen. Without the --use-sage-attention flag it takes 5.4s/it, with the sageattn_qk_int8_pv_fp16_cuda I get 4.5s/it. Additionally, I am able to use TorchCompile when using fp8_e5m2_scaled, and that brings it to 3.9s/it So, something still broken, but with the Patch Sage Attention node I can keep the --use-sage-attention flag and it still works.

Yes this is only solution until they fix it. Thanks

It does not work for 30 series video card

Pythonpa avatar Sep 04 '25 14:09 Pythonpa

Ok, I have an update. Yesterday I did try the workaround using Patch Sage Attention and I tried all the options. At the time I got a black screen, or it crashed comfy. sageattn_qk_int8_pv_fp16_cuda gave me a weird CUDA error I don't recall, maybe something about something missing. Since then I've updated Triton-windows from 3.3.1 => v3.4.0-windows.post20 and SageAttention from 2.2.0-windows.post0 => 2.2.0-windows.post1. (edit: I was already on Pytorch 2.8) Today I just again tried the Patch Sage Attention node with sageattn_qk_int8_pv_fp16_cuda and I do not get a black screen. Without the --use-sage-attention flag it takes 5.4s/it, with the sageattn_qk_int8_pv_fp16_cuda I get 4.5s/it. Additionally, I am able to use TorchCompile when using fp8_e5m2_scaled, and that brings it to 3.9s/it So, something still broken, but with the Patch Sage Attention node I can keep the --use-sage-attention flag and it still works.

Yes this is only solution until they fix it. Thanks

It does not work for 30 series video card

Use the latest versions of Comfy, Triton and Sage

sinand99 avatar Sep 04 '25 19:09 sinand99

I'm also seeing this on Linux. Was working fine with sageattention v2 a month ago. Then, after not using Comfy for a month, I did an update today and now I'm getting black images on Wan2.2 (never, ever, had this issue before with Wan, but have had it with Qwen).

What I have installed (from comfy-cli):

sageattention 2.2.0
torch 2.9.0
torchaudio 2.9.0
torchvision 0.24.0
triton 3.5.0

Did a complete fresh install 3 times with different things I've tried and still no luck.

JamesMowery avatar Oct 29 '25 04:10 JamesMowery

go to ComfyUI/venv/lib/python3.10/site-packages/sageattention/core.py go to line 118 replace with this arch = get_cuda_arch_versions()[q.device.index] if arch in ("sm80", "sm86"): return sageattn_qk_int8_pv_fp16_cuda(q, k, v, tensor_layout=tensor_layout, is_causal=is_causal, sm_scale=sm_scale, return_lse=return_lse, pv_accum_dtype="fp32") # elif arch == "sm86": # return sageattn_qk_int8_pv_fp16_triton(q, k, v, tensor_layout=tensor_layout, is_causal=is_causal, sm_scale=sm_scale, return_lse=return_lse) elif arch == "sm89":

jandan520 avatar Mar 10 '26 20:03 jandan520