Repository navigation
add flash linear attention triton kernel - #10239
Conversation
There was a problem hiding this comment.
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
-
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. ↩
There was a problem hiding this comment.
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.
| raise DeprecationWarning( | ||
| "head_first is deprecated and will be removed in a future version. " | ||
| "Please use head_first=False for now instead." | ||
| ) |
There was a problem hiding this comment.
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
)| 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]: |
There was a problem hiding this comment.
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]:
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