{"record":{"id":"4986a033372e5dbb","repo":"xai-org/x-algorithm","slug":"tensor-name-context-clause-with-shape-tensor-s","errorCode":null,"errorMessage":"{tensor_name}{context_clause} with shape {tensor.shape} cannot be expanded to expected shape {expected_shape}.{hint_clause}","messagePattern":"(.+?)(.+?) with shape (.+?) cannot be expanded to expected shape (.+?)\\.(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phoenix/xrex/cutedsl/ranker_fa4/block_sparsity.py","lineNumber":217,"sourceCode":"    return min_block_size\n\n\ndef _expand_sparsity_tensor(\n    tensor: torch.Tensor,\n    expected_shape: Tuple[int, ...],\n    tensor_name: str,\n    context: str | None,\n    hint: str | Callable[[], str] | None,\n) -> torch.Tensor:\n    needs_expand = tensor.shape != expected_shape\n    if not needs_expand:\n        return tensor\n    can_expand = all(map(lambda cur, tgt: cur == tgt or cur == 1, tensor.shape, expected_shape))\n    if not can_expand:\n        context_clause = f\" ({context})\" if context else \"\"\n        resolved_hint = hint() if callable(hint) else hint\n        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\"","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/phoenix/xrex/cutedsl/ranker_fa4/block_sparsity.py#L199-L235","documentation":"Sparsity metadata tensors may be broadcast-expanded to the expected full shape only when each dimension either already matches or is 1. _expand_sparsity_tensor checks this and raises with the tensor name, optional context, shapes, and an optional hint when expansion is impossible (e.g. a dim of 3 where 4 is expected).","triggerScenarios":"Passing a block_sparsity cnt/idx/metadata tensor whose shape disagrees with the expected shape in any dimension that is not 1, via _check_and_expand_block or _check_and_expert_metadata_tensor into normalize_block_sparse_tensors.","commonSituations":"Changing num_experts, batch, or head counts without regenerating sparsity metadata; hand-built layout tensors with a wrong tile count; broadcasting expectations from a different model config.","solutions":["Print tensor.shape vs the expected_shape named in the message and fix the generating code so they match","Regenerate sparsity metadata with the current config (heads, experts, block counts)","Only rely on size-1 dims for broadcasting; never expect arbitrary dims to expand"],"exampleFix":"# before\ncnt = torch.ones((2, 12, 31))          # expected (2, 12, 32)\nres = _expand_sparsity_tensor(cnt, (2, 12, 32), \"k_block_cnt\", ctx, None)\n# after\ncnt = torch.ones((2, 12, 32))\nres = _expand_sparsity_tensor(cnt, (2, 12, 32), \"k_block_cnt\", ctx, None)","handlingStrategy":"validation","validationCode":"def can_expand_to(shape, expected):\n    return all(c == t or c == 1 for c, t in zip(shape, expected))\nassert can_expand_to(tuple(cnt.shape), tuple(expected_shape)), \\\n    f\"{cnt.shape} cannot expand to {expected_shape}\"","typeGuard":"def is_expandable(t: torch.Tensor, expected_shape) -> bool:\n    return all(c == t or c == 1 for c, t in zip(t.shape, expected_shape))","tryCatchPattern":null,"preventionTips":["Generate sparsity metadata programmatically from the model config rather than by hand","Log expected vs actual shapes in data prep","Add shape assertions next to metadata creation"],"tags":["pytorch","block-sparse","shape-mismatch","broadcasting"],"backgroundTag":"shape-broadcast-mismatch","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}