Run a model in a few lines, pipelines and Auto classes
What you’ll learn
Section titled “What you’ll learn”This is the lesson where Track 14 stops describing transformers and starts running them. You will run a real task on a pretrained model with a single pipeline() call, then reproduce that same result by hand to see the three steps the one-liner hides. The source curriculum is the Hugging Face LLM Course, Chapter 2, freely available and Apache-2.0 licensed at huggingface.co/learn/llm-course/chapter2.
You will call pipeline(task) and get labels and scores back; learn the three steps it groups (preprocessing, the model, postprocessing); use AutoTokenizer to turn text into input_ids and an attention_mask; see the difference between the base AutoModel and a task-specific AutoModelFor<Task>; learn why models output logits and how a softmax plus id2label turns them into an answer; and meet the from_pretrained(checkpoint) idiom that the whole library runs on.
Where this fits
Section titled “Where this fits”This is lesson 2 of 12, the second lesson of Phase 1 (the Transformers library). Lesson 1 gave the conceptual picture; this lesson makes it runnable. It is also the foundation for everything that follows: fine-tuning (lesson 3) changes the model in step two, the tokenizer lessons open up step one, and the task lessons swap the head in step two. Opening the box here is what makes the rest of the track reachable.
Before you start
Section titled “Before you start”Prerequisites: lesson 1 of this track (the working picture: tokens in, tokens out, the three architectural shapes), which this lesson turns into running code. You should be comfortable reading and running basic Python. A notebook environment makes this painless; Google Colab needs no setup, or run locally after pip install transformers.
About the math
Section titled “About the math”None, but there is code. This is a hands-on lesson: every concept comes with a runnable snippet, and you will get the most from it by running them. The only near-math is a softmax call you make with one line; you do not need to know how softmax works internally, only that it turns raw scores into probabilities.
By the end, you’ll be able to
Section titled “By the end, you’ll be able to”The single capability this lesson builds: run a common task on a pretrained model with pipeline(), and reproduce it by hand with the Auto classes (tokenizer, model, postprocessing). Concretely, you will be able to:
- Run a common task on a pretrained model with a single
pipeline()call - Name the three steps a pipeline hides (tokenizer, model, postprocessing)
- Use
AutoTokenizerto turn text intoinput_idsand anattention_mask - Distinguish
AutoModel(hidden states) fromAutoModelFor<Task>(a task head) and read logits - Postprocess logits into probabilities with softmax and attach labels with
id2label
Time and difficulty
Section titled “Time and difficulty”- Read time: about 11 minutes
- Practice time: about 12 minutes (run a pipeline, then rebuild it by hand with the Auto classes, plus flashcards)
- Difficulty: standard (your first code lesson, but every snippet is short and runnable)