feat: capture decode-step KV cache in auto-generated module configs - #136
Draft
kiszk wants to merge 2 commits into
Draft
feat: capture decode-step KV cache in auto-generated module configs#136kiszk wants to merge 2 commits into
kiszk wants to merge 2 commits into
Conversation
Signed-off-by: Kazuaki Ishizaki <ishizaki@jp.ibm.com>
kiszk
force-pushed
the
feat/oot-cache-kwarg-reconstruction
branch
from
August 5, 2026 09:45
ef4d93d to
22048dc
Compare
Signed-off-by: Kazuaki Ishizaki <ishizaki@jp.ibm.com>
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.
What this PR does
The auto-generated module-test configs drive each
DecoderLayerthrough both aprefill and a decode invocation, but the forward hook was skipping
past_key_valuesentirely:As a result the decode invocation was replayed with
past_key_values=None. InHF attention the cache-update branch
is then never taken, so the "decode" step silently degrades to a 1-token
self-attention (
Q[1] × K[1]) instead of attending over the cached past.Both eager and compiled runs share the same wrong input, so
test_eager_vs_compilepasses without actually exercising decode.
This PR captures the decode-step KV cache so the module test can rebuild an
equivalent cache and drive the real "attend over past + new token" path. The
companion torch-spyre change reconstructs the cache on the test side.
Changes (
utils/module_discovery/auto_generate_module_config.py)_resolve_layer_idx(module)— resolves the decoder layer index. Intransformers ≥5
layer_idxlives onself_attn, not theDecoderLayer, sowe fall back through both.
_extract_cache_info(...)— snapshots the target layer's populated K/V slicefrom a
StaticCacheinto acachespec (cache_path,layer_idx,key/value,max_cache_len,config_path,config_kwargs):get_seq_length(), not the whole-cache length(which over-counts by one at layer i>0 once earlier layers have updated
this pass).
[:, :, :past_len, :]slice (StaticCacheallocates the full
max_cache_lenup front).StaticCacheis recorded; a growableDynamicCachehas no fixedshape to reconstruct, so it emits a warning and is skipped.
past_key_values=None.past_key_values; it records thecachespec instead._convert_captured_input_to_sample_input/_extract_patterngain acachebranch (the latter keeps prefill and decode as distinct invocation signatures).
_validate_cache_mask_consistency(...)— generation-time sanity check that acached decode invocation also carries an
attention_maskwhose key length cancover the cached past (logged, non-fatal).
Verification (CPU, transformers 5.12)
GptOssForCausalLM(same modeling code asgpt-oss-20b, incl.
past_key_values.update()) driven with an explicitStaticCache; the written YAML'sGptOssDecoderLayerdecode invocationcontains the
cachespec (StaticCache,layer_idx=0, key[1, 2, 16, 16]=[B, num_kv_heads, past_len, head_dim]).StaticCache(seq_length 16) → realDecoderLayerdecode forward runsupdate()over past 16 + new 1 token(output
[1, 1, 64], cache length 16 → 17).DynamicCachestill warns and records no cache.Notes for reviewer
which adds the
cachespec reconstruction to the OOT framework.generate_*_config.pydrivers assume the reusablecapture_module_invocations/load_model_only/write_module_configAPI which Refactor auto_generate_module_config: extract reusable forward-pass API from main() #131 willintroduce. So the driver is not runnable here until Refactor auto_generate_module_config: extract reusable forward-pass API from main() #131 lands.