{"record":{"id":"f5f033cc6d5b4488","repo":"xai-org/x-algorithm","slug":"name-block-tensors-must-have-dtype-torch-int32","errorCode":null,"errorMessage":"{name}_block tensors must have dtype torch.int32","messagePattern":"(.+?)_block tensors must have dtype torch\\.int32","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phoenix/xrex/cutedsl/ranker_fa4/block_sparsity.py","lineNumber":240,"sourceCode":"\n\ndef _check_and_expand_block(\n    name: str,\n    cnt: torch.Tensor | None,\n    idx: torch.Tensor | None,\n    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(f\"{name}_block_cnt and {name}_block_idx must be on the same device\")\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    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":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/phoenix/xrex/cutedsl/ranker_fa4/block_sparsity.py#L222-L258","documentation":"Block-sparsity cnt/idx tensors must be torch.int32 because the downstream CUDA kernels consume 32-bit indices. _check_and_expand_block rejects any other dtype before expansion/validation, since int64 metadata would be reinterpreted incorrectly or silently truncated.","triggerScenarios":"Passing {name}_block_cnt or {name}_block_idx created with torch.zeros/ones defaults (int64), or loaded from a numpy array (default int64 on Linux), into normalize_block_sparse_tensors.","commonSituations":"torch tensor factory defaults producing int64; saving/loading metadata through numpy which upgrades to int64; exporting from a different framework without an explicit dtype.","solutions":["Create/convert with dtype=torch.int32: torch.tensor(..., dtype=torch.int32) or t.int()","When saving checkpoints, keep the dtype or cast on load: cnt.load(...).to(torch.int32)","Add a unit test asserting metadata dtypes before the kernel call"],"exampleFix":"# before\ncnt = torch.zeros((B, H, M), device='cuda')  # int64 by default\n# after\ncnt = torch.zeros((B, H, M), dtype=torch.int32, device='cuda')","handlingStrategy":"type-guard","validationCode":"cnt = None if cnt is None else cnt.to(torch.int32)\nidx = None if idx is None else idx.to(torch.int32)","typeGuard":"def is_int32_cuda_pair(cnt, idx) -> bool:\n    return (\n        (cnt is None) == (idx is None)\n        and (cnt is None or (cnt.dtype == torch.int32 and cnt.is_cuda))\n        and (idx is None or (idx.dtype == torch.int32 and idx.is_cuda))\n    )","tryCatchPattern":null,"preventionTips":["Always pass dtype=torch.int32 when creating block metadata","Cast to int32 on checkpoint load","Assert dtypes in a small pre-flight check before normalize_block_sparse_tensors"],"tags":["pytorch","block-sparse","dtype-validation"],"backgroundTag":"unsupported-dtype","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}