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 Optimum Pipeline Factory

From Leeroopedia

Overview

Factory function that creates hardware-accelerated inference pipelines with automatic backend selection and parameter routing.

Source

File: optimum/pipelines/__init__.py L49-288

Repository: optimum

Signature

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

Import

from optimum.pipelines import pipeline

I/O

Direction Type Description
Input: task Optional[str] Task string defining the pipeline type (e.g., "text-classification", "question-answering", "text-generation")
Input: model Optional[Union[str, PreTrainedModel]] Model identifier string or pre-loaded model instance
Input: accelerator Optional[str] Accelerator choice: "ort", "ov", "ipex", or None for auto-detection
Output transformers.Pipeline A standard transformers Pipeline backed by the selected accelerated backend

Auto-Detection Logic (L216-238)

When accelerator is None, the function auto-detects the best available backend:

if accelerator is None:
    if is_optimum_intel_available() and is_openvino_available():
        accelerator = "ov"
    elif is_optimum_onnx_available() and is_onnxruntime_available():
        accelerator = "ort"
    elif is_optimum_intel_available() and is_ipex_available():
        accelerator = "ipex"
    else:
        raise ImportError(
            "You need to install either `optimum-onnx[onnxruntime]` to use ONNX Runtime as an accelerator, "
            "or `optimum-intel[openvino]` to use OpenVINO as an accelerator, "
            "or `optimum-intel[ipex]` to use IPEX as an accelerator."
        )

Note that the auto-detection checks both the backend runtime and the corresponding Optimum integration package (e.g., both optimum-intel and openvino must be installed for OpenVINO to be selected).

Dispatch Logic (L240-288)

After determining the accelerator, the function dispatches to the backend-specific pipeline:

Accelerator Dispatch Target Lines
"ort" optimum.onnxruntime.pipeline L240-262
"ov" or "ipex" optimum.intel.pipeline L263-286
other Raises ValueError L287-288

Usage Example

from optimum.pipelines import pipeline

# Explicit ONNX Runtime accelerator
pipe = pipeline("text-classification", model="distilbert-base-uncased", accelerator="ort")
result = pipe("This movie is great!")

# Auto-detect best backend
pipe = pipeline("sentiment-analysis", model="distilbert-base-uncased")

# With pre-loaded model
from optimum.onnxruntime import ORTModelForTokenClassification
from transformers import AutoTokenizer

model = ORTModelForTokenClassification.from_pretrained("dbmdz/bert-large-cased-finetuned-conll03-english")
tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased")
recognizer = pipeline("ner", model=model, tokenizer=tokenizer)

Dependencies

from optimum.utils.import_utils import (
    is_ipex_available,
    is_onnxruntime_available,
    is_openvino_available,
    is_optimum_intel_available,
    is_optimum_onnx_available,
)

Related

Page Connections

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