Skip to content

[executorch][native-ggml] Truncate attention for sliding-window and shared masks - #25190

Closed
SS-JIA wants to merge 1 commit into
gh/SS-JIA/727/basefrom
gh/SS-JIA/727/head
Closed

SS-JIA wants to merge 1 commit into
gh/SS-JIA/727/basefrom
gh/SS-JIA/727/head

Conversation

@SS-JIA

@SS-JIA SS-JIA commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Ulterior Motive

Closing the decode gap between ExecuTorch Native's GGML engine and llama.cpp on CUDA, measured with Gemma 4 31B Q4_K_M on an A100.

Rationale

What: The engine finds key extents for attention masks that are products of comparisons, and for masks that share values.

Why: Attention covers only the keys below an extent the engine computes on the host, but only for masks it matches. It matched a single comparison whose values fed nothing but that mask. Gemma 4 builds its sliding-window mask as the causal comparison times a window comparison, and its global layers use the causal comparison itself. Both were rejected, so every layer's flash attention read all 4,096 cache rows at every decode step.

Details

  • aten.mul of boolean masks matches as their logical and. A mask attends the keys every one of its comparisons attends.
  • Masks that share a value form one group. The group is computed over the widest extent any of its masks attends, and its values may feed only each other or SDPA masks over the same keys.
  • Host-evaluated integer constants may hold up to 2^16 elements instead of 64, so the key positions of a 4,096-row static cache evaluate on the host.

Gemma 4 31B decode, per step:

  • Before: attention over all 4,096 keys in all 60 layers; flash attention kernels took 2.90 ms.
  • After: attention over the keys up to the end of the 256-key bucket holding the position; 0.88 ms, vs 0.93 ms in llama.cpp.

Tests:

  • test_sdpa_key_prefix_of_masks_that_share_values: a causal mask and a sliding-window product that shares its comparison.
  • test_sdpa_key_prefix_of_a_mask_group_is_its_widest: masks of one group that end in different buckets.
  • test_sdpa_key_prefix_of_a_mask_read_by_other_ops: a comparison that also feeds a sum keeps attention over every key.
  • test_sdpa_key_prefix_of_constant_key_positions: key positions held by a constant.

Authored with Claude Code.

Differential Revision: D130721994

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 0f2b106 with merge base 6aebee6 (image):
💚 Looks good so far! There are no failures yet. 💚

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 Nov 17, 2026

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

Review automatically exported from Phabricator review in Meta.

@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

SS-JIA added a commit that referenced this pull request Nov 17, 2026
…hared masks

Pull Request resolved: #25190

## Ulterior Motive

Closing the decode gap between ExecuTorch Native's GGML engine and llama.cpp on CUDA, measured with Gemma 4 31B Q4_K_M on an A100.

## Rationale

**What**: The engine finds key extents for attention masks that are products of comparisons, and for masks that share values.

**Why**: Attention covers only the keys below an extent the engine computes on the host, but only for masks it matches. It matched a single comparison whose values fed nothing but that mask. Gemma 4 builds its sliding-window mask as the causal comparison times a window comparison, and its global layers use the causal comparison itself. Both were rejected, so every layer's flash attention read all 4,096 cache rows at every decode step.

## Details

- `aten.mul` of boolean masks matches as their logical and. A mask attends the keys every one of its comparisons attends.
- Masks that share a value form one group. The group is computed over the widest extent any of its masks attends, and its values may feed only each other or SDPA masks over the same keys.
- Host-evaluated integer constants may hold up to 2^16 elements instead of 64, so the key positions of a 4,096-row static cache evaluate on the host.

Gemma 4 31B decode, per step:

- **Before**: attention over all 4,096 keys in all 60 layers; flash attention kernels took 2.90 ms.
- **After**: attention over the keys up to the end of the 256-key bucket holding the position; 0.88 ms, vs 0.93 ms in llama.cpp.

Tests:

- `test_sdpa_key_prefix_of_masks_that_share_values`: a causal mask and a sliding-window product that shares its comparison.
- `test_sdpa_key_prefix_of_a_mask_group_is_its_widest`: masks of one group that end in different buckets.
- `test_sdpa_key_prefix_of_a_mask_read_by_other_ops`: a comparison that also feeds a sum keeps attention over every key.
- `test_sdpa_key_prefix_of_constant_key_positions`: key positions held by a constant.

Authored with Claude Code.

Differential Revision: [D130721994](https://our.internmc.facebook.com/intern/diff/D130721994/)
ghstack-source-id: 476599442
SS-JIA added a commit that referenced this pull request Nov 18, 2026
…hared masks

Pull Request resolved: #25190

## Ulterior Motive

Closing the decode gap between ExecuTorch Native's GGML engine and llama.cpp on CUDA, measured with Gemma 4 31B Q4_K_M on an A100.

## Rationale

**What**: The engine finds key extents for attention masks that are products of comparisons, and for masks that share values.

**Why**: Attention covers only the keys below an extent the engine computes on the host, but only for masks it matches. It matched a single comparison whose values fed nothing but that mask. Gemma 4 builds its sliding-window mask as the causal comparison times a window comparison, and its global layers use the causal comparison itself. Both were rejected, so every layer's flash attention read all 4,096 cache rows at every decode step.

## Details

- `aten.mul` of boolean masks matches as their logical and. A mask attends the keys every one of its comparisons attends.
- Masks that share a value form one group. The group is computed over the widest extent any of its masks attends, and its values may feed only each other or SDPA masks over the same keys.
- Host-evaluated integer constants may hold up to 2^16 elements instead of 64, so the key positions of a 4,096-row static cache evaluate on the host.

Gemma 4 31B decode, per step:

- **Before**: attention over all 4,096 keys in all 60 layers; flash attention kernels took 2.90 ms.
- **After**: attention over the keys up to the end of the 256-key bucket holding the position; 0.88 ms, vs 0.93 ms in llama.cpp.

Tests:

- `test_sdpa_key_prefix_of_masks_that_share_values`: a causal mask and a sliding-window product that shares its comparison.
- `test_sdpa_key_prefix_of_a_mask_group_is_its_widest`: masks of one group that end in different buckets.
- `test_sdpa_key_prefix_of_a_mask_read_by_other_ops`: a comparison that also feeds a sum keeps attention over every key.
- `test_sdpa_key_prefix_of_constant_key_positions`: key positions held by a constant.

Authored with Claude Code.
ghstack-source-id: 477270600

Differential Revision: [D130721994](https://our.internmc.facebook.com/intern/diff/D130721994/)
SS-JIA added a commit that referenced this pull request Nov 18, 2026
…hared masks

Pull Request resolved: #25190

## Ulterior Motive

Closing the decode gap between ExecuTorch Native's GGML engine and llama.cpp on CUDA, measured with Gemma 4 31B Q4_K_M on an A100.

## Rationale

**What**: The engine finds key extents for attention masks that are products of comparisons, and for masks that share values.

**Why**: Attention covers only the keys below an extent the engine computes on the host, but only for masks it matches. It matched a single comparison whose values fed nothing but that mask. Gemma 4 builds its sliding-window mask as the causal comparison times a window comparison, and its global layers use the causal comparison itself. Both were rejected, so every layer's flash attention read all 4,096 cache rows at every decode step.

## Details

- `aten.mul` of boolean masks matches as their logical and. A mask attends the keys every one of its comparisons attends.
- Masks that share a value form one group. The group is computed over the widest extent any of its masks attends, and its values may feed only each other or SDPA masks over the same keys.
- Host-evaluated integer constants may hold up to 2^16 elements instead of 64, so the key positions of a 4,096-row static cache evaluate on the host.

Gemma 4 31B decode, per step:

- **Before**: attention over all 4,096 keys in all 60 layers; flash attention kernels took 2.90 ms.
- **After**: attention over the keys up to the end of the 256-key bucket holding the position; 0.88 ms, vs 0.93 ms in llama.cpp.

Tests:

- `test_sdpa_key_prefix_of_masks_that_share_values`: a causal mask and a sliding-window product that shares its comparison.
- `test_sdpa_key_prefix_of_a_mask_group_is_its_widest`: masks of one group that end in different buckets.
- `test_sdpa_key_prefix_of_a_mask_read_by_other_ops`: a comparison that also feeds a sum keeps attention over every key.
- `test_sdpa_key_prefix_of_constant_key_positions`: key positions held by a constant.

Authored with Claude Code.
ghstack-source-id: 477270600

Differential Revision: [D130721994](https://our.internmc.facebook.com/intern/diff/D130721994/)
SS-JIA added a commit that referenced this pull request Nov 18, 2026
…hared masks

Pull Request resolved: #25190

## Ulterior Motive

Closing the decode gap between ExecuTorch Native's GGML engine and llama.cpp on CUDA, measured with Gemma 4 31B Q4_K_M on an A100.

## Rationale

**What**: The engine finds key extents for attention masks that are products of comparisons, and for masks that share values.

**Why**: Attention covers only the keys below an extent the engine computes on the host, but only for masks it matches. It matched a single comparison whose values fed nothing but that mask. Gemma 4 builds its sliding-window mask as the causal comparison times a window comparison, and its global layers use the causal comparison itself. Both were rejected, so every layer's flash attention read all 4,096 cache rows at every decode step.

## Details

- `aten.mul` of boolean masks matches as their logical and. A mask attends the keys every one of its comparisons attends.
- Masks that share a value form one group. The group is computed over the widest extent any of its masks attends, and its values may feed only each other or SDPA masks over the same keys.
- Host-evaluated integer constants may hold up to 2^16 elements instead of 64, so the key positions of a 4,096-row static cache evaluate on the host.

Gemma 4 31B decode, per step:

- **Before**: attention over all 4,096 keys in all 60 layers; flash attention kernels took 2.90 ms.
- **After**: attention over the keys up to the end of the 256-key bucket holding the position; 0.88 ms, vs 0.93 ms in llama.cpp.

Tests:

- `test_sdpa_key_prefix_of_masks_that_share_values`: a causal mask and a sliding-window product that shares its comparison.
- `test_sdpa_key_prefix_of_a_mask_group_is_its_widest`: masks of one group that end in different buckets.
- `test_sdpa_key_prefix_of_a_mask_read_by_other_ops`: a comparison that also feeds a sum keeps attention over every key.
- `test_sdpa_key_prefix_of_constant_key_positions`: key positions held by a constant.

Authored with Claude Code.
ghstack-source-id: 477270600

Differential Revision: [D130721994](https://our.internmc.facebook.com/intern/diff/D130721994/)
@SS-JIA SS-JIA closed this Nov 18, 2026

This branch had an error being deployed

1 failed and 1 inactive deployments
cherry-pick-bot — 0f2b106c Deployed Nov 18, 2026 by SS-JIA
cadence — 0f2b106c Deployed Nov 17, 2026 by SS-JIA
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants