-
Notifications
You must be signed in to change notification settings - Fork 6.3k
[docs] Attention backends #12320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[docs] Attention backends #12320
Conversation
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. |
There was a problem hiding this 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.
- Through setting attention backend name
model. set_attention_backend("<backend_name>")
. - 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
Flex uses a different path in
torch
:
out = flex_attention.flex_attention( -
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 withscaled_dot_product_attention(
).
Docs for enabling different attention backends.