Skip to content

feat(inference): support sliding-window attention in the dynamic batc… - #5138

Open
shanmugamr1992 wants to merge 1 commit into
NVIDIA:mainfrom
shanmugamr1992:shanmugamr/swa-dynamic-inference
Open

feat(inference): support sliding-window attention in the dynamic batc…#5138
shanmugamr1992 wants to merge 1 commit into
NVIDIA:mainfrom
shanmugamr1992:shanmugamr/swa-dynamic-inference

Conversation

@shanmugamr1992

Copy link
Copy Markdown
Contributor

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

  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do ?

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact @NVIDIA/mcore-oncall.

Issue tracking

For PRs from open-source community contributors:

  • New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
  • Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.

Linked issue:

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

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"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
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, the Final Review label 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 Approved label is applied automatically.

Merge

Any member of mcore-engineers will be able to merge your PR.

…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>
@shanmugamr1992
shanmugamr1992 requested review from a team as code owners June 3, 2026 17:09
@copy-pr-bot

copy-pr-bot Bot commented Jun 3, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft June 3, 2026 17:09
@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

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:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@shanmugamr1992
shanmugamr1992 marked this pull request as ready for review June 3, 2026 17:11

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

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

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.

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.

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.

3 participants