Skip to content

[misc] FlashAttention 4 support#1114

Open
XOR-op wants to merge 3 commits intohao-ai-lab:mainfrom
XOR-op:fa4-support
Open

[misc] FlashAttention 4 support#1114
XOR-op wants to merge 3 commits intohao-ai-lab:mainfrom
XOR-op:fa4-support

Conversation

@XOR-op
Copy link
Collaborator

@XOR-op XOR-op commented Feb 19, 2026

Install from https://github.com/Dao-AILab/flash-attention/tree/main/flash_attn/cute

What's in this PR:

  • Integrate into FlashAttnBackend now, given no specific reason to choose fa2 over fa4 when fa4 is available
  • Use torch.custom_op for torch.compile support

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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

  • FlashAttention 4 Support: Added support for FlashAttention 4 by attempting to import flash_attn_func from flash_attn.cute.
  • Version Fallback Mechanism: Implemented a robust fallback mechanism that prioritizes FlashAttention 4, then FlashAttention 3, and finally FlashAttention 2 if newer versions are not available.
  • Output Handling: Updated the forward method to correctly handle potential tuple outputs from flash_attn_func, ensuring only the primary output is used.
  • Version Logging: Introduced logging to clearly indicate which FlashAttention backend version is currently being utilized.

🧠 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
  • fastvideo/attention/backends/flash_attn.py
    • Implemented a version-aware import strategy for FlashAttention, prioritizing v4, then v3, then v2.
    • Added logging to report the active FlashAttention version.
    • Adjusted the forward method to correctly process tuple outputs from FlashAttention functions.
Activity
  • No human activity has been recorded for this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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.

Comment on lines +15 to +29
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
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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

@SolitaryThinker SolitaryThinker added the go Trigger Buildkite CI label Feb 19, 2026
@XOR-op XOR-op marked this pull request as draft February 19, 2026 21:05
@XOR-op XOR-op removed the go Trigger Buildkite CI label Feb 19, 2026
@XOR-op
Copy link
Collaborator Author

XOR-op commented Feb 19, 2026

Converted to WIP: after discussion with @jzhang38, we decide we should add a new FA4 backend instead and wrap it in a custom op

@XOR-op XOR-op marked this pull request as ready for review February 24, 2026 23:36
@XOR-op XOR-op added the go Trigger Buildkite CI label Feb 24, 2026
@XOR-op XOR-op requested a review from jzhang38 February 25, 2026 20:29
@SolitaryThinker
Copy link
Collaborator

Do we need to gate fa4 behind cuda arch?

@XOR-op
Copy link
Collaborator Author

XOR-op commented Feb 25, 2026

Added the gate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Trigger Buildkite CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants