{"record":{"id":"db888d808b9c6f78","repo":"keras-team/keras","slug":"dot-product-attention-only-supports-4d-inputs-r-db888d","errorCode":null,"errorMessage":"`dot_product_attention` only supports 4D inputs. Received: query.shape={query.shape}, key.shape={key.shape}, value.shape={value.shape}.","messagePattern":"`dot_product_attention` only supports 4D inputs\\. Received: query\\.shape=(.+?), key\\.shape=(.+?), value\\.shape=(.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/numpy/nn.py","lineNumber":1435,"sourceCode":"    mask=None,\n    scale=None,\n    is_causal=False,\n    flash_attention=None,\n    attn_logits_soft_cap=None,\n):\n    if flash_attention is None:\n        flash_attention = False\n    if flash_attention:\n        raise ValueError(\"Flash attention is not supported in numpy backend.\")\n\n    # Ref: jax.nn.dot_product_attention\n    # https://github.com/jax-ml/jax/blob/jax-v0.4.32/jax/_src/nn/functions.py#L828\n    # Not support `query_seq_lengths` and `key_value_seq_lengths` args\n    query = convert_to_tensor(query)\n    key = convert_to_tensor(key)\n    value = convert_to_tensor(value)\n    if len(query.shape) != 4:\n        raise ValueError(\n            \"`dot_product_attention` only supports 4D inputs. \"\n            f\"Received: query.shape={query.shape}, key.shape={key.shape}, \"\n            f\"value.shape={value.shape}.\"\n        )\n    compute_dtype = backend.result_type(query.dtype, key.dtype, value.dtype)\n    query = cast(query, compute_dtype)\n    key = cast(key, compute_dtype)\n    value = cast(value, compute_dtype)\n    if bias is not None:\n        bias = convert_to_tensor(bias, dtype=compute_dtype)\n\n    _, _, _, H = key.shape\n    scale = (1.0 / np.sqrt(H)) if scale is None else scale\n    return _dot_product_attention_xla(\n        query, key, value, bias, mask, is_causal, scale\n    )\n\n","sourceCodeStart":1417,"sourceCodeEnd":1453,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/numpy/nn.py#L1417-L1453","documentation":"A rank guard in the NumPy `dot_product_attention`: after converting query, key and value to tensors, it verifies all three are 4D (batch, heads, sequence, feature dims), the layout the attention implementation assumes. If any of the three inputs has a rank other than 4 (e.g. a missing head or batch axis), this error reports all three shapes so you can see which tensor is malformed.","triggerScenarios":"Thrown at keras/src/backend/numpy/nn.py:1435 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Expand missing axes on query/key/value, e.g. query = query[None, ...] or keras.ops.expand_dims to add batch and/or head dimensions until each is (batch, heads, seq, features)","Split a 3D tensor into per-head 4D tensors if you folded the head axis into features","Pass tensors that already come from a MultiHeadAttention-style projection which produces 4D q/k/v"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}