{"record":{"id":"251800a50da61f92","repo":"sgl-project/sglang","slug":"qkv-tensors-must-be-cuda-bfloat16-tensors","errorCode":null,"errorMessage":"QKV tensors must be CUDA bfloat16 tensors","messagePattern":"QKV tensors must be CUDA bfloat16 tensors","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/diffusion/rope/hunyuan_qkv_pack_triton.py","lineNumber":165,"sourceCode":"    tl.store(output_ptr + 2 * plane_stride + output_row + even[None, :], v0, mask=mask)\n    tl.store(output_ptr + 2 * plane_stride + output_row + odd[None, :], v1, mask=mask)\n\n\ndef hunyuan_qkv_rope_pack(\n    img_q: torch.Tensor,\n    img_k: torch.Tensor,\n    img_v: torch.Tensor,\n    txt_q: torch.Tensor,\n    txt_k: torch.Tensor,\n    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\")","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/diffusion/rope/hunyuan_qkv_pack_triton.py#L147-L183","documentation":"The Hunyuan QKV RoPE pack Triton kernel is written exclusively for CUDA bfloat16; every img/txt Q/K/V tensor must be on GPU and torch.bfloat16, otherwise the loads/stores and dtype assumptions break.","triggerScenarios":"Passing fp16 or fp32 QKV tensors, or CPU tensors not yet moved to GPU, to hunyuan_qkv_rope_pack.","commonSituations":"Running the model in float16 precision mode, or under a CPU meta/init pass where projections are still on CPU; mixed-precision configs that leave attention in fp32.","solutions":["Cast all six tensors to bfloat16 and .cuda() before the call","Run the model with bfloat16 dtype (torch_dtype=torch.bfloat16)","Use a non-fused RoPE path for other precisions"],"exampleFix":"# before\nout = hunyuan_qkv_rope_pack(img_q, ...)  # fp16 tensors\n# after\nout = hunyuan_qkv_rope_pack(img_q.to(torch.bfloat16).cuda(), img_k.to(torch.bfloat16).cuda(), ...)","handlingStrategy":"validation","validationCode":"if not all(t.is_cuda and t.dtype == torch.bfloat16 for t in tensors):\n    tensors = [t.to(torch.bfloat16).cuda() for t in tensors]","typeGuard":"def is_cuda_bf16(t: torch.Tensor) -> bool:\n    return t.is_cuda and t.dtype == torch.bfloat16","tryCatchPattern":null,"preventionTips":["Run Hunyuan attention in bfloat16 end-to-end","Move tensors to GPU before fused rope/pack"],"tags":["dtype","device","rope","bfloat16","hunyuan"],"backgroundTag":"unsupported-tensor-dtype","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}