{"record":{"id":"0e0629c8ab427f6e","repo":"sgl-project/sglang","slug":"text-qkv-shapes-must-match","errorCode":null,"errorMessage":"text QKV shapes must match","messagePattern":"text QKV shapes must match","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/diffusion/rope/hunyuan_qkv_pack_triton.py","lineNumber":175,"sourceCode":"    txt_v: torch.Tensor,\n    cos: torch.Tensor,\n    sin: torch.Tensor,\n) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:\n    tensors = (img_q, img_k, img_v, txt_q, txt_k, txt_v)\n    if any(x.ndim != 4 for x in tensors):\n        raise ValueError(\"QKV tensors must have shape [B, S, H, D]\")\n    if any(not x.is_cuda or x.dtype != torch.bfloat16 for x in tensors):\n        raise ValueError(\"QKV tensors must be CUDA bfloat16 tensors\")\n    if any(x.device != img_q.device for x in tensors):\n        raise ValueError(\"QKV tensors must be on the same CUDA device\")\n    batch, img_tokens, num_heads, head_dim = img_q.shape\n    txt_tokens = txt_q.shape[1]\n    expected_img = (batch, img_tokens, num_heads, head_dim)\n    expected_txt = (batch, txt_tokens, num_heads, head_dim)\n    if any(tuple(x.shape) != expected_img for x in (img_q, img_k, img_v)):\n        raise ValueError(\"image QKV shapes must match\")\n    if any(tuple(x.shape) != expected_txt for x in (txt_q, txt_k, txt_v)):\n        raise ValueError(\"text QKV shapes must match\")\n    if any(x.stride(-1) != 1 for x in tensors):\n        raise ValueError(\"QKV last dimensions must be contiguous\")\n    if head_dim <= 0 or head_dim > 128 or head_dim % 2:\n        raise ValueError(\"head_dim must be positive, even, and <= 128\")\n    if cos.ndim != 2 or sin.ndim != 2 or cos.shape != sin.shape:\n        raise ValueError(\"cos and sin must have matching [S, D/2] shapes\")\n    if cos.shape[0] < img_tokens or cos.shape[1] != head_dim // 2:\n        raise ValueError(\"cos/sin shape does not cover image tokens and head_dim\")\n    if not cos.is_cuda or not sin.is_cuda or cos.stride(-1) != 1 or sin.stride(-1) != 1:\n        raise ValueError(\"cos and sin must be CUDA and last-dim contiguous\")\n    if cos.device != img_q.device or sin.device != img_q.device:\n        raise ValueError(\"QKV and cos/sin tensors must be on the same CUDA device\")\n\n    total_tokens = img_tokens + txt_tokens\n    storage = torch.empty(\n        (3, batch, total_tokens, num_heads, head_dim),\n        device=img_q.device,\n        dtype=img_q.dtype,","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/diffusion/rope/hunyuan_qkv_pack_triton.py#L157-L193","documentation":"txt_q, txt_k, txt_v must share the exact (B, txt_tokens, num_heads, head_dim) shape derived from txt_q (note: txt_tokens may differ from img_tokens, but head count/dim and batch must match the img side too).","triggerScenarios":"txt_k/txt_v shaped differently from txt_q — different text token counts (mismatched text masks), or head counts mismatched after a projection split.","commonSituations":"Text conditioning stream truncated/padded inconsistently with the img stream; chunk boundaries off when splitting a fused QKV projection.","solutions":["Derive all three txt tensors from the same projection output chunked on the last dim, guaranteeing identical shapes","Assert txt_q.shape == txt_k.shape == txt_v_v.shape before the call"],"exampleFix":"# before\nq, k, v = txt_proj(x).split([H*D, H_kv*D, H_kv*D], dim=-1)\n# after\nq, k, v = txt_proj(x).chunk(3, dim=-1)  # equal sizes -> matching shapes\nq = q.view(B, S_txt, H, D); k = k.view(B, S_txt, H, D); v = v.view(B, S_txt, H, D)","handlingStrategy":"validation","validationCode":"expected = tuple(txt_q.shape)\nassert all(tuple(t.shape) == expected for t in (txt_k, txt_v))","typeGuard":"def txt_qkv_match(q, k, v) -> bool:\n    return q.shape == k.shape == v.shape","tryCatchPattern":null,"preventionTips":["Chunk the txt QKV projection into three equal parts","Keep text sequence length consistent with the attention mask used"],"tags":["shape","attention","hunyuan","rope"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}