Skip to content

Add Support for Page Size greater than 1 for Flashinfer MLA Backend - #8593

Merged
zhyncs merged 30 commits into
sgl-project:mainfrom
pavanimajety:ps-greater-than-1
Aug 22, 2025
Merged

zhyncs merged 30 commits into
sgl-project:mainfrom
pavanimajety:ps-greater-than-1

Conversation

@pavanimajety

@pavanimajety pavanimajety commented Jul 31, 2025 •

Copy link
Copy Markdown
Collaborator

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

  1. Changes the logic of the triton kernel create_flashinfer_kv_indices_triton
  2. Changes the plan metadata preparation for decode updater and prefill updater.

Accuracy Test

#8593 (comment)

Batch Size Page Size Accuracy toks/s Latency
512 16 0.963 1362 98
1024 16 0.958 1417 94
1024 1 0.955 1379 98

Benchmark & 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 @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_triton kernel 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 FlashInferMLABackend and its associated IndicesUpdater classes to pass the configured page_size to Flashinfer's plan calls and to reshape KV buffers (k_buf, k_buffer) appropriately for page-based indexing during forward_extend and forward_decode operations.
  • 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 in test_flashmla.py for 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

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

@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 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.

Comment thread test/srt/test_create_kvindices.py Resolved
@pavanimajety

Copy link
Copy Markdown
Collaborator Author

Testing Cuda Graphs now, eager mode is working.

@pavanimajety

Copy link
Copy Markdown
Collaborator Author
root@umbriel-b200-005:/workspace/scratch-pmaj-1/gh-pm-sglang# python3 benchmark/gsm8k/bench_sglang.py --num-shots 8 --num-questions 256 --parallel 128 --port=8000
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 256/256 [00:37<00:00,  6.81it/s]
Accuracy: 0.977
Invalid: 0.000
Latency: 37.702 s
Output throughput: 662.118 token/s
root@umbriel-b200-005:/workspace/scratch-pmaj-1/gh-pm-sglang# python3 benchmark/gsm8k/bench_sglang.py --num-shots 8 --num-questions 1319 --parallel 128 --port=8000
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1319/1319 [02:11<00:00, 10.02it/s]
Accuracy: 0.959
Invalid: 0.000
Latency: 131.705 s
Output throughput: 1018.955 token/s
root@umbriel-b200-005:/workspace/scratch-pmaj-1/gh-pm-sglang# python3 benchmark/gsm8k/bench_sglang.py --num-shots 8 --num-questions 1319 --parallel 256  --port=8000
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1319/1319 [01:27<00:00, 15.03it/s]
Accuracy: 0.962
Invalid: 0.000
Latency: 87.839 s
Output throughput: 1533.251 token/s
root@umbriel-b200-005:/workspace/scratch-pmaj-1/gh-pm-sglang# python3 benchmark/gsm8k/bench_sglang.py --num-shots 8 --num-questions 1319 --parallel 512  --port=8000
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1319/1319 [01:12<00:00, 18.21it/s]
Accuracy: 0.960
Invalid: 0.000
Latency: 72.497 s
Output throughput: 1876.438 token/s
root@umbriel-b200-005:/workspace/scratch-pmaj-1/gh-pm-sglang# python3 benchmark/gsm8k/bench_sglang.py --num-shots 8 --num-questions 1319 --parallel 1024  --port=8000
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1319/1319 [00:48<00:00, 26.97it/s]
Accuracy: 0.956
Invalid: 0.000
Latency: 48.994 s
Output throughput: 2768.887 token/s

Tested across various batch sizes as shown above. The server command used is:

python3 -m sglang.launch_server --trust-remote-code --enable-dp-attention --max-running-requests 1024 --chunked-prefill-size 32768 --mem-fraction-static 0.85 --cuda-graph-max-bs 1024 --max-prefill-tokens 32768 --attention-backend flashinfer --model-path=/tmp/DeepSeek-R1-FP4/snapshots/574fdb8a5347fdbc06b2c18488699c0c17d71e05/ --host 0.0.0.0 --port 8000 --tensor-parallel-size=4 --data-parallel-size=4  --quantization=modelopt_fp4 --page-size=16

@pavanimajety

Copy link
Copy Markdown
Collaborator Author

PR is ready for preliminary review. Will clean up the changes made to tests.

@yyihuang
yyihuang self-requested a review August 8, 2025 07:33
@pavanimajety

Copy link
Copy Markdown
Collaborator Author

With DP Attention and page size 16

Server: 
 python3 -m sglang.launch_server --trust-remote-code  --attention-backend flashinfer --enable-dp-attention --disable-radix-cache --max-running-requests 1024 --chunked-prefill-size 32768 --mem-fraction-static 0.85 --cuda-graph-max-bs 1024 --max-prefill-tokens 32768 --model-path=/tmp/DeepSeek-R1-FP4/snapshots/574fdb8a5347fdbc06b2c18488699c0c17d71e05/ --host 0.0.0.0 --port 8000 --tensor-parallel-size=4 --data-parallel-size=4 --enable-flashinfer-trtllm-moe --quantization modelopt_fp4 --page-size 16

Client:

python3 benchmark/gsm8k/bench_sglang.py --num-shots 8 --num-questions 1319 --parallel 1319 --port=8000 
100%|███████████████████████████████████████████████████████████████████| 1319/1319 [01:23<00:00, 15.73it/s]
Accuracy: 0.943
Invalid: 0.010
Latency: 84.651 s
Output throughput: 1642.907 token/s

With page size 1

Server: 

python3 -m sglang.launch_server --attention-backend flashinfer --trust-remote-code --enable-dp-attention --disable-radix-cache --max-running-requests 1024 --chunked-prefill-size 32768 --mem-fraction-static 0.85 --cuda-graph-max-bs 1024 --max-prefill-tokens 32768  --model-path=/tmp/DeepSeek-R1-FP4/snapshots/574fdb8a5347fdbc06b2c18488699c0c17d71e05/ --host 0.0.0.0 --port 8000 --tensor-parallel-size=4 --data-parallel-size=4 --enable-flashinfer-trtllm-moe --quantization modelopt_fp4 --page-size 1

Client:
python3 benchmark/gsm8k/bench_sglang.py --num-shots 8 --num-questions 1319 --parallel 1319 --port=8000
100%|██████████████████████████████████████████████████████████| 1319/1319 [01:21<00:00, 16.27it/s]
Accuracy: 0.941
Invalid: 0.013
Latency: 81.316 s
Output throughput: 1752.122 token/s

TP Attention

Page Size: 1

 python3 -m sglang.launch_server --trust-remote-code --d
isable-radix-cache --max-running-requests 1024 --chunked-prefill-size 32768 --mem-fraction-static 0.85      --cuda-gr
aph-max-bs 1024 --max-prefill-tokens 32768 --attention-backend flashinfer --model-path=/tmp/DeepSeek-R1-FP4/snapshots
/574fdb8a5347fdbc06b2c18488699c0c17d71e05/ --host 0.0.0.0 --port 8000 --tensor-parallel-size=4 --enable-flashinfer-tr
tllm-moe --quantization modelopt_fp4 --page-size 1

python3 benchmark/gsm8k/bench_sglang.py --num-shots 8 --num-questions 1319 --parallel 1319 --port=8000
100%|██████████████████████████████████████████████████████████| 1319/1319 [01:33<00:00, 14.09it/s]
Accuracy: 0.962
Invalid: 0.000
Latency: 93.835 s
Output throughput: 1418.619 token/s

Page Size 16

Server: 

python3 -m sglang.launch_server --trust-remote-code --disable-radix-cache --max-running-requests 1024 --chunked-prefill-size 32768 --mem-fraction-static 0.85      --cuda-graph-max-bs 1024 --max-prefill-tokens 32768 --attention-backend flashinfer --model-path=/tmp/DeepSeek-R1-FP4/snapshots/574fdb8a5347fdbc06b2c18488699c0c17d71e05/ --host 0.0.0.0 --port 8000 --tensor-parallel-size=4 --enable-flashinfer-trtllm-moe --quantization modelopt_fp4 --page-size 16

Client:

python3 benchmark/gsm8k/bench_sglang.py --num-shots 8 --num-questions 1319 --parallel 1319 --port=8000
100%|██████████████████████████████████████████████████████████████████████████████| 1319/1319 [01:29<00:00, 14.81it/s]
Accuracy: 0.961
Invalid: 0.000
Latency: 89.187 s
Output throughput: 1498.503 token/s

Overall the perf seems pretty similar between page size 1 and page size > 1

@pavanimajety

Copy link
Copy Markdown
Collaborator Author

The failed tests are not related to the PR -
gpt-oss test

  File "/public_sglang_ci/runner-l1e-gpu-4567/_work/sglang/sglang/python/sglang/srt/models/gpt_oss.py", line 772, in _load_weights_mxfp4
    mxfp4_loaded_params = self._load_mxfp4_experts_weights(mxfp4_weights)
  File "/public_sglang_ci/runner-l1e-gpu-4567/_work/sglang/sglang/python/sglang/srt/models/gpt_oss.py", line 860, in _load_mxfp4_experts_weights
    weight_loader(
  File "/public_sglang_ci/runner-l1e-gpu-4567/_work/sglang/sglang/python/sglang/srt/layers/moe/fused_moe_triton/layer.py", line 486, in weight_loader
    param.data[:, :dim1, :dim2].copy_(loaded_weight)
RuntimeError: The size of tensor a (360) must match the size of tensor b (368) at non-singleton dimension 2

and the third error is

huggingface_hub.errors.HfHubHTTPError: 504 Server Error: Gateway Timeout for url: https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct/resolve/main/hf_quant_config.json (Request ID: Root=1-68a79824-4e3205d92ec11b526719cd64;f928ca63-f461-400e-b6d0-d31b91b46bb1)

The request is taking longer than expected, please try again later.

@Fridge003 Please let me know if you would like any other verifications.

@Fridge003 Fridge003 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@zhyncs
zhyncs merged commit 3cc3d9b into sgl-project:main Aug 22, 2025
zhyncs added a commit that referenced this pull request Aug 25, 2025
@gemini-code-assist gemini-code-assist Bot mentioned this pull request Aug 25, 2025
4 tasks
zhyncs added a commit that referenced this pull request Aug 25, 2025
MahmoudAshraf97 pushed a commit to MahmoudAshraf97/sglang that referenced this pull request Sep 8, 2025
MahmoudAshraf97 pushed a commit to MahmoudAshraf97/sglang that referenced this pull request Sep 8, 2025
0826joyce pushed a commit to 0826joyce/sglang-perf-opt that referenced this pull request May 19, 2026
0826joyce pushed a commit to 0826joyce/sglang-perf-opt that referenced this pull request May 19, 2026
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.

9 participants