Repository navigation
Description
System Info
transformersversion:5.25.0.dev0(main@d7c980c)- Platform: Windows 11 / Linux (CPU)
- Python version: 3.12 / 3.14
- PyTorch version:
2.15.0+cpu
Who can help?
Reproduction
Following #51180 ([cache][generate] Allow rollback on recurrent states caches!), Cache.get_recurrent_states_to_keep is annotated as returning int (def get_recurrent_states_to_keep(self, layer_idx: int | None = None) -> int:), and callers across modeling_*.py and generation/utils.py pass its return value directly into min(...) / max(...):
- In 23+ hybrid/recurrent modeling files (
modeling_mamba.py,modeling_mamba2.py,modeling_falcon_h1.py,modeling_bamba.py,modeling_qwen3_5.py,modeling_qwen3_next.py,modeling_jamba.py,modeling_zamba2.py, etc.):num_intermediate_recurrent_states = ( cache.get_recurrent_states_to_keep(self.layer_idx) if cache is not None else 0 ) num_intermediate_recurrent_states = min(num_intermediate_recurrent_states, seq_len)
- In
DeferredStopCheck.__init__(src/transformers/generation/utils.py:430):self.cache.activate_past_recording( recurrent_states_to_keep=max(1, self.cache.get_recurrent_states_to_keep()) )
However, there are two bugs in LinearAttentionCacheLayerMixin.activate_past_recording and Cache.get_recurrent_states_to_keep (src/transformers/cache_utils.py):
activate_past_recording()with defaultrecurrent_states_to_keep=NonestoresNoneon the layer:
LinearAttentionCacheLayerMixin.activate_past_recording(self, recurrent_states_to_keep: int | None = None)setsself.recurrent_states_to_keep = None. Because the attribute exists on the layer with valueNone,getattr(self.layers[layer_idx], "recurrent_states_to_keep", 0)returnsNoneinstead of0:cache.get_recurrent_states_to_keep(layer_idx)returnsNone, causingmin(num_intermediate_recurrent_states, seq_len)in modelforward()to crash withTypeError: '<' not supported between instances of 'int' and 'NoneType'.cache.get_recurrent_states_to_keep()(layer_idx=None) crashes insidemax(...)withTypeError: '>' not supported between instances of 'NoneType' and 'int'(or'NoneType' and 'NoneType').
- Calling
get_recurrent_states_to_keep()on an unpopulatedDynamicCache()raisesValueError:
When a freshDynamicCache()(whereself.layersis lazily populated on the first forward, solen(self.layers) == 0) is queried viacache.get_recurrent_states_to_keep(),max(...)receives an empty generator withoutdefault=0and raisesValueError: max() iterable argument is empty(andcache.get_recurrent_states_to_keep(0)raisesIndexError: list index out of range).
Minimal reproduction script:
from transformers.cache_utils import (
Cache,
DynamicCache,
DynamicLayer,
LinearAttentionAndFullAttentionLayer,
LinearAttentionLayer,
)
# 1. Default activate_past_recording() on a cache with linear/hybrid attention layers
cache = Cache(layers=[DynamicLayer(), LinearAttentionLayer(), LinearAttentionAndFullAttentionLayer()])
cache.activate_past_recording()
print("layer 1 recurrent_states_to_keep:", cache.get_recurrent_states_to_keep(1))
# Actual: None (Expected: 0)
# Crashes model forward at `min(num_intermediate_recurrent_states, seq_len)`:
# TypeError: '<' not supported between instances of 'int' and 'NoneType'
try:
cache.get_recurrent_states_to_keep()
except TypeError as e:
print("cache.get_recurrent_states_to_keep() raised TypeError:", e)
# Actual: TypeError: '>' not supported between instances of 'NoneType' and 'int'
# 2. Fresh DynamicCache() before layers are lazily populated
empty_cache = DynamicCache()
try:
empty_cache.get_recurrent_states_to_keep()
except ValueError as e:
print("empty_cache.get_recurrent_states_to_keep() raised ValueError:", e)
# Actual: ValueError: max() iterable argument is emptyOutput on main:
layer 1 recurrent_states_to_keep: None
cache.get_recurrent_states_to_keep() raised TypeError: '>' not supported between instances of 'NoneType' and 'int'
empty_cache.get_recurrent_states_to_keep() raised ValueError: max() iterable argument is empty
Expected behavior
Cache.get_recurrent_states_to_keep(layer_idx) and Cache.get_recurrent_states_to_keep() should always return an int (defaulting to 0 when recurrent_states_to_keep was not specified in activate_past_recording(), or when self.layers is empty / layer_idx >= len(self.layers)).
I have prepared and tested a fix + unit test (test_get_recurrent_states_to_keep in tests/utils/test_cache_utils.py) on my remote fork branch here:
- Branch: https://github.com/avnps/transformers/tree/fix/cache-get-recurrent-states-to-keep
- Diff: main...avnps:transformers:fix/cache-get-recurrent-states-to-keep
Could you please let me know if this looks good and assign this issue to me so I can open a PR?
Activity
avnps commentedon Dec 4, 2026
Closing this issue since PR #51440 was closed as not reachable in real generation flows. Thanks @Cyrilvallez for the review!