AUTOMATIC1111 Performance Optimizations: Complete Guide to xFormers, Attention Slicing, and Cross-Attention Backends

AUTOMATIC1111's Stable Diffusion WebUI provides interchangeable cross-attention optimizers—including xFormers, sub-quadratic attention, and split-attention (Doggettx)—that reduce VRAM usage and accelerate inference by dynamically swapping forward pass implementations in modules/sd_hijack_optimizations.py.

The AUTOMATIC1111/stable-diffusion-webui repository ships with a modular optimization framework that lets users trade memory consumption for generation speed. These AUTOMATIC1111 performance optimizations target the cross-attention layers within the U-Net architecture, offering hardware-specific backends ranging from NVIDIA-optimized xFormers kernels to Intel Arc XPU slicing algorithms.

Available Cross-Attention Optimizers

The WebUI implements six distinct optimization strategies in modules/sd_hijack_optimizations.py, each encapsulated in an SdOptimization subclass. Only one optimizer can be active at a time; the system selects the highest-priority option that passes hardware availability checks.

xFormers with Flash-Attention

The xFormers optimizer delivers the fastest inference on modern NVIDIA GPUs by calling xformers.ops.memory_efficient_attention. According to the source code in modules/sd_hijack_optimizations.py, the SdOptimizationXformers class checks for CUDA capability between 6.0 and 9.0 before activating:

class SdOptimizationXformers(SdOptimization):
    name = "xformers"
    cmd_opt = "xformers"
    priority = 100

    def is_available(self):
        return shared.cmd_opts.force_enable_xformers or (
            shared.xformers_available
            and torch.cuda.is_available()
            and (6, 0) <= torch.cuda.get_device_capability(shared.device) <= (9, 0)
        )

When applied, this swaps CrossAttention.forward with xformers_attention_forward and AttnBlock.forward with xformers_attnblock_forward, enabling memory-efficient attention kernels with optional Flash-Attention support.

Scaled Dot-Product Attention (SDP)

PyTorch 2.0+ users can leverage SDP through two variants:

  • SdOptimizationSdp: Uses the memory-efficient SDP implementation when available
  • SdOptimizationSdpNoMem: Forces the standard path without memory optimizations for compatibility with older hardware

Both classes reside in modules/sd_hijack_optimizations.py and rely on torch.nn.functional.scaled_dot_product_attention.

Sub-Quadratic Attention

The SdOptimizationSubQuad class implements chunk-based matmul splitting that keeps Q-K-V operations within L2/L3 cache limits. This optimization dramatically reduces VRAM pressure when generating high-resolution images (above 1024×1024) on consumer GPUs with limited memory.

Split-Attention (Doggettx)

Often referred to as "attention-slicing," the Doggettx optimizer (class SdOptimizationDoggettx) processes query tensors in small steps (default 2) to prevent large intermediate allocations. This algorithm works universally across NVIDIA, AMD, and Intel GPUs, making it ideal for 8 GB VRAM cards. The implementation uses split_cross_attention_forward and cross_attention_attnblock_forward functions to slice the attention computation.

InvokeAI Slice

SdOptimizationInvokeAI provides an alternative slicing implementation that maintains compatibility with the original InvokeAI codebase. It serves the same low-VRAM use case as Doggettx but uses per-step chunking logic specific to the InvokeAI attention mechanism.

XPU-Specific Slicing

For Intel Arc GPUs, modules/xpu_specific.py contains torch_xpu_scaled_dot_product_attention, which caps each SDPA chunk to ARC_SINGLE_ALLOCATION_LIMIT (approximately VRAM divided by 8). This prevents the 4 GB single-allocation bug inherent to Intel's XPU architecture.

How Optimizers Are Selected and Applied

The selection pipeline begins in modules/shared_cmd_options.py, which parses CLI flags like --xformers and --opt_split_attention. The function cross_attention_optimizations() in modules/shared_items.py populates the Settings dropdown:

def cross_attention_optimizations():
    import modules.sd_hijack
    return ["Automatic"] + [x.title() for x in modules.sd_hijack.optimizers] + ["None"]

When the UI initializes, it iterates through optimizer classes by priority (highest first) and calls is_available(). The first passing optimizer has its apply() method executed, which monkey-patches the forward functions in ldm.modules.attention and sgm.modules.attention:

def apply(self):
    ldm.modules.attention.CrossAttention.forward = xformers_attention_forward
    ldm.modules.diffusionmodules.model.AttnBlock.forward = xformers_attnblock_forward
    sgm.modules.attention.CrossAttention.forward = xformers_attention_forward
    sgm.modules.diffusionmodules.model.AttnBlock.forward = xformers_attnblock_forward

The undo() method restores the default hypernetwork.attention_CrossAttention_forward implementation on shutdown.

Enabling Optimizers via CLI and Web UI

Command-Line Flags

For reproducible deployments, specify optimizers at launch using flags defined in modules/cmd_args.py:

Flag Effect
--xformers Enable xFormers if available (requires CUDA capability 6.0+)
--force-enable-xformers Bypass GPU capability checks (may cause crashes)
--xformers-flash-attention Use Flash-Attention variant (op=get_xformers_flash_attention_op)
--opt_split_attention Force Doggettx split-attention
--opt_sub_quad_attention Force sub-quadratic implementation
--opt_sdp_attention Force memory-efficient SDP
--opt_sdp_no_mem_attention Force non-memory-efficient SDP
--reinstall-xformers Reinstall xFormers version 0.0.23.post1

Example launch command for RTX 40-series cards:

python launch.py --xformers --xformers-flash-attention

Web Interface Configuration

  1. Navigate to Settings → Stable Diffusion → Cross-attention optimization
  2. Select xformers, Doggettx, Sub-quadratic, or another option from the dropdown generated by SdOptimization.title()
  3. Click Apply changes and restart the WebUI

Choosing "Automatic" allows the system to select the highest-priority available optimizer based on the is_available() checks.

Runtime Verification and Testing

Verify which optimizer is currently active using the Python console or extensions:

from modules import shared, sd_hijack

print(f"Active cross-attention: {shared.opts.cross_attention_optimization}")
print(f"xFormers available: {shared.xformers_available}")
print(f"Current optimizer: {sd_hijack.optimizers[0].name}")

Manually force a specific optimizer in notebooks or scripts:

from modules.sd_hijack_optimizations import SdOptimizationDoggettx, SdOptimizationXformers

# Enable split-attention

SdOptimizationDoggettx().apply()

# Revert to defaults

SdOptimizationDoggettx().undo()

Test xFormers installation and attention shapes:

import torch, xformers.ops
q = torch.randn(1, 77, 64, device="cuda")
k = torch.randn_like(q)
v = torch.randn_like(q)

out = xformers.ops.memory_efficient_attention(q, k, v)
print(out.shape)  # torch.Size([1, 77, 64])

Summary

  • xFormers provides maximum speed on NVIDIA RTX 30/40-series GPUs with CUDA capability 6.0+, utilizing xformers_attention_forward in modules/sd_hijack_optimizations.py
  • Split-attention (Doggettx) reduces VRAM through query-slicing, ideal for 8 GB cards and universal GPU compatibility
  • Sub-quadratic attention chunks matmul operations to fit cache layers, enabling high-resolution generation on limited VRAM
  • SDP variants leverage PyTorch 2.0 native kernels, with "No-Mem" fallback for older hardware
  • XPU slicing automatically activates for Intel Arc GPUs via modules/xpu_specific.py to avoid 4 GB allocation limits
  • Optimizers are mutually exclusive; the system selects based on priority values (xFormers = 100) and hardware availability checks

Frequently Asked Questions

What is the difference between xFormers and SDP attention in AUTOMATIC1111?

xFormers uses the third-party xformers.ops.memory_efficient_attention kernel with optional Flash-Attention, requiring specific NVIDIA CUDA capabilities (6.0–9.0) and delivering maximum speed. SDP (Scaled Dot-Product) uses PyTorch 2.0's native torch.nn.functional.scaled_dot_product_attention, offering broader hardware compatibility but potentially lower performance than optimized xFormers kernels on supported GPUs.

How do I enable xFormers Flash Attention?

Launch the WebUI with both --xformers and --xformers-flash-attention flags, or select xFormers in the Settings dropdown after ensuring your GPU reports CUDA capability 6.0 or higher in torch.cuda.get_device_capability(). The Flash-Attention operator is passed via op=get_xformers_flash_attention_op in the attention forward function.

When should I use split-attention (Doggettx) instead of xFormers?

Use Doggettx (enabled via --opt_split_attention) when running on AMD GPUs, Intel Arc without XPU specific drivers, or NVIDIA cards with 8 GB VRAM generating images above 1024×1024 resolution. While slower than xFormers, it guarantees stable operation by processing attention in fixed-size steps rather than full tensor operations.

Why does my optimizer selection show as unavailable in the dropdown?

The is_available() method in each SdOptimization class performs hardware validation; xFormers requires both shared.xformers_available (set in modules/shared.py lines 29–30) and CUDA capability checks. If xFormers appears unavailable despite installation, verify your NVIDIA driver supports CUDA 11.8+ or use --force-enable-xformers to bypass checks (may cause runtime errors on unsupported hardware).

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →