Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Huggingface Transformers Pipeline Function

From Leeroopedia
Knowledge Sources
Domains NLP, Inference
Last Updated 2026-02-13 00:00 GMT

Overview

Concrete tool for constructing task-specific inference pipelines provided by the HuggingFace Transformers library.

Description

The pipeline() function is the primary factory method for creating inference pipelines. It accepts a task name (e.g., "text-generation", "sentiment-analysis") and optionally a model identifier, then automatically resolves and loads the appropriate model class, tokenizer, image processor, feature extractor, or processor. The function returns a callable Pipeline subclass instance that encapsulates the full inference workflow: preprocessing, model forward pass, and postprocessing.

Key behaviors include:

  • Task-to-class resolution: Looks up the task string in the SUPPORTED_TASKS registry to determine which pipeline subclass and model auto-class to use.
  • Automatic component loading: If the user does not provide a tokenizer, feature extractor, image processor, or processor, the function infers and loads them from the model's configuration.
  • PEFT adapter support: Detects adapter configuration files and loads the base model with the adapter applied.
  • Device and precision management: Supports explicit device assignment, device_map="auto" for multi-GPU distribution, and dtype for precision control.
  • Custom pipeline support: Models with custom_pipelines in their configuration can define and load custom pipeline classes when trust_remote_code=True.

Usage

Use pipeline() when you need a high-level, ready-to-use inference interface without manually wiring together model, tokenizer, and postprocessing logic. It is the recommended entry point for most inference tasks in the Transformers library.

Code Reference

Source Location

  • Repository: transformers
  • File: src/transformers/pipelines/__init__.py (lines 472-1028)

Signature

def pipeline(
    task: str | None = None,
    model: str | PreTrainedModel | None = None,
    config: str | PreTrainedConfig | None = None,
    tokenizer: str | PreTrainedTokenizer | PreTrainedTokenizerFast | None = None,
    feature_extractor: str | PreTrainedFeatureExtractor | None = None,
    image_processor: str | BaseImageProcessor | None = None,
    processor: str | ProcessorMixin | None = None,
    revision: str | None = None,
    use_fast: bool = True,
    token: str | bool | None = None,
    device: int | str | torch.device | None = None,
    device_map: str | dict[str, int | str] | None = None,
    dtype: str | torch.dtype | None = "auto",
    trust_remote_code: bool | None = None,
    model_kwargs: dict[str, Any] | None = None,
    pipeline_class: Any | None = None,
    **kwargs: Any,
) -> Pipeline:

Import

from transformers import pipeline

I/O Contract

Inputs

Name Type Required Description
task str No (if model given) The task defining which pipeline will be returned (e.g., "text-generation", "sentiment-analysis", "question-answering").
model str or PreTrainedModel No (if task given) Model identifier on the Hub or a pre-instantiated model object.
config str or PreTrainedConfig No Model configuration. Inferred from model if not provided.
tokenizer str or PreTrainedTokenizer No Tokenizer for encoding text inputs. Inferred from model if not provided.
feature_extractor str or PreTrainedFeatureExtractor No Feature extractor for non-NLP models (speech, audio).
image_processor str or BaseImageProcessor No Image processor for vision models.
processor str or ProcessorMixin No Multi-modal processor combining tokenizer and image/audio processing.
revision str No Git revision (branch, tag, or commit hash) for Hub model. Defaults to "main".
use_fast bool No Whether to use a fast (Rust-backed) tokenizer. Defaults to True.
token str or bool No Authentication token for private models on the Hub.
device int, str, or torch.device No Device for pipeline allocation (e.g., "cpu", "cuda:0", "mps").
device_map str or dict No Device map for multi-GPU distribution. Use "auto" for automatic placement.
dtype str or torch.dtype No Model precision. Defaults to "auto".
trust_remote_code bool No Allow execution of custom code from Hub repositories. Defaults to False.
model_kwargs dict No Additional keyword arguments passed to from_pretrained().
pipeline_class type No Override the auto-detected pipeline class.

Outputs

Name Type Description
pipeline Pipeline A callable pipeline instance for the specified task. Calling it with inputs returns task-specific predictions.

Usage Examples

Basic Usage

from transformers import pipeline

# Create a text generation pipeline with default model
generator = pipeline("text-generation")
result = generator("Once upon a time", max_new_tokens=50)
print(result[0]["generated_text"])

Specifying a Model and Device

from transformers import pipeline

generator = pipeline(
    "text-generation",
    model="meta-llama/Llama-2-7b-hf",
    device_map="auto",
    dtype="float16",
    token="hf_...",
)
result = generator("The meaning of life is", max_new_tokens=100)

Sentiment Analysis

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
result = classifier("I love using HuggingFace Transformers!")
# [{'label': 'POSITIVE', 'score': 0.9998}]

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment