huggingface/transformers · error · ValueError

out_indices must be valid indices for stage_names {self.stag

Error message

out_indices must be valid indices for stage_names {self.stage_names}, got {self._out_indices}

What it means

Raised during out_indices validation when at least one index (after mapping negative indices via idx % len(stage_names)) is outside range(len(stage_names)). The library validates each index against the number of stages in stage_names before it is ever used to index stage lists, which would otherwise IndexError downstream.

Source

Thrown at src/transformers/backbone_utils.py:103

                raise ValueError(
                    f"out_features must be a subset of stage_names: {self.stage_names} got {self._out_features}"
                )
            if len(self._out_features) != len(set(self._out_features)):
                raise ValueError(f"out_features must not contain any duplicates, got {self._out_features}")
            if self._out_features != (
                sorted_feats := [feat for feat in self.stage_names if feat in self._out_features]
            ):
                raise ValueError(
                    f"out_features must be in the same order as stage_names, expected {sorted_feats} got {self._out_features}"
                )

        if self._out_indices is not None:
            if not isinstance(self._out_indices, list):
                raise ValueError(f"out_indices must be a list, got {type(self._out_indices)}")
            # Convert negative indices to their positive equivalent: [-1,] -> [len(stage_names) - 1,]
            positive_indices = tuple(idx % len(self.stage_names) if idx < 0 else idx for idx in self._out_indices)
            if any(idx for idx in positive_indices if idx not in range(len(self.stage_names))):
                raise ValueError(
                    f"out_indices must be valid indices for stage_names {self.stage_names}, got {self._out_indices}"
                )
            if len(positive_indices) != len(set(positive_indices)):
                msg = f"out_indices must not contain any duplicates, got {self._out_indices}"
                msg += f"(equivalent to {positive_indices}))" if positive_indices != self._out_indices else ""
                raise ValueError(msg)
            if positive_indices != tuple(sorted(positive_indices)):
                sorted_negative = [
                    idx for _, idx in sorted(zip(positive_indices, self._out_indices), key=lambda x: x[0])
                ]
                raise ValueError(
                    f"out_indices must be in the same order as stage_names, expected {sorted_negative} got {self._out_indices}"
                )

        if self._out_features is not None and self._out_indices is not None:
            if len(self._out_features) != len(self._out_indices):
                raise ValueError("out_features and out_indices should have the same length if both are set")
            if self._out_features != [self.stage_names[idx] for idx in self._out_indices]:

View on GitHub (pinned to a597f97485)

Solutions

  1. Check len(config.stage_names) and keep every index in 0..len-1
  2. Use negative indexing from the end: [-1] maps to the last stage
  3. If you targeted a deeper model, load that model's config instead

Example fix

# before (stage_names has 5 entries)
config.out_indices = [0, 1, 2, 5]  # 5 is out of range

# after
config.out_indices = [0, 1, 2, 4]  # max valid index is len(stage_names)-1
Defensive patterns

Strategy: validation

Validate before calling

n = len(config.stage_names)
assert all(0 <= (i % n if i < 0 else i) < n for i in out_indices), f"indices out of range for {n} stages"

Type guard

def indices_in_range(out_indices: list[int], stage_names: list[str]) -> bool:
    n = len(stage_names)
    return all((i % n if i < 0 else i) in range(n) for i in out_indices)

Prevention

When it happens

Trigger: out_indices contains an index >= number of stages, e.g. [0, 1, 5] for a 4-stage backbone (len(stage_names)==4); or a negative value whose modulo wraps to nothing meaningful (any negative int is folded by modulo, so this fires mainly for too-large positives). Triggered on model instantiation or config.verify_out_features_out_indices().

Common situations: Copying out_indices tuned for a deeper variant of a model family (e.g. resnet101 indices on resnet18); off-by-one mistakes counting stages; forgetting the stem counts as a stage in some configs.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/90b860b30480faf2. Report an issue: GitHub.