Commit 8a2dfb5
committed
Add --sliding_window flag to CoreML static LLM export
Models trained with sliding-window attention (Mistral 7B, Gemma 3,
Gemma 4, Llama 4 Scout, …) only need each layer to attend to the
last `W` tokens, but `export_static_llm_coreml.py` was always
sizing the per-layer KV cache to `max_context_len - input_len`.
That made longer contexts proportionally more expensive in both KV
cache memory and per-token attention compute, even though the model
was trained to ignore everything outside the window.
Add a `--sliding_window` flag that caps the cache at the trained
window. The downstream pieces — `StaticAttentionMask` invariants
under cache eviction and the `StaticAttentionIOManager`'s per-layer
`cache_lens` plumbing — already support this; the export script
just needed to expose it. Per-layer mixed sliding/full attention
(Gemma 3/4) is left for a follow-up; this PR uses one window for
every layer.
The cache_len computation is factored into `_resolve_cache_len` so
it is unit-testable, and the README's ANE Optimizations section
documents the new option.
### Memory savings example
For a 32-layer / n_kv_heads=8 / head_dim=128 model exported with
`max_context_len=8192` in fp16, dropping the cache from 8160 to
4096 cuts the per-method KV cache from ~1.07 GB to ~0.54 GB.1 parent 94d2881 commit 8a2dfb5
3 files changed
Lines changed: 133 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
170 | 171 | | |
171 | 172 | | |
172 | 173 | | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
173 | 196 | | |
174 | 197 | | |
175 | 198 | | |
| |||
410 | 433 | | |
411 | 434 | | |
412 | 435 | | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
413 | 448 | | |
414 | 449 | | |
415 | 450 | | |
| |||
481 | 516 | | |
482 | 517 | | |
483 | 518 | | |
484 | | - | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
485 | 522 | | |
486 | 523 | | |
487 | 524 | | |
488 | 525 | | |
| 526 | + | |
| 527 | + | |
489 | 528 | | |
490 | 529 | | |
491 | 530 | | |
| |||
513 | 552 | | |
514 | 553 | | |
515 | 554 | | |
516 | | - | |
517 | | - | |
518 | | - | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
519 | 558 | | |
520 | 559 | | |
521 | 560 | | |
| |||
643 | 682 | | |
644 | 683 | | |
645 | 684 | | |
646 | | - | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
647 | 690 | | |
648 | 691 | | |
649 | 692 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| 72 | + | |
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
| |||
94 | 95 | | |
95 | 96 | | |
96 | 97 | | |
| 98 | + | |
97 | 99 | | |
98 | 100 | | |
99 | 101 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
| 17 | + | |
| 18 | + | |
15 | 19 | | |
16 | 20 | | |
17 | 21 | | |
| |||
44 | 48 | | |
45 | 49 | | |
46 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
47 | 125 | | |
48 | 126 | | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
0 commit comments