MCPcopy Create free account
hub / github.com/huggingface/transformers

github.com/huggingface/transformers

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.0.0 ↗ · + Follow · compare 6 versions
5,062 symbols 19,251 edges 327 files ⚖ Apache-2.0 1,395 documented · 28% updated todayv5.14.1 · 2026-07-16★ 163,271893 open issues

Browse by type

Functions 4,119 Types & classes 939 Endpoints 4
What it actually does AI analysis from the code graph — generated when you open this
loading…
README
<img src="https://raw.githubusercontent.com/huggingface/transformers/master/docs/source/imgs/transformers_logo_name.png" width="400"/>









<a href="https://circleci.com/gh/huggingface/transformers">
    <img alt="Build" src="https://img.shields.io/circleci/build/github/huggingface/transformers/master">
</a>
<a href="https://github.com/huggingface/transformers/blob/master/LICENSE">
    <img alt="GitHub" src="https://img.shields.io/github/license/huggingface/transformers.svg?color=blue">
</a>
<a href="https://huggingface.co/transformers/index.html">
    <img alt="Documentation" src="https://img.shields.io/website/http/huggingface.co/transformers/index.html.svg?down_color=red&down_message=offline&up_message=online">
</a>
<a href="https://github.com/huggingface/transformers/releases">
    <img alt="GitHub release" src="https://img.shields.io/github/release/huggingface/transformers.svg">
</a>

State-of-the-art Natural Language Processing for PyTorch and TensorFlow 2.0

🤗 Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides state-of-the-art general-purpose architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet, T5, CTRL...) for Natural Language Understanding (NLU) and Natural Language Generation (NLG) with over thousands of pretrained models in 100+ languages and deep interoperability between PyTorch & TensorFlow 2.0.

Recent contributors

Features

  • High performance on NLU and NLG tasks
  • Low barrier to entry for educators and practitioners

State-of-the-art NLP for everyone - Deep learning researchers - Hands-on practitioners - AI/ML/NLP teachers and educators

Lower compute costs, smaller carbon footprint - Researchers can share trained models instead of always retraining - Practitioners can reduce compute time and production costs - Dozens of architectures with over 1,000 pretrained models, some in more than 100 languages

Choose the right framework for every part of a model's lifetime - Train state-of-the-art models in 3 lines of code - Deep interoperability between TensorFlow 2.0 and PyTorch models - Move a single model between TF2.0/PyTorch frameworks at will - Seamlessly pick the right framework for training, evaluation, production

Section Description
Installation How to install the package
Model architectures Architectures (with pretrained weights)
Online demo Experimenting with this repo’s text generation capabilities
Quick tour: Usage Tokenizers & models usage: Bert and GPT-2
Quick tour: TF 2.0 and PyTorch Train a TF 2.0 model in 10 lines of code, load it in PyTorch
Quick tour: pipelines Using Pipelines: Wrapper around tokenizer and models to use finetuned models
Quick tour: Fine-tuning/usage scripts Using provided scripts: GLUE, SQuAD and Text generation
Quick tour: Share your models Upload and share your fine-tuned models with the community
Migrating from pytorch-transformers to transformers Migrating your code from pytorch-transformers to transformers
Migrating from pytorch-pretrained-bert to pytorch-transformers Migrating your code from pytorch-pretrained-bert to transformers
Documentation Full API documentation and more

Installation

This repo is tested on Python 3.6+, PyTorch 1.0.0+ (PyTorch 1.3.1+ for examples) and TensorFlow 2.0.

You should install 🤗 Transformers in a virtual environment. If you're unfamiliar with Python virtual environments, check out the user guide.

Create a virtual environment with the version of Python you're going to use and activate it.

Now, if you want to use 🤗 Transformers, you can install it with pip. If you'd like to play with the examples, you must install it from source.

With pip

First you need to install one of, or both, TensorFlow 2.0 and PyTorch. Please refer to TensorFlow installation page and/or PyTorch installation page regarding the specific install command for your platform.

When TensorFlow 2.0 and/or PyTorch has been installed, 🤗 Transformers can be installed using pip as follows:

pip install transformers

From source

Here also, you first need to install one of, or both, TensorFlow 2.0 and PyTorch. Please refer to TensorFlow installation page and/or PyTorch installation page regarding the specific install command for your platform.

When TensorFlow 2.0 and/or PyTorch has been installed, you can install from source by cloning the repository and running:

git clone https://github.com/huggingface/transformers
cd transformers
pip install .

When you update the repository, you should upgrade the transformers installation and its dependencies as follows:

git pull
pip install --upgrade .

Run the examples

Examples are included in the repository but are not shipped with the library.

Therefore, in order to run the latest versions of the examples, you need to install from source, as described above.

Look at the README for how to run examples.

Tests

A series of tests are included for the library and for some example scripts. Library tests can be found in the tests folder and examples tests in the examples folder.

Depending on which framework is installed (TensorFlow 2.0 and/or PyTorch), the irrelevant tests will be skipped. Ensure that both frameworks are installed if you want to execute all tests.

Here's the easiest way to run tests for the library:

pip install -e ".[testing]"
make test

and for the examples:

pip install -e ".[testing]"
pip install -r examples/requirements.txt
make test-examples

For details, refer to the contributing guide.

Do you want to run a Transformer model on a mobile device?

You should check out our swift-coreml-transformers repo.

It contains a set of tools to convert PyTorch or TensorFlow 2.0 trained Transformer models (currently contains GPT-2, DistilGPT-2, BERT, and DistilBERT) to CoreML models that run on iOS devices.

At some point in the future, you'll be able to seamlessly move from pre-training or fine-tuning models to productizing them in CoreML, or prototype a model or an app in CoreML then research its hyperparameters or architecture from TensorFlow 2.0 and/or PyTorch. Super exciting!

Model architectures

🤗 Transformers currently provides the following NLU/NLG architectures:

  1. BERT (from Google) released with the paper BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova.
  2. GPT (from OpenAI) released with the paper Improving Language Understanding by Generative Pre-Training by Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever.
  3. GPT-2 (from OpenAI) released with the paper Language Models are Unsupervised Multitask Learners by Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei and Ilya Sutskever.
  4. Transformer-XL (from Google/CMU) released with the paper Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context by Zihang Dai, Zhilin Yang, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov.
  5. XLNet (from Google/CMU) released with the paper ​XLNet: Generalized Autoregressive Pretraining for Language Understanding by Zhilin Yang, Zihang Dai, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le.
  6. XLM (from Facebook) released together with the paper Cross-lingual Language Model Pretraining by Guillaume Lample and Alexis Conneau.
  7. RoBERTa (from Facebook), released together with the paper a Robustly Optimized BERT Pretraining Approach by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov.
  8. DistilBERT (from HuggingFace), released together with the paper DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into DistilGPT2, RoBERTa into DistilRoBERTa, Multilingual BERT into DistilmBERT and a German version of DistilBERT.
  9. CTRL (from Salesforce) released with the paper CTRL: A Conditional Transformer Language Model for Controllable Generation by Nitish Shirish Keskar, Bryan McCann, Lav R. Varshney, Caiming Xiong and Richard Socher.
  10. CamemBERT (from Inria/Facebook/Sorbonne) released with the paper CamemBERT: a Tasty French Language Model by Louis Martin, Benjamin Muller, Pedro Javier Ortiz Suárez*, Yoann Dupont, Laurent Romary, Éric Villemonte de la Clergerie, Djamé Seddah and Benoît Sagot.
  11. ALBERT (from Google Research and the Toyota Technological Institute at Chicago) released with the paper ALBERT: A Lite BERT for Self-supervised Learning of Language Representations, by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.
  12. T5 (from Google AI) released with the paper Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
  13. XLM-RoBERTa (from Facebook AI), released together with the paper Unsupervised Cross-lingual Representation Learning at Scale by Alexis Conneau, Kartikay Khandelwal, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmán, Edouard Grave, Myle Ott, Luke Zettlemoyer and Veselin Stoyanov.
  14. MMBT (from Facebook), released together with the paper a Supervised Multimodal Bitransformers for Classifying Images and Text by Douwe Kiela, Suvrat Bhooshan, Hamed Firooz, Davide Testuggine.
  15. FlauBERT (from CNRS) released with the paper FlauBERT: Unsupervised Language Model Pre-training for French by Hang Le, Loïc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Be

Core symbols most depended-on inside this repo

browse all functions →

Shape

Method 3,509
Class 939
Function 610
Route 4

Languages

Python100%
TypeScript1%

Modules by API surface

src/transformers/modeling_tf_mobilebert.py100 symbols
src/transformers/modeling_mobilebert.py96 symbols
src/transformers/modeling_bert.py85 symbols
src/transformers/modeling_tf_bert.py82 symbols
src/transformers/modeling_reformer.py82 symbols
src/transformers/tokenization_utils_base.py77 symbols
src/transformers/modeling_utils.py75 symbols
src/transformers/data/processors/glue.py72 symbols
src/transformers/pipelines.py67 symbols
src/transformers/modeling_tf_albert.py65 symbols
src/transformers/modeling_bart.py65 symbols
examples/seq2seq/bertabs/modeling_bertabs.py64 symbols

Dependencies from manifests, versioned

git-python1.0.3 · 1×
gitpython3.0.2 · 1×
h5py2.10.0 · 1×
knockknock0.1.8.1 · 1×
numpy1.18.2 · 1×
psutil5.6.6 · 1×
pytorch-lightning0.8.1 · 1×
scipy1.3.1 · 1×
tensorboard1.14.0 · 1×
tensorboardX1.8 · 1×
torch1.4.0 · 1×

For agents

$ claude mcp add transformers \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page