Pipelines: Turning Model Contracts into Inference
A loaded checkpoint can be used directly, but many inference tasks repeat the same boundary work: choose a task, prepare inputs, place tensors, run without gradients, and turn raw model outputs into something a person or application can use. Transformers’ Pipeline is the project’s task-oriented adapter for that path. This chapter explains what Pipeline adds to a coherent model package, how its stages protect the execution boundary, and when batching or convenience should give way to direct control.
Pipeline does not add new learned capability. It organizes the capabilities already provided by the model, preprocessor, and task head. That distinction lets a reader use it confidently without treating a short call as proof that every model, workload, or output is interchangeable.
The path from task name to friendly output
A pipeline begins with a task identifier such as text classification, image classification, or automatic speech recognition. Transformers keeps a registry that associates task names with a pipeline implementation, compatible Auto model classes, expected input modality, and often a default model pinned to a known revision. The registry is a compatibility boundary. It prevents a task name from being paired with an arbitrary model class and gives the factory enough information to construct a useful default when the caller has not supplied a particular checkpoint.
The caller can override the default model, but the task still matters. A text-generation pipeline needs a model that can generate. An image-classification pipeline needs a model head whose outputs can be interpreted as image labels. An audio pipeline needs a processor and model combination that understands the audio representation. The short interface hides the selection work; it does not abolish the model contract established in the preceding chapters.
Once the task and model are resolved, a pipeline call follows a clear sequence. preprocess accepts a user-facing input and converts it into the tensors and auxiliary values expected by the model. _forward performs the model-facing work. postprocess translates the raw output into task-oriented results, often simple Python values such as labels, scores, text, timestamps, or dictionaries. The separation is useful because each stage owns a different boundary: raw data, model execution, and user-facing interpretation.
The forward method around forward supplies important safeguards. It places tensors on the pipeline’s selected device, enters an inference context that disables gradient tracking, invokes the model, and moves outputs back for post-processing. This is not merely a wrapper around a function call.