Transformers: a model-definition framework

Transformers is the model-definition framework for machine learning, for both inference and training.

Verified

On this page

What it is

Transformers acts as the model-definition framework for machine learning. The framework covers vision, audio, video, and multimodal models, for both inference and training. Transformers centralizes the model definition so that this definition is agreed upon across the ecosystem.

The adjacent modeling libraries llama.cpp and mlx leverage the model definition from transformers. The project README reports over 1M usable Transformers model checkpoints on the Hugging Face Hub.

Core facts

Read by a script from the official API, not rewritten by a model.

License
Apache-2.0
Languages
Python
Maintainer
huggingface
Verified

Main features

  1. Pipeline is a high-level inference class that supports text, audio, vision, and multimodal tasks.

  2. Pipeline handles preprocessing the input and returns the appropriate output.

  3. In the text generation pipeline example, a pipeline is instantiated and a model is specified for text generation.

  4. In that text generation example, the model is downloaded and cached.

  5. When chatting with a model, the only difference is that a chat history between the user and the system must be constructed, and it is the input to Pipeline.

  6. The README says that model files can be used independently of the library for quick experiments.

  7. The README says the Transformers project provides examples for each architecture to reproduce results published by its original authors.

  8. One feature the README lists is few user-facing abstractions, with just 3 classes to learn.

  9. Among the README's example models, translation uses T5.

  10. Among the README's example models, summarization uses BART.

  11. Among the README's example models, text generation uses Llama.

  12. Among the README's example models, named entity recognition uses Gemma.

  13. Among the README's example models, image classification uses DINO v2.

  14. Among the README's example models, text to speech uses CSM.

Getting started

  1. Transformers works with Python 3.11 or newer and PyTorch 2.6 or newer.

  2. Create and activate a virtual environment with venv or uv.

  3. Then install Transformers in that virtual environment.

  4. Install Transformers from source if you want the latest changes in the library or are interested in contributing.

  5. However, the latest version may not be stable.

From the README
python
# venv
python -m venv .my-env
source .my-env/bin/activate
# uv
uv venv .my-env
source .my-env/bin/activate
From the README
python
# pip
pip install "transformers[torch]"

# uv
uv pip install "transformers[torch]"
From the README
Shell
git clone https://github.com/huggingface/transformers.git
cd transformers

# pip
pip install '.[torch]'

# uv
uv pip install '.[torch]'
From the README
python
from transformers import pipeline

pipeline = pipeline(task="text-generation", model="Qwen/Qwen2.5-1.5B")
pipeline("the secret to baking a really good cake is ")
[{'generated_text': 'the secret to baking a really good cake is 1) to use the right ingredients and 2) to follow the recipe exactly. the recipe for the cake is as follows: 1 cup of sugar, 1 cup of flour, 1 cup of milk, 1 cup of butter, 1 cup of eggs, 1 cup of chocolate chips. if you want to make 2 cakes, how much sugar do you need? To make 2 cakes, you will need 2 cups of sugar.'}]
From the README
python
import torch
from transformers import pipeline

chat = [
    {"role": "system", "content": "You are a sassy, wise-cracking robot as imagined by Hollywood circa 1986."},
    {"role": "user", "content": "Hey, can you tell me any fun things to do in New York?"}
]

pipeline = pipeline(task="text-generation", model="meta-llama/Meta-Llama-3-8B-Instruct", dtype=torch.bfloat16, device_map="auto")
response = pipeline(chat, max_new_tokens=512)
print(response[0]["generated_text"][-1]["content"])

Who it is for

Transformers is not a modular toolbox of building blocks for neural nets. The training API is optimized to work with PyTorch models provided by Transformers. For generic machine learning loops, the README says to use another library such as Accelerate.

Sources

This page is written from the official sources below. Open them to check the original text.

Verified ·

This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.