{"record":{"id":"d7e6abd8e20d6ad2","repo":"keras-team/keras","slug":"all-dimensions-of-value-and-key-except-the-la","errorCode":null,"errorMessage":"All dimensions of `value` and `key`, except the last one, must be equal. Received: value_shape={value_shape} and key_shape={key_shape}","messagePattern":"All dimensions of `value` and `key`, except the last one, must be equal\\. Received: value_shape=(.+?) and key_shape=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/attention/grouped_query_attention.py","lineNumber":585,"sourceCode":"    def compute_output_shape(\n        self,\n        query_shape,\n        value_shape,\n        key_shape=None,\n    ):\n        if key_shape is None:\n            key_shape = value_shape\n\n        if query_shape[-1] != value_shape[-1]:\n            raise ValueError(\n                \"The last dimension of `query_shape` and `value_shape` \"\n                f\"must be equal, but are {query_shape[-1]}, {value_shape[-1]}. \"\n                f\"Received: query_shape={query_shape}, \"\n                f\"value_shape={value_shape}\"\n            )\n\n        if value_shape[1:-1] != key_shape[1:-1]:\n            raise ValueError(\n                \"All dimensions of `value` and `key`, except the last one, \"\n                f\"must be equal. Received: value_shape={value_shape} and \"\n                f\"key_shape={key_shape}\"\n            )\n\n        return query_shape\n\n    def compute_output_spec(\n        self,\n        query,\n        value,\n        key=None,\n        query_mask=None,\n        value_mask=None,\n        key_mask=None,\n        attention_mask=None,\n        return_attention_scores=False,\n        training=None,","sourceCodeStart":567,"sourceCodeEnd":603,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/attention/grouped_query_attention.py#L567-L603","documentation":"GroupedQueryAttention.compute_output_shape requires value and key shapes to agree on every dimension except the last one (value_shape[1:-1] == key_shape[1:-1]), because keys and values must be paired per token. A mismatch raises ValueError with both shapes during shape inference, typically at build or first call.","triggerScenarios":"Passing key with a different sequence length or intermediate dims than value, e.g. attn(q, k=Input((5,64)), v=Input((10,64))); building a Functional model where the K and V inputs are declared with different shapes.","commonSituations":"Hand-built cross-attention where K and V are preprocessed independently (different pooling or windowing); accidental V/K argument swap so one of them receives the query tensor.","solutions":["Ensure key and value derive from the same tensor or have identical shapes except the last dimension.","Check for swapped positional args: the Keras attention call signature is (query, value, key); passing (query, key, value) is the classic cause.","Align preprocessing (same padding and truncation) for the K and V streams before the layer."],"exampleFix":"# before\nout = attn(query, key_tensor, value_tensor)  # K/V shapes mismatch -> ValueError\n\n# after: Keras convention is call(query, value, key)\nout = attn(query, value_tensor, key_tensor)","handlingStrategy":"validation","validationCode":"assert list(value_shape)[1:-1] == list(key_shape)[1:-1], f'K/V mismatch: {key_shape} vs {value_shape}'","typeGuard":"def kv_aligned(value_shape, key_shape) -> bool:\n    v, k = list(value_shape), list(key_shape)\n    return len(v) == len(k) and v[1:-1] == k[1:-1]","tryCatchPattern":null,"preventionTips":["Remember the Keras attention call order is (query, value, key), not (query, key, value).","Derive K and V from the same tensor whenever possible.","Share padding and truncation between K and V preprocessing."],"tags":["keras","attention","shape-mismatch","validation","grouped-query-attention"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}