xai-org/x-algorithm · error · ValueError
Expected {k.shape=} to be {kv_shape} (inferred from q)
Error message
Expected {k.shape=} to be {kv_shape} (inferred from q) What it means
After unpacking q's shape, _attention_forward infers the expected kv shape (batch_size, kv_seq_len, num_kv_heads, head_dim) from q and k, and verifies k matches exactly: batch, head_dim and kv-head count must agree with q; only seq_len may differ.
Source
Thrown at phoenix/xrex/pallas/ranker_attention_fa3.py:95
def _attention_forward(
q,
k,
v,
config: TuningConfig,
save_residuals: bool = False,
bound=None,
sm_scale: float = 1.0,
cap: float = -1.0,
cap_method: str = "tanh",
):
if q.ndim != 4 or k.ndim != 4 or v.ndim != 4:
raise ValueError(f"q, k, and v should all be 4D, got: {q.ndim=}, {k.ndim=}, {v.ndim=}")
batch_size, q_seq_len, num_q_heads, head_dim = q.shape
_, kv_seq_len, num_kv_heads, _ = k.shape
kv_shape = (batch_size, kv_seq_len, num_kv_heads, head_dim)
if k.shape != kv_shape:
raise ValueError(f"Expected {k.shape=} to be {kv_shape} (inferred from q)")
if v.shape != kv_shape:
raise ValueError(f"Expected {v.shape=} to be {kv_shape} (inferred from q)")
if (dtype := q.dtype) != k.dtype or dtype != v.dtype:
raise ValueError(
f"q, k, and v should all have the same dtype, got: {q.dtype}, {k.dtype}, {v.dtype}"
)
if num_q_heads % num_kv_heads:
raise ValueError(f"{num_q_heads=} must be divisible by and {num_kv_heads=}")
q_heads_per_kv_head = num_q_heads // num_kv_heads
if head_dim % 64:
raise ValueError(f"{head_dim=} must be divisible by 64")
if jnp.dtype(dtype) not in map(jnp.dtype, [jnp.float16, jnp.bfloat16]):
raise NotImplementedError(f"Only f16 and bf16 are supported, got dtype: {dtype}")
max_concurrent_steps = min(config.max_concurrent_steps, kv_seq_len // config.block_kv)
block_q, block_kv = config.block_q, config.block_kv
if kv_seq_len % block_kv:
raise ValueError(f"{kv_seq_len=} must be a multiple of {block_kv=}")View on GitHub (pinned to 24c60942c5)
Solutions
- Group kv heads correctly: num_kv_heads = num_q_heads / group_size
- Ensure batch size and head_dim match q exactly
- Verify with an assert/print of q.shape and k.shape before the call
Example fix
# before k = k # (B, S_kv, num_q_heads, D) — ungrouped # after k = k[:, :, :num_kv_heads, :] # or repeat correctly for GQA: (B, S_kv, num_kv_heads, D)
Defensive patterns
Strategy: validation
Validate before calling
B, _, Hq, D = q.shape assert k.shape == (B, k.shape[1], k.shape[2], D) and k.shape[2] in divisors(Hq)
Prevention
- Log q.shape/k.shape once before training
- Write a GQA shape test comparing kv head grouping against config
When it happens
Trigger: k with a different batch size, head_dim, or num_kv_heads than q (e.g. k reshaped to (B, S_kv, H_q, D) instead of grouping kv heads for GQA).
Common situations: GQA setups where k/v still carry the full query-head count; mixed dtypes/layouts after rearrange; off-by-one head grouping in MQA.
Understand the failure class
Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.
Related errors
- q, k, and v should all be 4D, got: {q.ndim=}, {k.ndim=}, {v.
- Expected {v.shape=} to be {kv_shape} (inferred from q)
- {num_q_heads=} must be divisible by and {num_kv_heads=}
- {kv_seq_len=} must be a multiple of {block_kv=}
- {q_seq_len=} must be a multiple of {block_q * 2=}
AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28).
Data as JSON: /api/errors/d8b12f4331555373.
Report an issue: GitHub.