xai-org/x-algorithm · error · ValueError

Number of names must match number of dimensions (shape: {sha

Error message

Number of names must match number of dimensions (shape: {shape}, names: {names})

What it means

NamedShape validates that len(names) == len(shape). If you supply names for a different number of dimensions than the shape has, the constructor raises this ValueError, preventing later silent mis-assignment of dimension names to mesh axes.

Source

Thrown at phoenix/xrex/models/sharding_context.py:37


def axis_group_size(axis_names: PyTree, mesh: Mesh) -> int:
    flattened_axis_names, _ = jax.tree.flatten(axis_names)
    if not flattened_axis_names:
        return 0
    return math.prod([mesh.shape[n] for n in flattened_axis_names])


class NamedShape:
    names: tuple[str, ...] | None

    def __init__(self, shape: Shape | None = None, names: tuple[str, ...] | None = None):
        if names is None and len(shape) > 0:
            raise ValueError(
                f"Unable to create named shape with unnamed dimensions (shape: {shape})"
            )
        if names is not None and len(names) != len(shape):
            raise ValueError(
                f"Number of names must match number of dimensions (shape: {shape}, names: {names})"
            )
        self.shape = shape
        self.names = names

    def __repr__(self) -> str:
        shape_str = ", ".join(
            f"{name or 'unnamed'}={size}" for name, size in zip(self.names, self.shape)
        )
        return f"NamedShape({shape_str})"


class ShardingContext:
    def __init__(
        self,
        name: str,
        mesh: jax.sharding.Mesh,
        sharding_rules: dict[str, ShardingRule] | None = None,

View on GitHub (pinned to 24c60942c5)

Solutions

  1. Count the dimensions of the shape and make names exactly that length
  2. Derive names programmatically where possible so they cannot drift from the shape

Example fix

# before
NamedShape((8, 16, 1024), ("batch", "features"))
# after
NamedShape((8, 16, 1024), ("batch", "model", "features"))
Defensive patterns

Strategy: validation

Validate before calling

assert len(names) == len(shape), f"{len(names)} names for {len(shape)} dims"

Type guard

def names_match_shape(shape, names) -> bool:
    return names is not None and len(names) == len(shape)

Prevention

When it happens

Trigger: Calling NamedShape((8, 16, 1024), ("batch", "features")) — 3 dims, 2 names; or adding/removing a dimension in the shape while keeping a stale names tuple.

Common situations: Shapes changed by a refactor (e.g. adding head dimension) while the names list was not updated; hardcoded names copied from a different layer.

Related errors


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