feat(inference): support sliding-window attention in the dynamic batc… - #5138
feat(inference): support sliding-window attention in the dynamic batc…#5138shanmugamr1992 wants to merge 1 commit into
Conversation
…hing path The dynamic batching engine calls flash_decode_and_prefill, which dispatched to FA2/FA3/FA4 with window_size hardcoded to full attention. The static path already honors config.window_size via the TE wrapper; this change brings the dynamic path to parity. For each call, resolve the per-layer window via is_layer_window_attention (the same helper the TE static path uses), then plumb the (left, right) tuple to every kernel: flash_attn4_varlen_func, the FA3 _flash_attn_forward wrapper, flash_attn_varlen_func, flash_attn3_with_kvcache, and flash_attn_with_kvcache (decode). FlashMLA does not support SWA, so the MLA branch asserts window_size == (-1, -1). Tested on H100/FA3 via cog with three SWA configs, including the gpt-oss configuration (window_size=(127, 0), window_attn_skip_freq=2). The regression test_simple (no-SWA path) still passes. Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: shanmugamr1992 <shanmugamr1992@gmail.com>
|
This PR has been automatically converted to draft because all PRs must start as drafts. When you are ready for review, click Ready for Review to begin the review process. This will:
See the contribution guide for more details. |
cuichenx
left a comment
There was a problem hiding this comment.
I left one inline note from the GPT-OSS/YARN dynamic-inference validation.
| if is_layer_window_attention( | ||
| self.config.window_size, self.config.window_attn_skip_freq, self.layer_number | ||
| ): | ||
| window_size = self.config.window_size |
There was a problem hiding this comment.
While validating GPT-OSS/YARN on this dynamic path, I found another required plumbing fix adjacent to the SWA window plumbing: dynamic RoPE currently needs the YaRN concentration factor passed through. Static and dynamic matched through hidden input and QKV projection, then diverged immediately after RoPE unless both dynamic key and query RoPE calls pass mscale=_yarn_get_concentration_factor_from_config(self.config) into inference_context.apply_rotary_emb_key(...) / apply_rotary_emb_query(...). Can you include that here or land it as a prerequisite? Without it, SWA+YARN models can diverge before the attention kernel sees matching Q/K.
…hing path
The dynamic batching engine calls flash_decode_and_prefill, which dispatched to FA2/FA3/FA4 with window_size hardcoded to full attention. The static path already honors config.window_size via the TE wrapper; this change brings the dynamic path to parity.
For each call, resolve the per-layer window via is_layer_window_attention (the same helper the TE static path uses), then plumb the (left, right) tuple to every kernel: flash_attn4_varlen_func, the FA3 _flash_attn_forward wrapper, flash_attn_varlen_func, flash_attn3_with_kvcache, and flash_attn_with_kvcache (decode). FlashMLA does not support SWA, so the MLA branch asserts window_size == (-1, -1).
Tested on H100/FA3 via cog with three SWA configs, including the gpt-oss configuration (window_size=(127, 0), window_attn_skip_freq=2). The regression test_simple (no-SWA path) still passes.
What does this PR do ?
Issue tracking
For PRs from open-source community contributors:
Linked issue:
Contribution process
Pre-checks
Code review
Feel free to message or comment @NVIDIA/mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!
All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.
Step 1: Mark PR as "Ready for Review"
.github/CODEOWNERS.Final Review might get declined if these requirements are not fulfilled.
Step 2: Final Review
For PRs that change
megatron/core, once all expert reviewers have approved, theFinal Reviewlabel is applied automatically and final reviewers are assigned.For PRs outside
megatron/core, this step is skipped.Step 3: Approved
Once all required reviewers have approved, the
Approvedlabel is applied automatically.Merge
Any member of mcore-engineers will be able to merge your PR.