Hugging Face Transformers is a Python library that provides unified access to thousands of pretrained transformer models across natural language processing, computer vision, audio, and multimodal tasks. With the release of Transformers v5, the library went PyTorch-only, made quantization a first-class feature, and introduced a modular AttentionInterface plus a built-in transformers serve command for OpenAI-compatible inference. The Auto classes intelligently detect model architectures, while the Pipeline API and TRL post-training library give practitioners everything from instant inference to full RLHF alignment workflows.
What This Cheat Sheet Covers
This topic spans 19 focused tables and 188 indexed concepts, 116 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Core Loading and Auto Classes
The Auto classes are the front door to the whole library β instead of importing a specific model class, you hand from_pretrained a checkpoint name and the right tokenizer, config, and architecture are resolved for you. Which AutoModelFor* you pick declares the task head you want bolted on, from causal generation to token classification, and the same pattern extends to multimodal processors and GGUF-quantized weights.
| Class | Example | Description | |
|---|---|---|---|
from transformers import AutoTokenizertokenizer = AutoTokenizer.from_pretrained("gpt2") | β’ Automatically selects and loads the correct tokenizer for a given model β’ handles text-to-token conversion with model-specific vocabulary. | ||
from transformers import AutoModelmodel = AutoModel.from_pretrained("bert-base-uncased") | β’ Loads the base model architecture from a checkpoint β’ returns raw hidden states without task-specific heads. | ||
from transformers import AutoModelForCausalLMmodel = AutoModelForCausalLM.from_pretrained("gpt2") | β’ Loads model for causal (left-to-right) language modeling β’ predicts next token given previous context, used for text generation. | ||
model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased", num_labels=3) | β’ Loads model with a classification head for labeling entire sequences β’ commonly used for sentiment analysis or topic classification. | ||
from transformers import AutoProcessorprocessor = AutoProcessor.from_pretrained("openai/clip-vit-base-patch32") | β’ Loads the unified processor for multimodal models β’ handles combined image + text inputs in a single interface, first-class in v5. | ||
from transformers import AutoConfigconfig = AutoConfig.from_pretrained("t5-small") | β’ Loads model configuration settings (hidden size, layers, attention heads) β’ enables architecture inspection without loading weights. | ||
model = AutoModelForTokenClassification.from_pretrained("dbmdz/bert-large-cased-finetuned-conll03-english") | β’ Loads model with per-token classification heads β’ used for named entity recognition (NER) or part-of-speech tagging. | ||
model = AutoModelForQuestionAnswering.from_pretrained("distilbert-base-cased-distilled-squad") | β’ Loads model for extractive question answering β’ predicts start and end positions of answer spans in context. | ||
model = AutoModelForMaskedLM.from_pretrained("bert-base-uncased") | β’ Loads model for masked language modeling (predicting masked tokens) β’ used for bidirectional pretraining tasks like BERT. | ||
model = AutoModel.from_pretrained("model-id", torch_dtype=torch.float16, token="hf_...") | β’ Core method to load model weights from Hub or local path β’ use token= (replaces deprecated use_auth_token) for private repos. | ||
model = AutoModelForCausalLM.from_pretrained("TheBloke/TinyLlama-GGUF", gguf_file="tinyllama.Q6_K.gguf") | β’ Loads GGUF-format quantized models directly into PyTorch for fine-tuning or inference β’ dequantizes to fp32 on load. |