Skip to content

[BugFix][Attention] Fix sliding window attention in V1 giving incorrect results #17574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

LucasWilkinson
Copy link
Collaborator

@LucasWilkinson LucasWilkinson commented May 2, 2025

The FA3 update to use a AOT scheduler (#13111) did not properly handle sliding window attention. FIX #17476

Tested using:

python -m pytest -vs tests/v1/e2e/test_correctness_sliding_window.py

Signed-off-by: Lucas Wilkinson <lwilkinson@neuralmagic.com>
Copy link

github-actions bot commented May 2, 2025

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

@mergify mergify bot added the v1 label May 2, 2025
@simon-mo simon-mo requested a review from Copilot May 2, 2025 05:08
Copy link
Contributor

@Copilot Copilot AI left a comment

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 an issue where the FA3 update did not properly handle sliding window attention by ensuring a consistent sliding window configuration is used with the AOT scheduler. Key changes include:

  • Introducing a new helper function (_get_sliding_window_configs) to extract sliding window configurations from the model.
  • Updating the FlashAttentionMetadataBuilder to initialize and propagate a uniform sliding window configuration.
  • Modifying the scheduling lambda to pass the sliding window configuration to the underlying flash attention kernel.
Comments suppressed due to low confidence (1)

vllm/v1/attention/backends/flash_attn.py:284

  • FlashAttentionImpl is referenced here but is not imported in this file. Consider adding an import for FlashAttentionImpl to ensure the assertion works correctly.
assert isinstance(layer.impl, FlashAttentionImpl)

@heheda12345
Copy link
Collaborator

getattr(self.hf_text_config, "sliding_window", None) checks whether this model only contains sliding window layers
getattr(hf_text_config, "interleaved_sliding_window", None) is not None checks whether this model contains both sliding window and non-sliding window.
Hope these two APIs can help to make this PR more clean.

@LucasWilkinson
Copy link
Collaborator Author

getattr(self.hf_text_config, "sliding_window", None) checks whether this model only contains sliding window layers getattr(hf_text_config, "interleaved_sliding_window", None) is not None checks whether this model contains both sliding window and non-sliding window. Hope these two APIs can help to make this PR more clean.

Thanks for the pointer! Ya I thought about that, but:

    def get_hf_config_sliding_window(
            self) -> Union[Optional[int], list[Optional[int]]]

can return a list of list[Optional[int]], and was sure how to handle that. So I decided to with this route so that way its a guaranteed source of truth, and we are robust against new models using something other than sliding_window or interleaved_sliding_window (or not respecting the precedent you mentioned).

Copy link
Collaborator

@heheda12345 heheda12345 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've confirmed that it solves my problem in #17476.

I can approve it as a quick bug fix, but want to do some refactor after #17483 which moves the initialization of attention metadata builder after model loading.

@heheda12345 heheda12345 added the ready ONLY add when PR is ready to merge/full CI is needed label May 2, 2025
@LucasWilkinson
Copy link
Collaborator Author

I can approve it as a quick bug fix, but want to do some refactor after #17483 which moves the initialization of attention metadata builder after model loading.

agreed, I think we should also unify with infer_global_hyperparameters in FlashInfer

@LucasWilkinson LucasWilkinson changed the title [Attention] Fix sliding window attention in V1 giving incorrect results [BugFix][Attention] Fix sliding window attention in V1 giving incorrect results May 2, 2025
@heheda12345 heheda12345 enabled auto-merge (squash) May 2, 2025 14:45
@simon-mo simon-mo added this to the v0.9.0 milestone May 2, 2025
@simon-mo simon-mo disabled auto-merge May 2, 2025 18:01
@simon-mo simon-mo merged commit 0f87d8f into vllm-project:main May 2, 2025
72 of 74 checks passed
simon-mo pushed a commit that referenced this pull request May 2, 2025
…ct results (#17574)

Signed-off-by: Lucas Wilkinson <lwilkinson@neuralmagic.com>
RichardoMrMu pushed a commit to RichardoMrMu/vllm that referenced this pull request May 12, 2025
…ct results (vllm-project#17574)

Signed-off-by: Lucas Wilkinson <lwilkinson@neuralmagic.com>
Signed-off-by: Mu Huai <tianbowen.tbw@antgroup.com>
dtrifiro pushed a commit to red-hat-data-services/vllm that referenced this pull request May 13, 2025
…ct results (vllm-project#17574)

Signed-off-by: Lucas Wilkinson <lwilkinson@neuralmagic.com>
dtrifiro pushed a commit to red-hat-data-services/vllm that referenced this pull request May 13, 2025
Syncing midstream NM fork to Upstream tag of
[v0.8.5.post1](https://github.com/vllm-project/vllm/tree/v0.8.5.post1) +
cherry pick of
vllm-project@be633fb
needed for benchmarks +
[CP](neuralmagic/nm-vllm-ent@1fe447d)
for compressed tensor bump +
[CP](vllm-project#17677) for lora on AMD +
[CP](vllm-project#17315) for llama4 w/ pure
dense layers

```
commit 31c73ba (HEAD -> upstream-v0.8.5, nm-fork/upstream-v0.8.5)
Author: Chauncey <chaunceyjiang@gmail.com>
Date:   Wed Apr 30 15:11:04 2025 +0800

    [Bugfix] Fix AttributeError: 'State' object has no attribute 'engine_client' (vllm-project#17434)
    
    Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>

commit f8db0bd
Author: Lucas Wilkinson <LucasWilkinson@users.noreply.github.com>
Date:   Fri May 2 14:01:38 2025 -0400

    [BugFix][Attention] Fix sliding window attention in V1 giving incorrect results (vllm-project#17574)
    
    Signed-off-by: Lucas Wilkinson <lwilkinson@neuralmagic.com>

commit e335c34
Author: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com>
Date:   Fri May 2 04:07:03 2025 -0400

    [BugFix] Fix Memory Leak (vllm-project#17567)
    
    Signed-off-by: rshaw@neuralmagic.com <robertgshaw2@gmail.com>

commit cc463fe
Merge: 1e358ff ba41cc9
Author: Selbi Nuryyeva <selbi@redhat.com>
Date:   Tue Apr 29 12:34:57 2025 -0400

    Merge branch 'tag-upstream-v0.8.5' into upstream-v0.8.5

commit ba41cc9 (tag: v0.8.5, tag-upstream-v0.8.5)
Author: Michael Goin <mgoin64@gmail.com>
Date:   Mon Apr 28 16:20:24 2025 -0600

    [Model] Add tuned triton fused_moe configs for Qwen3Moe (vllm-project#17328)
    
    Signed-off-by: mgoin <mgoin64@gmail.com>

commit dcbac4c
Author: Simon Mo <simon.mo@hey.com>
Date:   Mon Apr 28 14:12:01 2025 -0700

    [Model] Qwen3 Dense FP8 Compat Fixes (vllm-project#17318)
    
    Signed-off-by: simon-mo <xmo@berkeley.edu>
[...]
```

Commands
```
git fetch upstream
git checkout -b upstream-v0.8.5
git merge upstream/v0.8.5
git cherry-pick be633fb
```

TEST PLAN
accept sync:
https://github.com/neuralmagic/nm-cicd/actions/runs/14841223552
related PR in cicd: neuralmagic/nm-cicd#99
release workflow:
https://github.com/neuralmagic/nm-cicd/actions/runs/14845693864
mawong-amd pushed a commit to ROCm/vllm that referenced this pull request May 14, 2025
…ct results (vllm-project#17574)

Signed-off-by: Lucas Wilkinson <lwilkinson@neuralmagic.com>
zzzyq pushed a commit to zzzyq/vllm that referenced this pull request May 24, 2025
…ct results (vllm-project#17574)

Signed-off-by: Lucas Wilkinson <lwilkinson@neuralmagic.com>
Signed-off-by: Yuqi Zhang <yuqizhang@google.com>
minpeter pushed a commit to minpeter/vllm that referenced this pull request Jun 24, 2025
…ct results (vllm-project#17574)

Signed-off-by: Lucas Wilkinson <lwilkinson@neuralmagic.com>
Signed-off-by: minpeter <kali2005611@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready ONLY add when PR is ready to merge/full CI is needed v1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Flash attention with sliding window
3 participants