Pading one more block for fa3 prefill#4674
Merged
Merged
Conversation
ec90f8a to
7ddd64d
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes FA3 prefill behavior when using recycled paged KV blocks by padding the flattened KV buffer by one additional cache block and by passing the correct max_seqlen_k (per-sequence max KV length) into FA3’s varlen flash-attention call.
Changes:
- Pad
flatten_kv_cache(..., out_size=...)to the next block multiple plus one extra block inFA3Impl._forward_prefill. - Pass
attn_metadata.max_kv_seqlen(instead of total flattened size) asmax_seqlen_kto FA3 varlen attention. - Add a regression unit test to assert the guarded flatten size and
max_seqlen_kwiring for FA3 prefill.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lmdeploy/pytorch/backends/cuda/attention/fa3.py | Pads flattened KV buffer by one extra block and fixes max_seqlen_k for FA3 prefill. |
| tests/pytorch/kernel/test_fa3_attention.py | Adds regression test validating guarded flatten size and correct max_seqlen_k usage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| out = impl._forward_prefill(query, k_cache, v_cache, metadata, max_q_seqlen=int(q_seqlens.max().item())) | ||
|
|
||
| assert out.shape == query.shape | ||
| assert captured['flatten_start_loc'] is metadata.kv_start_loc |
grimoire
approved these changes
Jun 18, 2026
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.
Motivation
Pading one more block for fa3 prefill
Modification
Please briefly describe what modification is made in this PR.
BC-breaking (Optional)
Does the modification introduce changes that break the backward-compatibility of the downstream repositories?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.
Use cases (Optional)
If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.
Checklist