feat: openai oss attention sink support with trtllm-gen backend#8825
feat: openai oss attention sink support with trtllm-gen backend#8825yyihuang wants to merge 5 commits intosgl-project:oss-oaifrom
Conversation
Co-authored-by: averyhuang <averyh@nvidia.com>
There was a problem hiding this comment.
Summary of Changes
Hello @yyihuang, 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 support for the 'attention sink' mechanism within the trtllm-gen backend, specifically for OpenAI OSS models. The changes integrate a new attention_sink parameter into the multi-head attention forward passes, allowing for an additional value to be applied per head in the softmax denominator, which can be crucial for managing context and improving model stability or performance in long sequences.
Highlights
- Attention Sink Variable Initialization: Introduced an
attention_sinkvariable intrtllm_mha_backend.pythat conditionally holdslayer.attention_sinksiflayer.enable_attention_sinkis true, otherwiseNone. This variable is intended to represent an additional value per head for the softmax denominator in attention calculations. - Decode Path Integration: The newly defined
attention_sinkvariable is now passed as asinkargument to the underlying attention computation function within theforward_decodemethod, enabling attention sink functionality during single token generation (lines 267-268). - Extend Path Integration: Similarly, the
attention_sinkvariable is also passed as asinkargument to the attention computation function within theforward_extendmethod, extending attention sink support to the context processing/prefill phase (lines 324-325).
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 or fill out our survey to provide feedback.
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 support for attention sinks in the TRT-LLM MHA backend, which is a great feature.
I've identified a couple of areas for improvement regarding code maintainability:
- Code Duplication: There's an opportunity to refactor duplicated logic for determining the
attention_sinkvalue into a helper method. This aligns with the DRY (Don't Repeat Yourself) principle. - Stale TODOs: The
TODOcomments related to adding attention sink support should be updated or removed now that the feature is being implemented to avoid confusion.
A significant concern is the lack of tests for this new functionality. The PR checklist for adding unit tests is also unchecked. Adding tests would be crucial to ensure correctness and prevent future regressions.
Please see my detailed comments on the code. Overall, good work on adding this feature.
| bmm2_scale=bmm2_scale, | ||
| window_left=self.sliding_window_size, | ||
| # TODO: add attention_sink operation or nvfp4 scale factor if needed | ||
| sink=attention_sink, |
There was a problem hiding this comment.
With the addition of the sink parameter, the TODO comment on the preceding line (266) appears to be at least partially outdated. It's good practice to update or remove TODO comments when the work is done to avoid confusion for future developers. If the nvfp4 scale factor part is still pending, the comment could be updated to reflect only that.
| # TODO: bmm1_scale and bmm2_scale might require modification | ||
| # TODO: Change once quantization is supported | ||
| # sink: additional value per head in the denominator of the softmax. | ||
| attention_sink = layer.attention_sinks if layer.enable_attention_sink else None |
| cum_seq_lens_kv=self.forward_metadata.cu_seqlens_k, | ||
| window_left=self.sliding_window_size, | ||
| # TODO: add attention_sink operation or nvfp4 scale factor if needed | ||
| sink=attention_sink, |
…roject#8825 (sgl-project#8834) Co-authored-by: averyhuang <averyh@nvidia.com>
…roject#8825 (sgl-project#8834) Co-authored-by: averyhuang <averyh@nvidia.com>
Motivation
Modifications
Accuracy Test
python3 -m sglang.launch_server --model-path lmsys/gpt-oss-20b-bf16 --trust-remote-code --attention-backend trtllm_mha --enable-triton-kernel-moe --mem-fraction-static 0.7 --tp-size 8 --disable-cuda-graph --disable-hybrid-swa-memorythroughput
python3 benchmark/gsm8k/bench_sglang.py --num-shots 8 --num-questions 1000 --parallel 1000lm_eval
Benchmark & Profiling
Checklist