Repository navigation
Support MHA with chunked prefix cache for flashinfer/flashmla backend, support page size > 1 for MHA chunked prefix - #8616
Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @xu-yfei, 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 comprehensive support for Multi-Head Attention (MHA) with a chunked prefix cache within the FlashInfer backend. The primary goal is to significantly enhance performance, particularly for processing very long sequences, by optimizing KV cache management. This involves adding new attention logic, a dedicated runner for chunked operations, and updating the attention dispatch mechanism to leverage these improvements.
Highlights
- FlashInfer Chunked MHA Support: Implemented the necessary logic to enable Multi-Head Attention (MHA) with a chunked prefix cache when using the FlashInfer attention backend. This allows for more efficient processing of long sequences.
- Performance Optimization: Benchmarking results demonstrate significant performance improvements (reduced TTFT - Time To First Token) for long context lengths (e.g., 6*8192+646) when the chunked prefix cache is enabled with FlashInfer, making it more competitive with or superior to other backends like FA3 in these scenarios.
- Dedicated Chunking Runner: Introduced a new
FlashInferMhaChunkKVRunnerclass to encapsulate the specific operations and metadata management required for FlashInfer's chunked KV cache attention, promoting modularity and clarity. - Dynamic Attention Dispatch: Updated the model's attention dispatch mechanism to intelligently select the new
MHA_CHUNKED_KVmethod for FlashInfer when processing extended sequences that meet the defined chunking criteria, ensuring the optimized path is used automatically.
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 MHA with chunked prefix cache for the flashinfer backend, which shows significant performance improvements for long sequences. The implementation is well-structured, introducing a new FlashInferMhaChunkKVRunner class to handle the new attention logic. I've identified a critical issue in deepseek_v2.py that could lead to a runtime error due to a potential UnboundLocalError. I've also pointed out a minor maintainability issue regarding a magic number. Once these are addressed, the PR should be in great shape.
|
@xu-yfei Thank you for your contribution~ Let me review for this weekend |
3a6d514 to
d87bc66
Compare
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
1 similar comment
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
@xu-yfei Just added a few comments. Please have a look. |
8931182 to
eefa65d
Compare
@Fridge003 The logic of flashinfer and fa3 has been combined:
|
|
@Fridge003 I would like to ask an additional question: What is the reason that chunk kv is disabled when page size > 1? The page size should only affect the allocation of kvcache, i.e., whether it is continuous blocks of tokens with a size of page-size. It is a special case where kv tokens with page-size=1 are distributed relatively randomly. I deleted the following code, and flashmla chunk kv can run normally. # srt/model_executor/model_runner.py
elif self.page_size > 1:
logger.info("Disable chunked prefix cache when page size > 1.")
server_args.disable_chunked_prefix_cache = True |
467c3ba to
bf967fb
Compare
OK, currently PR page size>1 is only for mha with chunk prefix cache, the title has been changed. |
|
@zhyncs This PR has not been reviewed by the code owner for a long time. In the PD scenario, this PR can significantly reduce the time consumption of TTFT. |
|
hi, may I know whether it is fp8 or bf16 (or both is supported)? |
Flashinfer MHA supports bfloat16 with normal precision. However, errors occur when using flashinfer MHA with fp8: Therefore, the K and V in MHA will first be converted to bfloat16.
|
@fzyzcjy @Fridge003 Correcting the previous conclusion about precision degradation: After rerunning the FP8 KV cache test, both FA3 and FlashInfer show normal accuracy. The dataset used was Frame Oracle with long-text inputs, averaging 15K context length. Implementation details: For FA3: Input Q, K, V are in FP8 format, with Q forcibly cast to the FP8 data type of K and V. For FlashInfer: Input K and V are converted from FP8 to bfloat16 to match Q’s data type. Performance improvement: |
|
Hi @xu-yfei , is it possible to add some accuracy results for GPQA dataset, for checking long context correctness? A sample command for running gpqa: OPENAI_API_KEY=dummy python -m gpt_oss.evals --base-url http://localhost:40010/v1 --model dummy --reasoning-effort medium --eval gpqa --n-threads 1000 |
Before PR, 'score': 0.7001262626262627 |
|
@Fridge003
|
@Fridge003 |
…, support page size > 1 for MHA chunked prefix (sgl-project#8616) Co-authored-by: xuyongfei.xyf <[email protected]>
…, support page size > 1 for MHA chunked prefix (sgl-project#8616) Co-authored-by: xuyongfei.xyf <[email protected]>
Motivation
Based on #5113, for issue #6959, support flashinfer and flashmla (actually flashinfer as well) backend. remove the page-size=1 restriction: support page size > 1.
Modifications
Accuracy Test
gsm8k flashinfer page-size=1
gsm8k flashmla page-size=64
mmlu flashinfer page-size=1
mmlu flashmla page-size=64
frame oracle, avg input len 15767, in 8*H20(96GB)
normal range: 80.46 to 82.04, with possible accuracy fluctuations.
flashinfer page size 1, mixed, disable chunk: 80.83
enable chunk kv:
flashmla page size 64, without mixed: 81.67
flashinfer page size 64, enable mixed: 80.10
flashinfer page size 1, mixed: 81.92
Benchmark & Profiling
add
--disable-chunked-prefix-cachedisable chunked prefix.flashinfer MHA with chunked prefix:
flashinfer MHA without chunked prefix:
compared to fa3 backend with chunked prefix
profiler detail context len: 6*8192+646
with mha chunked prefix
without mha chunked prefix
flashinfer mha chunked prefix increase attention time cost from (19807+160) to (4862+965), time cost was reduced by 14140 ms, which is consistent with the reduction of ttft from 23.99 to 10.22.
Checklist