Commit 6f31ee3
committed
Plumb transposed cache config through export pipeline
Benchmarking shows transposed KV cache [B, H, S, D] significantly outperforms
standard layout [B, S, H, D] in custom_sdpa, especially at longer cache fills:
1.64x at start_pos=1024, 1.14x at start_pos=512, 1.13x for prefill seq_len=512
(Llama 3 8B config, Apple M-series). The improvement comes from better memory
locality in the attn_score @ V GEMM where V stride along S_kv changes from
H*D to D.
This commit replaces the hardcoded `is_seq_at_dim_2=True # hacking temporarily`
values in sdpa.py and custom_kv_cache.py with a proper configurable parameter
threaded through the export pipeline:
- Add `use_transposed_cache: bool = True` to ModelConfig in llm_config.py
- Thread it through _get_source_transforms in export_llama_lib.py
- Add `is_seq_at_dim_2` parameter to replace_kv_cache_with_custom_kv_cache
and replace_sdpa_with_custom_op (defaulting to True for backward compat)
Also fixes:
- torchao aarch64:matmul BUCK: deps -> exported_deps for :macro, fixing
transitive header visibility on arm64
- op_update_cache.cpp: %zd -> PRId64 for int64_t format strings
Authored with Claude.
Differential Revision: [D99677679](https://our.internmc.facebook.com/intern/diff/D99677679/)
[ghstack-poisoned]1 parent 2bd6e06 commit 6f31ee3
5 files changed
Lines changed: 32 additions & 21 deletions
File tree
- examples/models/llama
- source_transformation
- extension/llm
- custom_ops
- export/config
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
765 | 765 | | |
766 | 766 | | |
767 | 767 | | |
| 768 | + | |
768 | 769 | | |
769 | 770 | | |
770 | 771 | | |
| |||
1603 | 1604 | | |
1604 | 1605 | | |
1605 | 1606 | | |
| 1607 | + | |
1606 | 1608 | | |
1607 | 1609 | | |
1608 | 1610 | | |
| |||
1639 | 1641 | | |
1640 | 1642 | | |
1641 | 1643 | | |
| 1644 | + | |
1642 | 1645 | | |
1643 | 1646 | | |
1644 | 1647 | | |
| |||
1734 | 1737 | | |
1735 | 1738 | | |
1736 | 1739 | | |
1737 | | - | |
| 1740 | + | |
| 1741 | + | |
| 1742 | + | |
1738 | 1743 | | |
1739 | 1744 | | |
1740 | 1745 | | |
1741 | 1746 | | |
1742 | 1747 | | |
1743 | | - | |
| 1748 | + | |
1744 | 1749 | | |
1745 | 1750 | | |
1746 | | - | |
| 1751 | + | |
| 1752 | + | |
| 1753 | + | |
1747 | 1754 | | |
1748 | 1755 | | |
1749 | 1756 | | |
| |||
Lines changed: 8 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
391 | 391 | | |
392 | 392 | | |
393 | 393 | | |
394 | | - | |
| 394 | + | |
395 | 395 | | |
396 | 396 | | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | | - | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
401 | 400 | | |
402 | 401 | | |
403 | 402 | | |
404 | 403 | | |
405 | | - | |
| 404 | + | |
406 | 405 | | |
407 | 406 | | |
408 | | - | |
| 407 | + | |
409 | 408 | | |
410 | 409 | | |
411 | 410 | | |
| |||
422 | 421 | | |
423 | 422 | | |
424 | 423 | | |
425 | | - | |
| 424 | + | |
426 | 425 | | |
427 | 426 | | |
428 | 427 | | |
429 | | - | |
| 428 | + | |
430 | 429 | | |
431 | 430 | | |
432 | 431 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | | - | |
| 85 | + | |
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
95 | | - | |
| 95 | + | |
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
| 99 | + | |
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
| 107 | + | |
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | | - | |
| 122 | + | |
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
127 | | - | |
| 127 | + | |
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
132 | | - | |
| 132 | + | |
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
213 | | - | |
| 213 | + | |
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
174 | 174 | | |
175 | 175 | | |
176 | 176 | | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
177 | 181 | | |
178 | 182 | | |
179 | 183 | | |
| |||
194 | 198 | | |
195 | 199 | | |
196 | 200 | | |
| 201 | + | |
197 | 202 | | |
198 | 203 | | |
199 | 204 | | |
| |||
0 commit comments