{"record":{"id":"33f618ff2602b9d2","repo":"xai-org/x-algorithm","slug":"block-sparse-tensors-context-must-share-the-same","errorCode":null,"errorMessage":"Block sparse tensors{context} must share the same m-block dimension.","messagePattern":"Block sparse tensors(.+?) must share the same m-block dimension\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phoenix/xrex/cutedsl/ranker_fa4/block_sparsity.py","lineNumber":359,"sourceCode":"        raise ValueError(\"mask_block_cnt and mask_block_idx must be provided for block sparsity.\")\n    if mask_block_cnt.ndim != 3 or mask_block_idx.ndim != 4:\n        raise ValueError(\n            f\"Block sparse tensors{context} must have shapes (B, H, M) and (B, H, M, N).\"\n        )\n    for dim_name, cur, tgt in (\n        (\"batch\", mask_block_cnt.shape[0], expected_count_shape[0]),\n        (\"head\", mask_block_cnt.shape[1], expected_count_shape[1]),\n    ):\n        if cur != tgt and cur != 1:\n            raise ValueError(f\"Block sparse tensors{context} {dim_name} dim must be {tgt} or 1.\")\n    for dim_name, cur, tgt in (\n        (\"batch\", mask_block_idx.shape[0], expected_index_shape[0]),\n        (\"head\", mask_block_idx.shape[1], expected_index_shape[1]),\n    ):\n        if cur != tgt and cur != 1:\n            raise ValueError(f\"Block sparse tensors{context} {dim_name} dim must be {tgt} or 1.\")\n    if mask_block_cnt.shape[2] != mask_block_idx.shape[2]:\n        raise ValueError(f\"Block sparse tensors{context} must share the same m-block dimension.\")\n    if mask_block_idx.shape[3] > expected_n_blocks:\n        raise ValueError(\n            f\"Block sparse tensors{context} n-block dimension must be <= {expected_n_blocks}.\"\n        )\n    if expected_m_blocks != num_m_blocks:\n        raise ValueError(\n            f\"Block sparse tensors{context} m-block dimension {num_m_blocks} does not match \"\n            f\"sparse_block_size_q={sparse_block_size_q}. \"\n            f\"Set BlockSparseTensorsTorch.block_size to match the BlockMask BLOCK_SIZE.\"\n        )\n    return expected_count_shape, expected_index_shape, q_subtile_factor\n\n\ndef get_block_sparse_expected_shapes_bwd(\n    batch_size: int,\n    num_head: int,\n    seqlen_q: int,\n    seqlen_k: int,","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/phoenix/xrex/cutedsl/ranker_fa4/block_sparsity.py#L341-L377","documentation":"mask_block_cnt (B, H, M) and mask_block_idx (B, H, M, N) must agree on their third dimension — the number of M (query) blocks per (batch, head). A mismatch means the per-row block counts don't line up with the index rows.","triggerScenarios":"Building cnt and idx from different sources or after independently reshaping/slicing them, so cnt.shape[2] != idx.shape[2]; e.g. truncating the M dimension of one tensor but not the other.","commonSituations":"Padding or cropping sequence blocks for varlen batches applied to only one of the two tensors; regenerating one tensor after a seqlen change while caching the other.","solutions":["Rebuild cnt and idx together from the same BlockMask/sparsity pattern so M always matches","If cropping/padding M blocks, apply the same operation to both tensors","Assert cnt.shape[2] == idx.shape[2] in your mask-construction code"],"exampleFix":"# before\ncnt = cnt[:, :, :new_m]              # cropped\nidx = idx                             # not cropped -> mismatch\n\n# after\ncnt = cnt[:, :, :new_m]\nidx = idx[:, :, :new_m, :]            # keep M dims in sync\nassert cnt.shape[2] == idx.shape[2]","handlingStrategy":"validation","validationCode":"assert tensors.mask_block_cnt.shape[2] == tensors.mask_block_idx.shape[2], \\\n    (tensors.mask_block_cnt.shape, tensors.mask_block_idx.shape)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always build cnt and idx in the same function","Apply M-dim crops/pads to both tensors"],"tags":["block-sparsity","shape-validation","consistency"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}