{"record":{"id":"d97e36a6b265477f","repo":"xai-org/x-algorithm","slug":"q-k-and-v-should-all-have-the-same-dtype-got","errorCode":null,"errorMessage":"q, k, and v should all have the same dtype, got: {q.dtype}, {k.dtype}, {v.dtype}","messagePattern":"q, k, and v should all have the same dtype, got: (.+?), (.+?), (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phoenix/xrex/pallas/ranker_attention_fa3.py","lineNumber":99,"sourceCode":"    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):\n        batch = lax.axis_index(\"batch\")\n        q_head = lax.axis_index(\"heads\")","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/phoenix/xrex/pallas/ranker_attention_fa3.py#L81-L117","documentation":"_attention_forward requires q, k, v to share one dtype because the kernel does in-register mixed-precision math (tma loads + f32 accumulate) and assumes uniform element width.","triggerScenarios":"Passing q in bfloat16 but k (or v) in float16 or float32 — common when q is cast for stability but kv cache stores another dtype.","commonSituations":"Loading a half-precision kv cache while keeping queries in fp32; mixing params saved under different dtypes after checkpoint conversion.","solutions":["Cast all of q, k, v to the same dtype (bf16 or fp16) at the call site","Pick bf16 as it is the safer default for stability"],"exampleFix":"# before\nout = attention(q.astype(jnp.float32), k, v)\n# after\nout = attention(q.astype(k.dtype), k, v)","handlingStrategy":"validation","validationCode":"assert q.dtype == k.dtype == v.dtype\nq, k, v = (x.astype(jnp.bfloat16) for x in (q, k, v))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Cast at the module boundary, not deep inside the model","Set one global compute dtype constant and use it everywhere"],"tags":["jax","attention","dtype-validation"],"backgroundTag":"tensor-dtype-mismatch","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}