{"record":{"id":"bda904f1c9d352e1","repo":"sgl-project/sglang","slug":"qkv-and-cos-sin-tensors-must-be-on-the-same-cuda-d","errorCode":null,"errorMessage":"QKV and cos/sin tensors must be on the same CUDA device","messagePattern":"QKV and cos/sin tensors must be on the same CUDA device","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/diffusion/rope/hunyuan_qkv_pack_triton.py","lineNumber":187,"sourceCode":"    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,\n    )\n    args = []\n    for x in tensors:\n        args.extend((x.stride(0), x.stride(1), x.stride(2)))\n    with torch.cuda.device(img_q.device):\n        _hunyuan_qkv_rope_pack_kernel[\n            lambda meta: (\n                batch * total_tokens,\n                triton.cdiv(num_heads, meta[\"BLOCK_HEADS\"]),\n            )\n        ](\n            *tensors,","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/diffusion/rope/hunyuan_qkv_pack_triton.py#L169-L205","documentation":"All QKV tensors and the cos/sin RoPE tables must live on the same CUDA device. The kernel launches on img_q's device and cross-device pointers would be invalid.","triggerScenarios":"img_q on cuda:1 while cos was created on cuda:0 (or vice versa) in multi-GPU / tensor-parallel Hunyuan inference.","commonSituations":"Tensor-parallel or pipeline-parallel setups where RoPE tables are cached on device 0 but projections run on another rank's device.","solutions":["Move cos/sin to img_q.device before the call.","Create per-rank RoPE tables using the local device in TP code.","Set CUDA_VISIBLE_DEVICES or torch.cuda.set_device consistently."],"exampleFix":"// before\ncos = cos.to('cuda:0')  # while img_q is on cuda:1\n// after\ncos = cos.to(img_q.device)\nsin = sin.to(img_q.device)","handlingStrategy":"validation","validationCode":"assert cos.device == img_q.device and sin.device == img_q.device","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use img_q.device (not hardcoded 'cuda') when moving tensors.","Build per-rank tables using the local device under TP."],"tags":["cuda","multi-gpu","device-mismatch","rope"],"backgroundTag":"cuda-device-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}