huggingface/transformers · error · ValueError

Model {cls.__name__} has no config_class or model_type.

Error message

Model {cls.__name__} has no config_class or model_type.

What it means

Before wiring hub-kernel replacements/fusions into a model class, `register_kernel_replacements_and_fusions` needs `cls.config_class.model_type` to build the correct patch mapping. If the model class has no `config_class` attribute, or the config class lacks `model_type`, this ValueError fires — it indicates the object passed as a model class is not a standard `PreTrainedModel` subclass.

Source

Thrown at src/transformers/integrations/hub_kernels.py:857

        original_init(self, *args, **kwargs)
        children = [getattr(self, name) for name in child_names]
        kernel_instance = kernel_cls(*children)
        setattr(self, child_names[0], kernel_instance)
        for name in child_names[1:]:
            setattr(self, name, nn.Identity())

    patched_cls = type(f"Fused{parent_cls.__name__}", (parent_cls,), {"__init__": patched_init})
    patched_cls.__qualname__ = f"Fused{parent_cls.__qualname__}"
    return patched_cls


def register_kernel_replacements_and_fusions(
    cls: "type[PreTrainedModel]",
    config: "PretrainedConfig",
    kernel_config: "KernelConfig",
) -> None:
    if not hasattr(cls, "config_class") or not hasattr(cls.config_class, "model_type"):
        raise ValueError(f"Model {cls.__name__} has no config_class or model_type.")
    model_type = cls.config_class.model_type

    patch_mapping: dict[str, type] = {}
    new_mapping: dict = {}

    # We might need to instantiate the model on meta device.
    # We do it lazily, only if we encounter a fused kernel.
    meta_model = None

    for layer_name, hub_repo in kernel_config.kernel_mapping.items():
        if isinstance(hub_repo, (str, tuple)):
            hub_repo = {None: hub_repo}

        if isinstance(hub_repo, dict):
            if len(hub_repo.values()) != 1:
                raise ValueError(
                    f"Expected exactly one kernel repo regardless of device/mode specificity, got {hub_repo}"
                )

View on GitHub (pinned to a597f97485)

Solutions

  1. Ensure the class is a proper PreTrainedModel subclass with `config_class = MyConfig` and `MyConfig.model_type = "my_model"` set.
  2. Pass the top-level model class (the one registered with AutoModel), not an individual layer class.
  3. If you generated the class dynamically, copy `config_class` from the parent: `patched_cls.config_class = parent_cls.config_class`.

Example fix

# before
class MyModel(nn.Module):  # no config_class
    ...
register_kernel_replacements_and_fusions(MyModel, config, kernel_config)  # ValueError

# after
from transformers import PreTrainedModel, PretrainedConfig

class MyConfig(PretrainedConfig):
    model_type = "my_model"

class MyModel(PreTrainedModel):
    config_class = MyConfig

register_kernel_replacements_and_fusions(MyModel, config, kernel_config)
Defensive patterns

Strategy: validation

Validate before calling

assert hasattr(cls, "config_class") and hasattr(getattr(cls, "config_class", None), "model_type"), (
    f"{cls.__name__} must be a PreTrainedModel subclass with config_class.model_type"
)

Type guard

def is_kernelizable_model_class(cls) -> bool:
    return hasattr(cls, "config_class") and hasattr(cls.config_class, "model_type")

Try / catch

try:
    register_kernel_replacements_and_fusions(cls, config, kernel_config)
except ValueError as e:
    if "no config_class or model_type" in str(e):
        cls.config_class = type(config)  # attach a proper config class, then retry
        if not hasattr(cls.config_class, "model_type"):
            cls.config_class.model_type = config.model_type
        register_kernel_replacements_and_fusions(cls, config, kernel_config)
    else:
        raise

Prevention

When it happens

Trigger: Calling `register_kernel_replacements_and_fusions(cls, config, kernel_config)` with a custom layer/class that never set `config_class`, or a dynamically created model class where `config_class` points at a bare `PretrainedConfig` without a `model_type`; also reachable when the class-level `config_class` was shadowed or deleted.

Common situations: Kernelizing custom or experimental model implementations that skip the `config_class` annotation; wrapping a modeling class with `type(...)` dynamically (fused-class generation) without re-attaching `config_class`; passing a layer class instead of the top-level model class.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/4bd2ba65be4ccf55. Report an issue: GitHub.