{"record":{"id":"c889a28d98e7cbcd","repo":"huggingface/transformers","slug":"layer-type-layer-idx-requested-but-config-lay","errorCode":null,"errorMessage":"Layer type '{layer_idx}' requested, but config.layer_types is not defined. ","messagePattern":"Layer type '(.+?)' requested, but config\\.layer_types is not defined\\. ","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/integrations/heterogeneity/configuration_utils.py","lineNumber":222,"sourceCode":"        if attr == \"skip\":\n            continue\n        setattr(output_config, attr, value)\n\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). \"","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/integrations/heterogeneity/configuration_utils.py#L204-L240","documentation":"_PerLayerConfigView.__getitem__ supports indexing by layer-type name (e.g. config.per_layer_config[\"full\"] or [\"linear_attention\"]) for models whose config declares layer_types. That string-indexing path requires config.layer_types to exist; if the attribute is absent (the model never declared layer types), the view cannot map a name to layers and raises ValueError. Note the message has a trailing space and mentions the requested name, not the available set.","triggerScenarios":"Calling config.per_layer_config[\"some_type\"] on a model config that has no layer_types attribute — e.g. a homogeneous model, or a heterogeneous config built only with numeric indices.","commonSituations":"Generic code that assumes every model exposes layer_types (they only appeared with hybrid/heterogeneous architectures like Gemma-3, xLSTM-style models); accessing per-layer views on older checkpoints whose configs predate the field.","solutions":["Index by integer layer index instead of type name when layer_types is undefined","If the model should have layer types, add config.layer_types = [\"full\", \"sliding\", ...] with one entry per layer","Guard string indexing with hasattr(config, \"layer_types\")"],"exampleFix":"# before\ncfg = AutoConfig.from_pretrained(model_id)  # no layer_types\ncfg.per_layer_config[\"sliding\"]  # ValueError\n\n# after\nif getattr(cfg, \"layer_types\", None) is not None:\n    view = cfg.per_layer_config[\"sliding\"]\nelse:\n    view = cfg.per_layer_config[0]","handlingStrategy":"type-guard","validationCode":"if getattr(config, \"layer_types\", None) is None:\n    layer_cfg = config.per_layer_config[0]  # integer index is always safe\nelse:\n    layer_cfg = config.per_layer_config[\"sliding\"]","typeGuard":"def supports_layer_type_indexing(config) -> bool:\n    \"\"\"True only when config.layer_types is declared (required for string indexing).\"\"\"\n    return getattr(config, \"layer_types\", None) is not None","tryCatchPattern":null,"preventionTips":["Check hasattr(config, 'layer_types') before indexing per_layer_config by a type name","Prefer integer layer indices in generic code; reserve type-name indexing for models that declare layer_types"],"tags":["heterogeneity","config","layer-types","indexing"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}