$npx -y skills add itsmostafa/llm-engineering-skills --skill transformersLoading and using pretrained models with Hugging Face Transformers. Use when working with pretrained models from the Hub, running inference with Pipeline API, fine-tuning models with Trainer, or handling text, vision, audio, and multimodal tasks.
| 1 | # Using Hugging Face Transformers |
| 2 | |
| 3 | Transformers is the model-definition framework for state-of-the-art machine learning across text, vision, audio, and multimodal domains. It provides unified APIs for loading pretrained models, running inference, and fine-tuning. |
| 4 | |
| 5 | ## Table of Contents |
| 6 | |
| 7 | - [Core Concepts](#core-concepts) |
| 8 | - [Pipeline API](#pipeline-api) |
| 9 | - [Model Loading](#model-loading) |
| 10 | - [Inference Patterns](#inference-patterns) |
| 11 | - [Fine-tuning with Trainer](#fine-tuning-with-trainer) |
| 12 | - [Working with Modalities](#working-with-modalities) |
| 13 | - [Memory and Performance](#memory-and-performance) |
| 14 | - [Best Practices](#best-practices) |
| 15 | - [References](#references) |
| 16 | |
| 17 | ## Core Concepts |
| 18 | |
| 19 | ### The Three Core Classes |
| 20 | |
| 21 | Every model in Transformers has three core components: |
| 22 | |
| 23 | ```python |
| 24 | from transformers import AutoConfig, AutoModel, AutoTokenizer, AutoProcessor |
| 25 | |
| 26 | # Configuration: hyperparameters and architecture settings |
| 27 | config = AutoConfig.from_pretrained("bert-base-uncased") |
| 28 | |
| 29 | # Model: the neural network weights |
| 30 | model = AutoModel.from_pretrained("bert-base-uncased") |
| 31 | |
| 32 | # Tokenizer: converts text inputs to tensors |
| 33 | tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") |
| 34 | |
| 35 | # Processor: unified preprocessing for vision, audio, and multimodal models |
| 36 | processor = AutoProcessor.from_pretrained("openai/whisper-large-v3") |
| 37 | ``` |
| 38 | |
| 39 | ### The `from_pretrained` Pattern |
| 40 | |
| 41 | All loading uses `from_pretrained()` which handles downloading, caching, and device placement: |
| 42 | |
| 43 | ```python |
| 44 | from transformers import AutoModelForCausalLM, AutoTokenizer |
| 45 | import torch |
| 46 | |
| 47 | model_name = "meta-llama/Llama-3.2-1B" |
| 48 | |
| 49 | tokenizer = AutoTokenizer.from_pretrained(model_name) |
| 50 | model = AutoModelForCausalLM.from_pretrained( |
| 51 | model_name, |
| 52 | dtype=torch.bfloat16, |
| 53 | device_map="auto", # Automatic device placement |
| 54 | ) |
| 55 | ``` |
| 56 | |
| 57 | Transformers v5 examples use `dtype`. On Transformers v4, the equivalent argument is `torch_dtype`. |
| 58 | |
| 59 | ### Auto Classes |
| 60 | |
| 61 | Use task-specific Auto classes for the correct model head: |
| 62 | |
| 63 | ```python |
| 64 | from transformers import ( |
| 65 | AutoModelForCausalLM, # Text generation (GPT, Llama) |
| 66 | AutoModelForSeq2SeqLM, # Encoder-decoder (T5, BART) |
| 67 | AutoModelForSequenceClassification, # Classification |
| 68 | AutoModelForTokenClassification, # NER, POS tagging |
| 69 | AutoModelForQuestionAnswering, # Extractive QA |
| 70 | AutoModelForMaskedLM, # BERT-style masked LM |
| 71 | AutoModelForImageClassification, # Vision models |
| 72 | AutoModelForSpeechSeq2Seq, # Speech recognition |
| 73 | ) |
| 74 | ``` |
| 75 | |
| 76 | ## Pipeline API |
| 77 | |
| 78 | The `pipeline()` function provides high-level inference with minimal code: |
| 79 | |
| 80 | ### Text Tasks |
| 81 | |
| 82 | ```python |
| 83 | from transformers import pipeline |
| 84 | |
| 85 | # Text generation |
| 86 | generator = pipeline("text-generation", model="Qwen/Qwen2.5-1.5B") |
| 87 | output = generator("The secret to success is", max_new_tokens=50) |
| 88 | |
| 89 | # Text classification |
| 90 | classifier = pipeline("sentiment-analysis") |
| 91 | result = classifier("I love this product!") |
| 92 | # [{'label': 'POSITIVE', 'score': 0.9998}] |
| 93 | |
| 94 | # Named entity recognition |
| 95 | ner = pipeline("ner", aggregation_strategy="simple") |
| 96 | entities = ner("Hugging Face is based in New York City.") |
| 97 | |
| 98 | # Question answering |
| 99 | qa = pipeline("question-answering") |
| 100 | answer = qa(question="What is the capital?", context="Paris is the capital of France.") |
| 101 | |
| 102 | # Summarization |
| 103 | summarizer = pipeline("summarization", model="facebook/bart-large-cnn") |
| 104 | summary = summarizer(long_text, max_length=130, min_length=30) |
| 105 | |
| 106 | # Translation |
| 107 | translator = pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr") |
| 108 | result = translator("Hello, how are you?") |
| 109 | ``` |
| 110 | |
| 111 | ### Chat/Conversational |
| 112 | |
| 113 | ```python |
| 114 | from transformers import pipeline |
| 115 | import torch |
| 116 | |
| 117 | pipe = pipeline( |
| 118 | "text-generation", |
| 119 | model="meta-llama/Llama-3.2-3B-Instruct", |
| 120 | dtype=torch.bfloat16, |
| 121 | device_map="auto", |
| 122 | ) |
| 123 | |
| 124 | messages = [ |
| 125 | {"role": "system", "content": "You are a helpful assistant."}, |
| 126 | {"role": "user", "content": "Explain quantum computing in simple terms."}, |
| 127 | ] |
| 128 | |
| 129 | response = pipe(messages, max_new_tokens=256) |
| 130 | print(response[0]["generated_text"][-1]["content"]) |
| 131 | ``` |
| 132 | |
| 133 | ### Vision Tasks |
| 134 | |
| 135 | ```python |
| 136 | classifier = pipeline("image-classification", model="google/vit-base-patch16-224") |
| 137 | detector = pipeline("object-detection", model="facebook/detr-resnet-50") |
| 138 | ``` |
| 139 | |
| 140 | ### Audio Tasks |
| 141 | |
| 142 | ```python |
| 143 | transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-large-v3") |
| 144 | text = transcriber("path/to/audio.mp3") |
| 145 | ``` |
| 146 | |
| 147 | ### Multimodal Tasks |
| 148 | |
| 149 | ```python |
| 150 | vqa = pipeline("visual-question-answering", model="Salesforce/blip-vqa-base") |
| 151 | captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") |
| 152 | ``` |
| 153 | |
| 154 | ## Model Loading |
| 155 | |
| 156 | ### Device Placement |
| 157 | |
| 158 | ```python |
| 159 | from transformers import AutoModelForCausalLM |
| 160 | import torch |
| 161 | |
| 162 | # Automatic placement across available de |