{"record":{"id":"6076eb65e3778abd","repo":"sgl-project/sglang","slug":"all-block-sparse-tensors-must-be-on-the-same-devic","errorCode":null,"errorMessage":"All block sparse tensors must be on the same device","messagePattern":"All block sparse tensors must be on the same device","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/kernels/ops/attention/flash_attn/cute/block_sparsity.py","lineNumber":472,"sourceCode":"        context,\n        hint,\n    )\n    if mask_cnt is None or mask_idx is None:\n        raise ValueError(\n            \"mask_block_cnt and mask_block_idx must be provided for block sparsity.\"\n        )\n\n    full_cnt, full_idx = _check_and_expand_block(\n        \"full\",\n        tensors.full_block_cnt,\n        tensors.full_block_idx,\n        expected_count_shape,\n        expected_index_shape,\n        context,\n        hint,\n    )\n    if full_cnt is not None and mask_cnt.device != full_cnt.device:\n        raise ValueError(\"All block sparse tensors must be on the same device\")\n\n    dq_write_order = _check_and_expand_metadata_tensor(\n        \"dq_write_order\",\n        tensors.dq_write_order,\n        tuple(mask_idx.shape),\n        context,\n        hint,\n        mask_cnt.device,\n    )\n    dq_write_order_full = _check_and_expand_metadata_tensor(\n        \"dq_write_order_full\",\n        tensors.dq_write_order_full,\n        tuple(full_idx.shape) if full_idx is not None else expected_index_shape,\n        context,\n        hint,\n        mask_cnt.device,\n    )\n    spt = tensors.spt","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/kernels/ops/attention/flash_attn/cute/block_sparsity.py#L454-L490","documentation":"Raised when optional full_block_cnt is provided but sits on a different device than mask_block_cnt. All tensors in the block-sparse bundle must be co-located on one CUDA device.","triggerScenarios":"mask tensors on cuda:0 and full_block_cnt on cuda:1 or CPU in normalize_block_sparse_tensors.","commonSituations":"Adding full-block hints later (e.g. for a fused dense/sparse path) from a cache built on another device.","solutions":["full_cnt = full_cnt.to(mask_cnt.device)","Build all four tensors on the same device (torch.cuda.current_device()) from the start"],"exampleFix":"// before\ntensors = BlockSparseTensorsTorch(cnt, idx, full_block_cnt=full_cpu)\n// after\ntensors = BlockSparseTensorsTorch(cnt, idx, full_block_cnt=full_cpu.to(cnt.device))","handlingStrategy":"validation","validationCode":"if tensors.full_block_cnt is not None:\n    assert tensors.full_block_cnt.device == tensors.mask_block_cnt.device","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Move full_block_* to mask device when adding them","Build the whole bundle in one device-scoped function"],"tags":["block-sparse","device-mismatch"],"backgroundTag":"tensor-device-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}