xai-org/x-algorithm · error · ValueError
mask_block_cnt and mask_block_idx must be provided for block
Error message
mask_block_cnt and mask_block_idx must be provided for block sparsity.
What it means
infer_block_sparse_expected_shapes requires the mask_block_idx tensor to be present when block sparsity is enabled; None means the sparsity pattern itself is missing, so expected shapes cannot be inferred. This instance fires early, before num_m_blocks is read from mask_block_idx.shape[2].
Source
Thrown at phoenix/xrex/cutedsl/ranker_fa4/block_sparsity.py:313
batch_size: int,
num_head: int,
seqlen_q: int,
seqlen_k: int,
m_block_size: int,
n_block_size: int,
q_stage: int,
context: str,
sparse_block_size_q: int | None = None,
sparse_block_size_kv: int | None = None,
) -> Tuple[Tuple[int, int, int], Tuple[int, int, int, int], int]:
base_m_block = q_stage * m_block_size
base_n_block = n_block_size
if sparse_block_size_kv is None:
sparse_block_size_kv = base_n_block
if sparse_block_size_kv != base_n_block:
raise ValueError(f"Block sparse tensors{context} require BLOCK_SIZE_KV={base_n_block}.")
if tensors.mask_block_idx is None:
raise ValueError("mask_block_cnt and mask_block_idx must be provided for block sparsity.")
num_m_blocks = tensors.mask_block_idx.shape[2]
if sparse_block_size_q is None:
sparse_block_size_q = get_sparse_q_block_size(tensors, seqlen_q)
if sparse_block_size_q is None and base_m_block != 1:
raise ValueError(
f"Block sparse tensors{context} require explicit sparse_block_size[0] "
f"to disambiguate block size for seqlen_q={seqlen_q} and num_m_blocks={num_m_blocks}."
)
if sparse_block_size_q is None:
sparse_block_size_q = ceildiv(seqlen_q, num_m_blocks)
if sparse_block_size_q % base_m_block != 0:
raise ValueError(
f"Block sparse tensors{context} have block size {sparse_block_size_q}, "
f"which must be a multiple of {base_m_block}."
)
View on GitHub (pinned to 24c60942c5)
Solutions
- Provide both mask_block_cnt and mask_block_idx tensors
- Check for None fields on the container before calling the API
- Disable block sparsity entirely if it was enabled by mistake
Example fix
# before tensors = BlockSparseTensors(mask_block_cnt=cnt, mask_block_idx=None) # after tensors = BlockSparseTensors(mask_block_cnt=cnt, mask_block_idx=idx)
Defensive patterns
Strategy: type-guard
Validate before calling
assert tensors.mask_block_cnt is not None and tensors.mask_block_idx is not None
Type guard
def has_block_sparsity_pair(t) -> bool:
return t.mask_block_cnt is not None and t.mask_block_idx is not None Prevention
- Make both mask fields required (no defaults) in your wrapper dataclass
- Log when sparsity is enabled with incomplete metadata
When it happens
Trigger: Enabling block sparsity (passing mask_block_cnt or a sparse config) but leaving tensors.mask_block_idx as None; constructing the sparse-tensor container with only one of the two mask tensors.
Common situations: Partially initialized dataclasses/containers where the idx field was never populated; conditionally loading only counts; forgetting to attach the precomputed index tensor when wiring up the ranker inputs.
Related errors
- {name}_block_cnt and {name}_block_idx must be on the same de
- {name} must have dtype torch.int32
- valid_block_upper and valid_block_lower must both be provide
- type checking expression %s failed: invalid argument type: %
- Non-optional parameter %s must be declared before optional p
AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28).
Data as JSON: /api/errors/5c2beadccc996f35.
Report an issue: GitHub.