Visitar URL original
Move off-graph HF attention to extension/llm/cache by metascroy · Pull Request #24634 · pytorch/executorch · GitHub
Skip to content

Move off-graph HF attention to extension/llm/cache - #24634

Draft
metascroy wants to merge 1 commit into
mainfrom
offgraph-kv-hf-attention
Draft

metascroy wants to merge 1 commit into
mainfrom
offgraph-kv-hf-attention

Conversation

@metascroy

@metascroy metascroy commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Stack

Off-graph KV cache for the native ggml engine, through serving Muse Glimmer on it. Review bottom-up; each PR builds and passes its tests on its own.

Summary

Moves the HuggingFace attention function that routes attention through torch.ops.kvcache.update_and_attend from backends/mlx/llm/hf_attention.py to extension/llm/cache/hf_attention.py, so backends other than MLX can export with an off-graph KV cache.

Nothing in it was MLX-specific: it only emits the cache op, works out the YOCO donor cache id, and replaces HF's mask with a pass-through. The new module exposes offgraph_attention_forward, sdpa_mask_passthrough, and register_offgraph_attention(name). The MLX module keeps the names its exporters import (register_mlx_offgraph_attention, OffGraphExportWrapper, ...), so MLX callers are unchanged.

Also adds a hf_attention BUCK target in extension/llm/cache.

Test plan

  • Imported every name the MLX exporters use (export_llm_hf.py, dflash/export.py) and checked that register_mlx_offgraph_attention() registers the shared function.
  • The native export tests in the next PR export through the new module.

@pytorch-bot

pytorch-bot Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 136 New Failures, 5 Unrelated Failures, 6 Unclassified Failures

As of commit 57123b7 with merge base c7f7931 (image):

NEW FAILURES - The following jobs have failed:

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

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

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@github-actions

github-actions Bot commented Nov 5, 2026

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.

Comment thread backends/mlx/llm/hf_attention.py Outdated
Comment on lines +47 to +48
_cache_id,
offgraph_attention_forward as mlx_offgraph_attention_forward,

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.

_cache_id and mlx_offgraph_attention_forward aren't used here, register_offgraph_attention uses both inside the shared module, probably we don't need to import these

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, removed both imports (and the now-unneeded # noqa: F401). Nothing else imported them through the MLX module.

The HF attention function that routes attention through
kvcache::update_and_attend is backend-neutral: it only emits the cache op.
Move it from backends/mlx/llm into extension/llm/cache so other backends can
export with an off-graph KV cache. register_mlx_offgraph_attention and the
other names MLX exporters import are unchanged.

This branch had an error being deployed

1 failed deployment
cadence — 57123b7b Deployed Nov 11, 2026 by metascroy via vision-build / vision #37842
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