Eliminate persistent V cache for v_from_value_emb target layers#13
Open
Upcccccc wants to merge 1 commit into
Open
Eliminate persistent V cache for v_from_value_emb target layers#13Upcccccc wants to merge 1 commit into
Upcccccc wants to merge 1 commit into
Conversation
KVCache now allocates V cache slots per-layer: tensor for non-target layers, None for target layers. A token_ids_history field tracks the sequence so attention can gather V from each layer's VE_table on the fly. CausalSelfAttention.forward adds a v_from_value_emb inference branch that bypasses flash_attn_with_kvcache (which would write to v_cache), manually appends K to k_cache, and runs flash_attn_func with V gathered from the per-layer embedding table. GPTBase.forward updates token_ids_history at the start of forward (once per call, not per layer) and gathers full-history V for target layers when in v_from_value_emb inference mode. Standard (non-v_from_value_emb) inference path is unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
743585d to
09c4288
Compare
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
KVCache: target layers getNoneslot (no allocation), non-target layers keep their tensor as before. Atoken_ids_historyfield lets attention rebuild the full V history fromvalue_embeds[layer]at each step.v_from_value_embinference that bypassesflash_attn_with_kvcache(which would write to v_cache), manually appends K tok_cache, and runsflash_attn_funcwith gathered V.GPTBase.forwardnow gathers V for the full history (T0+T) when the cache is inv_from_value_embmode, so attention has every position's V available without persistent storage.Verification
tests/test_v_cache_elimination.pychecks two things:KVCacheallocates v_cache slots only for non-target layers (target layers →None);token_ids_historyis allocated.0.0000e+00at every position; greedy argmax matches (" Berlin"for the "capital of Germany" test).Memory savings (empirical, d24 second_half model, B=1)
Same model, two inference paths (A: full v_cache; B: new no-v_cache path) — VE-table overhead identical in both, so the delta is pure V-cache savings.
Asymptotic savings:
L_t / (2L)of total KV cache. Forsecond_half(L_t/L = 1/2) this is 25%.FLOPs savings
estimate_flopsalready accounts for "deadc_v" weights at target layers:Files
nanochat/engine.py:KVCachev_from_value_emb mode with per-layer slotsnanochat/model/gpt_base.py: new attention forward branch + outer forward maintains token_ids_historytests/test_v_cache_elimination.py: bit-exact + structural correctness testsscripts/inspect/profile_inference_memory.py: cross-model gpt_base vs v_from_value_emb profilerscripts/inspect/profile_inference_memory_ablation.py: same-model A/B clean ablationdocs/v_cache_memory_analysis.md: theoretical formulas + empirical fitsTest plan
tests/test_v_cache_elimination.pypassesscripts/inspect/profile_inference_memory_ablation.pyreproduces 20.5% baseline reduction at T=128Kv_from_value_emb) unaffected —v_from_value_emb=Falsecache path is the legacy implementation untouched🤖 Generated with Claude Code