{"record":{"id":"90b860b30480faf2","repo":"huggingface/transformers","slug":"out-indices-must-be-valid-indices-for-stage-names","errorCode":null,"errorMessage":"out_indices must be valid indices for stage_names {self.stage_names}, got {self._out_indices}","messagePattern":"out_indices must be valid indices for stage_names (.+?), got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/backbone_utils.py","lineNumber":103,"sourceCode":"                raise ValueError(\n                    f\"out_features must be a subset of stage_names: {self.stage_names} got {self._out_features}\"\n                )\n            if len(self._out_features) != len(set(self._out_features)):\n                raise ValueError(f\"out_features must not contain any duplicates, got {self._out_features}\")\n            if self._out_features != (\n                sorted_feats := [feat for feat in self.stage_names if feat in self._out_features]\n            ):\n                raise ValueError(\n                    f\"out_features must be in the same order as stage_names, expected {sorted_feats} got {self._out_features}\"\n                )\n\n        if self._out_indices is not None:\n            if not isinstance(self._out_indices, list):\n                raise ValueError(f\"out_indices must be a list, got {type(self._out_indices)}\")\n            # Convert negative indices to their positive equivalent: [-1,] -> [len(stage_names) - 1,]\n            positive_indices = tuple(idx % len(self.stage_names) if idx < 0 else idx for idx in self._out_indices)\n            if any(idx for idx in positive_indices if idx not in range(len(self.stage_names))):\n                raise ValueError(\n                    f\"out_indices must be valid indices for stage_names {self.stage_names}, got {self._out_indices}\"\n                )\n            if len(positive_indices) != len(set(positive_indices)):\n                msg = f\"out_indices must not contain any duplicates, got {self._out_indices}\"\n                msg += f\"(equivalent to {positive_indices}))\" if positive_indices != self._out_indices else \"\"\n                raise ValueError(msg)\n            if positive_indices != tuple(sorted(positive_indices)):\n                sorted_negative = [\n                    idx for _, idx in sorted(zip(positive_indices, self._out_indices), key=lambda x: x[0])\n                ]\n                raise ValueError(\n                    f\"out_indices must be in the same order as stage_names, expected {sorted_negative} got {self._out_indices}\"\n                )\n\n        if self._out_features is not None and self._out_indices is not None:\n            if len(self._out_features) != len(self._out_indices):\n                raise ValueError(\"out_features and out_indices should have the same length if both are set\")\n            if self._out_features != [self.stage_names[idx] for idx in self._out_indices]:","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/backbone_utils.py#L85-L121","documentation":"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.","triggerScenarios":"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().","commonSituations":"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.","solutions":["Check len(config.stage_names) and keep every index in 0..len-1","Use negative indexing from the end: [-1] maps to the last stage","If you targeted a deeper model, load that model's config instead"],"exampleFix":"# before (stage_names has 5 entries)\nconfig.out_indices = [0, 1, 2, 5]  # 5 is out of range\n\n# after\nconfig.out_indices = [0, 1, 2, 4]  # max valid index is len(stage_names)-1","handlingStrategy":"validation","validationCode":"n = len(config.stage_names)\nassert all(0 <= (i % n if i < 0 else i) < n for i in out_indices), f\"indices out of range for {n} stages\"","typeGuard":"def indices_in_range(out_indices: list[int], stage_names: list[str]) -> bool:\n    n = len(stage_names)\n    return all((i % n if i < 0 else i) in range(n) for i in out_indices)","tryCatchPattern":null,"preventionTips":["Print len(config.stage_names) before choosing indices","Prefer negative indices (-1 = last stage) over computing absolute positions","Re-validate out_indices whenever you swap in a config from a different model depth"],"tags":["config","backbone","validation","index-error"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}