{"record":{"id":"e32d68dde2cfcb3a","repo":"sgl-project/sglang","slug":"name-block-tensors-must-live-on-cuda","errorCode":null,"errorMessage":"{name}_block tensors must live on CUDA","messagePattern":"(.+?)_block tensors must live on CUDA","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/flash_attn/cute/block_sparsity.py","lineNumber":254,"sourceCode":"    expected_count_shape: Tuple[int, ...],\n    expected_index_shape: Tuple[int, ...],\n    context: str | None,\n    hint: str | Callable[[], str] | None,\n) -> Tuple[torch.Tensor | None, torch.Tensor | None]:\n    if (cnt is None) != (idx is None):\n        raise ValueError(\n            f\"{name}_block_cnt and {name}_block_idx must both be provided or both be None\"\n        )\n    if cnt is None or idx is None:\n        return None, None\n    if cnt.dtype != torch.int32 or idx.dtype != torch.int32:\n        raise ValueError(f\"{name}_block tensors must have dtype torch.int32\")\n    if cnt.device != idx.device:\n        raise ValueError(\n            f\"{name}_block_cnt and {name}_block_idx must be on the same device\"\n        )\n    if not cnt.is_cuda or not idx.is_cuda:\n        raise ValueError(f\"{name}_block tensors must live on CUDA\")\n    expanded_cnt = _expand_sparsity_tensor(\n        cnt, expected_count_shape, f\"{name}_block_cnt\", context, hint\n    )\n    # [Note] Allow Compact block sparse indices\n    # Allow the last dimension (n_blocks) of idx to be <= expected, since\n    # FA4 only accesses indices 0..cnt-1 per query tile. This enables compact\n    # index tensors that avoid O(N^2) memory at long sequence lengths.\n    if idx.ndim == 4 and idx.shape[3] <= expected_index_shape[3]:\n        expected_index_shape = (*expected_index_shape[:3], idx.shape[3])\n    expanded_idx = _expand_sparsity_tensor(\n        idx, expected_index_shape, f\"{name}_block_idx\", context, hint\n    )\n    return expanded_cnt, expanded_idx\n\n\ndef _check_and_expand_metadata_tensor(\n    name: str,\n    tensor: torch.Tensor | None,","sourceCodeStart":236,"sourceCodeEnd":272,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/flash_attn/cute/block_sparsity.py#L236-L272","documentation":"Raised when mask/full block count or index tensors are not on CUDA. The FA4 cute block-sparse kernels read these tensors directly from GPU memory, so CPU-resident tensors are rejected during normalization.","triggerScenarios":"Passing mask_block_cnt or mask_block_idx (or full_block_* / dq_write_order) that was created with torch.randint(..., device='cpu') or from a numpy conversion, without calling .cuda()/.to('cuda').","commonSituations":"Prototyping a BlockMask on CPU, loading sparse metadata from disk/numpy, or forgetting the device= argument when generating test tensors.","solutions":["Move all block sparse tensors to CUDA: cnt = cnt.to('cuda')","Create them directly on GPU: torch.zeros(shape, dtype=torch.int32, device='cuda')","Check tensor.is_cuda in a helper before calling the attention op"],"exampleFix":"// before\ncnt = torch.zeros((B,H,M), dtype=torch.int32)  # CPU\n// after\ncnt = torch.zeros((B,H,M), dtype=torch.int32, device='cuda')","handlingStrategy":"validation","validationCode":"assert mask_block_cnt.is_cuda and mask_block_idx.is_cuda, 'block sparse tensors must be CUDA'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Create int32 tensors directly with device='cuda'","Keep a helper that .cuda()s every tensor in the bundle before the call"],"tags":["block-sparse","cuda","cpu-tensor","attention"],"backgroundTag":"tensor-not-on-gpu","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}