Skip to content

Apply local sliding-window mask in StaticAttentionIOManager single-chunk prefill (#21384) - #21384

Open
YIWENX14 wants to merge 1 commit into
pytorch:mainfrom
YIWENX14:export-D113574455
Open

Apply local sliding-window mask in StaticAttentionIOManager single-chunk prefill (#21384)#21384
YIWENX14 wants to merge 1 commit into
pytorch:mainfrom
YIWENX14:export-D113574455

Conversation

@YIWENX14

@YIWENX14 YIWENX14 commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Summary:

StaticAttentionIOManager.prefill applied a full-causal input mask to every
layer. For local (sliding-window) attention layers, the window is normally
realized via KV-cache eviction across prefill chunks. But the numeric-comparison
harness prefills the whole prompt in a single chunk, so the cache-eviction
window is never exercised and local layers attend to the entire causal history
instead of their cache_len-sized window. This diverged from the rlformers
reference (which applies the correct local window via BlockDiagonalCausalMask)
and made local-global (LGA) checkpoints look badly degraded in numeric SNR
comparisons, while global-only checkpoints were unaffected.

Fix: for local layers (0 < cache_len < input_len), also mask keys older than
the window by adding tril(diagonal=-cache_len). No-op for global layers
(cache_len >= input_len) and on-device chunked prefill (input_len <= cache_len); skip layers (cache_len == 0) are excluded.

Input mask, input_len=5, local window (cache_len)=2:

Before (full causal, applied to ALL layers):

       j=0   1    2    3    4
i=0     0  -inf -inf -inf -inf
i=1     0    0  -inf -inf -inf
i=2     0    0    0  -inf -inf
i=3     0    0    0    0  -inf
i=4     0    0    0    0    0     <- attends to all of j=0..4

After (local layer, window=2):

       j=0   1    2    3    4
i=0     0  -inf -inf -inf -inf
i=1     0    0  -inf -inf -inf
i=2   -inf   0    0  -inf -inf
i=3   -inf -inf   0    0  -inf
i=4   -inf -inf -inf   0    0     <- attends to j=3,4 only (window=2)

Reviewed By: billmguo

Differential Revision: D113574455

@pytorch-bot

pytorch-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21384

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 Cancelled Job

As of commit 91d3836 with merge base b3d69a9 (image):

CANCELLED JOB - The following job was cancelled. Please retry:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 24, 2026
@meta-codesync

meta-codesync Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

@YIWENX14 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D113574455.

@YIWENX14

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: none"

@pytorch-bot pytorch-bot Bot added the release notes: none Do not include this in the release notes label Jul 24, 2026
@meta-codesync meta-codesync Bot changed the title Apply local sliding-window mask in StaticAttentionIOManager single-chunk prefill Apply local sliding-window mask in StaticAttentionIOManager single-chunk prefill (#21384) Jul 27, 2026
YIWENX14 added a commit to YIWENX14/executorch that referenced this pull request Jul 27, 2026
…unk prefill (pytorch#21384)

Summary:

`StaticAttentionIOManager.prefill` applied a full-causal input mask to every
layer. For local (sliding-window) attention layers, the window is normally
realized via KV-cache eviction across prefill chunks. But the numeric-comparison
harness prefills the whole prompt in a single chunk, so the cache-eviction
window is never exercised and local layers attend to the entire causal history
instead of their `cache_len`-sized window. This diverged from the rlformers
reference (which applies the correct local window via BlockDiagonalCausalMask)
and made local-global (LGA) checkpoints look badly degraded in numeric SNR
comparisons, while global-only checkpoints were unaffected.

Fix: for local layers (`0 < cache_len < input_len`), also mask keys older than
the window by adding `tril(diagonal=-cache_len)`. No-op for global layers
(`cache_len >= input_len`) and on-device chunked prefill (`input_len <=
cache_len`); skip layers (`cache_len == 0`) are excluded.

Input mask, input_len=5, local window (cache_len)=2:

Before (full causal, applied to ALL layers):
```
       j=0   1    2    3    4
i=0     0  -inf -inf -inf -inf
i=1     0    0  -inf -inf -inf
i=2     0    0    0  -inf -inf
i=3     0    0    0    0  -inf
i=4     0    0    0    0    0     <- attends to all of j=0..4
```

After (local layer, window=2):
```
       j=0   1    2    3    4
i=0     0  -inf -inf -inf -inf
i=1     0    0  -inf -inf -inf
i=2   -inf   0    0  -inf -inf
i=3   -inf -inf   0    0  -inf
i=4   -inf -inf -inf   0    0     <- attends to j=3,4 only (window=2)
```

Reviewed By: billmguo

Differential Revision: D113574455
@YIWENX14
YIWENX14 force-pushed the export-D113574455 branch 2 times, most recently from 1fb1945 to b5446d4 Compare July 27, 2026 18:06
YIWENX14 added a commit to YIWENX14/executorch that referenced this pull request Jul 27, 2026
…unk prefill (pytorch#21384)

Summary:

`StaticAttentionIOManager.prefill` applied a full-causal input mask to every
layer. For local (sliding-window) attention layers, the window is normally
realized via KV-cache eviction across prefill chunks. But the numeric-comparison
harness prefills the whole prompt in a single chunk, so the cache-eviction
window is never exercised and local layers attend to the entire causal history
instead of their `cache_len`-sized window. This diverged from the rlformers
reference (which applies the correct local window via BlockDiagonalCausalMask)
and made local-global (LGA) checkpoints look badly degraded in numeric SNR
comparisons, while global-only checkpoints were unaffected.

Fix: for local layers (`0 < cache_len < input_len`), also mask keys older than
the window by adding `tril(diagonal=-cache_len)`. No-op for global layers
(`cache_len >= input_len`) and on-device chunked prefill (`input_len <=
cache_len`); skip layers (`cache_len == 0`) are excluded.

Input mask, input_len=5, local window (cache_len)=2:

Before (full causal, applied to ALL layers):
```
       j=0   1    2    3    4
i=0     0  -inf -inf -inf -inf
i=1     0    0  -inf -inf -inf
i=2     0    0    0  -inf -inf
i=3     0    0    0    0  -inf
i=4     0    0    0    0    0     <- attends to all of j=0..4
```

After (local layer, window=2):
```
       j=0   1    2    3    4
i=0     0  -inf -inf -inf -inf
i=1     0    0  -inf -inf -inf
i=2   -inf   0    0  -inf -inf
i=3   -inf -inf   0    0  -inf
i=4   -inf -inf -inf   0    0     <- attends to j=3,4 only (window=2)
```

Reviewed By: billmguo

Differential Revision: D113574455
YIWENX14 added a commit to YIWENX14/executorch that referenced this pull request Jul 28, 2026
…unk prefill (pytorch#21384)

Summary:

`StaticAttentionIOManager.prefill` applied a full-causal input mask to every
layer. For local (sliding-window) attention layers, the window is normally
realized via KV-cache eviction across prefill chunks. But the numeric-comparison
harness prefills the whole prompt in a single chunk, so the cache-eviction
window is never exercised and local layers attend to the entire causal history
instead of their `cache_len`-sized window. This diverged from the rlformers
reference (which applies the correct local window via BlockDiagonalCausalMask)
and made local-global (LGA) checkpoints look badly degraded in numeric SNR
comparisons, while global-only checkpoints were unaffected.

Fix: for local layers (`0 < cache_len < input_len`), also mask keys older than
the window by adding `tril(diagonal=-cache_len)`. No-op for global layers
(`cache_len >= input_len`) and on-device chunked prefill (`input_len <=
cache_len`); skip layers (`cache_len == 0`) are excluded.

Input mask, input_len=5, local window (cache_len)=2:

Before (full causal, applied to ALL layers):
```
       j=0   1    2    3    4
i=0     0  -inf -inf -inf -inf
i=1     0    0  -inf -inf -inf
i=2     0    0    0  -inf -inf
i=3     0    0    0    0  -inf
i=4     0    0    0    0    0     <- attends to all of j=0..4
```

After (local layer, window=2):
```
       j=0   1    2    3    4
i=0     0  -inf -inf -inf -inf
i=1     0    0  -inf -inf -inf
i=2   -inf   0    0  -inf -inf
i=3   -inf -inf   0    0  -inf
i=4   -inf -inf -inf   0    0     <- attends to j=3,4 only (window=2)
```

Reviewed By: billmguo

Differential Revision: D113574455
@YIWENX14
YIWENX14 force-pushed the export-D113574455 branch from b5446d4 to cbebbe5 Compare July 28, 2026 04:13
…unk prefill (pytorch#21384)

Summary:

`StaticAttentionIOManager.prefill` applied a full-causal input mask to every
layer. For local (sliding-window) attention layers, the window is normally
realized via KV-cache eviction across prefill chunks. But the numeric-comparison
harness prefills the whole prompt in a single chunk, so the cache-eviction
window is never exercised and local layers attend to the entire causal history
instead of their `cache_len`-sized window. This diverged from the rlformers
reference (which applies the correct local window via BlockDiagonalCausalMask)
and made local-global (LGA) checkpoints look badly degraded in numeric SNR
comparisons, while global-only checkpoints were unaffected.

Fix: for local layers (`0 < cache_len < input_len`), also mask keys older than
the window by adding `tril(diagonal=-cache_len)`. No-op for global layers
(`cache_len >= input_len`) and on-device chunked prefill (`input_len <=
cache_len`); skip layers (`cache_len == 0`) are excluded.

Input mask, input_len=5, local window (cache_len)=2:

Before (full causal, applied to ALL layers):
```
       j=0   1    2    3    4
i=0     0  -inf -inf -inf -inf
i=1     0    0  -inf -inf -inf
i=2     0    0    0  -inf -inf
i=3     0    0    0    0  -inf
i=4     0    0    0    0    0     <- attends to all of j=0..4
```

After (local layer, window=2):
```
       j=0   1    2    3    4
i=0     0  -inf -inf -inf -inf
i=1     0    0  -inf -inf -inf
i=2   -inf   0    0  -inf -inf
i=3   -inf -inf   0    0  -inf
i=4   -inf -inf -inf   0    0     <- attends to j=3,4 only (window=2)
```

Reviewed By: billmguo

Differential Revision: D113574455
@YIWENX14
YIWENX14 force-pushed the export-D113574455 branch from cbebbe5 to 91d3836 Compare July 28, 2026 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. meta-exported release notes: none Do not include this in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants