Optimize flash varlen paged KV cache addressing#3564
Open
yysheng26 wants to merge 1 commit into
Open
Conversation
0x45f
reviewed
May 28, 2026
| ).to(tl.int64) | ||
| else: | ||
| page_block_index = tl.load(page_table_ptr + virtual_page_index).to(tl.int64) | ||
| if IS_CONTIGUOUS_KVCACHE: |
Collaborator
There was a problem hiding this comment.
after we remove k_page_stride from do_not_specialize list, are we still need this if IS_CONTIGUOUS_KVCACHE ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Category
Benchmark
Type of Change
Performance Optimization
Description
Optimize paged KV cache address calculation in
flash_attn_varlen_func.This PR adds a contiguous KV cache fastpath and lets
k_page_strideparticipate in Triton kernel specialization. For contiguous paged KV cache, the kernel uses the simpler row-id based offset formula. For non-contiguous paged KV cache, specializingk_page_strideavoids the slow generic runtime-stride address path.A non-contiguous KV cache benchmark is also added to cover the case where
k.stride(0) != block_size * k.stride(-3).Issue
Associated with PR #3410.
Progress
Performance
Benchmark command:
pytest benchmark/test_flash_attn_varlen_func.py -v -sLong paged KV case, fp16:
0.797 ms1.62 ms0.80 msThe non-contiguous long case is now close to the contiguous fastpath performance.