huggingface/transformers · error · ValueError

backbone_type {self.backbone_type} not supported.

Error message

backbone_type {self.backbone_type} not supported.

What it means

Defensive ValueError in BackboneMixin.__init__: after determining backbone_type from the presence of the timm_backbone kwarg (TIMM if present, TRANSFORMERS otherwise), an else-branch raises if backbone_type is neither enum value. With the current code path this is effectively unreachable — the if/else above always assigns one of the two enum members. Hitting it means backbone_type was set to an unexpected value externally (e.g. manually assigned or a stale pickle) or the mixin was used outside its intended flow.

Source

Thrown at src/transformers/backbone_utils.py:205

    def __init__(self, *args, **kwargs) -> None:
        """
        Method to initialize the backbone. This method is called by the constructor of the base class after the
        pretrained model weights have been loaded.
        """
        super().__init__(*args, **kwargs)
        timm_backbone = kwargs.pop("timm_backbone", None)
        if timm_backbone is not None:
            self.backbone_type = BackboneType.TIMM
        else:
            self.backbone_type = BackboneType.TRANSFORMERS

        if self.backbone_type == BackboneType.TIMM:
            self._init_timm_backbone(backbone=timm_backbone)
        elif self.backbone_type == BackboneType.TRANSFORMERS:
            self._init_transformers_backbone()
        else:
            raise ValueError(f"backbone_type {self.backbone_type} not supported.")

    def post_init(self):
        """
        Override `post_init` to always install capturing hooks, as backbone will ALWAYS capture outputs. We need to do
        it in `post_init`, as modules need to be already instantiated.
        It avoids some mixups with `torch.compile`, as the first hook installation will need/create a graph break,
        which can clash with external user call such as `model = torch.compile(model...)`.
        """
        # NOTE: Since this class is ALWAYS used as a Mixin with another PreTrainedModel class, this `super` call
        # will call the PreTrained's `post_init`
        super().post_init()
        maybe_install_capturing_hooks(self)

    def _init_timm_backbone(self, backbone) -> None:
        """
        Initialize the backbone model from timm. The backbone must already be loaded to backbone
        """

View on GitHub (pinned to a597f97485)

Solutions

  1. Do not assign backbone_type yourself; let the mixin derive it from the timm_backbone kwarg
  2. Ensure your subclass __init__ calls super().__init__(*args, **kwargs) unchanged
  3. If you need a timm backbone, pass use_timm_backend=True / timm_backbone kwarg instead of forcing the enum

Example fix

# before
self.backbone_type = 'timm'  # string, not enum

# after
# let the mixin decide: pass the timm_backbone kwarg
model = MyBackbone(config, timm_backbone='resnet50')
Defensive patterns

Strategy: validation

Validate before calling

from transformers.modeling_backbones import BackboneType
assert backbone_type in (BackboneType.TIMM, BackboneType.TRANSFORMERS) or backbone_type is None

Type guard

from transformers.modeling_backbones import BackboneType

def is_backbone_type(v) -> bool:
    return v is None or v in (BackboneType.TIMM, BackboneType.TRANSFORMERS)

Prevention

When it happens

Trigger: Manually setting backbone_type on a BackboneMixin subclass to a non-BackboneType value before super().__init__() logic runs; monkeypatching; loading a state dict/class whose __init__ was overridden so the assignment above is skipped but a bogus attribute is present.

Common situations: Custom model classes inheriting BackboneMixin with a custom __init__ that forgets to call the mixin init properly; deepcopy/pickling edge cases; almost never seen in normal usage.

Related errors


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