{"record":{"id":"bc02895aef243fab","repo":"keras-team/keras","slug":"expected-t-name-to-have-shape-b-1-t-s-or-bc0289","errorCode":null,"errorMessage":"Expected `{t_name}` to have shape (B, 1, T, S) or (B, N, T, S) with N={N} but got {t.shape}.","messagePattern":"Expected `(.+?)` to have shape \\(B, 1, T, S\\) or \\(B, N, T, S\\) with N=(.+?) but got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/jax/ops/nn.py","lineNumber":1857,"sourceCode":"\n    # _dot_product_attention_xla\n    B, T, N, H = query.shape\n    G = N // K\n    query = jnp.reshape(query, (B, T, K, G, H))\n\n    def _reshape_to_grouped(t, t_name):\n        if t is not None:\n            while t.ndim < 4:\n                if t.ndim == 3 and t.shape[1] == N:\n                    t = jnp.expand_dims(t, axis=2)\n                else:\n                    t = jnp.expand_dims(t, axis=1)\n            tB, tN, tT, tS = t.shape\n            if tN == 1:\n                t = jnp.broadcast_to(t[:, :, None, :, :], (tB, tN, G, tT, tS))\n            else:\n                if tN != N:\n                    raise ValueError(\n                        f\"Expected `{t_name}` to have shape (B, 1, T, S) or \"\n                        f\"(B, N, T, S) with N={N} but got {t.shape}.\"\n                    )\n                t = jnp.reshape(t, (tB, K, G, tT, tS))\n        return t\n\n    bias = _reshape_to_grouped(bias, \"bias\")\n    mask = _reshape_to_grouped(mask, \"mask\")\n    vmapped_fn = jax.vmap(\n        _dot_product_attention_core,\n        in_axes=(3, None, None, 2, 2, None, None),\n        out_axes=3,\n    )\n    encoded = vmapped_fn(query, key, value, bias, mask, is_causal, scale)\n    return jnp.reshape(encoded, output_shape)\n\n\ndef unfold(input, kernel_size, dilation=1, padding=0, stride=1):","sourceCodeStart":1839,"sourceCodeEnd":1875,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/jax/ops/nn.py#L1839-L1875","documentation":"Shape check inside _dot_product_attention_xla's grouped-attention helper: an optional tensor (bias or mask, named t_name) is promoted to 4D and its head dimension (axis 1) must be either 1 (broadcast over heads) or exactly N (query heads) so it can be broadcast across the G head groups. Any other head count fails this guard.","triggerScenarios":"Thrown at keras/src/backend/jax/ops/nn.py:1857 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Reshape the bias/mask so its head axis is 1 (shared across all heads) or N (one per head)","Tile a per-group bias to full head count: np.repeat(bias, G, axis=1) if you have G groups","Verify N equals query.shape[2] and that you didn't pass a (B, T, S) or (B, G, T, S) tensor where heads are expected"],"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"}