{"record":{"id":"1a7a11cc58b4e793","repo":"sgl-project/sglang","slug":"cos-and-sin-must-be-cuda-and-last-dim-contiguous","errorCode":null,"errorMessage":"cos and sin must be CUDA and last-dim contiguous","messagePattern":"cos and sin must be CUDA and last-dim contiguous","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/diffusion/rope/hunyuan_qkv_pack_triton.py","lineNumber":185,"sourceCode":"        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,\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            )","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/diffusion/rope/hunyuan_qkv_pack_triton.py#L167-L203","documentation":"cos and sin must be CUDA tensors with unit stride in the last dimension; the Triton kernel loads them directly from GPU memory assuming a dense innermost dimension. CPU tables or non-contiguous slices are rejected.","triggerScenarios":"Passing cos/sin computed on CPU (not moved with .cuda()), or a non-contiguous slice like freqs[:, ::2] as the table.","commonSituations":"Precomputing RoPE tables on CPU at model init and forgetting .to(device), or column-subsampled frequency tables.","solutions":["Move tables to the same CUDA device: cos = cos.cuda() (or .to(img_q.device)).","Call .contiguous() if the table was sliced.","Precompute tables once on GPU at model setup."],"exampleFix":"// before\ncos, sin = build_rope_table(seq, head_dim)  # CPU tensors\n// after\ncos, sin = build_rope_table(seq, head_dim)\ncos, sin = cos.to('cuda').contiguous(), sin.to('cuda').contiguous()","handlingStrategy":"validation","validationCode":"assert cos.is_cuda and sin.is_cuda and cos.stride(-1) == 1 and sin.stride(-1) == 1","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Create RoPE tables on GPU once at model init.","Apply .contiguous() after any table slicing."],"tags":["rope","cuda","device-placement","contiguity"],"backgroundTag":"tensor-on-wrong-device","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}