Skip to content

Pading one more block for fa3 prefill#4674

Merged
lvhan028 merged 1 commit into
InternLM:mainfrom
RunningLeon:fix-fa3-prefill
Jun 18, 2026
Merged

Pading one more block for fa3 prefill#4674
lvhan028 merged 1 commit into
InternLM:mainfrom
RunningLeon:fix-fa3-prefill

Conversation

@RunningLeon

Copy link
Copy Markdown
Collaborator

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

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit tests to ensure the correctness.
  3. If the modification has a dependency on downstream projects of a newer version, this PR should be tested with all supported versions of downstream projects.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

@RunningLeon RunningLeon requested a review from grimoire June 11, 2026 12:59
@RunningLeon RunningLeon marked this pull request as ready for review June 18, 2026 03:48
Copilot AI review requested due to automatic review settings June 18, 2026 03:48

Copilot AI 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.

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 in FA3Impl._forward_prefill.
  • Pass attn_metadata.max_kv_seqlen (instead of total flattened size) as max_seqlen_k to FA3 varlen attention.
  • Add a regression unit test to assert the guarded flatten size and max_seqlen_k wiring 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
@lvhan028 lvhan028 merged commit 70d089c into InternLM:main Jun 18, 2026
5 checks passed
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.

4 participants