Skip to content

Conversation

@bingchenlll
Copy link
Contributor

This PR adds support for Flash Attention 4.
Tested on Qwen-Image, generated images look good. Compared to FA3, there is 15% speed improvement on B200, and no speed improvement on H100.

Installation of FA4:

git clone https://github.com/Dao-AILab/flash-attention.git
cd flash-attention
pip install flash_attn/cute

Use of FA4:
from flash_attn.cute.interface import flash_attn_func as flash_attn4

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @bingchenlll, 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 introduces Flash Attention 4 (FA4) as a new, high-performance attention mechanism. The primary goal is to enhance computational efficiency, particularly on B200 GPUs, by providing an optimized alternative to existing attention implementations. The changes involve updating configuration options, dynamically importing the FA4 library, and integrating it into the core attention function with appropriate compatibility checks.

Highlights

  • Flash Attention 4 Support: Integrated Flash Attention 4 (FA4) into the system, adding it as a new attention implementation option.
  • Performance Improvements: FA4 demonstrates a 15% speed improvement on B200 GPUs compared to Flash Attention 3, though no speedup was observed on H100 GPUs.
  • Compatibility and Limitations: FA4 shares the same head dimension limitation as FA3 (maximum 256) and currently does not support attention masks.
  • Dynamic Availability Check: The system now dynamically checks for the availability of Flash Attention 4 and logs its status upon startup.

🧠 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.

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 Flash Attention 4, including detection of its availability and integration into the attention mechanism. The changes look good and follow the existing structure for adding new attention implementations. I've provided a couple of suggestions to improve code maintainability by reducing duplication and simplifying logging statements. Overall, a good contribution.

Comment on lines +242 to +245
attn_out = flash_attn4(q, k, v, softmax_scale=scale)
if isinstance(attn_out, tuple):
attn_out = attn_out[0]
return attn_out
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This block of code for calling flash_attn4 and processing its output is identical to the one in lines 161-164. Duplicating code can lead to maintenance issues where a bug fix or change in one place is not applied to the other. It would be best to refactor this into a helper function to avoid repetition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm following the existing code pattern and don't want to introduce unnecessary refactoring.

Comment on lines +11 to +14
if FLASH_ATTN_4_AVAILABLE:
logger.info("Flash attention 4 is available")
else:
logger.info("Flash attention 4 is not available")
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This if/else block for logging availability is a common pattern in this file. You can simplify this and reduce duplication with a single line of code using an f-string and a ternary operator.

logger.info(f"Flash attention 4 {'is' if FLASH_ATTN_4_AVAILABLE else 'is not'} available")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm following the existing code pattern and don't want to introduce unnecessary refactoring.

from yunchang import LongContextAttention
from yunchang.kernels import AttnType

assert attn_impl in [
Copy link
Contributor

Choose a reason for hiding this comment

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

@bingchenlll FA4 does not work with sequence parallel right now because of dependency of yunchang. We are considering a refactor of the attention backend to remove dependency, which is overlap with this PR and please expect changes will be overwritten.

@akaitsuki-ii akaitsuki-ii merged commit 7a93e13 into modelscope:main Jan 8, 2026
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.

2 participants