huggingface/transformers · error · ValueError
`has_previous_state` can only be called on LinearAttention l
Error message
`has_previous_state` can only be called on LinearAttention layers, and the current Cache seem to only contain Attention layers.
What it means
Cache.has_previous_state() raises ValueError (StopIteration fallback) when layer_idx is None and no layer is a LinearAttentionCacheLayerMixin. The method scans layers from last to first for a linear attention layer to inspect its previous-state flags; on an all-attention cache there is none.
Source
Thrown at src/transformers/cache_utils.py:1539
return max(layer.get_max_length() for layer in self.layers)
else:
return self.layers[layer_idx].get_max_length()
def has_previous_state(self, layer_idx: int | None = None, state_idx: int | None = None) -> bool:
"""Returns whether the LinearAttention layer at index `layer_idx` has previous state or not."""
if layer_idx is not None and layer_idx >= len(self.layers):
return False
# In this case, use last LinearAttention layer
if layer_idx is None:
try:
layer_idx = next(
idx
for idx in range(len(self) - 1, -1, -1)
if isinstance(self.layers[idx], LinearAttentionCacheLayerMixin)
)
except StopIteration:
raise ValueError(
"`has_previous_state` can only be called on LinearAttention layers, and the current Cache seem to "
"only contain Attention layers."
)
elif not isinstance(self.layers[layer_idx], LinearAttentionCacheLayerMixin):
raise ValueError(
f"You called `has_previous_state` on layer index {layer_idx}, but this layer is an Attention layer, which "
"does not support calling it."
)
# We may have several conv/recurrent states in the same layers. In this case, if `state_idx` is not provided, check if all
# of them have previous state
if state_idx is None:
return all(self.layers[layer_idx].has_previous_state.values())
return self.layers[layer_idx].has_previous_state[state_idx]
def get_mask_sizes(self, query_length: int, layer_idx: int) -> tuple[int, int]:
"""
Return a tuple (kv_length, kv_offset) corresponding to the length and offset that will be returned forView on GitHub (pinned to a597f97485)
Solutions
- Only call has_previous_state on caches that contain linear attention layers
- Guard with any(isinstance(l, LinearAttentionCacheLayerMixin) for l in cache.layers)
- For attention-only caches use get_seq_length()/is_initialized to decide decode readiness
Example fix
# before
if cache.has_previous_state(): # attention-only cache
...
# after
if any(isinstance(l, LinearAttentionCacheLayerMixin) for l in cache.layers) and cache.has_previous_state():
... Defensive patterns
Strategy: type-guard
Validate before calling
from transformers.cache_utils import LinearAttentionCacheLayerMixin
if any(isinstance(l, LinearAttentionCacheLayerMixin) for l in cache.layers):
ready = cache.has_previous_state() Type guard
from transformers.cache_utils import LinearAttentionCacheLayerMixin
def cache_has_linear_attention_layers(cache) -> bool:
return any(isinstance(l, LinearAttentionCacheLayerMixin) for l in cache.layers) Prevention
- Treat has_previous_state as a linear-attention-only API
- For attention-only caches, gate decoding on cache.get_seq_length() > 0 or is_initialized instead
When it happens
Trigger: Calling cache.has_previous_state() with no layer_idx on a standard attention-only cache (e.g. DynamicCache for a Llama-style model).
Common situations: Generic code that probes has_previous_state to decide between prefill and decode paths, run on a model without linear attention layers; sharing utilities between Mamba-style and attention models.
Related errors
- Cannot call `update_conv_state` on a non-LinearAttention lay
- You called `get_seq_length` on layer index {layer_idx}, but
- `get_seq_length` can only be called on Attention layers, and
- You called `has_previous_state` on layer index {layer_idx},
- You called `get_mask_sizes` on layer index {layer_idx}, but
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/2e5d768e64d8dd59.
Report an issue: GitHub.