pytorch/pytorch · error · TypeError

split expects at least a 1-dimension tensor

Error message

split expects at least a 1-dimension tensor

What it means

On the all-Dims split path, the wrapper computes the tensor's ndim from its levels and refuses to split a 0-dimensional tensor when dim was not given as a Dim object. With no axes there is nothing to locate, and the default dim resolution (DimEntry(-ndim) i.e. -0) would be meaningless, so it raises TypeError.

Source

Thrown at functorch/dim/__init__.py:1269

            raise TypeError(
                "when dim is specified as a Dim object, split sizes must also be dimensions."
            )
        return _Tensor._torch_function_fallback(
            torch.Tensor.split,
            (type(tensor),),
            (tensor, split_size_or_sections),
            {"dim": dim},
        )

    if not all_dims:
        raise TypeError("split list must be ints or dims but got a mix")

    # All are Dim objects - handle first-class dimension split
    self_info = TensorInfo.create(tensor, ensure_batched=False, ensure_present=False)
    ndim = self_info.ndim()

    if not dim_is_object and ndim == 0:
        raise TypeError("split expects at least a 1-dimension tensor")

    # Wrap the dimension
    dim_l = _wrap_dim(dim, ndim, False) if dim is not None else DimEntry(-ndim)

    # Find the index of the dimension in levels
    idx = None
    for i, level in enumerate(self_info.levels):
        if level == dim_l:
            idx = i
            break

    if idx is None:
        if dim is None:
            dim = 0
        raise TypeError(f"tensor does not contain dimension {dim}")

    # Calculate split indices
    indices = []

View on GitHub (pinned to dcd2ecae77)

Solutions

  1. Guard for scalars before splitting: if t.ndim == 0: handle separately.
  2. Fix the producer so the tensor keeps at least one dimension (e.g. keepdim=True on reductions).
  3. Pass an explicit Dim as dim if you genuinely intend named-dim semantics — though a scalar still has no axis, so this usually indicates a logic bug upstream.

Example fix

t = loss.sum()  # 0-d
p = t.split([d1, d2])

# after
t = loss.sum(keepdim=True)  # still 1-d
p = t.split([d1, d2])
Defensive patterns

Strategy: validation

Validate before calling

if tensor.ndim == 0:
    raise ValueError('cannot split a scalar tensor')
pieces = tensor.split(sections)

Prevention

When it happens

Trigger: Calling split with Dim sizes on a scalar (0-d) tensor without passing an explicit dim, or passing dim=None: t = torch.tensor(3.0); t.split([d1, d2]).

Common situations: A reduction upstream (e.g. .sum() or indexing away all dims) leaving a scalar that then flows into a split; variable-length pipelines where a size-0 or scalar edge case was not covered.

Related errors


AI-assisted analysis of pytorch/pytorch@dcd2ecae77 (2026-08-14). Data as JSON: /api/errors/7be20e8eff98be38. Report an issue: GitHub.