Skip to content

one_layer's decode writes its K/V into the cache slot in place - #528

Merged
student-Jungmin merged 1 commit into
pytorchsim-triton-backendfrom
pr/pts-4-decode-writes-kv
Nov 10, 2026
Merged

student-Jungmin merged 1 commit into
pytorchsim-triton-backendfrom
pr/pts-4-decode-writes-kv

Conversation

@student-Jungmin

Copy link
Copy Markdown
Collaborator

Summary

Stacked on #527. Pairs with PSAL-POSTECH/tpu-perf-archive#15.

one_layer's decode step writes its own K and V into the cache, one slot in place, as the TPU decode row does. The harness used to hand the layer a prefilled cache and drop the K/V it computed, so k_proj, v_proj and k_norm were dead code and the decode rows measured no projection and no cache write. The slot is a Python int, so under torch.compile(dynamic=False) the store address is a compile-time constant: no kernel reads cache_position, and decode times without a functional run.

Verification

tests/system/test_one_layer_kv_write.py (headwise and tokenwise) fails on the tree before. Decode 1024 cycles: Qwen3-8B 456,635 -> 485,213, Llama3-8B 515,429 -> 542,958, Gemma3-1B 97,223 -> 103,415, Gemma2-2B 212,536 -> 230,436.

…, one slot in place

The decode harness handed the layer a prefilled cache and discarded the K and V the layer computed, so k_proj, v_proj and k_norm were dead code and the decode rows measured no projection and no cache write. The cache now has S slots, 0 to S - 2 from the model's own prefill, and the layer's update index_copy_s its K and V into the last slot in place (a sliding-window layer into the last slot of its window) and hands the whole buffer to attention. The compiler writes one token in place, no copy of the cache: on the tokenwise layout the write goes into the page tensor itself, K and V interleaved per head. A minimal cache is used rather than StaticCache, whose cumulative_length.add_ adds a kernel the TPU row has no counterpart for. The TPU harness writes the same slot.

After the run each written slot is checked against the float32 reference and every slot before it must be exactly what was placed; one verdict per grid point as before. Decode 1024 in one session: Qwen3-8B 456,635 to 485,213, Llama3-8B 515,429 to 542,958, Gemma3-1B 97,223 to 103,415, Gemma2-2B 212,536 to 230,436 (k_proj, k_norm with rope and the write, v_proj with the write). Pin: tests/system/test_one_layer_kv_write.py, headwise and tokenwise, failing on the tree before.

The slot is a Python int (S - 1, or the window's last slot for a sliding layer), written with a slice copy_ into the cache or the tokenwise page tensor, so under torch.compile(dynamic=False) the store address is a compile-time constant: the K write kernel no longer reads cache_position from device memory, so it declares no runtime attribute; with the PTB dram-trace compiler the V write fused into v_proj needs none either, so decode times without a functional run (tiny Qwen3 decode 128: 80,569 cycles with and without it). Still a one-token store in both layouts; Qwen3-8B decode 1024 cycles unchanged.
@student-Jungmin
student-Jungmin changed the base branch from pr/pts-3-gqa-shared-kv to pytorchsim-triton-backend November 10, 2026 18:07
@student-Jungmin
student-Jungmin merged commit 7fb24fd into pytorchsim-triton-backend Nov 10, 2026
@YWHyuk
YWHyuk deleted the pr/pts-4-decode-writes-kv branch November 19, 2026 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant