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_length

View on GitHub (pinned to a597f97485)

Solutions

  1. Pass a layer_idx that refers to a linear attention layer, or omit layer_idx to auto-select the last one
  2. Filter candidate indices by isinstance(layer, LinearAttentionCacheLayerMixin)
  3. 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

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


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/f4f13acedd412040. Report an issue: GitHub.