Repository navigation
one_layer's decode writes its K/V into the cache slot in place - #528
Merged
student-Jungmin merged 1 commit intoNov 10, 2026
Merged
Conversation
…, 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
changed the base branch from
pr/pts-3-gqa-shared-kv
to
pytorchsim-triton-backend
November 10, 2026 18:07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 undertorch.compile(dynamic=False)the store address is a compile-time constant: no kernel readscache_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.