{"record":{"id":"f93d18acd249c544","repo":"huggingface/transformers","slug":"layer-type-layer-idx-not-found-in-config-layer","errorCode":null,"errorMessage":"Layer type '{layer_idx}' not found in config.layer_types: {layer_types}. Available layer types: {set(layer_types)}","messagePattern":"Layer type '(.+?)' not found in config\\.layer_types: (.+?)\\. Available layer types: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/integrations/heterogeneity/configuration_utils.py","lineNumber":225,"sourceCode":"\n    return output_config\n\n\nclass _PerLayerConfigView(Sequence[\"PreTrainedConfig\"]):\n    def __init__(self, config: PreTrainedConfig) -> None:\n        self._config = config\n\n    def __len__(self) -> int:\n        return self._config.num_hidden_layers\n\n    def __getitem__(self, layer_idx: int | slice | str) -> PreTrainedConfig | list[PreTrainedConfig]:\n        # Return the config for a specific layer type, if the model is homogeneous for that layer type\n        if isinstance(layer_idx, str):\n            if (layer_types := getattr(self._config, \"layer_types\", None)) is None:\n                raise ValueError(f\"Layer type '{layer_idx}' requested, but config.layer_types is not defined. \")\n\n            if layer_idx not in layer_types:\n                raise ValueError(\n                    f\"Layer type '{layer_idx}' not found in config.layer_types: {layer_types}. \"\n                    f\"Available layer types: {set(layer_types)}\"\n                )\n\n            # Config is actually homogeneous so just return the global config\n            if not self._config.is_heterogeneous:\n                return self._config\n\n            # Ensure that all layers of the requested type have the same overrides\n            layer_overrides = self._config._heterogeneity_spec.per_layer_overrides\n            reference_overrides = layer_overrides.get(layer_types.index(layer_idx), {})\n            for idx, layer_type in enumerate(layer_types):\n                if layer_type == layer_idx and layer_overrides.get(idx, {}) != reference_overrides:\n                    raise ValueError(\n                        f\"Layer type '{layer_idx}' is not homogeneous across layers (layer {idx} differs). \"\n                        f\"Use an integer index to access a specific layer's config.\"\n                    )\n","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/integrations/heterogeneity/configuration_utils.py#L207-L243","documentation":"`per_layer_config` on a heterogeneity-aware config accepts a string key to fetch the shared config of all layers of one type (e.g. \"sliding_attention\"). The string is matched against `config.layer_types`; if it is not one of the declared layer type names, this ValueError is raised, listing the types that are actually available. It exists to catch typos and layer-type names that the model config simply does not declare.","triggerScenarios":"Calling `model.config.per_layer_config[\"full_attention\"]` (or any string index on the PerLayerConfig accessor) when `config.layer_types` is a list like `[\"sliding_attention\", \"full_attention\"]` that does not contain that string, or when `layer_types` uses different names than the caller assumes (e.g. custom layer type names on a heterogeneous Llama-style model).","commonSituations":"Working with heterogeneous/mixed-layer models (e.g. models mixing sliding-window and full attention) and guessing the layer-type string instead of reading `config.layer_types`; porting code between models whose `layer_types` vocabulary differs; typos in the layer type name.","solutions":["Print `config.layer_types` and use one of the exact strings listed in the error message.","If you want a specific layer regardless of type, index with an integer: `config.per_layer_config[i]`.","If you want several layers, use a slice: `config.per_layer_config[start:stop]`.","If the model should have that layer type, fix the config (`layer_types=[...]`) before constructing the model."],"exampleFix":"# before\ncfg = AutoConfig.from_pretrained(model_id)\nlayer_cfg = cfg.per_layer_config[\"attention\"]  # ValueError: not in layer_types\n\n# after\nprint(cfg.layer_types)  # e.g. ['sliding_attention', 'full_attention']\nlayer_cfg = cfg.per_layer_config[\"full_attention\"]","handlingStrategy":"validation","validationCode":"layer_type = \"full_attention\"\nif layer_type not in set(getattr(config, \"layer_types\", []) or []):\n    raise KeyError(f\"unknown layer type {layer_type!r}; config declares {config.layer_types}\")\nlayer_cfg = config.per_layer_config[layer_type]","typeGuard":"def is_valid_layer_type(config, name: str) -> bool:\n    return name in set(getattr(config, \"layer_types\", None) or [])","tryCatchPattern":"try:\n    layer_cfg = config.per_layer_config[name]\nexcept ValueError as e:\n    # message lists available types; re-derive from config.layer_types\n    available = set(config.layer_types or [])\n    raise KeyError(f\"{name!r} not in {available}\") from e","preventionTips":["Never hard-code layer-type strings; always derive them from config.layer_types.","Log config.layer_types once at startup when working with heterogeneous models.","Prefer integer or slice indexing when the layer type is not the point of the access."],"tags":["config","heterogeneity","layer-types","validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}