xai-org/x-algorithm · error · ValueError

{name}_block_cnt and {name}_block_idx must both be provided

Error message

{name}_block_cnt and {name}_block_idx must both be provided or both be None

What it means

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.

Source

Thrown at phoenix/xrex/cutedsl/ranker_fa4/block_sparsity.py:234

        hint_clause = f" Hint: {resolved_hint}" if resolved_hint else ""
        raise ValueError(
            f"{tensor_name}{context_clause} with shape {tensor.shape} cannot be expanded to expected shape {expected_shape}."
            f"{hint_clause}"
        )
    return tensor.expand(*expected_shape)


def _check_and_expand_block(
    name: str,
    cnt: torch.Tensor | None,
    idx: torch.Tensor | None,
    expected_count_shape: Tuple[int, ...],
    expected_index_shape: Tuple[int, ...],
    context: str | None,
    hint: str | Callable[[], str] | None,
) -> Tuple[torch.Tensor | None, torch.Tensor | None]:
    if (cnt is None) != (idx is None):
        raise ValueError(
            f"{name}_block_cnt and {name}_block_idx must both be provided or both be None"
        )
    if cnt is None or idx is None:
        return None, None
    if cnt.dtype != torch.int32 or idx.dtype != torch.int32:
        raise ValueError(f"{name}_block tensors must have dtype torch.int32")
    if cnt.device != idx.device:
        raise ValueError(f"{name}_block_cnt and {name}_block_idx must be on the same device")
    if not cnt.is_cuda or not idx.is_cuda:
        raise ValueError(f"{name}_block tensors must live on CUDA")
    expanded_cnt = _expand_sparsity_tensor(
        cnt, expected_count_shape, f"{name}_block_cnt", context, hint
    )
    if idx.ndim == 4 and idx.shape[3] <= expected_index_shape[3]:
        expected_index_shape = (*expected_index_shape[:3], idx.shape[3])
    expanded_idx = _expand_sparsity_tensor(
        idx, expected_index_shape, f"{name}_block_idx", context, hint
    )

View on GitHub (pinned to 24c60942c5)

Solutions

  1. Pass both {name}_block_cnt and {name}_block_idx, or omit both
  2. Check config plumbing: any code path that sets one must set the other
  3. When loading from checkpoints, assert the pair is present together before calling

Example fix

# before
res = normalize_block_sparse_tensors(..., k_block_cnt=cnt)  # idx missing
# after
res = normalize_block_sparse_tensors(..., k_block_cnt=cnt, k_block_idx=idx)
Defensive patterns

Strategy: validation

Validate before calling

assert (k_block_cnt is None) == (k_block_idx is None), \
    "cnt/idx must be provided together"

Type guard

def valid_block_pair(cnt, idx):
    return (cnt is None) == (idx is None)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28). Data as JSON: /api/errors/6c201ee79ac03ce4. Report an issue: GitHub.