{"record":{"id":"6c201ee79ac03ce4","repo":"xai-org/x-algorithm","slug":"name-block-cnt-and-name-block-idx-must-both-be","errorCode":null,"errorMessage":"{name}_block_cnt and {name}_block_idx must both be provided or both be None","messagePattern":"(.+?)_block_cnt and (.+?)_block_idx must both be provided or both be None","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phoenix/xrex/cutedsl/ranker_fa4/block_sparsity.py","lineNumber":234,"sourceCode":"        hint_clause = f\" Hint: {resolved_hint}\" if resolved_hint else \"\"\n        raise ValueError(\n            f\"{tensor_name}{context_clause} with shape {tensor.shape} cannot be expanded to expected shape {expected_shape}.\"\n            f\"{hint_clause}\"\n        )\n    return tensor.expand(*expected_shape)\n\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    )","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/phoenix/xrex/cutedsl/ranker_fa4/block_sparsity.py#L216-L252","documentation":"Block-sparsity metadata comes in pairs: {name}_block_cnt (counts) and {name}_block_idx (indices). _check_and_expand_block enforces that they are both provided or both None; passing exactly one is treated as an incomplete configuration and raises immediately.","triggerScenarios":"Calling normalize_block_sparse_tensors (or building its inputs) with, e.g., k_block_cnt set but k_block_idx None, or the same for q/v metadata.","commonSituations":"Optional sparsity partially wired through a config object where one field defaults to None; refactoring that renames one of the pair; conditionally loading only idx from a checkpoint.","solutions":["Pass both {name}_block_cnt and {name}_block_idx, or omit both","Check config plumbing: any code path that sets one must set the other","When loading from checkpoints, assert the pair is present together before calling"],"exampleFix":"# before\nres = normalize_block_sparse_tensors(..., k_block_cnt=cnt)  # idx missing\n# after\nres = normalize_block_sparse_tensors(..., k_block_cnt=cnt, k_block_idx=idx)","handlingStrategy":"validation","validationCode":"assert (k_block_cnt is None) == (k_block_idx is None), \\\n    \"cnt/idx must be provided together\"","typeGuard":"def valid_block_pair(cnt, idx):\n    return (cnt is None) == (idx is None)","tryCatchPattern":null,"preventionTips":["Store cnt/idx as a tuple or NamedTuple in configs","Write a loader that refuses checkpoints containing only one of the pair"],"tags":["pytorch","block-sparse","argument-validation"],"backgroundTag":"paired-argument-missing","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}