{"record":{"id":"cbb72156ff1f7f69","repo":"huggingface/transformers","slug":"get-seq-length-can-only-be-called-on-attention-l","errorCode":null,"errorMessage":"`get_seq_length` can only be called on Attention layers, and the current Cache seem to only contain LinearAttention layers.","messagePattern":"`get_seq_length` can only be called on Attention layers, and the current Cache seem to only contain LinearAttention layers\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/cache_utils.py","lineNumber":1502,"sourceCode":"\n    def get_seq_length(self, layer_idx: int = 0) -> int:\n        \"\"\"Returns the sequence length of the cache for the given layer.\"\"\"\n        if layer_idx >= len(self.layers):\n            return 0\n\n        # For alternating attention/linear attention  caches, `get_seq_length` needs to use attention layer idx when called with default layer_idx\n        if not isinstance(self.layers[layer_idx], CacheLayerMixin):\n            # If this is called with non-default arg, raise\n            if layer_idx != 0:\n                raise ValueError(\n                    f\"You called `get_seq_length` on layer index {layer_idx}, but this layer is a LinearAttention layer, which \"\n                    \"does not track sequence length.\"\n                )\n            try:\n                # Use the first attention layer\n                layer_idx = next(idx for idx in range(len(self)) if isinstance(self.layers[idx], CacheLayerMixin))\n            except StopIteration:\n                raise ValueError(\n                    \"`get_seq_length` can only be called on Attention layers, and the current Cache seem to only contain \"\n                    \"LinearAttention layers.\"\n                )\n\n        return self.layers[layer_idx].get_seq_length()\n\n    def get_max_length(self, layer_idx: int | None = None) -> int:\n        \"\"\"\n        Returns the maximum length of the cache. If `layer_idx` is not provided (default), this returns the maximum\n        across all layers. Otherwise, return the maximum supported value for the given layer.\n        A value of `-1` means no maximum, or undefined maximum, e.g. for dynamic attention layers that can grow indefinitely,\n        or linear attention layer that do not have a sequence length dimension.\n        \"\"\"\n        # For DynamicCache, where the layers are created at runtime\n        if layer_idx is not None and layer_idx >= len(self.layers):\n            return -1\n\n        if layer_idx is None:","sourceCodeStart":1484,"sourceCodeEnd":1520,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/cache_utils.py#L1484-L1520","documentation":"Cache.get_seq_length() raises ValueError (via StopIteration fallback) when the default layer_idx=0 lands on a linear attention layer and NO layer in the cache is an attention layer. The method searches for the first CacheLayerMixin; on an all-linear-attention cache there is nothing to measure sequence length on.","triggerScenarios":"Calling get_seq_length() on a cache built exclusively from LinearAttentionCacheLayerMixin layers (pure Mamba/linear-attention models).","commonSituations":"Generic generation or logging code that unconditionally calls cache.get_seq_length() on any cache; switching a pipeline from a hybrid model to a pure linear-attention model.","solutions":["Skip sequence-length queries for pure linear attention models — their caches have no seq dimension","Guard with any(isinstance(l, CacheLayerMixin) for l in cache.layers) before calling","Use model-specific state handling (has_previous_state) for linear-attention models instead"],"exampleFix":"# before\npast_len = cache.get_seq_length()  # pure linear attention cache\n\n# after\nif any(isinstance(l, CacheLayerMixin) for l in cache.layers):\n    past_len = cache.get_seq_length()\nelse:\n    past_len = None  # linear attention cache: no sequence length","handlingStrategy":"type-guard","validationCode":"from transformers.cache_utils import CacheLayerMixin\n\nif any(isinstance(l, CacheLayerMixin) for l in cache.layers):\n    seq_len = cache.get_seq_length()\nelse:\n    seq_len = None  # pure linear attention cache: no sequence length","typeGuard":"from transformers.cache_utils import CacheLayerMixin\n\ndef cache_has_attention_layers(cache) -> bool:\n    return any(isinstance(l, CacheLayerMixin) for l in cache.layers)","tryCatchPattern":null,"preventionTips":["Branch shared generation code on cache composition before querying sequence length","Remember linear attention caches are state-sized, not sequence-sized"],"tags":["cache","linear-attention","get-seq-length","valueerror"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}