ComfyUI icon indicating copy to clipboard operation
ComfyUI copied to clipboard

Flash Attention support for VAE

Open Reber01Good opened this issue 7 months ago • 7 comments

Feature Idea

Can you please support flash attention in VAE (when using --use-flash-attention) instead of forcing split attention? I have tested it by modifying the files and it works fine.

Existing Solutions

No response

Other

No response

Reber01Good avatar May 01 '26 14:05 Reber01Good

A few weeks ago --use-flash-attention worked fine, you saw it on the console. But something happened in the past 1 o 2 ComfyUI versions and Flash is gone... or so it seems?

Using --use-flash-attention now shows "using xformers" on the console, yet, I did some fooling around with coding and I think Flash gets enabled, but what is wrong is the message on the console.

Using Sage works and shows great, I've seen this only affects Flash. For instance, here's part of the console output of my testing with my notes:

(...)
pytorch version: 2.11.0+cu130
Flash Attention backend detected <-- Added by me if Flash Attention wheel is verified as properly installed
Using Flash Attention <-- Added by me to verify if Flash Attn is indeed activated/loaded
xformers version: 0.0.35
Set vram state to: HIGH_VRAM
Device: cuda:0 NVIDIA GeForce RTX 3090 : cudaMallocAsync
Using async weight offloading with 2 streams
Enabled pinned memory 25995.0
Using xformers attention <- Yet, below, ComfyUI insists on showing me is using xformers
(...)

Two attention methods on the same run does not make sense. Since Flash Attn is installed and enabled, verified it gets activated, my reasoning is the second message ("Using xformers attention") is ill informing.

I'd have to recode the test to upload it if anyone feels like trying my experiment -deleted all modified files but still have the source test code-, but something smelly is going on with Flash.

In a nutshell, I'd say this falls into the issue of a bug more like a feature request, Flash Attention was working perfectly until versions ago.

gershu-ar avatar May 01 '26 21:05 gershu-ar

Loosely related to the subject: I found out by chance, a few weeks ago, trying random stuff, that xFormers + Flash Attention 2 makes my Wan 2.2 workflows slower instead of faster. I uninstalled both, I keep only Sage Attention. PyTorch default attention apparently works better. I'm using PyTorch nightly Cuda 13.2 builds and I have a RTX 5060 Ti 16 GB, I'm not sure is like that for everyone else.

jovan2009 avatar May 01 '26 22:05 jovan2009

I do not use xformers, it is performing slightly worse than flash even though it is supposed to be based on it. Pure flash attention is just simpler and doesn't need tinkering in xformers install files to make it work the way you want.

@gershu-ar I never saw flash being used for vae, only xformers or pytorch. Checking the code shows that is exactly what happens. Pure flash attention was never in the vae code.

Reber01Good avatar May 02 '26 00:05 Reber01Good

I do not use xformers, it is performing slightly worse than flash even though it is supposed to be based on it. Pure flash attention is just simpler and doesn't need tinkering in xformers install files to make it work the way you want.

@gershu-ar I never saw flash being used for vae, only xformers or pytorch. Checking the code shows that is exactly what happens. Pure flash attention was never in the vae code.

You not seeing it does not mean is does not exist :)

Flash Attention does not come by default nor preinstalled in ComfyUI but is ready to be implemented. So I did. It was working fine for me a few versions ago, now it doesn't or it is but is showing wrongly on the console. Until there's not an official word about the Flash Attention, I'll have doubts well beyond what code is telling.

For Wan 2.x Sage is great for me , Flash does a better work at seed_3 / exponential and some other combinations for still images. Again: for me. Testing results vary from hardware combinations, hence why tinkering is all what ComfyUI is about.

As for Flash, Sage, xformes and pytorch, here's a great simple guide: https://diffusiondoodles.substack.com/p/demystifying-attention-in-comfyui

And here are the Windows wheels: https://github.com/wildminder/AI-windows-whl

gershu-ar avatar May 02 '26 01:05 gershu-ar

Loosely related to the subject: I found out by chance, a few weeks ago, trying random stuff, that xFormers + Flash Attention 2 makes my Wan 2.2 workflows slower instead of faster. I uninstalled both, I keep only Sage Attention. PyTorch default attention apparently works better. I'm using PyTorch nightly Cuda 13.2 builds and I have a RTX 5060 Ti 16 GB, I'm not sure is like that for everyone else.

Sage Attention works wonders in Wan 2.2, been using that combination myself. Sage is more efficient because it reduces VRAM usage and speeds up long sequence processing, making video generation faster and more stable compared to PyTorch or Flash Attention.

For still images, my suggestion is use Flash or pytorch, Sage will slow you down in single renders.

gershu-ar avatar May 02 '26 01:05 gershu-ar

@gershu-ar I can't use sage attention because it has not implemented support for my gpu. I wouldn't use it anyway, it quantizes attention, reducing quality to gain some memory reduction and maybe a bit of speed. Since it also works with vae, it should also be supported in the code without requiring modification.

If you want a tip for faster vae, try torch compile (KJnodes has a node for it).

Reber01Good avatar May 03 '26 00:05 Reber01Good

See #14083 Should be fixed. Use the .bat file to upgrade to latest comfyui (not the one to update cuda etc)

Heliumrich avatar May 28 '26 14:05 Heliumrich

See #14083 Should be fixed. Use the .bat file to upgrade to latest comfyui (not the one to update cuda etc)

Solved now and working like a charm, thank you!

gershu-ar avatar May 28 '26 19:05 gershu-ar

I made some comparisons on the same wan 2.2 workflow (no xFormers installed)

  • with sage attention ~33 seconds/iteration (but the output doesn't follow the prompt properly, so the speed is useless on this particular workflow)
  • with flash attention 2.8.4 ~ 75 seconds/iteration
  • with no flash attention (I uninstalled it, to be sure), using pytorch default attention ~72 seconds/iteration

I was really hoping to see some benefits from flash attention because I bothered to build the wheel myself (which takes some not negligible amount of time), but... It might be that Pytorch is already as optimized as it can be in this regard.

(RTX 5060 Ti, PyTorch nightly + cuda 13.2)

jovan2009 avatar May 29 '26 12:05 jovan2009

afaik sageattention 2++ is virtually lossless while 3.0 degrade quality (and not even that much faster than 2++)

Heliumrich avatar May 29 '26 13:05 Heliumrich

afaik sageattention 2++ is virtually lossless while 3.0 degrade quality (and not even that much faster than 2++)

I use only sage attention 2.2. If I was looking only at the visual quality I wouldn't have noticed anything bad. But this particular workflow I keep as a "benchmark" because apparently sage attention makes wan 2.2 to switch between left and right. Not much use if the image looks good but the objects in the scene do the opposite of what you ask regarding left and right directions.

jovan2009 avatar May 29 '26 13:05 jovan2009

@Heliumrich

Unfortunately this is not a proper fix. It works but it is not correct. Giving the raw vae (sdxl) tensor to flash attention passes the 512 head dimension into the channel parameter instead, surprisingly this massive error gets executed and the resulting vae looks normal. Reshaping the tensor to give flash attention the correct parameters results in a true 512 headdim which crashes it, since flash supports max 256 headdim. The easy fix is to just split, which loses some quality but keeps the attention computation legit.

I studied this and also asked AI (without context) about this, and it came to the same conclusion. Since then I have been using a custom patch that reshapes the tensor correctly and also splits on headdim in case it is too large. This worked fine on sdxl, flux, flux2, and qwen.

Please, if anyone knows this stuff better than me, I would like a confirmation and proper check by an experienced human.

Reber01Good avatar May 31 '26 20:05 Reber01Good

Isn't vae step extremely fast compared to the main model (even a turbo one) anyway ?

Heliumrich avatar Jun 01 '26 01:06 Heliumrich

Vae is fast, but between split/pytorch and flash, flash is faster. They all use about the same memory, but since I have ck fa2, the optimized kernels it has run faster, giving the speedup. Speedup seems to grow exponentially with vae tile size.

Reber01Good avatar Jun 01 '26 17:06 Reber01Good