Hugging Face Transformers Cheat Sheet
Hugging Face Transformers reference covering the pipeline API, AutoTokenizer/AutoModel classes, and fine-tuning with the Trainer API.
Pipeline API
Zero-setup inference for common tasks.
from transformers import pipelineclassifier = pipeline("sentiment-analysis")result = classifier("I love using transformers!")# [{'label': 'POSITIVE', 'score': 0.9998}]generator = pipeline("text-generation", model="gpt2")generator("Once upon a time", max_length=30, num_return_sequences=1)qa = pipeline("question-answering")qa(question="Who built the library?", context="Hugging Face built Transformers.")
Tokenizer & Model
Lower-level access for custom workflows.
from transformers import AutoTokenizer, AutoModelForSequenceClassificationimport torchtokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=2)inputs = tokenizer("Hello world!", return_tensors="pt", padding=True, truncation=True)with torch.no_grad(): outputs = model(**inputs)logits = outputs.logitsprobs = torch.softmax(logits, dim=-1)
Fine-Tuning with Trainer
Train on a custom dataset.
from transformers import TrainingArguments, Trainertraining_args = TrainingArguments( output_dir="./results", per_device_train_batch_size=16, num_train_epochs=3, learning_rate=2e-5, logging_steps=50,)trainer = Trainer( model=model, args=training_args, train_dataset=train_dataset, eval_dataset=eval_dataset,)trainer.train()metrics = trainer.evaluate()
Key Classes
Core building blocks of the library.
- AutoTokenizer- loads the correct tokenizer for any model checkpoint
- AutoModel / AutoModelForXxx- loads architecture + weights matched to a task
- pipeline()- highest-level API for inference in a few lines
- Trainer / TrainingArguments- training loop with logging, checkpointing, evaluation
- Dataset (datasets library)- efficient, memory-mapped dataset loading
- save_pretrained() / from_pretrained()- persist and reload models/tokenizers/configs
4-Bit Quantization with bitsandbytes
Load large models in reduced precision to cut GPU memory usage drastically.
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfigimport torchbnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_use_double_quant=True,)tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")model = AutoModelForCausalLM.from_pretrained( "meta-llama/Llama-2-7b-hf", quantization_config=bnb_config, device_map="auto",)
LoRA Fine-Tuning with PEFT
Fine-tune only a small set of low-rank adapter weights instead of the full model.
from peft import LoraConfig, get_peft_model, TaskTypelora_config = LoraConfig( task_type=TaskType.CAUSAL_LM, r=8, lora_alpha=32, lora_dropout=0.05, target_modules=["q_proj", "v_proj"],)peft_model = get_peft_model(model, lora_config)peft_model.print_trainable_parameters() # e.g. trainable params: 0.06% of totalpeft_model.save_pretrained("./lora-adapter") # saves only the small adapter weights
Generation & Decoding Parameters
Control sampling behavior and repetition when generating text.
inputs = tokenizer("The future of AI is", return_tensors="pt").to(model.device)output = model.generate( **inputs, max_new_tokens=100, do_sample=True, temperature=0.7, top_p=0.9, top_k=50, repetition_penalty=1.2, num_beams=1, pad_token_id=tokenizer.eos_token_id,)print(tokenizer.decode(output[0], skip_special_tokens=True))
Accelerate for Multi-GPU Training
Scale a training loop across GPUs and nodes with minimal code changes.
from accelerate import Acceleratoraccelerator = Accelerator(mixed_precision="bf16", gradient_accumulation_steps=4)model, optimizer, train_dataloader = accelerator.prepare(model, optimizer, train_dataloader)for batch in train_dataloader: with accelerator.accumulate(model): outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() optimizer.zero_grad()# launch across GPUs/nodes with: accelerate launch --multi_gpu train.py
Advanced Concepts
Memory, batching, and export tools for production-scale usage.
- device_map="auto"- shards a model automatically across available GPUs, CPU, and disk
- torch_dtype- loads weights directly in fp16/bf16 to roughly halve memory versus default fp32
- gradient_checkpointing_enable()- trades extra compute for memory by recomputing activations during backward
- DataCollatorWithPadding / DataCollatorForLanguageModeling- dynamically pads and batches examples at collate time
- streaming=True (datasets)- iterates a dataset without downloading it fully to disk
- safetensors- safe, fast weight serialization format, now the default over pickle-based .bin files
- optimum / ONNX export- converts models to ONNX or TensorRT for optimized inference serving
Always load the tokenizer and model with the same checkpoint name via from_pretrained() — mismatched tokenizer/model vocabularies silently produce garbage predictions instead of raising an error.