Skip to content

[XNNPACK] Add the off-graph flat KV-cache - #21822

Open
kiymetakdemir wants to merge 1 commit into
pytorch:mainfrom
kiymetakdemir:xnnpack-offgraph-kvcache
Open

[XNNPACK] Add the off-graph flat KV-cache#21822
kiymetakdemir wants to merge 1 commit into
pytorch:mainfrom
kiymetakdemir:xnnpack-offgraph-kvcache

Conversation

@kiymetakdemir

Copy link
Copy Markdown
Contributor

Summary

Adds the byte layer for the off-graph KV cache on XNNPACK: XnnSequenceCache stores K/V in per-layer host pools behind the neutral SequenceCache, which keeps the length bookkeeping and step planning. A step's tokens are copied to the slots the planner names, and the window comes back as an AttendSpec: base pointers plus the pool's extent (slots) and the live prefix (valid_len), pointer-typed so the consumer wraps it at the call site instead of the cache owning tensor state.

Files

  • backends/xnnpack/runtime/XnnCache.hAttendSpec (K/V base pointers, slots, valid_len, and the mask kind
    None/Causal/Explicit with an optional additive mask) plus the XnnCache op face, kept separate from the neutral
    tensor-free face so one cache exposes both without a diamond. Masking is the cache's to declare, not the caller's, which is what lets a windowed or tree layer slot in behind the same op; Explicit has no producer until a ring layer needs one.
  • backends/xnnpack/runtime/XnnSequenceCache.hPool (per-layer host buffer, BHSD-major [1, H, slots, D], one copy per head at the slots the planner names, lazy doubling growth that re-lays-out every head because the row stride widens) and XnnSequenceCache : cache::SequenceCache, XnnCache. Construction goes through a static create() returning Result, since a constructor cannot reject a bad config without exceptions. A ring step can span two runs or start mid-pool, and update_and_fetch rejects both rather than half-serving them.
  • backends/xnnpack/test/runtime/test_xnn_sequence_cache.cpp, backends/xnnpack/test/CMakeLists.txt — the tests.

Testing

20 GTest cases. The centrepiece drives the cache through a realistic step sequence — prefill, chunked prefill, decodes, crossing four pool doublings — while an independent naive model appends each step to a flat per-head history, and compares the pool read as [1, H, slots, D] over [0, valid_len) after every step.

cmake . -DEXECUTORCH_BUILD_XNNPACK=ON -DEXECUTORCH_BUILD_TESTS=ON -Bcmake-out
cmake --build cmake-out --target backends_xnnpack_test
ctest --test-dir cmake-out -R backends_xnnpack_test --output-on-failure

@pytorch-bot

pytorch-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 6 New Failures, 4 Unrelated Failures, 1 Unclassified Failure

As of commit 601c570 with merge base 564e655 (image):

NEW FAILURES - The following jobs have failed:

UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:

  • pull / test-arduino-library / run / arduino-library (gh) (this job did not run on the merge base, so DrCI cannot tell whether the failure is pre-existing)
    RuntimeError: Command docker exec -t 6ad4f13f364669a974a55d0be653571319e62cbabc3d7813769e841954e84a70 /exec failed with exit code 2

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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 Aug 13, 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.

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.

1 participant