Fix DynamicCache.reset() leaving the cache in a corrupt, non-empty state - #48509
Open
tritsystem wants to merge 1 commit into
Open
Fix DynamicCache.reset() leaving the cache in a corrupt, non-empty state#48509tritsystem wants to merge 1 commit into
tritsystem wants to merge 1 commit into
Conversation
`CacheLayerMixin.reset()` zeroes `keys` / `values` in place with the shape unchanged and leaves `is_initialized` True. That is correct for `StaticLayer` (a pre-allocated buffer with a separate `cumulative_length` counter), but `DynamicLayer` -- the layer inside `DynamicCache`, the default cache for generative models -- has no length counter: its length *is* `keys.shape[-2]`, and it grows by `torch.cat`. So after `DynamicCache.reset()`: - `get_seq_length()` still returns the old length, - `is_initialized` stays True, - the next `update()` concatenates the new states onto the stale zeros, returning `old + new` positions with `old` leading zero vectors. Add `DynamicLayer.reset()` that returns the layer to its uninitialised state (`keys = values = None`, `is_initialized = False`, and `cumulative_length = 0` for `DynamicSlidingWindowLayer`). Update `DynamicIndexedLayer.reset()` to empty the (also concatenation-grown) indexer key cache rather than zero it in place. `LinearAttentionAndFullAttentionLayer` / `...SlidingWindowAttentionLayer` already delegate to these, so they are fixed too. Adds `SyntheticCacheTest.test_dynamic_cache_reset_returns_to_empty_state` (fails on main, passes with this change).
Contributor
CI recapDashboard: View test results in Grafana |
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.
Fix
DynamicCache.reset()leaving the cache in a corrupt, non-empty stateWhat does this PR do?
Cache.reset()is a public method. On aDynamicCache(the default cache forgenerative models) it does not empty the cache — it leaves it in a corrupt
state where
get_seq_length()still reports the old length and the nextupdate()concatenates onto stale zeros.Reproduction (on
main)Root cause
CacheLayerMixin.reset()zeroeskeys/valuesin place (shape unchanged)and leaves
is_initialized = True:That is correct for
StaticLayer— a pre-allocated buffer whose logical lengthis a separate
cumulative_lengthcounter.DynamicLayerhas no lengthcounter: its length is
keys.shape[-2], and it grows viatorch.cat. It doesnot override
reset(), so afterreset()the tensors are zeroed but keep theirold shape and
is_initializedstaysTrue→get_seq_length()returns thestale length and
update()doestorch.cat([stale_zeros, new_states], dim=-2).Same for
DynamicSlidingWindowLayer(cumulative_lengthgets reset by the base,but the K/V tensors and
is_initializeddo not) andDynamicIndexedLayer(itsreset()callssuper().reset()and then zeroes the also-concatenation-grownindexer_keysin place).generate()itself is unaffected — it only ever resets aStaticCache— but anyuser code that reuses a cache across calls hits this.
Fix
DynamicLayer.reset()that returns the layer to its uninitialisedstate (
keys = values = None,is_initialized = False, andcumulative_length = 0when present, coveringDynamicSlidingWindowLayer).DynamicIndexedLayer.reset(): emptyindexer_keys(None+is_indexer_initialized = False) instead of zeroing it in place.LinearAttentionAndFullAttentionLayer.reset()/LinearAttentionAndSlidingWindowAttentionLayer.reset()already delegate toDynamicLayer.reset/DynamicSlidingWindowLayer.reset, so they are fixedtransitively.
After the fix,
DynamicCache.reset()givesget_seq_length() == 0,is_initialized is False, and a subsequentupdate(3)returns a length-3sequence with no stale positions.
reset()on a fresh cache stays a no-op.Not in scope (noted)
QuantizedLayer(KIVI-style quantized KV cache) subclassesDynamicLayerandgets the
keys/values/is_initializedreset from this change, but keepsits
_quantized_keys/_quantized_valuesresidual buffers. If you'd like, I canadd a small
QuantizedLayer.reset()in this PR or a follow-up.Tests
tests/utils/test_cache_utils.py::SyntheticCacheTest::test_dynamic_cache_reset_returns_to_empty_state— fails on
main(AssertionError: 6 != 0), passes with this change.ruff check/ruff format --checkclean on both files.Before submitting