{"record":{"id":"f4f13acedd412040","repo":"huggingface/transformers","slug":"you-called-has-previous-state-on-layer-index-la","errorCode":null,"errorMessage":"You called `has_previous_state` on layer index {layer_idx}, but this layer is an Attention layer, which does not support calling it.","messagePattern":"You called `has_previous_state` on layer index (.+?), but this layer is an Attention layer, which does not support calling it\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/cache_utils.py","lineNumber":1544,"sourceCode":"        \"\"\"Returns whether the LinearAttention layer at index `layer_idx` has previous state or not.\"\"\"\n        if layer_idx is not None and layer_idx >= len(self.layers):\n            return False\n\n        # In this case, use last LinearAttention layer\n        if layer_idx is None:\n            try:\n                layer_idx = next(\n                    idx\n                    for idx in range(len(self) - 1, -1, -1)\n                    if isinstance(self.layers[idx], LinearAttentionCacheLayerMixin)\n                )\n            except StopIteration:\n                raise ValueError(\n                    \"`has_previous_state` can only be called on LinearAttention layers, and the current Cache seem to \"\n                    \"only contain Attention layers.\"\n                )\n        elif not isinstance(self.layers[layer_idx], LinearAttentionCacheLayerMixin):\n            raise ValueError(\n                f\"You called `has_previous_state` on layer index {layer_idx}, but this layer is an Attention layer, which \"\n                \"does not support calling it.\"\n            )\n\n        # We may have several conv/recurrent states in the same layers. In this case, if `state_idx` is not provided, check if all\n        # of them have previous state\n        if state_idx is None:\n            return all(self.layers[layer_idx].has_previous_state.values())\n        return self.layers[layer_idx].has_previous_state[state_idx]\n\n    def get_mask_sizes(self, query_length: int, layer_idx: int) -> tuple[int, int]:\n        \"\"\"\n        Return a tuple (kv_length, kv_offset) corresponding to the length and offset that will be returned for\n        the given layer at `layer_idx`.\n        The masks are then prepared according to the given lengths (kv_length, kv_offset) and patterns for each layer.\n        \"\"\"\n        # For DynamicCache, where the layers are created at runtime -> if it was not yet created, the size is\n        # simply the query_length","sourceCodeStart":1526,"sourceCodeEnd":1562,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/cache_utils.py#L1526-L1562","documentation":"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.","triggerScenarios":"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.","commonSituations":"Per-layer readiness checks in custom generate loops for hybrid models; assuming the API is layer-type agnostic.","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"],"exampleFix":"# before\nok = all(cache.has_previous_state(i) for i in range(len(cache.layers)))\n\n# after\nok = all(\n    cache.has_previous_state(i)\n    for i in range(len(cache.layers))\n    if isinstance(cache.layers[i], LinearAttentionCacheLayerMixin)\n)","handlingStrategy":"type-guard","validationCode":"from transformers.cache_utils import LinearAttentionCacheLayerMixin\n\nif layer_idx is None or isinstance(cache.layers[layer_idx], LinearAttentionCacheLayerMixin):\n    ok = cache.has_previous_state(layer_idx)","typeGuard":"from transformers.cache_utils import LinearAttentionCacheLayerMixin\n\ndef supports_previous_state(layer) -> bool:\n    return isinstance(layer, LinearAttentionCacheLayerMixin)","tryCatchPattern":null,"preventionTips":["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"],"tags":["cache","linear-attention","layer-type","valueerror"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}