ComfyUI Attention Backends Explained: SDPA, SageAttention, xFormers, and How to Choose the Right One

Most ComfyUI users tune checkpoints, LoRAs, and samplers to make a workflow faster or fit in VRAM — but almost nobody touches the attention backend, even though it can be the single largest performance lever outside of the model itself. The attention computation is where transformer-based models spend a disproportionate share of their time and memory, and ComfyUI ships several interchangeable implementations of it. Choosing the wrong one for your GPU, or leaving a default that silently falls back to a slow path, is a common cause of “why is this suddenly slower” and “why did this OOM after I upgraded.”

This guide walks through the attention backends ComfyUI registers, what each one actually does, the command-line flags that control them, the fallback order, and how to verify which backend your workflow is really using. Everything below is grounded in the current ComfyUI source and the official documentation, not synthetic benchmarks.

What an Attention Backend Actually Does

Every transformer-based diffusion or video model applies scaled dot-product attention across its layers: the model computes query, key, and value tensors, then multiplies them to produce attention scores. Written naively in PyTorch, this materializes an N × N attention matrix that grows quadratically with sequence length — fine for a 77-token text prompt, catastrophic for a high-resolution image or a video with thousands of spatial-temporal tokens.

An attention backend is a specialized implementation of this operation. The differences between backends boil down to three practical concerns:

  • Speed — does it use fused GPU kernels (Flash Attention, cuDNN, Triton) or fall back to generic operations?
  • VRAM — does it materialize the full attention matrix, or compute it in tiled blocks that avoid the huge intermediate tensor?
  • Precision and compatibility — does it force fp32, respect fp16/bf16, and does it work with masked attention (which video models like Wan heavily rely on)?

ComfyUI exposes these backends through its comfy/ldm/modules/attention.py module and selects one at startup based on command-line flags and what is installed in your environment. Understanding that selection logic is the key to troubleshooting “my workflow got slow” or “OOM after update” reports.

The Backends ComfyUI Registers

ComfyUI registers multiple named attention functions and picks a default. The most relevant ones today are:

  • pytorch (SDPA) — PyTorch’s native scaled_dot_product_attention. This is the modern default on most installs.
  • sage (SageAttention) — the quantized attention kernel backed by the sageattention package, enabled with --use-sage-attention.
  • xformers — Meta’s memory_efficient_attention, used when xFormers is installed and enabled.
  • sub_quad — ComfyUI’s sub-quadratic attention, a memory-saving fallback.
  • split — split cross-attention, another memory-conservation option (--use-split-cross-attention).
  • flash — a Flash Attention path with an automatic fallback to SDPA if it fails.
  • basic — the naive PyTorch implementation, the slowest and most memory-hungry path.

There is also an experimental comfy_kitchen_int8 backend and, for Blackwell-generation GPUs, a sageattn3 backend (sageattn3_blackwell). Most users on consumer hardware will realistically choose between SDPA, SageAttention, and xFormers.

Within the SDPA path specifically, ComfyUI sets an explicit backend priority order via torch.nn.attention.sdpa_kernel: Flash Attention → cuDNN Attention → Efficient (memory-efficient) Attention → Math. If your PyTorch is too old to support set_priority, ComfyUI logs a warning and lets PyTorch pick. This means “SDPA” is itself a family of kernels, and the actual kernel can change depending on your PyTorch build and GPU.

Symptom: “OOM After Update” or “Suddenly Slower”

The classic sign that an attention backend changed underneath you is a workflow that worked fine one day and either runs out of memory or slows down dramatically the next. A ComfyUI or PyTorch update can shift which backend gets selected without you changing a single workflow setting.

Diagnose it by looking at the startup console. The relevant lines appear near the top when ComfyUI initializes the model management layer. You are looking for one of these messages:

  • Using pytorch attention — SDPA is active
  • Using xformers attention — xFormers path selected
  • Using sub quadratic optimization for attention… — the sub-quadratic backend is active, which the message itself flags as a possible speed issue
  • Using split optimization for attention — split cross-attention is active

If you see the sub-quadratic or split line and you did not intentionally enable it for a low-VRAM card, that is your answer: the backend fell back to a slower, memory-focused path. Conversely, if a recent update pulled in a newer PyTorch that lost Flash Attention support (for example, a driver/torch mismatch), generation can drop to the Math kernel and become several times slower with no error at all — it just works badly.

Another diagnostic is the xFormers bug guard in ComfyUI. Versions 0.0.21 through 0.0.26 of xFormers are treated as broken (a CUDA error triggers when the batch dimension of the query exceeds 65535) and are automatically disabled by ComfyUI. If you pinned an old xFormers version for a legacy workflow and it silently stops being used, this guard is why.

Choosing a Backend for Your GPU and VRAM

There is no single “best” backend — the right choice depends on your GPU generation, driver, PyTorch build, and whether you are VRAM-constrained. The following decision flow reflects how the backends behave in practice:

  • Modern NVIDIA GPU (Ampere or newer) with a current PyTorch: leave SDPA as the default. Flash Attention and cuDNN attention are fused kernels that are both fast and memory-efficient, and the automatic priority order handles the rest.
  • Want more speed at the same VRAM, on a Turing/Ampere/Ada card: try SageAttention. It uses int8 quantization of the attention computation and can measurably accelerate generation for diffusion and video models, especially on higher-end cards. Install it with python -m pip install sageattention in your ComfyUI environment, then relaunch with --use-sage-attention.
  • Very low VRAM (8 GB or less): consider --use-split-cross-attention (or let it fall back to sub-quadratic), which trades speed for reduced peak memory by processing attention in slices.
  • Blackwell (RTX 50-series): use the sageattn3 package (sageattn3_blackwell) rather than the original sageattention, which targets older architectures.

A practical caution on SageAttention: enabling it requires the package to be present, and if it is not, ComfyUI logs an explicit error telling you to run the pip install command. SageAttention also has a mask-support caveat — ComfyUI checks whether the installed sageattn signature accepts attn_mask (stored as SAGE_ATTENTION_SUPPORTS_MASK) because many video and editing models pass a mask. If your version predates mask support, masked attention will not behave correctly even though the backend loads.

For memory-troubleshooting specifically, the relevant knob is not the backend alone but how it is combined with model offloading. The split and sub-quadratic backends reduce peak attention memory but do not magically make a 16 GB model fit into 8 GB — they only help at the margin. If you are trying to run a 14B-class video model on a 16 GB card, the effective tools are offloading (moving weights off the GPU between blocks) and quantization, which we cover in a separate guide on running Wan2.2 Animate 14B on 16GB VRAM with BlockSwap.

Reproducible Steps to Verify Your Active Backend

Do not guess which backend you are on — verify it. The reliable sequence is:

  1. Launch ComfyUI from a terminal (not a desktop shortcut) so the startup log is visible. Capture the full console output.
  2. Search the first hundred lines for the attention selection messages listed above (Using pytorch attention, Using xformers attention, etc.).
  3. Confirm your environment: run python -c "import torch; print(torch.__version__)" and, if relevant, python -c "import xformers; print(xformers.__version__)" inside the ComfyUI Python environment.
  4. If you changed a flag (for example, added --use-sage-attention), relaunch and confirm the log now reflects the change. Re-run one queue prompt and compare wall-clock time against your baseline for the same seed and workflow.
  5. If you see a fallback you did not expect, check the ComfyUI console for the specific warning — it usually names the reason (missing package, broken xFormers version, too-old PyTorch).

Because the SDPA path silently ranks Flash Attention over cuDNN over Efficient over Math, a good sanity check for “am I actually getting the fast kernel” is to watch the first inference after a cold start: if the fast fused kernel is active, there is no giant single-tensor allocation spike and generation completes in line with your expectations. A sudden multi-second pause with high CPU usage right at the first denoise step often indicates a fallback to a non-fused path.

Common Pitfalls to Avoid

  • Installing SageAttention but not passing the flag. The package alone does nothing — it must be enabled via --use-sage-attention at launch. If the flag is present but the package is missing, ComfyUI errors out rather than silently ignoring it.
  • Pinning an old xFormers. Versions 0.0.21–0.0.26 are auto-disabled, so your “working” config may already have reverted to SDPA without you noticing.
  • Assuming attention is the bottleneck. Before chasing backend speedups, rule out the real culprits: model loading off disk, VRAM swapping (watch for CUDA out of memory retries that degrade to CPU), and VAE decode, which is a separate memory spike entirely. We covered the VAE-decode OOM case specifically in our open-source video generation stack overview.
  • Ignoring the mask-support caveat on SageAttention. Video and inpainting models that rely on masked attention can produce corrupt output on older sageattn versions, even when everything else looks healthy.
  • Mixing environments. The pip install must target the exact Python interpreter that runs ComfyUI. A system-level pip install sageattention against the wrong environment is the most common reason the flag appears to “do nothing” while ComfyUI logs the missing-package error.

Primary Documentation

For the authoritative reference on attention behavior and the full list of command-line options, consult the official ComfyUI documentation and the ComfyUI source repository, particularly the attention.py, ops.py, and model_management modules. Command-line flags and backend selection change between releases, so verify against the version you actually have installed rather than relying on second-hand summaries.

Understanding which attention backend is active — and why — turns a frustrating “why is this slow/OOM” investigation into a five-minute check of your startup log. It is one of the highest-leverage fixes available to local ComfyUI users, and it costs nothing but a careful read of your own console output.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *