{"record":{"id":"2bd820f2bd648210","repo":"huggingface/transformers","slug":"get-mask-sizes-can-only-be-called-on-attention-l","errorCode":null,"errorMessage":"`get_mask_sizes` can only be called on Attention layers, and the current Cache seem to only contain LinearAttention layers.","messagePattern":"`get_mask_sizes` 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":1578,"sourceCode":"        \"\"\"\n        # For DynamicCache, where the layers are created at runtime -> if it was not yet created, the size is\n        # simply the query_length\n        if layer_idx >= len(self.layers):\n            return query_length, 0\n\n        # For alternating attention/linear attention caches, `get_mask_sizes` 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_mask_sizes` 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_mask_sizes` 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_mask_sizes(query_length)\n\n    def get_query_offset(self, layer_idx: int = 0) -> int:\n        \"\"\"Returns the current offset of the query for the given `layer_idx`. It's always equal to the cache length, i.e.\n        `get_seq_length(layer_idx)`, except for MTP layers.\n        \"\"\"\n        # It's simply equal to the length of the past states, except in very specific cases, see `MtpCache`\n        return self.get_seq_length(layer_idx=layer_idx)\n\n    def reset(self):\n        \"\"\"Recursively reset all layers tensors\"\"\"\n        for layer_idx in range(len(self.layers)):\n            self.layers[layer_idx].reset()\n","sourceCodeStart":1560,"sourceCodeEnd":1596,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/cache_utils.py#L1560-L1596","documentation":"Cache.get_mask_sizes() raises ValueError (StopIteration fallback) when the default layer_idx points at a linear attention layer and the cache contains no attention layers at all. With no CacheLayerMixin layer there is no KV length to size masks against.","triggerScenarios":"Calling get_mask_sizes() on a pure linear-attention cache (Mamba-style model with no attention layers).","commonSituations":"Generic generation utilities that prepare masks for every cache type; switching pipelines from hybrid to pure linear-attention models.","solutions":["Do not prepare attention masks for pure linear attention models — they are unused","Guard with any(isinstance(l, CacheLayerMixin) for l in cache.layers) before calling","Branch on model architecture (presence of layer_types == 'full_attention') in shared code"],"exampleFix":"# before\nkv_len, offset = cache.get_mask_sizes(q_len)  # all-linear cache\n\n# after\nif any(isinstance(l, CacheLayerMixin) for l in cache.layers):\n    kv_len, offset = cache.get_mask_sizes(q_len)\nelse:\n    kv_len, offset = q_len, 0","handlingStrategy":"type-guard","validationCode":"from transformers.cache_utils import CacheLayerMixin\n\nif any(isinstance(l, CacheLayerMixin) for l in cache.layers):\n    kv_len, offset = cache.get_mask_sizes(query_length)\nelse:\n    kv_len, offset = query_length, 0  # pure linear attention: no attention masks needed","typeGuard":"from transformers.cache_utils import CacheLayerMixin\n\ndef needs_attention_masks(cache) -> bool:\n    return any(isinstance(l, CacheLayerMixin) for l in cache.layers)","tryCatchPattern":null,"preventionTips":["Skip mask construction entirely for architectures without attention layers","Feature-detect on cache composition rather than model name lists"],"tags":["cache","linear-attention","mask","valueerror"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}