Repository navigation
Add Support for Page Size greater than 1 for Flashinfer MLA Backend - #8593
Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @pavanimajety, 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 significantly enhances the Flashinfer MLA backend by introducing support for page sizes greater than 1. This change is fundamental for optimizing memory utilization and performance in paged attention systems, as it allows the KV cache to be managed in larger, contiguous blocks. The modifications span core attention logic, Triton kernels for index generation and memory allocation, and corresponding test updates to ensure correctness and compatibility with the new page-aware scheme.
Highlights
- Flashinfer MLA Page Size Support: Implemented support for page sizes greater than 1 within the Flashinfer Multi-Head Latent Attention (MLA) backend. This enables more efficient memory management by allowing KV cache entries to be stored and accessed in larger, page-aligned blocks.
- KV Cache Indexing Logic Update: Modified the
create_flashinfer_kv_indices_tritonkernel to correctly generate KV indices based on page numbers (token_id // PAGE_SIZE) rather than individual token IDs. This change is crucial for Flashinfer to correctly interpret the paged KV cache. - Attention Backend Integration: Updated the
FlashInferMLABackendand its associatedIndicesUpdaterclasses to pass the configuredpage_sizeto Flashinfer'splancalls and to reshape KV buffers (k_buf,k_buffer) appropriately for page-based indexing duringforward_extendandforward_decodeoperations. - Triton Kernel Documentation: Added comprehensive docstrings to several Triton kernels (
alloc_extend_kernel,set_mla_kv_buffer_kernel,set_mla_kv_buffer_triton) to clearly explain their functionality, arguments, and the underlying page-aware allocation and data transfer logic. - Test Coverage Expansion: Enhanced unit tests for KV index creation (
test_create_kvindices.py) to validate the new page-based indexing logic across various batch sizes and page sizes. Additionally, several Flash Attention test flags were enabled, and test models/configurations were updated intest_flashmla.pyfor broader validation.
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 page sizes greater than one in the Flashinfer MLA backend, which is a significant enhancement for paged attention mechanisms. The changes primarily involve updating how KV cache indices are calculated and passed to the attention kernels, moving from token-based indexing to page-based indexing.
Overall, the changes are in the right direction. The core logic for handling paged attention seems mostly correct, with updates to the Triton kernels and the backend code that calls them. However, I've identified a couple of critical bugs in the Triton kernels related to loop bounds that could lead to incorrect behavior or incomplete processing of pages. Additionally, there are several instances of commented-out debugging code and TODOs that should be cleaned up before merging.
My review provides specific suggestions to fix the identified bugs and improve code hygiene.
956fdf9 to
e9529c9
Compare
|
Testing Cuda Graphs now, eager mode is working. |
Tested across various batch sizes as shown above. The server command used is: |
482e81e to
0b68dbe
Compare
|
PR is ready for preliminary review. Will clean up the changes made to tests. |
b0cbe1c to
4b8c365
Compare
Signed-off-by: Pavani Majety <[email protected]>
Signed-off-by: Pavani Majety <[email protected]>
|
With DP Attention and page size 16 With page size 1 TP Attention Page Size: 1 Page Size 16 Overall the perf seems pretty similar between page size 1 and page size > 1 |
Signed-off-by: Pavani Majety <[email protected]>
…to ps-greater-than-1
Signed-off-by: Pavani Majety <[email protected]>
…to ps-greater-than-1
Signed-off-by: Pavani Majety <[email protected]>
Signed-off-by: Pavani Majety <[email protected]>
|
The failed tests are not related to the PR - and the third error is @Fridge003 Please let me know if you would like any other verifications. |
…gl-project#8593) Signed-off-by: Pavani Majety <[email protected]>
…gl-project#8593) Signed-off-by: Pavani Majety <[email protected]>
Motivation
This change modifies the create_flashinfe_kv_indices kernelto take in page size as input
and return the paged kv indices. Follows a similar style as FlashMLA for converting req_to_token
mapping to page index for the token/request.
Modifications
create_flashinfer_kv_indices_tritonAccuracy Test
#8593 (comment)
Benchmark & Profiling
Checklist