Repository navigation
Optimize CPU SDPA for ring attention - #23122
JacobSzwejbka wants to merge 7 commits into
Conversation
🔗 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 FailuresAs of commit 0aa9a11 with merge base 328e52a ( NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
mergennachin
left a comment
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
[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.
| 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, ( |
There was a problem hiding this comment.
[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.
bc50ded to
2f9df1c
Compare
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.
|
@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.
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:
extension/llm/custom_ops/op_sdpa.cppwith the existing CMake compile configuration.test_ring_kv_cache: 9 passed, 2 skipped.test_attention_sink: 26 passed, 2 skipped.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.