{"record":{"id":"5fb0df7e3707aa0e","repo":"huggingface/transformers","slug":"out-indices-must-be-in-the-same-order-as-stage-nam","errorCode":null,"errorMessage":"out_indices must be in the same order as stage_names, expected {sorted_negative} got {self._out_indices}","messagePattern":"out_indices must be in the same order as stage_names, expected (.+?) got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/backbone_utils.py","lineNumber":114,"sourceCode":"\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]:\n                raise ValueError(\"out_features and out_indices should correspond to the same stages if both are set\")\n\n    @property\n    def out_features(self):\n        return self._out_features\n\n    @out_features.setter\n    def out_features(self, out_features: list[str]):\n        \"\"\"\n        Set the out_features attribute. This will also update the out_indices attribute to match the new out_features.\n        \"\"\"","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/backbone_utils.py#L96-L132","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Sort out_indices ascending: config.out_indices = sorted(config.out_indices)","Keep negative indices in ascending order too ([-3, -1] is fine, [-1, -3] is not)","Generate from stage_names: [stage_names.index(f) for f in desired_out_features]"],"exampleFix":"# before\nconfig.out_indices = [3, 1]\n\n# after\nconfig.out_indices = [1, 3]","handlingStrategy":"validation","validationCode":"n = len(config.stage_names)\nnormalized = [i % n if i < 0 else i for i in out_indices]\nassert normalized == sorted(normalized), \"out_indices must be ascending after normalization\"","typeGuard":"def indices_sorted(out_indices: list[int], stage_names: list[str]) -> bool:\n    n = len(stage_names)\n    norm = [i % n if i < 0 else i for i in out_indices]\n    return norm == sorted(norm)","tryCatchPattern":null,"preventionTips":["Write out_indices in ascending order from the start","Apply sorted(out_indices) before assigning to config","Derive indices from an ordered out_features list instead of hand-writing them"],"tags":["config","backbone","validation","ordering"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}