Open-Source Wikis

/

Transformers

/

Features

/

Auto classes

huggingface/transformers

Auto classes

AutoConfig, AutoModel, AutoModelFor<Task>, AutoTokenizer, AutoImageProcessor, AutoVideoProcessor, AutoFeatureExtractor, and AutoProcessor are the late-binding factories most users interact with. They take a checkpoint identifier, look at its model_type, and instantiate the right concrete class.

Why they exist

The library has 462 architectures. Forcing every user to import the exact class (from transformers import LlamaForCausalLM) would be brittle: a checkpoint might switch architectures on the Hub, or a notebook might try several models. The auto factories decouple the user code from the architecture.

Where they live

src/transformers/models/auto/
├── __init__.py
├── auto_factory.py             # the magic: from_pretrained → resolve class → instantiate
├── auto_mappings.py            # OrderedDicts mapping model_type → class
├── configuration_auto.py       # AutoConfig + CONFIG_MAPPING
├── feature_extraction_auto.py  # AutoFeatureExtractor + FEATURE_EXTRACTOR_MAPPING
├── image_processing_auto.py    # AutoImageProcessor + IMAGE_PROCESSOR_MAPPING
├── modeling_auto.py            # Auto*Model* classes + MODEL_*_MAPPING
├── processing_auto.py          # AutoProcessor + PROCESSOR_MAPPING
├── tokenization_auto.py        # AutoTokenizer + TOKENIZER_MAPPING
└── video_processing_auto.py    # AutoVideoProcessor + VIDEO_PROCESSOR_MAPPING

auto_factory.py defines the meta-class machinery; auto_mappings.py is the registry of model_type strings to classes.

How AutoModelForCausalLM.from_pretrained resolves

graph LR
    User --> AM[AutoModelForCausalLM.from_pretrained repo_id]
    AM --> CFG[AutoConfig.from_pretrained]
    CFG --> JSON[Read config.json]
    JSON --> MT[config.model_type = llama]
    MT --> Map[MODEL_FOR_CAUSAL_LM_MAPPING llama → LlamaForCausalLM]
    Map --> Inst[LlamaForCausalLM.from_pretrained]

The mapping MODEL_FOR_CAUSAL_LM_MAPPING (and dozens of siblings: MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_VISION_2_SEQ_MAPPING, MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING, …) is hand-edited in auto_mappings.py but make fix-repo enforces alphabetical order via utils/sort_auto_mappings.py.

All auto-class flavours

Auto class Mapping Use
AutoConfig CONFIG_MAPPING Load any config
AutoModel MODEL_MAPPING Backbone (no head)
AutoModelForCausalLM MODEL_FOR_CAUSAL_LM_MAPPING Decoder LMs
AutoModelForMaskedLM MODEL_FOR_MASKED_LM_MAPPING BERT-style
AutoModelForSeq2SeqLM MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING T5, BART, …
AutoModelForSequenceClassification Classification
AutoModelForTokenClassification NER
AutoModelForQuestionAnswering Extractive QA
AutoModelForMultipleChoice Multi-choice
AutoModelForVision2Seq Image captioning, OCR
AutoModelForImageTextToText VLMs
AutoModelForImageClassification ViT-style
AutoModelForObjectDetection DETR-style
AutoModelForSemanticSegmentation Segmenter
AutoModelForUniversalSegmentation OneFormer / Mask2Former
AutoModelForDepthEstimation Depth nets
AutoModelForVideoClassification TimeSformer, VideoMAE
AutoModelForAudioClassification AST
AutoModelForCTC, AutoModelForSpeechSeq2Seq, AutoModelForAudioFrameClassification, AutoModelForXVector, AutoModelForAudioXVector ASR / audio
AutoModelForTextToWaveform, AutoModelForTextToSpectrogram TTS
AutoModelForMaskGeneration SAM
AutoModelForKeypointDetection, AutoModelForKeypointMatching Keypoints
AutoModelForZeroShotImageClassification, AutoModelForZeroShotObjectDetection Zero-shot vision
AutoModelForAnyToAny "Any modality in, any modality out" models
AutoTokenizer TOKENIZER_MAPPING Tokenizer
AutoImageProcessor IMAGE_PROCESSOR_MAPPING Image preprocessor
AutoVideoProcessor VIDEO_PROCESSOR_MAPPING Video preprocessor
AutoFeatureExtractor FEATURE_EXTRACTOR_MAPPING Audio preprocessor
AutoProcessor PROCESSOR_MAPPING Multimodal bundler
AutoBackbone BACKBONE_MAPPING Backbone for detection/segmentation

The auto_mappings.py file is the single source of truth — auto_factory.py reads it at import time.

Registering a new model

When you add a new architecture, edit auto_mappings.py to add an entry:

("my_arch", "MyArchConfig"),                     # to CONFIG_MAPPING_NAMES
("my_arch", "MyArchForCausalLM"),                # to MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
("my_arch", ("MyArchTokenizer", "MyArchTokenizerFast")),  # to TOKENIZER_MAPPING_NAMES

make fix-repo then sorts the lists. The matching import is generated from the names; do not add manual imports.

Trust remote code

Auto classes also handle the trust_remote_code=True codepath. When a checkpoint declares auto_map: {AutoModelForCausalLM: <module.MyClass>} in its config, AutoModelForCausalLM.from_pretrained(..., trust_remote_code=True) will fetch that module from the Hub and instantiate the class. This is how community models that aren't merged into transformers still work.

The dynamic-loader is in src/transformers/dynamic_module_utils.py (37K LOC). resolve_trust_remote_code enforces an interactive confirmation when running outside a known-safe environment.

Integration points

  • ConfigurationAutoConfig is the entry point.
  • ModelingAutoModelFor<Task> instantiates concrete classes.
  • Pipelinespipeline() uses the auto classes internally.
  • from_pretrained — auto classes layer on top of the universal loader.

Entry points for modification

  • New auto class for a new task → add to modeling_auto.py, register the mapping, add tests in tests/models/auto/.
  • New model_type → edit auto_mappings.py and run make fix-repo.
  • Trust-remote-code policy → src/transformers/dynamic_module_utils.py.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Auto classes – Transformers wiki | Factory