fix: normalize past_key_values across transformers DynamicCache API (#210)#246
Open
ousamabenyounes wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: normalize past_key_values across transformers DynamicCache API (#210)#246ousamabenyounes wants to merge 1 commit intomicrosoft:mainfrom
ousamabenyounes wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…icrosoft#210) Older llmlingua code assumed past_key_values was a tuple of (k, v) pairs. Transformers >= 4.36 returns a DynamicCache object, breaking the `for k, v in past_key_values` loops and the `past_key_values[0][0].shape[2]` access in iterative_compress_prompt with `AttributeError: 'list' object has no attribute 'get_seq_length'` (as reported in microsoft#210) or the unpacking ValueError that the test suite catches. Normalize to the legacy tuple format after every model forward pass and convert back to DynamicCache before any call that feeds it to the model. Generated by Claude Code Vibe coded by ousamabenyounes Co-Authored-By: Claude <noreply@anthropic.com>
This was referenced Apr 11, 2026
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 does this PR do?
Fixes #210
llmlingua's iterative_compress_prompt relies on
past_key_valuesbeing a tuple-of-(k,v) per layer: it slicespast_key_values[0][0].shape[2]to read the cached length and iteratesfor k, v in past_key_valuesfour separate times. Since transformers 4.36 the model returns aDynamicCacheinstead, which is why issue #210 showsAttributeError: 'list' object has no attribute 'get_seq_length'and why the existing baseline tests have been failing withValueError: too many values to unpack (expected 2)atiterative_compress_prompt.This PR normalizes
past_key_valuesto the legacy tuple format after every model forward pass (viaDynamicCache.to_legacy_cache()on older transformers, and viaDynamicCache.layerson transformers >= 5.0 whereto_legacy_cachewas removed) and converts it back to aDynamicCache(viaDynamicCache.from_legacy_cache()or theddp_cache_data=constructor arg on newer versions) right before the next forward pass. The rest of the slicing/iteration code initerative_compress_promptis untouched.Before submitting
Verification
ValueError: too many values to unpack (expected 2)initerative_compress_promptatllmlingua/prompt_compressor.py:1659.tests/test_issue_210.py), 0 regressions.Tested against transformers 5.5.3 (latest on PyPI), which is what triggered the original issue. The helpers fall back to the legacy
to_legacy_cache/from_legacy_cachemethods when present, so older transformers releases keep working too.Generated by Claude Code
Vibe coded by ousamabenyounes