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. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Core Loading and Auto Classes
| 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. |