Conversation
Summary of ChangesHello @XOR-op, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly upgrades the FlashAttention backend by integrating support for FlashAttention 4, aiming to leverage the latest performance enhancements. It establishes a flexible version detection and fallback system to maintain compatibility across various FlashAttention installations. Additionally, the changes adapt the attention function's output processing to align with potential API changes in newer FlashAttention versions. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds support for FlashAttention v4 by updating the import logic to dynamically detect and use the appropriate version of flash_attn_func. It also correctly handles the output of the attention function, which may be a tuple. The changes look good, but I have a couple of suggestions to improve code clarity and reduce duplication, which should enhance maintainability.
| fa_ver = 2 | ||
| try: | ||
| from flash_attn_interface import flash_attn_func as flash_attn_3_func | ||
| from flash_attn.cute import flash_attn_func | ||
|
|
||
| # flash_attn 3 no longer have a different API, see following commit: | ||
| # https://github.com/Dao-AILab/flash-attention/commit/ed209409acedbb2379f870bbd03abce31a7a51b7 | ||
| flash_attn_func = flash_attn_3_func | ||
| fa_ver = 4 | ||
| except ImportError: | ||
| flash_attn_func = flash_attn_2_func | ||
| try: | ||
| from flash_attn_interface import flash_attn_func as flash_attn_3_func | ||
|
|
||
| from fastvideo.attention.backends.abstract import (AttentionBackend, | ||
| AttentionImpl, | ||
| AttentionMetadata, | ||
| AttentionMetadataBuilder) | ||
| from fastvideo.logger import init_logger | ||
| # flash_attn 3 no longer have a different API, see following commit: | ||
| # https://github.com/Dao-AILab/flash-attention/commit/ed209409acedbb2379f870bbd03abce31a7a51b7 | ||
| flash_attn_func = flash_attn_3_func | ||
| fa_ver = 3 | ||
| except ImportError: | ||
| flash_attn_func = flash_attn_2_func |
There was a problem hiding this comment.
The logic for detecting the FlashAttention version can be slightly simplified for better readability. By setting fa_ver within each block where a version is successfully imported, you can remove the initial assignment and make it clearer which version is being used.
| fa_ver = 2 | |
| try: | |
| from flash_attn_interface import flash_attn_func as flash_attn_3_func | |
| from flash_attn.cute import flash_attn_func | |
| # flash_attn 3 no longer have a different API, see following commit: | |
| # https://github.com/Dao-AILab/flash-attention/commit/ed209409acedbb2379f870bbd03abce31a7a51b7 | |
| flash_attn_func = flash_attn_3_func | |
| fa_ver = 4 | |
| except ImportError: | |
| flash_attn_func = flash_attn_2_func | |
| try: | |
| from flash_attn_interface import flash_attn_func as flash_attn_3_func | |
| from fastvideo.attention.backends.abstract import (AttentionBackend, | |
| AttentionImpl, | |
| AttentionMetadata, | |
| AttentionMetadataBuilder) | |
| from fastvideo.logger import init_logger | |
| # flash_attn 3 no longer have a different API, see following commit: | |
| # https://github.com/Dao-AILab/flash-attention/commit/ed209409acedbb2379f870bbd03abce31a7a51b7 | |
| flash_attn_func = flash_attn_3_func | |
| fa_ver = 3 | |
| except ImportError: | |
| flash_attn_func = flash_attn_2_func | |
| try: | |
| from flash_attn.cute import flash_attn_func | |
| fa_ver = 4 | |
| except ImportError: | |
| try: | |
| from flash_attn_interface import flash_attn_func as flash_attn_3_func | |
| # flash_attn 3 no longer have a different API, see following commit: | |
| # https://github.com/Dao-AILab/flash-attention/commit/ed209409acedbb2379f870bbd03abce31a7a51b7 | |
| flash_attn_func = flash_attn_3_func | |
| fa_ver = 3 | |
| except ImportError: | |
| flash_attn_func = flash_attn_2_func | |
| fa_ver = 2 |
|
Converted to WIP: after discussion with @jzhang38, we decide we should add a new FA4 backend instead and wrap it in a custom op |
|
Do we need to gate fa4 behind cuda arch? |
|
Added the gate |
Install from https://github.com/Dao-AILab/flash-attention/tree/main/flash_attn/cute
What's in this PR:
torch.compilesupport