{"record":{"id":"1162183adc2cc2e9","repo":"xai-org/x-algorithm","slug":"expected-v-shape-to-be-kv-shape-inferred-fro","errorCode":null,"errorMessage":"Expected {v.shape=} to be {kv_shape} (inferred from q)","messagePattern":"Expected (.+?) to be (.+?) \\(inferred from q\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phoenix/xrex/pallas/ranker_attention_fa3.py","lineNumber":97,"sourceCode":"    q,\n    k,\n    v,\n    config: TuningConfig,\n    save_residuals: bool = False,\n    bound=None,\n    sm_scale: float = 1.0,\n    cap: float = -1.0,\n    cap_method: str = \"tanh\",\n):\n    if q.ndim != 4 or k.ndim != 4 or v.ndim != 4:\n        raise ValueError(f\"q, k, and v should all be 4D, got: {q.ndim=}, {k.ndim=}, {v.ndim=}\")\n    batch_size, q_seq_len, num_q_heads, head_dim = q.shape\n    _, kv_seq_len, num_kv_heads, _ = k.shape\n    kv_shape = (batch_size, kv_seq_len, num_kv_heads, head_dim)\n    if k.shape != kv_shape:\n        raise ValueError(f\"Expected {k.shape=} to be {kv_shape} (inferred from q)\")\n    if v.shape != kv_shape:\n        raise ValueError(f\"Expected {v.shape=} to be {kv_shape} (inferred from q)\")\n    if (dtype := q.dtype) != k.dtype or dtype != v.dtype:\n        raise ValueError(\n            f\"q, k, and v should all have the same dtype, got: {q.dtype}, {k.dtype}, {v.dtype}\"\n        )\n    if num_q_heads % num_kv_heads:\n        raise ValueError(f\"{num_q_heads=} must be divisible by and {num_kv_heads=}\")\n    q_heads_per_kv_head = num_q_heads // num_kv_heads\n    if head_dim % 64:\n        raise ValueError(f\"{head_dim=} must be divisible by 64\")\n    if jnp.dtype(dtype) not in map(jnp.dtype, [jnp.float16, jnp.bfloat16]):\n        raise NotImplementedError(f\"Only f16 and bf16 are supported, got dtype: {dtype}\")\n\n    max_concurrent_steps = min(config.max_concurrent_steps, kv_seq_len // config.block_kv)\n    block_q, block_kv = config.block_q, config.block_kv\n    if kv_seq_len % block_kv:\n        raise ValueError(f\"{kv_seq_len=} must be a multiple of {block_kv=}\")\n\n    def kernel(q_ref, k_ref, v_ref, bound_ref, out_ref, lse_ref, scoped):","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/phoenix/xrex/pallas/ranker_attention_fa3.py#L79-L115","documentation":"Same inference check as for k: v must match the kv shape (batch_size, kv_seq_len, num_kv_heads, head_dim) inferred from q and k. v's batch, kv seq length, kv head count, and head_dim must all line up.","triggerScenarios":"v with a different head count or head_dim than k (e.g. v projected with a different value width), or mismatched sequence length after truncation/padding.","commonSituations":"Mismatched K and V projections in custom attention layers; slicing bugs that trim v's sequence but not k's.","solutions":["Make v exactly the same shape as k","Check the value projection output features equals head_dim * num_kv_heads","Re-run shape checks after any slicing/padding of kv cache"],"exampleFix":"# before\nv = proj_v(x[:, :kv_len - 1])  # off-by-one slice\n# after\nv = proj_v(x[:, :kv_len])","handlingStrategy":"validation","validationCode":"assert v.shape == k.shape, f\"v {v.shape} != k {k.shape}\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive k and v from the same reshaped cache tensor","Add shape asserts right after projections in the attention layer"],"tags":["jax","attention","shape-validation"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}