pytorch/pytorch · error · ValueError

at least {dims_indexed} indices were supplied but the tensor

Error message

at least {dims_indexed} indices were supplied but the tensor only has {total_dims} dimensions

What it means

The indexing pre-pass counts how many axes the index list consumes (each int, slice, Dim, pack, or ... counts at least one). If that minimum count exceeds the tensor's total levels (positional + named), indexing cannot proceed and ValueError reports both numbers. It fires only when the original torch getitem cannot be used, i.e. named dims are involved.

Source

Thrown at functorch/dim/_getsetitem.py:321

                dims_indexed += len(s._dims)
            dimlists.append(i)
        elif s is None:
            has_dimpacks_or_none = True
        elif is_dimpack(s):
            can_call_original_getitem = False
            has_dimpacks_or_none = True
            dims_indexed += 1
        else:
            dims_indexed += 1

    # Early return if we can use original getitem
    if can_call_original_getitem:
        return IndexingInfo(can_call_original=True)

    self_info = TensorInfo.create(self, False, True)
    total_dims = len(self_info.levels)  # Total dimensions (positional + named)
    if dims_indexed > total_dims:
        raise ValueError(
            f"at least {dims_indexed} indices were supplied but the tensor only has {total_dims} dimensions"
        )

    # Expand any unbound dimension list, or expand ... into individual : slices.
    expanding_dims = total_dims - dims_indexed
    if expanding_object != -1:
        if unbound_dim_list is not None:
            # Bind unbound dimension list to the expanding dimensions
            unbound_dim_list.bind_len(expanding_dims)
        else:
            # Expand ... into slice(None) objects
            no_slices = [slice(None)] * expanding_dims
            input_list = (
                input_list[:expanding_object]
                + no_slices
                + input_list[expanding_object + 1 :]
            )

View on GitHub (pinned to dcd2ecae77)

Solutions

  1. Match the number of indices to the tensor's levels (check len(t.order()) or t.ndim) and drop surplus indices.
  2. Fix upstream so rank is preserved (keepdim=True, no unconditional squeeze).
  3. Build indices from the tensor's actual dims rather than a fixed-length literal.

Example fix

out = x[a_d, b_d, c_d]  # x has only 2 levels after a squeeze

# after
x = raw.sum(-1, keepdim=True)  # or remove the squeeze
out = x[a_d, b_d, c_d]
Defensive patterns

Strategy: validation

Validate before calling

total = len(t._levels)
n_idx = sum(1 for x in index if x is not Ellipsis) + (1 if any(x is Ellipsis for x in index) else 0)
if n_idx > total:
    raise ValueError(f'{n_idx} indices for {total}-level tensor')

Prevention

When it happens

Trigger: t[d1, d2, d3] on a 2-d tensor; supplying an index list longer than the rank, e.g. t[:, :, :, d] on a 3-level tensor with named dims present (which disables the fast path).

Common situations: Code written for a higher-rank input reused on a squeezed/reduced tensor; hardcoded index arity after a preprocessing step dropped a channel dim.

Related errors


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