Skip to content

Optimize CPU SDPA for ring attention - #23122

Open
JacobSzwejbka wants to merge 7 commits into
mainfrom
perf/cpu-ring-attention-mask-skipping-v2
Open

JacobSzwejbka wants to merge 7 commits into
mainfrom
perf/cpu-ring-attention-mask-skipping-v2

Conversation

@JacobSzwejbka

Copy link
Copy Markdown
Contributor

Reapplies #22358 after it was reverted by #22660.

This restores ring KV-cache and attention-sink support and lets CPU SDPA precompute useful mask ranges so fully masked K/V regions can be skipped. The conflict with current main was resolved by retaining the newer causal key-range reduction while applying it relative to each retained ring range.

Validation:

  • Compiled extension/llm/custom_ops/op_sdpa.cpp with the existing CMake compile configuration.
  • test_ring_kv_cache: 9 passed, 2 skipped.
  • test_attention_sink: 26 passed, 2 skipped.
  • Five non-extension ring-attention behavior tests passed.
  • Python syntax compilation passed for all changed Python files.

The custom-op and quantized Python variants require locally built ExecuTorch Python bindings and could not run against the currently installed bindings.

Authored with OpenAI Codex.

@pytorch-bot

pytorch-bot Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 2 New Failures

As of commit 0aa9a11 with merge base 328e52a (image):

NEW FAILURES - The following jobs have failed:

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 Sep 24, 2026
@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.

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

Requesting changes for the two compatibility regressions noted inline. Both were reproduced on this PR and succeeded on its parent commit.

Validation: 51 targeted Python tests passed using existing native bindings. I did not rebuild the PR's C++ kernel, so these results do not validate the new native implementation.

Review assisted by OpenAI Codex.

head_dim,
dtype=torch.float32,
*,
window_size: int,

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.

[P1] Preserve compatibility with the pinned Optimum caller

The Optimum revision pinned in .ci/docker/ci_commit_pins/optimum-executorch.txt still constructs CustomRingKVCache without window_size in ETCustomHybridCache. Making this argument required causes that call to raise TypeError: CustomRingKVCache.__init__() missing 1 required keyword-only argument: 'window_size', breaking Gemma 3 export with the custom KV cache used by the multimodal CI job. The same call succeeds on the parent commit.

Please preserve the existing constructor semantics for callers using the old signature, or update the Optimum integration and dependency pin together with this change.

Comment thread examples/models/llama/attention.py Outdated
if max_seq_len is None:
max_seq_len = max_context_length
assert max_seq_len > 0, "Maximum sequence length must be positive"
assert window_size <= max_context_length, (

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.

[P2] Allow sliding windows larger than the configured context

A configuration such as local_global_attention=[512] with max_context_length=256 now fails this assertion during cache replacement. This is valid: every causal token available within the configured context fits inside the sliding window, so the layer can use a full-context cache. I verified that the parent implementation accepts this case and produces the expected causal mask, while the PR raises AssertionError.

Please allow this case and cap the allocation at max_context_length, as the return expression already does, rather than requiring users to reduce their attention window when exporting a shorter context.

Reapply the optimization from #22358 after its revert in #22660, preserving the newer causal key-range reduction on main.
Apply the line wrapping requested by clang-format 18.1.3.

Authored with OpenAI Codex.
Restore the legacy CustomRingKVCache constructor capacity and allow sliding windows larger than the configured context. Add regressions for legacy continuation prefill and full-context masks across cache types.

Authored with OpenAI Codex.
@meta-codesync

meta-codesync Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

@JacobSzwejbka has imported this pull request. If you are a Meta employee, you can view this in D123932412.

Match the is_seq_at_dim_1 parameter name reported by internal clang-tidy on D123932412.

Authored with OpenAI Codex.
Preserve the original window and doubled cache capacity when window_size is omitted from RingKVCache and QuantizedRingKVCache. Keep explicit window and chunk sizing available, and cover legacy construction, cache wrapping, and attention masks with regression tests.

Authored with OpenAI Codex.
Use RingKVCache when no sink tokens are retained, with capacity matching the RoPE ring and input chunk headroom. Cover cache type, dtype, wrapped positions, and retained attention history across cache sizes.

Authored with OpenAI Codex.

This branch was previously deployed

1 inactive deployment
cadence — 0aa9a116 Deployed Oct 14, 2026 by JacobSzwejbka
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants