Pipelines: inference in three lines

Hugging Face Transformers · Essentials · 10 min

Pipelines are the fastest way to try a model or ship a simple inference feature.

A pipeline bundles three steps: a preprocessor (tokenizer, image processor or feature extractor) that turns raw input into tensors, the model forward pass, and postprocessing that turns logits into readable results such as labels and scores.

clf = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")

The first argument is a task name. Always pass model= explicitly: without it the pipeline picks a default checkpoint, which can change between releases and logs a warning.

Useful arguments:

  • device=0 (first GPU), device="cpu", or device_map="auto" to let Accelerate place large models.
  • dtype="auto" (the v5 default) loads weights in the precision they were saved in; pass torch.bfloat16 to force half precision.
  • Pass a list of inputs and batch_size= to process many items efficiently.

Pipelines accept a string, a list of strings, or for vision/audio tasks a file path, URL or PIL image. The model weights are downloaded once to the local cache (~/.cache/huggingface/hub) and reused afterwards.

Pipelines trade control for convenience. When you need custom batching, access to hidden states, or training, drop down to the tokenizer and model classes covered next. Note that in v5 several legacy tasks such as summarization and translation were removed from pipelines; use a chat model through text-generation for those.

Example

from transformers import pipeline

clf = pipeline(
    "text-classification",
    model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
)
print(clf("I love this library!"))
# [{'label': 'POSITIVE', 'score': 0.999...}]

reviews = ["Terrible support.", "Works great, fast shipping."]
for r in clf(reviews, batch_size=2):
    print(r["label"], round(r["score"], 3))
# NEGATIVE 0.999...
# POSITIVE 0.999...

asr = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
# asr("meeting.wav") -> {'text': ' Hello everyone, ...'}

Remember

  • pipeline(task, model=...) = preprocess + forward + postprocess
  • Always pin model= instead of relying on the task default
  • Pass lists with batch_size for throughput; use device/device_map for GPUs

Watch out for

  • Calling a pipeline inside a loop one string at a time on GPU, which wastes most of its throughput.

Check yourself

  1. Why should you pass model= when creating a pipeline?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: huggingface.co/docs/transformers/pipeline_tutorial

Builds on

Nothing: this is a foundation.

Unlocks