Skip to content

Conversation

stevhliu
Copy link
Member

Docs for enabling different attention backends.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@stevhliu stevhliu requested a review from sayakpaul September 11, 2025 22:49
Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for starting this! Looking good. Before talking about specific attention backends, we could educate the users about the forms in which they can use them.

  1. Through setting attention backend name model. set_attention_backend("<backend_name>").
  2. Through the context manager.

Then we could maintain a table of attention backend names, their GitHub/official pages, and notes.

We could then take one example for a backend and make it complete.

This way, I think it will be leaner and easier to follow. WDYT?


There are several available FlashAttention variants, including variable length and the original FlashAttention. For a full list of supported implementations, check the list [here](https://github.com/huggingface/diffusers/blob/5e181eddfe7e44c1444a2511b0d8e21d177850a0/src/diffusers/models/attention_dispatch.py#L163).

The example below demonstrates how to enable the `_flash_3_hub` implementation. The [kernel](https://github.com/huggingface/kernels) library allows you to instantly use optimized compute kernels from the Hub without requiring any setup.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The example below demonstrates how to enable the `_flash_3_hub` implementation. The [kernel](https://github.com/huggingface/kernels) library allows you to instantly use optimized compute kernels from the Hub without requiring any setup.
The example below demonstrates how to enable the `_flash_3_hub` implementation for Flash Attention 3. The [kernels](https://github.com/huggingface/kernels) library allows you to instantly use optimized compute kernels from the Hub without requiring any setup.

But we should aso note the hardware restrictions in using FA3 as it's not supported in non-hopper architectures. In that case, regular Flash should be used through set_attention_backend("flash").


## PyTorch native

PyTorch includes a [native implementation](https://docs.pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html) of several optimized attention implementations including [FlexAttention](https://pytorch.org/blog/flexattention/), FlashAttention, memory-efficient attention, and a C++ version.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Flex uses a different path in torch:

    out = flex_attention.flex_attention(

  2. The backends that leverage nn.functional.scaled_dot_product_attention() can be found in https://github.com/huggingface/diffusers/blob/5e181eddfe7e44c1444a2511b0d8e21d177850a0/src/diffusers/models/attention_dispatch.py (search with scaled_dot_product_attention().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants