{"record":{"id":"2e5d768e64d8dd59","repo":"huggingface/transformers","slug":"has-previous-state-can-only-be-called-on-lineara","errorCode":null,"errorMessage":"`has_previous_state` can only be called on LinearAttention layers, and the current Cache seem to only contain Attention layers.","messagePattern":"`has_previous_state` can only be called on LinearAttention layers, and the current Cache seem to only contain Attention layers\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/cache_utils.py","lineNumber":1539,"sourceCode":"            return max(layer.get_max_length() for layer in self.layers)\n        else:\n            return self.layers[layer_idx].get_max_length()\n\n    def has_previous_state(self, layer_idx: int | None = None, state_idx: int | None = None) -> bool:\n        \"\"\"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","sourceCodeStart":1521,"sourceCodeEnd":1557,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/cache_utils.py#L1521-L1557","documentation":"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.","triggerScenarios":"Calling cache.has_previous_state() with no layer_idx on a standard attention-only cache (e.g. DynamicCache for a Llama-style model).","commonSituations":"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.","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"],"exampleFix":"# before\nif cache.has_previous_state():  # attention-only cache\n    ...\n\n# after\nif any(isinstance(l, LinearAttentionCacheLayerMixin) for l in cache.layers) and cache.has_previous_state():\n    ...","handlingStrategy":"type-guard","validationCode":"from transformers.cache_utils import LinearAttentionCacheLayerMixin\n\nif any(isinstance(l, LinearAttentionCacheLayerMixin) for l in cache.layers):\n    ready = cache.has_previous_state()","typeGuard":"from transformers.cache_utils import LinearAttentionCacheLayerMixin\n\ndef cache_has_linear_attention_layers(cache) -> bool:\n    return any(isinstance(l, LinearAttentionCacheLayerMixin) for l in cache.layers)","tryCatchPattern":null,"preventionTips":["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"],"tags":["cache","linear-attention","has-previous-state","valueerror"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}