{"record":{"id":"3263ecea5bd77d59","repo":"sgl-project/sglang","slug":"name-must-be-a-cuda-tensor","errorCode":null,"errorMessage":"{name} must be a CUDA tensor","messagePattern":"(.+?) must be a CUDA tensor","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/sparse_mla_q8kv8_prefill_sm90.py","lineNumber":425,"sourceCode":"        if attn_sink.shape != (h_q,) or attn_sink.dtype != torch.float32:\n            raise ValueError(\n                f\"attn_sink must be float32 with shape ({h_q},), got \"\n                f\"{tuple(attn_sink.shape)}/{attn_sink.dtype}\"\n            )\n        if not attn_sink.is_cuda:\n            raise ValueError(\"attn_sink must be a CUDA tensor\")\n        if attn_sink.device != device:\n            raise ValueError(\n                f\"attn_sink must be on q's device {device}, got {attn_sink.device}\"\n            )\n        if not attn_sink.is_contiguous():\n            raise ValueError(\"attn_sink must be contiguous\")\n\n    for name, scale in ((\"q_scale\", q_scale), (\"kv_scale\", kv_scale)):\n        if not isinstance(scale, torch.Tensor):\n            raise ValueError(f\"{name} must be a torch.Tensor\")\n        if not scale.is_cuda:\n            raise ValueError(f\"{name} must be a CUDA tensor\")\n        if scale.device != device:\n            raise ValueError(\n                f\"{name} must be on q's device {device}, got {scale.device}\"\n            )\n        if scale.dtype != torch.float32:\n            raise ValueError(f\"{name} must be float32, got {scale.dtype}\")\n        if scale.numel() != 1:\n            raise ValueError(\n                f\"{name} must be a scalar tensor, got shape {tuple(scale.shape)}\"\n            )\n        if not scale.is_contiguous():\n            raise ValueError(f\"{name} must be contiguous\")\n\n    if out is None:\n        out = torch.empty(s_q, h_q, d_v, dtype=torch.bfloat16, device=device)\n    else:\n        _check_out_buffer(out, \"out\", (s_q, h_q, d_v), torch.bfloat16, device)\n","sourceCodeStart":407,"sourceCodeEnd":443,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/sparse_mla_q8kv8_prefill_sm90.py#L407-L443","documentation":"Both q_scale and kv_scale must be CUDA tensors; the kernel reads them from GPU memory during the q8kv8 attention computation. A CPU tensor would produce an invalid device pointer at kernel launch, so the wrapper rejects it in Python.","triggerScenarios":"Passing torch.tensor(1.0) created without a device argument (defaults to CPU) as q_scale or kv_scale.","commonSituations":"Creating scale tensors at config-parse time on CPU and forgetting to move them when the model weights are later placed on GPU.","solutions":["Create the scale with device=q.device","Or move existing CPU scales: q_scale = q_scale.to(q.device)"],"exampleFix":"// before\nq_scale = torch.tensor(1.0, dtype=torch.float32)  # CPU\n// after\nq_scale = torch.tensor(1.0, dtype=torch.float32, device=q.device)","handlingStrategy":"validation","validationCode":"assert q_scale.is_cuda and kv_scale.is_cuda","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use device=q.device in every torch.tensor/torch.zeros for scale buffers","Keep a setup step that moves all small metadata tensors onto the model device"],"tags":["mla","quantization","cuda-device","scale-validation"],"backgroundTag":"tensor-on-wrong-device","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}