Skip to content

Fix DynamicCacheWithRepeat AttributeError on transformers Cache refactor - #216

Open
Amir Fathi (AmirF194) wants to merge 1 commit into
microsoft:mainfrom
AmirF194:fix/207-dynamiccachewithrepeat-key-cache-attributeerror
Open

Amir Fathi (AmirF194) wants to merge 1 commit into
microsoft:mainfrom
AmirF194:fix/207-dynamiccachewithrepeat-key-cache-attributeerror

Conversation

@AmirF194

Copy link
Copy Markdown

What does this PR do?

DynamicCacheWithRepeat.__init__ calls super().__init__() and counts on DynamicCache
to set self.key_cache/self.value_cache/self._seen_tokens, but update() and
get_seq_length() read those three directly and never touch DynamicCache's own storage.
Current transformers moved that storage to a self.layers list, so both methods raise
AttributeError the moment generate() calls get_seq_length(). BaseKVCache, a few
classes above in the same file, already self-initializes the same three attributes instead
of relying on the parent, for the same reason; this gives DynamicCacheWithRepeat the same
pattern.

Fixes #207
Fixes #195 (same traceback, same file and line, hit through run_infinitebench.py instead
of pipeline(), on transformers 4.57.1 rather than 5.17.0, five months apart)

Before submitting

Added tests/test_dynamic_cache_with_repeat.py, unittest-style like test_e2e.py. It fails
on main with transformers 5.17.0 (both methods raise AttributeError) and passes on this
branch; also ran it against transformers 4.48.0 (the version mtraining/requirements.txt
pins) to check the fix doesn't change behavior where key_cache already existed, and it
passes there too, before and after.

One thing worth flagging: constructing DynamicCacheWithRepeat through the normal
minference import needs vllm and the CUDA build, so the new test imports
minference.modules.kvcompression directly rather than through the package, sidestepping
that and an unrelated existing issue where kivi.py/retr_attn.py misread
_is_package_available's return value. No GPU here, so I couldn't run test_e2e.py or the
original repro's actual pipeline().generate() call; the two isolated methods are what I
verified.

DynamicCacheWithRepeat.__init__ calls super().__init__() and relies on the
parent DynamicCache to set self.key_cache/self.value_cache/self._seen_tokens,
but update() and get_seq_length() read those three attributes directly and
never touch DynamicCache's own storage. Current transformers no longer sets
them (Cache moved to a self.layers list), so both methods raise
AttributeError. Self-initialize the three attributes in __init__, matching
the pattern BaseKVCache already uses a few classes above in the same file.

Fixes microsoft#207
Fixes microsoft#195
@AmirF194

Copy link
Copy Markdown
Author

This has been open a week with no review yet. Let me know if the approach needs adjusting before you look closer.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: AttributeError: 'DynamicCacheWithRepeat' object has no attribute 'key_cache' [Bug]: Can't run run_infinitebench.py

1 participant