[MLX] Add off-graph KV cache export mode for HF models - #21680
[MLX] Add off-graph KV cache export mode for HF models#21680kiymetakdemir wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21680
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 2e8cb61 with merge base 14326d9 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
|
@claude review this code |
|
Claude finished @metascroy's task in 3m 39s —— View job Code Review — Off-graph KV cache export modeRead the diff plus Correctness1. assert softcap is None, "update_and_attend does not support attention logit softcapping"
assert head_mask is None, "update_and_attend does not support head_mask"This turns a silent-wrong-output footgun into a clear export-time error. Fix this → 2. Sliding-window masking is published but not enforced in the eager reference. The export publishes Consistency / minor3. 4. Module docstring is now stale ( 5. Nits
I did not run the export end-to-end (requires downloading the HF checkpoints), so the review is static only. The logic and the published layout look self-consistent. |
f0b551a to
8e7c18d
Compare
|
@claude review this code |
|
Claude finished @kiymetakdemir's task in 1m 50s —— View job Code Review — Off-graph KV cache export modeRe-read the diff against The design is coherent and the tracer-facing contract ( Correctness / worth verifying1. Does the dynamic seq dim admit 2. Sliding-window masking is published but not enforced by the eager oracle. 3. Consistency / minor4. Batch dim is implicitly 1. 5. 6. Boilerplate duplication. Nit
I did not run the export end-to-end (requires the HF checkpoints), so this is static review only. Items 2–6 are advisory; item 1 is the one I'd confirm before merge. |
8e7c18d to
2e8cb61
Compare
Summary
Adds --use-offgraph-cache, which exports a HuggingFace causal LM against kvcache::update_and_attend instead of an in-graph cache. The model runs with use_cache=False and past_key_values=None, so each attention layer emits one op fed only that step's k/v; history lives in a cache the runtime owns and binds by cache_key. KV-sharing layers address their donor's cache rather than one of their own, so gemma-4 E2B needs 15 caches for its 35 layers.
Files
Test
Exported Llama-3.2-1B and gemma-3-1b; both partition into a single MLX subgraph, and the published layout matches each architecture — 16 flat caches for llama, 26 for gemma-3 with full attention at layers 5/11/17/23.
python -m executorch.backends.mlx.examples.llm.export_llm_hf
--model-id unsloth/gemma-3-1b-it --output gemma3_offgraph.pte
--use-offgraph-cache --max-seq-len 2048 --dtype fp32
python -m executorch.backends.mlx.examples.llm.export_llm_hf
--model-id unsloth/Llama-3.2-1B-Instruct --output llama_offgraph.pte
--use-offgraph-cache --max-seq-len 2048 --dtype bf16
Add --qlinear 4w for INT4.