|
| 1 | +<!-- Copyright 2025 The HuggingFace Team. All rights reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +the License. You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +specific language governing permissions and limitations under the License. --> |
| 11 | + |
| 12 | +# Attention backends |
| 13 | + |
| 14 | +> [!TIP] |
| 15 | +> The attention dispatcher is an experimental feature. Please open an issue if you have any feedback or encounter any problems. |
| 16 | +
|
| 17 | +Diffusers provides several optimized attention algorithms that are more memory and computationally efficient through it's *attention dispatcher*. The dispatcher acts as a router for managing and switching between different attention implementations and provides a unified interface for interacting with them. |
| 18 | + |
| 19 | +Refer to the table below for an overview of the available attention families and to the [Available backends](#available-backends) section for a more complete list. |
| 20 | + |
| 21 | +| attention family | main feature | |
| 22 | +|---|---| |
| 23 | +| FlashAttention | minimizes memory reads/writes through tiling and recomputation | |
| 24 | +| SageAttention | quantizes attention to int8 | |
| 25 | +| PyTorch native | built-in PyTorch implementation using [scaled_dot_product_attention](./fp16#scaled-dot-product-attention) | |
| 26 | +| xFormers | memory-efficient attention with support for various attention kernels | |
| 27 | + |
| 28 | +This guide will show you how to set and use the different attention backends. |
| 29 | + |
| 30 | +## set_attention_backend |
| 31 | + |
| 32 | +The [`~ModelMixin.set_attention_backend`] method iterates through all the modules in the model and sets the appropriate attention backend to use. The attention backend setting persists until [`~ModelMixin.reset_attention_backend`] is called. |
| 33 | + |
| 34 | +The example below demonstrates how to enable the `_flash_3_hub` implementation for FlashAttention-3 from the [kernel](https://github.com/huggingface/kernels) library, which allows you to instantly use optimized compute kernels from the Hub without requiring any setup. |
| 35 | + |
| 36 | +> [!TIP] |
| 37 | +> FlashAttention-3 is not supported for non-Hopper architectures, in which case, use FlashAttention with `set_attention_backend("flash")`. |
| 38 | +
|
| 39 | +```py |
| 40 | +import torch |
| 41 | +from diffusers import QwenImagePipeline |
| 42 | + |
| 43 | +pipeline = QwenImagePipeline.from_pretrained( |
| 44 | + "Qwen/Qwen-Image", torch_dtype=torch.bfloat16, device_map="cuda" |
| 45 | +) |
| 46 | +pipeline.transformer.set_attention_backend("_flash_3_hub") |
| 47 | + |
| 48 | +prompt = """ |
| 49 | +cinematic film still of a cat sipping a margarita in a pool in Palm Springs, California |
| 50 | +highly detailed, high budget hollywood movie, cinemascope, moody, epic, gorgeous, film grain |
| 51 | +""" |
| 52 | +pipeline(prompt).images[0] |
| 53 | +``` |
| 54 | + |
| 55 | +To restore the default attention backend, call [`~ModelMixin.reset_attention_backend`]. |
| 56 | + |
| 57 | +```py |
| 58 | +pipeline.transformer.reset_attention_backend() |
| 59 | +``` |
| 60 | + |
| 61 | +## attention_backend context manager |
| 62 | + |
| 63 | +The [attention_backend](https://github.com/huggingface/diffusers/blob/5e181eddfe7e44c1444a2511b0d8e21d177850a0/src/diffusers/models/attention_dispatch.py#L225) context manager temporarily sets an attention backend for a model within the context. Outside the context, the default attention (PyTorch's native scaled dot product attention) is used. This is useful if you want to use different backends for different parts of a pipeline or if you want to test the different backends. |
| 64 | + |
| 65 | +```py |
| 66 | +import torch |
| 67 | +from diffusers import QwenImagePipeline |
| 68 | + |
| 69 | +pipeline = QwenImagePipeline.from_pretrained( |
| 70 | + "Qwen/Qwen-Image", torch_dtype=torch.bfloat16, device_map="cuda" |
| 71 | +) |
| 72 | +prompt = """ |
| 73 | +cinematic film still of a cat sipping a margarita in a pool in Palm Springs, California |
| 74 | +highly detailed, high budget hollywood movie, cinemascope, moody, epic, gorgeous, film grain |
| 75 | +""" |
| 76 | + |
| 77 | +with attention_backend("_flash_3_hub"): |
| 78 | + image = pipeline(prompt).images[0] |
| 79 | +``` |
| 80 | + |
| 81 | +## Available backends |
| 82 | + |
| 83 | +Refer to the table below for a complete list of available attention backends and their variants. |
| 84 | + |
| 85 | +| Backend Name | Family | Description | |
| 86 | +|--------------|--------|-------------| |
| 87 | +| `native` | [PyTorch native](https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend) | Default backend using PyTorch's scaled_dot_product_attention | |
| 88 | +| `flex` | [FlexAttention](https://docs.pytorch.org/docs/stable/nn.attention.flex_attention.html#module-torch.nn.attention.flex_attention) | PyTorch FlexAttention implementation | |
| 89 | +| `_native_cudnn` | [PyTorch native](https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend) | CuDNN-optimized attention | |
| 90 | +| `_native_efficient` | [PyTorch native](https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend) | Memory-efficient attention | |
| 91 | +| `_native_flash` | [PyTorch native](https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend) | PyTorch's FlashAttention | |
| 92 | +| `_native_math` | [PyTorch native](https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend) | Math-based attention (fallback) | |
| 93 | +| `_native_npu` | [PyTorch native](https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend) | NPU-optimized attention | |
| 94 | +| `_native_xla` | [PyTorch native](https://docs.pytorch.org/docs/stable/generated/torch.nn.attention.SDPBackend.html#torch.nn.attention.SDPBackend) | XLA-optimized attention | |
| 95 | +| `flash` | [FlashAttention](https://github.com/Dao-AILab/flash-attention) | FlashAttention-2 | |
| 96 | +| `flash_varlen` | [FlashAttention](https://github.com/Dao-AILab/flash-attention) | Variable length FlashAttention | |
| 97 | +| `_flash_3` | [FlashAttention](https://github.com/Dao-AILab/flash-attention) | FlashAttention-3 | |
| 98 | +| `_flash_varlen_3` | [FlashAttention](https://github.com/Dao-AILab/flash-attention) | Variable length FlashAttention-3 | |
| 99 | +| `_flash_3_hub` | [FlashAttention](https://github.com/Dao-AILab/flash-attention) | FlashAttention-3 from kernels | |
| 100 | +| `sage` | [SageAttention](https://github.com/thu-ml/SageAttention) | Quantized attention (INT8 QK) | |
| 101 | +| `sage_varlen` | [SageAttention](https://github.com/thu-ml/SageAttention) | Variable length SageAttention | |
| 102 | +| `_sage_qk_int8_pv_fp8_cuda` | [SageAttention](https://github.com/thu-ml/SageAttention) | INT8 QK + FP8 PV (CUDA) | |
| 103 | +| `_sage_qk_int8_pv_fp8_cuda_sm90` | [SageAttention](https://github.com/thu-ml/SageAttention) | INT8 QK + FP8 PV (SM90) | |
| 104 | +| `_sage_qk_int8_pv_fp16_cuda` | [SageAttention](https://github.com/thu-ml/SageAttention) | INT8 QK + FP16 PV (CUDA) | |
| 105 | +| `_sage_qk_int8_pv_fp16_triton` | [SageAttention](https://github.com/thu-ml/SageAttention) | INT8 QK + FP16 PV (Triton) | |
| 106 | +| `xformers` | [xFormers](https://github.com/facebookresearch/xformers) | Memory-efficient attention | |
0 commit comments