{"record":{"id":"4be712f1fd4b74dc","repo":"sgl-project/sglang","slug":"topk-length-must-be-a-cuda-tensor","errorCode":null,"errorMessage":"topk_length must be a CUDA tensor","messagePattern":"topk_length 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":385,"sourceCode":"        )\n\n    if indices.dtype != torch.int32:\n        raise ValueError(f\"indices must be int32, got {indices.dtype}\")\n\n    if topk == 0 or topk % 128 != 0:\n        raise ValueError(\n            \"Q8KV8 sparse-prefill topk width must be a positive multiple of 128, \"\n            f\"got {topk}\"\n        )\n\n    if topk_length is not None:\n        if topk_length.shape != (s_q,) or topk_length.dtype != torch.int32:\n            raise ValueError(\n                f\"topk_length must be int32 with shape ({s_q},), got \"\n                f\"{tuple(topk_length.shape)}/{topk_length.dtype}\"\n            )\n        if not topk_length.is_cuda:\n            raise ValueError(\"topk_length must be a CUDA tensor\")\n        if topk_length.device != device:\n            raise ValueError(\n                \"topk_length must be on q's device \"\n                f\"{device}, got {topk_length.device}\"\n            )\n        if not topk_length.is_contiguous():\n            raise ValueError(\"topk_length must be contiguous\")\n        if torch.any(topk_length < 0).item() or torch.any(topk_length > topk).item():\n            raise ValueError(\n                \"topk_length values must satisfy \" f\"0 <= topk_length <= topk ({topk})\"\n            )\n\n    if d_v != 512:\n        raise ValueError(\n            f\"sparse_mla_q8kv8_prefill_fwd only supports d_v=512, got {d_v}\"\n        )\n\n    if attn_sink is not None and topk_length is None:","sourceCodeStart":367,"sourceCodeEnd":403,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/sparse_mla_q8kv8_prefill_sm90.py#L367-L403","documentation":"topk_length is optional, but if provided it must be a CUDA tensor: the kernel reads it on-device to apply per-token variable lengths. A CPU tensor (e.g. a list converted with torch.tensor on CPU) is rejected.","triggerScenarios":"Passing torch.tensor([128]*s_q) (defaults to CPU) or lengths computed with numpy and converted without .cuda().","commonSituations":"Scheduler-computed lengths starting on CPU; debugging with CPU-constructed dummy tensors; multi-GPU runs where the default device is not set.","solutions":["Move to the right GPU: topk_length = topk_length.to(q.device)","Create it directly on device: torch.full((s_q,), k, dtype=torch.int32, device=q.device)","Set torch.cuda.set_device(local_rank) so default constructions land on the right device"],"exampleFix":"// before\ntopk_length = torch.tensor(lengths, dtype=torch.int32)\n// after\ntopk_length = torch.tensor(lengths, dtype=torch.int32, device=q.device)","handlingStrategy":"validation","validationCode":"assert topk_length.is_cuda, \"topk_length must be a CUDA tensor\"","typeGuard":"def cuda_int32_lengths(tl: torch.Tensor) -> bool:\n    return tl.is_cuda and tl.dtype == torch.int32","tryCatchPattern":null,"preventionTips":["Construct metadata tensors with device=q.device explicitly","Never build them from CPU lists at call time"],"tags":["cuda","device-mismatch","topk"],"backgroundTag":"cuda-device-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}