The Pipeline API provides a high-level, task-oriented interface for inference with transformer models. It abstracts the complexity of preprocessing inputs, running model inference, and postprocessing outputs into a simple, unified API. Pipelines handle tokenization, feature extraction, batching, device placement, and output formatting automatically.
This document covers the architecture, lifecycle, and usage of pipelines. For model loading and weight management, see Model Loading and Weight Management. For tokenization details, see Tokenization System. For multi-modal processing, see Multi-Modal Processing.
The Pipeline system consists of three layers: a factory function for instantiation, a base class defining the inference lifecycle, and task-specific implementations.
Title: Pipeline System Architecture
Sources: src/transformers/pipelines/__init__.py141-184 src/transformers/pipelines/base.py188-280 src/transformers/pipelines/base.py751-1000
Every pipeline follows a standardized three-stage lifecycle: preprocess → forward → postprocess. The base Pipeline class orchestrates this flow.
Title: Pipeline Execution Sequence
| Method | Purpose | Responsibilities |
|---|---|---|
_sanitize_parameters() | Separate kwargs into stage-specific params | Returns (preprocess_params, forward_params, postprocess_params) src/transformers/pipelines/text_generation.py128-146 |
preprocess() | Convert raw inputs to model-ready tensors | Tokenization, image processing, padding src/transformers/pipelines/base.py937-951 |
_forward() | Run model inference | Execute model.forward() or model.generate() src/transformers/pipelines/base.py953-967 |
postprocess() | Convert model outputs to user-friendly format | Decoding, score calculation src/transformers/pipelines/base.py969-983 |
Sources: src/transformers/pipelines/base.py932-983 src/transformers/pipelines/text_generation.py128-146
The pipeline() function is the primary entry point for creating pipelines. It handles task resolution, model loading, and component initialization.
The SUPPORTED_TASKS dictionary src/transformers/pipelines/__init__.py141-184 maps task identifiers to pipeline configurations.
| Key | Value Fields | Example |
|---|---|---|
| Task identifier | impl: Pipeline class | AutomaticSpeechRecognitionPipeline src/transformers/pipelines/__init__.py149 |
pt: Model classes tuple | (AutoModelForCTC, AutoModelForSpeechSeq2Seq) src/transformers/pipelines/__init__.py150 | |
default: Default model dict | {"model": ("facebook/wav2vec2-base-960h", "22aad52")} src/transformers/pipelines/__init__.py151 | |
type: Input modality | "multimodal", "text", "audio" src/transformers/pipelines/__init__.py152 |
Sources: src/transformers/pipelines/__init__.py141-184
The pipeline loads components based on flags defined in the specific pipeline class.
Title: Natural Language Space to Pipeline Code Entity Mapping
Each task-specific pipeline sets these flags:
TextGenerationPipeline: _load_tokenizer = True src/transformers/pipelines/text_generation.py90ImageTextToTextPipeline: _load_processor = True src/transformers/pipelines/image_text_to_text.py115AutomaticSpeechRecognitionPipeline: _load_feature_extractor = True and _load_tokenizer = True src/transformers/pipelines/automatic_speech_recognition.py157-158Sources: src/transformers/pipelines/base.py773-777 src/transformers/pipelines/text_generation.py86-90 src/transformers/pipelines/automatic_speech_recognition.py155-158
TextGenerationPipeline src/transformers/pipelines/text_generation.py23 handles causal language model generation with support for text prompts and chat conversations.
Key Features:
_pipeline_calls_generate = True src/transformers/pipelines/text_generation.py86_default_generation_config with max_new_tokens=256, do_sample=True, and temperature=0.7 src/transformers/pipelines/text_generation.py93-97tokenizer.padding_side = "left" for decoder-only models to ensure correct batched generation src/transformers/pipelines/text_generation.py105-106continue_final_message logic src/transformers/pipelines/text_generation.py140Sources: src/transformers/pipelines/text_generation.py23-146
AutomaticSpeechRecognitionPipeline src/transformers/pipelines/automatic_speech_recognition.py112 extracts text from audio waveforms or files.
Key Features:
ChunkPipeline src/transformers/pipelines/automatic_speech_recognition.py112 to support processing long audio files via windowed chunking.chunk_iter src/transformers/pipelines/automatic_speech_recognition.py61-85 to yield overlapping audio segments.WhisperTimeStampLogitsProcessor src/transformers/models/whisper/generation_whisper.py32 and dynamic time warping for timestamps src/transformers/models/whisper/generation_whisper.py64-115Sources: src/transformers/pipelines/automatic_speech_recognition.py112-174 src/transformers/models/whisper/generation_whisper.py64-115
Pipelines support efficient batching via DataLoader and custom collate functions.
Title: Batching Data Flow to Code Entities
Collation Functions:
pad_collate_fn() src/transformers/pipelines/base.py125-185: Pads sequences to the same length within a batch. It handles input_ids, pixel_values, and input_features with appropriate padding values src/transformers/pipelines/base.py167-181_pad() src/transformers/pipelines/base.py87-122: Core padding logic handling 1D, 2D, and 4D tensors (like mel spectrograms) src/transformers/pipelines/base.py100-102Sources: src/transformers/pipelines/base.py87-185
Pipelines handle device placement automatically in __init__ src/transformers/pipelines/base.py815-859
device_map for multi-GPU setups src/transformers/pipelines/base.py829is_torch_npu_available src/transformers/pipelines/base.py51 is_torch_mps_available src/transformers/pipelines/base.py49 or is_torch_xpu_available src/transformers/pipelines/base.py52For generation tasks, pipelines integrate with the library's Cache system.
DynamicCache is used by default for growing sequences docs/source/en/kv_cache.md35StaticCache can be enabled via cache_implementation="static" to support torch.compile optimizations docs/source/en/kv_cache.md72-86Sources: src/transformers/pipelines/base.py815-859 docs/source/en/kv_cache.md33-89
Refresh this wiki
This wiki was recently refreshed. Please wait 7 days to refresh again.