Skip to main content
Use Transformers for simple inference without extra dependencies, research and experimentation, or integration with the Hugging Face ecosystem.
Transformers provides the most flexibility for model development and is ideal for users who want direct access to model internals. For production deployments with high throughput, consider using vLLM.

Installation

We use uv to manage packages across all our code examples. It’s backwards compatible with pip.
Install the required dependencies:
GPU is recommended for faster inference.

Basic Usage

The Transformers library provides two interfaces for text generation: generate() for fine-grained control and pipeline() for simplicity. We use generate() here for direct access to model internals and explicit control over the generation process:
Model loading notes:
  • model_id: Can be a Hugging Face model ID (e.g., "LiquidAI/LFM2.5-1.2B-Instruct") or a local path
  • device_map="auto": Automatically distributes across available GPUs/CPU (requires accelerate). Use device="cuda" for single GPU or device="cpu" for CPU only
  • dtype="bfloat16": Recommended for modern GPUs. Use "auto" for automatic selection, or "float32" (slower, more memory)
The pipeline() interface provides a simpler API for text generation with automatic chat template handling. It wraps model loading and tokenization, making it ideal for quick prototyping.
Key parameters:
  • "text-generation": Task type for the pipeline
  • model_name_or_path: Model ID (e.g., "LiquidAI/LFM2.5-1.2B-Instruct") or local path (download locally with hf download --local-dir ./LFM2.5-1.2B-Instruct LiquidAI/LFM2.5-1.2B-Instruct)
  • dtype="auto": Automatically selects optimal dtype (bfloat16 on modern devices). Can use "bfloat16" explicitly or "float32" (slower, more memory)
  • device_map="auto": Automatically distributes across available GPUs/CPU (requires accelerate). Alternative: device="cuda" for single GPU, device="cpu" for CPU only. Don’t mix device_map and device
The pipeline automatically handles chat templates and tokenization, returning structured output with the generated text.

Generation Parameters

Control text generation behavior using GenerationConfig. Key parameters:
  • do_sample (bool): Enable sampling (True) or greedy decoding (False, default)
  • temperature (float, default 1.0): Controls randomness (0.0 = deterministic, higher = more random). Typical range: 0.1-2.0
  • top_p (float, default 1.0): Nucleus sampling - limits to tokens with cumulative probability ≤ top_p. Typical range: 0.1-1.0
  • top_k (int, default 50): Limits to top-k most probable tokens. Typical range: 1-100
  • min_p (float): Minimum token probability threshold. Typical range: 0.01-0.2
  • max_new_tokens (int): Maximum number of tokens to generate (preferred over max_length)
  • repetition_penalty (float, default 1.0): Penalty for repeating tokens (>1.0 = discourage repetition). Typical range: 1.0-1.5
  • stop_strings (str or list[str]): Strings that terminate generation when encountered
Use GenerationConfig to organize parameters:
For a complete list of parameters, see the GenerationConfig documentation.

Streaming Generation

Stream responses as they’re generated using TextStreamer:

Batch Generation

Process multiple prompts in a single batch for efficiency. See the batching documentation for more details:
Batching is not automatically a win for performance. For high-performance batching with optimized throughput, consider using vLLM.

Vision Models

LFM2-VL models support both text and images as input. Use generate() with the vision model and processor:

FAQ

You may find distributed inference with Transformers is not as fast as you would imagine. Transformers with device_map="auto" does not apply tensor parallelism, and it only uses one GPU at a time. For Transformers with tensor parallelism, please refer to its documentation.