You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 9958d39
Browse filesBrowse the repository at this point in the historyBrowse files
**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
- hf_attention.py — registers the mlx_offgraph attention implementation
and a mask function returning None, since the op masks internally from
the position it is given. OffGraphExportWrapper exposes the (input_ids,
cache_position) signature a runner drives and passes position_ids
explicitly, because HuggingFace otherwise derives them from
past_key_values.get_seq_length(), which is
- export_llm_hf.py — adds the off-graph export path and publishes the
layout as get_n_caches, get_kv_heads, get_head_dims, get_windows.
- cache.py — reshapes the HFStaticCache fallback's cache_position to
1-D. That path belongs to the in-graph cache, but it is the fix gemma-4
needs: gemma-4 calls update() without cache_kwargs, so the fallback
reads a 0-dim cumulative_length and torch.export fails indexing it.
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.
0 commit comments