{"record":{"id":"49c78503025de750","repo":"huggingface/transformers","slug":"out-features-and-out-indices-should-have-the-same","errorCode":null,"errorMessage":"out_features and out_indices should have the same length if both are set","messagePattern":"out_features and out_indices should have the same length if both are set","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/backbone_utils.py","lineNumber":120,"sourceCode":"            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        \"\"\"\n        self.set_output_features_output_indices(out_features=out_features, out_indices=None)\n\n    @property\n    def out_indices(self):\n        return self._out_indices\n","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/backbone_utils.py#L102-L138","documentation":"Raised when both out_features and out_indices are set on a backbone config but have different lengths. The two attributes are two views of the same selection (feature names vs positional indices of stage_names); a length mismatch means they cannot possibly describe the same stages, so verification fails fast before any model code runs.","triggerScenarios":"Setting config.out_features = ['stage1', 'stage2'] together with config.out_indices = [1] (or vice versa), then building the model or calling verify_out_features_out_indices(). Also fired by set_output_features_output_indices when callers pass mismatched pairs.","commonSituations":"Partially updating a fine-tuned config: user changes out_features but leaves stale out_indices from the base checkpoint; serialization/deserialization that drops one element; programmatic config surgery.","solutions":["Set only one of the two (out_features recommended); the setter keeps the other in sync automatically","If setting both, derive one from the other: out_indices = [stage_names.index(f) for f in out_features]","Set them together via config.set_output_features_output_indices(out_features, out_indices)"],"exampleFix":"# before\nconfig.out_features = ['stage1', 'stage2', 'stage3']\nconfig.out_indices = [1, 2]\n\n# after\nconfig.out_features = ['stage1', 'stage2', 'stage3']\nconfig.out_indices = [1, 2, 3]  # or drop this line; the setter syncs it","handlingStrategy":"validation","validationCode":"if config.out_features is not None and config.out_indices is not None:\n    assert len(config.out_features) == len(config.out_indices), \"length mismatch\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set only out_features; the config setter syncs out_indices automatically","When updating one attribute, update (or None out) the other in the same change","Use config.set_output_features_output_indices(f, i) to set both atomically"],"tags":["config","backbone","validation","consistency"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}