{"record":{"id":"4e9341d72b2ba020","repo":"huggingface/transformers","slug":"num-hidden-layers-self-num-hidden-layers-mus","errorCode":null,"errorMessage":"`num_hidden_layers` ({self.num_hidden_layers}) must be equal to the number of `{layer_types}` ({len(layers)})","messagePattern":"`num_hidden_layers` \\((.+?)\\) must be equal to the number of `(.+?)` \\((.+?)\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/configuration_utils.py","lineNumber":541,"sourceCode":"\n    def validate_layer_type(self):\n        \"\"\"Check that `mlp_layer_types` and `layer_types` is correctly defined.\"\"\"\n        for allowed_types, layer_types in zip(\n            [ALLOWED_ATTN_LAYER_TYPES, ALLOWED_MLP_LAYER_TYPES], [\"layer_types\", \"mlp_layer_types\"]\n        ):\n            layers = getattr(self, layer_types, None)\n            if not (layers is not None and hasattr(self, \"num_hidden_layers\")):\n                return\n            if self.is_custom_code():\n                # Custom code may have legacy layer types that need to be remapped\n                if (remapped := remap_legacy_layer_types(layers)) != layers:\n                    # Only try setattr if layers changed in case layer_types is a read-only property\n                    setattr(self, layer_types, remapped)\n                layers = remapped\n            if not all(layer_type in allowed_types for layer_type in layers):\n                raise ValueError(f\"The `{layer_types}` entries must be in {allowed_types} but got {layers}\")\n            elif self.num_hidden_layers is not None and self.num_hidden_layers != len(layers):\n                raise ValueError(\n                    f\"`num_hidden_layers` ({self.num_hidden_layers}) must be equal to the number of `{layer_types}` \"\n                    f\"({len(layers)})\"\n                )\n\n    @property\n    def rope_scaling(self):\n        return self.rope_parameters\n\n    @rope_scaling.setter\n    def rope_scaling(self, value):\n        self.rope_parameters = value\n\n    def save_pretrained(self, save_directory: str | os.PathLike, push_to_hub: bool = False, **kwargs):\n        \"\"\"\n        Save a configuration object to the directory `save_directory`, so that it can be re-loaded using the\n        [`~PreTrainedConfig.from_pretrained`] class method.\n\n        Args:","sourceCodeStart":523,"sourceCodeEnd":559,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/configuration_utils.py#L523-L559","documentation":"ValueError from the same layer-type validator, raised when every entry is valid but len(layer_types) (or len(mlp_layer_types)) does not equal num_hidden_layers. Each decoder layer needs exactly one entry describing its type, so a mismatch means the per-layer spec cannot be mapped onto the stack.","triggerScenarios":"num_hidden_layers=32 with layer_types of length 12 (e.g. only the repeating block specified); changing num_hidden_layers for a depth variant without regenerating layer_types; configs where mlp_layer_types was forgotten entirely for some layers.","commonSituations":"Depth-pruning or depth-extending experiments that edit num_hidden_layers only; layer lists built from a pattern with the wrong repetition count; merging configs from different depths.","solutions":["Regenerate layer_types to have exactly num_hidden_layers entries, e.g. (['self_attn'] * n_layers) or the model's repeating pattern tiled to depth","Or set num_hidden_layers = len(layer_types) if the layer list is the source of truth","Add an assertion len(cfg.layer_types) == cfg.num_hidden_layers right after building configs programmatically"],"exampleFix":"# before\ncfg.num_hidden_layers = 32\ncfg.layer_types = ['self_attn'] * 12\n# after\ncfg.num_hidden_layers = 32\ncfg.layer_types = ['self_attn'] * 32","handlingStrategy":"validation","validationCode":"assert len(layer_types) == num_hidden_layers, f'need exactly {num_hidden_layers} layer_types entries, got {len(layer_types)}'","typeGuard":"def layer_count_matches(layer_types: list[str], num_hidden_layers: int) -> bool:\n    return len(layer_types) == num_hidden_layers","tryCatchPattern":"try:\n    cfg.validate_layer_types()\nexcept ValueError as e:\n    if 'must be equal to the number of' in str(e):\n        cfg.layer_types = (cfg.layer_types * cfg.num_hidden_layers)[:cfg.num_hidden_layers]\n    else:\n        raise","preventionTips":["Regenerate layer_types whenever num_hidden_layers changes (pruning/depth variants)","Build layer lists with a tiling helper that always emits num_hidden_layers entries"],"tags":["config","architecture","layer-types","validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}