huggingface/transformers · error · ValueError
The `layer_types` entries must be in {ALLOWED_LAYER_TYPES}
Error message
The `layer_types` entries must be in {ALLOWED_LAYER_TYPES} What it means
ValueError from the deprecated free function layer_type_validation (warned for removal in v5.20; use PreTrainedConfig.validate_layer_type instead): a value in layer_types is not a member of ALLOWED_LAYER_TYPES (e.g. 'dense'/'linear' attention variants).
Source
Thrown at src/transformers/configuration_utils.py:1489
PreTrainedConfig.push_to_hub = copy_func(PreTrainedConfig.push_to_hub)
if PreTrainedConfig.push_to_hub.__doc__ is not None:
PreTrainedConfig.push_to_hub.__doc__ = PreTrainedConfig.push_to_hub.__doc__.format(
object="config", object_class="AutoConfig", object_files="configuration file"
)
# The alias is only here for BC - we did not have the correct CamelCasing before
PretrainedConfig = PreTrainedConfig
def layer_type_validation(layer_types: list[str], num_hidden_layers: int | None = None, attention: bool = True):
logger.warning(
"`layer_type_validation` is deprecated and will be removed in v5.20. "
"Use `PreTrainedConfig.validate_layer_type` instead"
)
if not all(layer_type in ALLOWED_LAYER_TYPES for layer_type in layer_types):
raise ValueError(f"The `layer_types` entries must be in {ALLOWED_LAYER_TYPES}")
if num_hidden_layers is not None and num_hidden_layers != len(layer_types):
raise ValueError(
f"`num_hidden_layers` ({num_hidden_layers}) must be equal to the number of layer types "
f"({len(layer_types)})"
)
View on GitHub (pinned to a597f97485)
Solutions
- Use only strings from transformers.configuration_utils.ALLOWED_LAYER_TYPES (check the constant in your version).
- Upgrade Transformers if the layer type is valid in a newer release.
- Migrate to the instance API: config.validate_layer_type(layer_types, num_hidden_layers).
Example fix
// before layer_type_validation(["dense", "full"], num_hidden_layers=2) # ValueError // after layer_type_validation(["dense", "linear"], num_hidden_layers=2)
Defensive patterns
Strategy: validation
Validate before calling
from transformers.configuration_utils import ALLOWED_LAYER_TYPES
bad = [t for t in layer_types if t not in ALLOWED_LAYER_TYPES]
if bad:
raise ValueError(f"unsupported layer types {bad}; allowed: {ALLOWED_LAYER_TYPES}") Type guard
def layer_types_are_valid(layer_types: list[str]) -> bool:
from transformers.configuration_utils import ALLOWED_LAYER_TYPES
return all(t in ALLOWED_LAYER_TYPES for t in layer_types) Prevention
- Import ALLOWED_LAYER_TYPES from your installed version rather than hardcoding the list.
- Migrate off the deprecated layer_type_validation to config.validate_layer_type.
When it happens
Trigger: Calling layer_type_validation(["dense", "sliding", "full"], ...) where 'sliding'/'full' are not in ALLOWED_LAYER_TYPES, or configs carrying a custom layer type string not yet supported by the installed version.
Common situations: New architectures adding a layer type before the installed Transformers version knows it (version skew); typos like 'Dense' with wrong casing.
Related errors
- `num_hidden_layers` ({num_hidden_layers}) must be equal to t
- The `{layer_types}` entries must be in {allowed_types} but g
- `num_hidden_layers` ({self.num_hidden_layers}) must be equal
- Layer type '{layer_idx}' not found in config.layer_types: {l
- out_indices must be a list, got {type(self._out_indices)}
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/aa5df2a719904b55.
Report an issue: GitHub.