huggingface/transformers · error · ValueError

out_indices must be in the same order as stage_names, expect

Error message

out_indices must be in the same order as stage_names, expected {sorted_negative} got {self._out_indices}

What it means

Raised when out_indices, after negative-to-positive normalization, is not sorted ascending. Feature extraction in backbone mixins assumes stages are emitted in network order; an unsorted selection would misalign feature maps with feature names. The message shows the expected sorted order (sorted_negative, which sorts pairs of (positive, original) so the suggested indices preserve your negative-style notation).

Source

Thrown at src/transformers/backbone_utils.py:114

        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]:
                raise ValueError("out_features and out_indices should correspond to the same stages if both are set")

    @property
    def out_features(self):
        return self._out_features

    @out_features.setter
    def out_features(self, out_features: list[str]):
        """
        Set the out_features attribute. This will also update the out_indices attribute to match the new out_features.
        """

View on GitHub (pinned to a597f97485)

Solutions

  1. Sort out_indices ascending: config.out_indices = sorted(config.out_indices)
  2. Keep negative indices in ascending order too ([-3, -1] is fine, [-1, -3] is not)
  3. Generate from stage_names: [stage_names.index(f) for f in desired_out_features]

Example fix

# before
config.out_indices = [3, 1]

# after
config.out_indices = [1, 3]
Defensive patterns

Strategy: validation

Validate before calling

n = len(config.stage_names)
normalized = [i % n if i < 0 else i for i in out_indices]
assert normalized == sorted(normalized), "out_indices must be ascending after normalization"

Type guard

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

Prevention

When it happens

Trigger: out_indices such as [2, 1], [3, 0, 1], or [-1, 0] (normalizes to [3, 0]) on any backbone config (e.g. BitBackboneConfig, TimmBackboneConfig) at init or verify time.

Common situations: Users listing stages by perceived importance rather than network order; merging config fragments that append earlier stages at the end; hand-editing configs from tutorials.

Related errors


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