huggingface/transformers · error · ValueError

out_indices must not contain any duplicates, got {self._out_

Error message

out_indices must not contain any duplicates, got {self._out_indices}(equivalent to {positive_indices}))

What it means

Raised when out_indices contains duplicates after negative indices are normalized to positive ones (e.g. [0, -5] both resolve to stage 0 when there are 5 stages). Duplicated stages would produce colliding feature names and ambiguous multi-feature outputs, so the config rejects them. The message appends the normalized positive indices when they differ from the input.

Source

Thrown at src/transformers/backbone_utils.py:109

                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]:
                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

View on GitHub (pinned to a597f97485)

Solutions

  1. Remove the duplicate stage index from out_indices
  2. If you mixed negative and positive forms, rewrite all indices as positive (or all negative) to make collisions obvious
  3. Print the normalized indices: [i % len(stage_names) if i < 0 else i for i in out_indices] to spot the collision

Example fix

# before (4 stages)
config.out_indices = [0, -4]  # -4 % 4 == 0 -> duplicate of 0

# after
config.out_indices = [0, 1]
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 len(normalized) == len(set(normalized)), "duplicate stages in out_indices"

Type guard

def no_duplicate_stages(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 len(norm) == len(set(norm))

Prevention

When it happens

Trigger: out_indices like [1, 1] or [1, -4] where both normalize to the same stage (with len(stage_names)==4: 1 and -4 % 4 == 0? — concretely [0, -4] with 4 stages: -4 % 4 == 0, duplicate with 0). Fires on config verification at model init.

Common situations: Mixing positive and negative indices that point at the same stage; editing a config by appending a stage index that already exists; checkpoint configs saved before dedup validation existed.

Related errors


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