Skip to content

Commit 2bd6e06

Browse files
committed
Add benchmarks for transposed vs standard KV cache layout
Benchmarks comparing transposed [B, H, S, D] vs standard [B, S, H, D] KV cache layouts in custom_sdpa and update_cache ops using Llama 3 8B config (32 Q heads, 8 KV heads, D=128). Both C++ (Google Benchmark) and Python benchmarks are included, covering decode (seq_len=1) at various cache fill levels and prefill scenarios. Results on Apple M-series show transposed cache significantly improves SDPA performance at longer cache fills (1.64x at start_pos=1024, 1.13x for prefill seq_len=512) due to better memory locality in the attn_score @ V GEMM — V stride along S_kv changes from H*D to D. Authored with Claude. Differential Revision: [D99677680](https://our.internmc.facebook.com/intern/diff/D99677680/) [ghstack-poisoned]
1 parent 01142cc commit 2bd6e06

3 files changed

Lines changed: 741 additions & 0 deletions

File tree

extension/llm/custom_ops/BUCK

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ fbcode_target(_kind = runtime.python_test,
105105
],
106106
)
107107

108+
fbcode_target(_kind = runtime.python_binary,
109+
name = "bench_transposed_cache_py",
110+
srcs = [
111+
"bench_transposed_cache.py",
112+
],
113+
main_function = "executorch.extension.llm.custom_ops.bench_transposed_cache.main",
114+
preload_deps = [
115+
":custom_ops_aot_lib_mkl_noomp",
116+
":custom_ops_aot_py",
117+
],
118+
deps = [
119+
"//caffe2:torch",
120+
],
121+
)
122+
108123
fbcode_target(_kind = cpp_benchmark,
109124
name = "bench_sdpa",
110125
srcs = ["bench_sdpa.cpp"],
@@ -117,3 +132,14 @@ fbcode_target(_kind = cpp_benchmark,
117132
"//executorch/runtime/core/exec_aten/testing_util:tensor_util",
118133
],
119134
)
135+
136+
cpp_benchmark(
137+
name = "bench_transposed_cache",
138+
srcs = ["bench_transposed_cache.cpp"],
139+
deps = [
140+
"fbsource//third-party/benchmark:benchmark",
141+
"//executorch/extension/llm/custom_ops:custom_ops_mkl_noomp",
142+
"//executorch/runtime/core/exec_aten:lib",
143+
"//executorch/runtime/core/exec_aten/testing_util:tensor_util",
144+
],
145+
)

0 commit comments

Comments
 (0)