Repository navigation
Optimize CPU SDPA for ring attention - #22358
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/22358
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 115 PendingAs of commit 84502cd with merge base 5b77187 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
|
@JacobSzwejbka has imported this pull request. If you are a Meta employee, you can view this in D118840114. |
| """Size an SWA cache for one retained window plus one in-flight chunk.""" | ||
| assert window_size > 0, "Sliding-window size must be positive" | ||
| if max_seq_len is None: | ||
| max_seq_len = max_context_length |
There was a problem hiding this comment.
can this blow up memory accidentally?
There was a problem hiding this comment.
the previous sizing was 2x window size which would produce incorrect outputs on really long sequences. It can meaningfully regress memory on enormous context lengths if we arent chunking the input but for anything in mobile world it should be fine.
|
@claude review this PR. |
|
Claude finished @digantdesai's task in 5m 11s —— View job PR Review: Optimize CPU SDPA for ring attention
Overall this is a well-constructed change. The mask-range precompute is shared across batch/heads (correct, since the mask is 2D), the bit-state logic is sound, and splitting a wrapped ring window into two flash-attention sub-blocks is mathematically valid because online softmax composes over disjoint column sets. Below are the findings, most important first. 1. Memory: answering @digantdesai's question on
|
| const int64_t num_ranges = use_mask_ranges | ||
| ? 2 * num_kv_blocks | ||
| : (num_keys + kvSplitSize - 1) / kvSplitSize; | ||
| for (int64_t range = 0; range < num_ranges; ++range) { |
There was a problem hiding this comment.
easy to introduce bugs here, I guess make sure we have enough test cases
66b6e95 to
d034210
Compare
d034210 to
5b4848e
Compare
Skip fully masked KV blocks and trim masked boundaries in CPU custom SDPA. Split wrapped ring windows around large masked gaps while preserving additive-mask semantics. Size regular, custom-op, quantized, and attention-sink KV caches for the retained window plus the largest in-flight prefill chunk. Extend coverage across sparse, causal, reduced-precision, and cache replacement paths. Authored with assistance from Codex.
5b4848e to
84502cd
Compare
Summary