{"record":{"id":"874e32f7ad3bc61e","repo":"huggingface/transformers","slug":"num-hidden-layers-num-hidden-layers-must-be","errorCode":null,"errorMessage":"`num_hidden_layers` ({num_hidden_layers}) must be equal to the number of layer types ({len(layer_types)})","messagePattern":"`num_hidden_layers` \\((.+?)\\) must be equal to the number of layer types \\((.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/configuration_utils.py","lineNumber":1491,"sourceCode":"    PreTrainedConfig.push_to_hub.__doc__ = PreTrainedConfig.push_to_hub.__doc__.format(\n        object=\"config\", object_class=\"AutoConfig\", object_files=\"configuration file\"\n    )\n\n\n# The alias is only here for BC - we did not have the correct CamelCasing before\nPretrainedConfig = PreTrainedConfig\n\n\ndef layer_type_validation(layer_types: list[str], num_hidden_layers: int | None = None, attention: bool = True):\n    logger.warning(\n        \"`layer_type_validation` is deprecated and will be removed in v5.20. \"\n        \"Use `PreTrainedConfig.validate_layer_type` instead\"\n    )\n\n    if not all(layer_type in ALLOWED_LAYER_TYPES for layer_type in layer_types):\n        raise ValueError(f\"The `layer_types` entries must be in {ALLOWED_LAYER_TYPES}\")\n    if num_hidden_layers is not None and num_hidden_layers != len(layer_types):\n        raise ValueError(\n            f\"`num_hidden_layers` ({num_hidden_layers}) must be equal to the number of layer types \"\n            f\"({len(layer_types)})\"\n        )\n","sourceCodeStart":1473,"sourceCodeEnd":1495,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/configuration_utils.py#L1473-L1495","documentation":"ValueError from layer_type_validation (deprecated free function): the length of layer_types must equal num_hidden_layers exactly, because every hidden layer needs one type entry. Passing a mismatched pair fails even when all entries are individually valid.","triggerScenarios":"layer_type_validation([\"dense\", \"linear\"], num_hidden_layers=32), or configs where num_hidden_layers was updated (e.g. reduced for a smaller variant) but layer_types was not regenerated.","commonSituations":"Scaling configs up/down programmatically; layer-type lists built by repetition with the wrong factor; MTP or distilled variants that changed depth.","solutions":["Regenerate layer_types to match depth: config.layer_types = [\"dense\"] * config.num_hidden_layers (or the intended pattern of that length).","When changing num_hidden_layers on an existing config, update layer_types in the same edit.","Pass num_hidden_layers=None if you only want entry validation and will validate the count elsewhere."],"exampleFix":"// before\nlayer_type_validation([\"dense\"] * 3, num_hidden_layers=4)  # ValueError\n\n// after\nlayer_type_validation([\"dense\"] * 4, num_hidden_layers=4)","handlingStrategy":"validation","validationCode":"assert num_hidden_layers is None or num_hidden_layers == len(layer_types), (\n    f\"num_hidden_layers={num_hidden_layers} != len(layer_types)={len(layer_types)}\")","typeGuard":"def layer_types_match_depth(layer_types: list[str], num_hidden_layers: int | None) -> bool:\n    return num_hidden_layers is None or num_hidden_layers == len(layer_types)","tryCatchPattern":null,"preventionTips":["Generate layer_types programmatically from num_hidden_layers so they cannot drift.","In config __post_init__/validation hooks, enforce len(layer_types) == num_hidden_layers."],"tags":["config","layer-types","validation","deprecated"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}