huggingface/transformers · error · ValueError
You called `has_previous_state` on layer index {layer_idx},
Error message
You called `has_previous_state` on layer index {layer_idx}, but this layer is an Attention layer, which does not support calling it. What it means
Cache.has_previous_state() raises ValueError when called with an explicit layer_idx whose layer is not a LinearAttentionCacheLayerMixin. Previous-state semantics (conv/recurrent state readiness for step decoding) only exist on linear attention layers.
Source
Thrown at src/transformers/cache_utils.py:1544
"""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 for
the given layer at `layer_idx`.
The masks are then prepared according to the given lengths (kv_length, kv_offset) and patterns for each layer.
"""
# For DynamicCache, where the layers are created at runtime -> if it was not yet created, the size is
# simply the query_lengthView on GitHub (pinned to a597f97485)
Solutions
- Pass a layer_idx that refers to a linear attention layer, or omit layer_idx to auto-select the last one
- Filter candidate indices by isinstance(layer, LinearAttentionCacheLayerMixin)
- Use config.layer_types to pick valid indices
Example fix
# before
ok = all(cache.has_previous_state(i) for i in range(len(cache.layers)))
# after
ok = all(
cache.has_previous_state(i)
for i in range(len(cache.layers))
if isinstance(cache.layers[i], LinearAttentionCacheLayerMixin)
) Defensive patterns
Strategy: type-guard
Validate before calling
from transformers.cache_utils import LinearAttentionCacheLayerMixin
if layer_idx is None or isinstance(cache.layers[layer_idx], LinearAttentionCacheLayerMixin):
ok = cache.has_previous_state(layer_idx) Type guard
from transformers.cache_utils import LinearAttentionCacheLayerMixin
def supports_previous_state(layer) -> bool:
return isinstance(layer, LinearAttentionCacheLayerMixin) Prevention
- Omit layer_idx to auto-target the last linear attention layer
- Keep a per-model list of linear attention layer indices derived from config.layer_types
When it happens
Trigger: cache.has_previous_state(layer_idx=k) where layers[k] is an attention layer — e.g. uniformly probing every layer index on a hybrid model.
Common situations: Per-layer readiness checks in custom generate loops for hybrid models; assuming the API is layer-type agnostic.
Related errors
- Cannot call `update_conv_state` on a non-LinearAttention lay
- You called `get_seq_length` on layer index {layer_idx}, but
- You called `get_mask_sizes` on layer index {layer_idx}, but
- Cannot call `update_indexer` on layer {layer_idx} which is a
- `get_seq_length` can only be called on Attention layers, and
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/f4f13acedd412040.
Report an issue: GitHub.