mirror of
https://github.com/harvard-edge/cs249r_book.git
synced 2026-07-17 08:28:07 -05:00
[PR #1953] [MERGED] fix(tinytorch): cached generation step excludes current token from self-attention #32217
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/harvard-edge/cs249r_book/pull/1953
Author: @Shashank-Tripathi-07
Created: 7/7/2026
Status: ✅ Merged
Merged: 7/10/2026
Merged by: @profvjreddi
Base:
dev← Head:fix/memoization-cached-generation-missing-self-attention📝 Commits (1)
267a534fix(tinytorch): cached generation step excludes current token from self-attention📊 Changes
1 file changed (+7 additions, -1 deletions)
View changed files
📝
tinytorch/src/18_memoization/18_memoization.py(+7 -1)📄 Description
Summary
_cached_generation_step()writes the new token's K/V into the cache viaupdate(), then callscache_obj.get()and uses its result directly asK_all/V_allfor the attention computation.KVCache.get()is correctly designed and tested (test_unit_kvcache, which explicitly asserts "Before advance, get() should return empty") to return only tokens already made visible byadvance()-- exactly not the token just written byupdate(), sinceadvance()is meant to run once per token after all layers finish.cache.get()returns a zero-length sequence, crashing the attention matmul withValueError: zero-size array to reduction operation maximum which has no identity-- cached generation was broken from the first token onward, not just subtly wrong.Fix
Concatenate the current step's own
K_heads/V_heads(already computed earlier in the same function, no extra work needed) ontocache.get()'s history, instead of using the cache's history alone.KVCacheitself is untouched -- it already works exactly as designed and tested.Test plan
KVCacheclass.cache.get()returns shape(1,1,0,4), and the subsequent softmax/matmul crashes withValueError: zero-size array to reduction operation maximum which has no identity.[1,0,0,0], the token attending fully to itself, the only token present) instead of crashing.advance().python -m py_compilepasses on the edited file.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.