{"record":{"id":"da43d5081ffe5a82","repo":"sgl-project/sglang","slug":"name-must-have-dtype-torch-int32","errorCode":null,"errorMessage":"{name} must have dtype torch.int32","messagePattern":"(.+?) must have dtype torch\\.int32","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/flash_attn/cute/block_sparsity.py","lineNumber":281,"sourceCode":"        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,\n    expected_shape: Tuple[int, ...],\n    context: str | None,\n    hint: str | Callable[[], str] | None,\n    device: torch.device,\n) -> torch.Tensor | None:\n    if tensor is None:\n        return None\n    if tensor.dtype != torch.int32:\n        raise ValueError(f\"{name} must have dtype torch.int32\")\n    if tensor.device != device:\n        raise ValueError(f\"{name} must be on the same device as block sparse tensors\")\n    if not tensor.is_cuda:\n        raise ValueError(f\"{name} must live on CUDA\")\n    return _expand_sparsity_tensor(tensor, expected_shape, name, context, hint)\n\n\ndef get_block_sparse_expected_shapes(\n    batch_size: int,\n    num_head: int,\n    seqlen_q: int,\n    seqlen_k: int,\n    m_block_size: int,\n    n_block_size: int,\n    q_stage: int,\n) -> Tuple[Tuple[int, int, int], Tuple[int, int, int, int]]:\n    \"\"\"Return (expected_count_shape, expected_index_shape) for block sparse normalization.\"\"\"\n    m_block_size_effective = q_stage * m_block_size","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/flash_attn/cute/block_sparsity.py#L263-L299","documentation":"Raised when an auxiliary block-sparse metadata tensor (e.g. dq_write_order) does not have dtype torch.int32. The kernels index with 32-bit ints, so any other dtype is rejected.","triggerScenarios":"Calling normalize_block_sparse_tensors with a dq_write_order tensor of dtype torch.long or torch.int16 instead of torch.int32.","commonSituations":"Creating dq_write_order via torch.arange(...) (defaults to int64) or torch.zeros without dtype=torch.int32, especially when enabling spt mode.","solutions":["Cast to int32: dq_write_order = dq_write_order.to(torch.int32)","Create with explicit dtype: torch.arange(M, dtype=torch.int32, device='cuda')"],"exampleFix":"// before\norder = torch.arange(num_m_blocks, device='cuda')  # int64\n// after\norder = torch.arange(num_m_blocks, dtype=torch.int32, device='cuda')","handlingStrategy":"type-guard","validationCode":"assert dq_write_order is None or dq_write_order.dtype == torch.int32","typeGuard":"def is_valid_meta(t): return t is None or (t.dtype == torch.int32 and t.is_cuda)","tryCatchPattern":null,"preventionTips":["Always pass dtype=torch.int32 when creating metadata tensors","Remember torch.arange defaults to int64"],"tags":["block-sparse","dtype","int32","metadata"],"backgroundTag":"tensor-dtype-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}