
The standard Python library for loading and running open models.
GitHub Stars
166.6K
Contributors
4.1K
PyPI / Month
92.2M
1%Hugging Face Transformers is a Python library maintained by Hugging Face that provides a unified API for loading, running, and fine-tuning tens of thousands of open models. Released in 2018 under the Apache 2.0 license, it is the most widely adopted library in the open model ecosystem. If you want to load a model from the Hugging Face hub and run inference in a few lines of Python, this is the default starting point.
Transformers occupies the category of a general-purpose inference library, not a dedicated serving engine. It is designed for flexibility and breadth of model support, not for maximum throughput or production serving optimization out of the box. Teams use it for prototyping, research, notebook-based workflows, and custom inference pipelines where control matters more than raw tokens per second. It competes with alternatives like vLLM, SGLang, or Ollama when serving becomes the primary concern, but it underpins most of the open model ecosystem and serves as the reference implementation for model compatibility.
Popularity signals confirm its dominance: over 161,000 GitHub stars, 4,000 contributors, and 157 million monthly PyPI downloads. No other model-loading library comes close to that adoption.
You run models with Transformers through its Python API. The core abstraction is the Pipeline class, which wraps the model and tokenizer into a single callable for tasks like text generation, classification, summarization, or image segmentation. Alternatively, you can load a raw AutoModel or AutoModelForCausalLM and write custom generation logic with the .generate() method.
Loading a model requires calling from_pretrained() with a model identifier from the hub (e.g., "meta-llama/Llama-3.2-1B") or a local path. The library auto-downloads weights, config, and tokenizer files, caching them locally. You specify the device with device_map or device="cuda". Inference then runs in a Python script, a Jupyter notebook, or wrapped in a simple web server (e.g., FastAPI). There is no built-in HTTP server, no OpenAI-compatible API endpoint, and no continuous batching. Exposing a model as a REST endpoint requires additional code or a wrapper like Hugging Face TGI, which itself uses Transformers under the hood.
For multi-GPU or large model support, you can use device_map="auto" to shard layers across available GPUs or leverage accelerate for parallel execution. The library manages KV cache internally during autoregressive generation, but does not implement paged attention or memory-efficient attention by default (those require installing flash-attn separately).
Transformers runs on NVIDIA GPUs (CUDA), AMD GPUs (ROCm), Apple Silicon (MPS backend), and plain CPUs. This breadth of hardware support is unmatched: the same API works across all targets, making it ideal for development environments where hardware varies.
Performance, however, varies significantly by setup. On a single NVIDIA GPU, Transformers can match or exceed other libraries for small models (under 7B parameters) when using FlashAttention 2, quantized models (bitsandbytes), and optimized generation parameters. For larger models, the lack of paged attention and continuous batching becomes a bottleneck. Throughput for text generation is lower than vLLM or TensorRT-LLM because Transformers allocates KV cache per request and recomputes it on context shifts, causing higher memory overhead and lower hardware utilization.
CPU inference is viable for smaller models (up to about 3B parameters) using quantization and the Intel Extension for PyTorch (via Optimum Intel). Apple Silicon performance is functional but not tuned for high throughput; it uses the MPS backend, which lags behind CUDA in kernel support.
Key hardware capabilities:
pip install flash-attn), bitsandbytes quantization (4-bit, 8-bit), and multi-GPU sharding with device_map="auto".accelerate; manual pipeline parallelism or tensor parallelism is not built-in..generate() method supports streaming via a callback (Streamer), allowing token-by-token output.The Pipeline API abstracts away model selection, tokenization, and device placement. A single line like classifier = pipeline("text-classification", model="roberta-large") gives you a callable for batched classification. This is the fastest path from idea to running inference, and it supports tasks ranging from text generation to image segmentation, zero-shot classification, and question answering.
The Hugging Face hub hosts hundreds of thousands of models, checkpoints, and datasets. Transformers loads any model that follows the library’s standardized configuration format. New models from providers like Meta, Google, Microsoft, and Mistral appear on the hub within days of release, often with ready-to-use Transformers code.
As covered above, the same from_pretrained() call works across CPU, CUDA, ROCm, Apple Silicon, and even Intel Gaudi (via Optimum). No code changes are needed to switch hardware; only the environment changes.
Transformers integrates with bitsandbytes for 4-bit and 8-bit quantization, enabling models like Llama 3.1 70B to run on a single 24 GB GPU. Loading is as simple as model = AutoModelForCausalLM.from_pretrained("...", load_in_4bit=True). This is critical for fitting large models into limited VRAM.
Because Transformers exposes the full PyTorch model graph, you can modify generation loops, implement custom sampling strategies, or add middleware for prompt processing. This flexibility is valuable for research and specialized applications where black-box servers are insufficient.
One-shot Python scripts and notebooks – Transformers is the default tool for exploring a new model. Researchers, data scientists, and engineers use it to quickly load a model, test prompts, and iterate on hyperparameters.
Prototyping and rapid experimentation – When evaluating which open model to use for a task, teams spin up a Transformers-based script to compare outputs across models with minimal setup.
Custom inference pipelines – For applications that require fine-grained control over generation (e.g., constrained decoding, multi-turn conversation with custom state), Transformers gives full access to the model internals.
Fine-tuning and training – While this description focuses on inference, Transformers is also the standard library for fine-tuning open models. Many teams use the same codebase for both training and inference, reducing context switching.
Local development – Developers run Transformers on their laptops (CPU or Apple Silicon) to test features before deploying to GPU servers. The library’s ability to load quantized models into small memory footprints makes this practical.
When not to use Transformers – For production serving requiring high throughput, low latency, or efficient batching, Transformers is not the right choice. Replacing it with vLLM, SGLang, or TGI will yield 2-10x throughput improvements at the same hardware cost. Transformers also lacks an OpenAI-compatible API out of the box, making integration with existing clients more work.
Install the library:
1pip install transformers
For GPU support, ensure PyTorch is installed with CUDA. For quantization, also install:
1pip install bitsandbytes accelerate
The shortest path to a running model:
1from transformers import pipeline23pipe = pipeline("text-generation", model="microsoft/Phi-3-mini-4k-instruct")4output = pipe("What is the capital of France?", max_new_tokens=50)5print(output[0]["generated_text"])
This works on CPU immediately. To use a GPU, set the device parameter or rely on device_map="auto" if accelerate is installed.
For custom generation without pipelines:
1from transformers import AutoModelForCausalLM, AutoTokenizer23model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf", device_map="auto")4tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")56inputs = tokenizer("Hello, how are you?", return_tensors="pt").to("cuda")7outputs = model.generate(**inputs, max_new_tokens=50)8print(tokenizer.decode(outputs[0]))
Documentation is at [huggingface.co/docs/transformers](https://huggingface.co/docs/transformers). The community is large and responsive on GitHub, the Hugging Face forum, and Discord.
vLLM is a dedicated serving engine optimized for high-throughput inference. It implements PagedAttention, continuous batching, and an OpenAI-compatible API. Transformers is the model library vLLM itself uses to load weights, but vLLM replaces the generation runtime. If you need to serve a model at scale (more than a few concurrent users), choose vLLM over Transformers. If you are prototyping, running batch inference in a notebook, or need to load a model that vLLM does not yet support (e.g., a vision-language model), Transformers is the practical choice.
Ollama wraps several inference engines (including llama.cpp and Transformers) into a simple CLI and server with OpenAI-compatible API. It sacrifices control for ease of use. Transformers gives you fine-grained access to model internals, custom sampling, and the ability to integrate with training workflows. Ollama is better for users who want a ready-to-use chat server with no code. Transformers is better for engineers who need to build custom logic or debug model behavior.
Even for production, many teams use Transformers as the reference implementation for testing: they write and verify inference logic in Transformers, then deploy with vLLM or TGI. Its ubiquity, documentation, and model coverage make it the lingua franca of open model inference. For any use case that prioritizes flexibility and model breadth over raw throughput, Transformers is the correct starting point.
What the engine gives you out of the box, in plain language.
A one-line helper to run common tasks like generation or classification.
Load from hundreds of thousands of models on the Hugging Face hub.
Run on CPU, CUDA, ROCm, or Apple Silicon with the same API.
The jobs this engine is best suited for.
Load a model and generate text in a notebook or short script.
Try new models the day they land on the hub.
Build bespoke generation logic when packaged servers do not fit.

Side-by-Side
Add a second or third engine and see stars, downloads, and capabilities lined up next to each other.
Close alternatives worth a look before you decide.
High-throughput GPU serving with an OpenAI-compatible API out of the box.
vLLM is the go-to engine for serving open models on NVIDIA and AMD GPUs at scale. Its PagedAttention memory trick and continuous batching push far more requests through a GPU than a naive setup, and it speaks the OpenAI API so most apps work without code changes.
High-throughput GPU serving
pip install vllmStars
92.6K
PyPI / mo
2.0M
Run open models locally with a single command.
Ollama is the easiest way to run open models on your own machine. One command pulls a model and starts a local server with an OpenAI-compatible API. It works on Mac, Windows, and Linux, and handles the messy parts of downloading and quantizing models for you.
One-line local model running
curl -fsSL https://ollama.com/install.sh | shStars
181.7K
PyPI / mo
—
Fine-tune open models faster and on less GPU memory.
Unsloth makes fine-tuning open models dramatically faster while using far less GPU memory. It rewrites the heavy parts of training to be more efficient, so you can adapt a model to your data on a single consumer or cloud GPU instead of a cluster.
Fast, low-memory fine-tuning
pip install unslothStars
76.8K
PyPI / mo
901.3K
Fine-tune over a hundred open models, with a UI or the command line.
LLaMA-Factory is a broad fine-tuning toolkit that supports a wide range of open models and methods. It offers both a command line and a web UI, so you can train without writing code. It covers everything from LoRA to full fine-tuning and preference tuning in one place.
Broad model support with a training UI
pip install llamafactoryStars
75.0K
PyPI / mo
17.9K
Program and optimize LLM pipelines instead of hand-tuning prompts.
DSPy from Stanford lets you build LLM pipelines as code, then optimize them automatically. Instead of tweaking prompts by hand, you define the task and let DSPy search for the prompts and examples that work best. It is widely used for building and improving agents.
Optimizing prompts and agent pipelines
pip install dspyStars
38.3K
PyPI / mo
5.1M
Fast serving engine tuned for structured output and complex prompting.
SGLang is a high-performance serving engine that competes with vLLM on throughput while adding strong support for structured output and reusable prompt prefixes. Popular with teams running agents and pipelines that reuse the same context across many calls.
High-throughput serving with structured output
pip install "sglang[all]"Stars
36.4K
PyPI / mo
12.2M
An inference engine is the software that runs a language model and turns your prompt into tokens. It loads the model weights, manages memory on your GPU or CPU, and serves the output, usually behind an API.
Hugging Face Transformers ships under the Apache 2.0 license. The source code lives on GitHub, so you can read it, fork it, and run it on your own hardware if your team prefers self-hosting.
Hugging Face Transformers is primarily a Python project. The implementation language matters less than the hardware it supports and the throughput it delivers, but it does affect how easily your team can extend or debug it.