Skip to content

add flash linear attention triton kernel - #10239

Merged
zhyncs merged 2 commits into
mainfrom
fla_support
Sep 11, 2025
Merged

zhyncs merged 2 commits into
mainfrom
fla_support

Conversation

@yizhang2077

Copy link
Copy Markdown
Collaborator

Motivation

Modifications

add flash linear attention triton kernel for further model support #10233 , current models do not use it.

Accuracy Tests

Benchmarking and Profiling

Checklist

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Summary of Changes

Hello @yizhang2077, 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 integrates a Flash Linear Attention (FLA) Triton kernel into the system. This foundational addition aims to enhance the system's capability to support more sophisticated model architectures by providing a highly optimized linear attention mechanism. While not immediately utilized by current models, it lays the groundwork for future performance improvements and broader model compatibility.

Highlights

  • New Flash Linear Attention (FLA) Kernel: Introduced a new Flash Linear Attention Triton kernel, designed to support advanced model architectures and improve performance for linear attention mechanisms.
  • Modular Triton Implementations: The FLA kernel is implemented modularly, with several specialized Triton kernels for different components such as hidden state updates, output computation, scaled dot products, cumulative sums, and triangular matrix inversions.
  • Comprehensive Utility Additions: Added a suite of utility functions for Triton, including environment checks, tensor caching, input guarding, L2 normalization, and gated LayerNorm/RMSNorm, all contributing to the robustness and efficiency of the new attention mechanism.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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. ↩

@yizhang2077 yizhang2077 mentioned this pull request Sep 9, 2025
4 tasks

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

Copy link
Copy Markdown
Contributor

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 a suite of Triton kernels for flash linear attention, adapted from the flash-linear-attention library. The changes introduce new functionalities under sglang.srt.layers.attention.fla. The overall structure is sound, adapting specialized kernels for performance. My review focuses on correctness and maintainability. I've found a critical issue regarding an incorrect use of DeprecationWarning that leads to unreachable code, and a minor suggestion for code cleanup.

Comment on lines +201 to +204
raise DeprecationWarning(
"head_first is deprecated and will be removed in a future version. "
"Please use head_first=False for now instead."
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

The raise DeprecationWarning statement will halt execution and makes the subsequent code on lines 205-207 unreachable. To achieve the intended behavior of issuing a warning while allowing the code to proceed, you should use warnings.warn instead. This appears to be a deviation from the original implementation which uses warnings.warn.

        warnings.warn(
            "head_first is deprecated and will be removed in a future version. "
            "Please use head_first=False for now instead.",
            DeprecationWarning
        )

Comment on lines +252 to +262
def chunk_gated_delta_rule_fwd_h(
k: torch.Tensor,
w: torch.Tensor,
u: torch.Tensor,
g: Optional[torch.Tensor] = None,
initial_state: Optional[torch.Tensor] = None,
output_final_state: bool = False,
chunk_size: int = 64, # SY: remove this argument and force chunk size 64?
save_new_value: bool = True,
cu_seqlens: Optional[torch.LongTensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

As the comment on line 259 suggests, chunk_size is always 64. It can be removed from the function signature for clarity. You will also need to update line 265 to BT = 64 and line 268 to use BT.

def chunk_gated_delta_rule_fwd_h(
    k: torch.Tensor,
    w: torch.Tensor,
    u: torch.Tensor,
    g: Optional[torch.Tensor] = None,
    initial_state: Optional[torch.Tensor] = None,
    output_final_state: bool = False,
    save_new_value: bool = True,
    cu_seqlens: Optional[torch.LongTensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor]:

@zhyncs zhyncs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please add unit test

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants