Paper commentary
HuggingFace's Transformers: State-of-the-art Natural Language Processing
Transformers is an open-source library that packages pretrained NLP models behind one API
- Problem Once pretraining became widespread, users still had to distribute, fine-tune, deploy and compress each pretrained Transformer model themselves.
- Method Transformers splits each model into a tokenizer, a transformer and a task head behind one API. Because the head is separate, one pretrained core can be paired with different heads.
- Results The Model Hub now holds 2,097 user models shared by the community.
Why pretrained models are hard to reuse
Transformers have become the dominant architecture for NLP, beating convolutional and recurrent networks on both understanding and generation. Pretraining on generic text lets one model be adapted to many specific tasks. Once pretraining is widespread, the practical work shifts to distributing, fine-tuning, deploying and compressing the core models.
The authors argue this calls for systems that train, analyze, scale and augment these models on many platforms.
The authors answer with an open-source library and a centralized hub, where users compare models through the same minimal API. The library is released under the Apache 2.0 license and has over 400 external contributors.
Three parts make up every model
The key idea is to cut every model into three parts that can each be reused or replaced. The tokenizer converts raw text into sparse index encodings. The transformer turns those indices into contextual embeddings, and the head uses the embeddings to make a task-specific prediction. Because the head is separate, one pretrained core can be paired with different heads for different tasks.
Architectures differ in positional representations, masking, padding and whether they use a sequence-to-sequence design, but they share a multi-headed attention core. Each model's base class builds the computation from an embedding projection through the self-attention layers to the final encoder hidden states. Most models sit in a single file, which keeps them easy to extend.
Auto classes read the pretrained model's configuration and instantiate the right class, so switching models needs little change in code.
Tokenizers store the vocabulary as a token-to-index map and encode and decode text the way each model expects. Users can add tokens, special tokens and resize the vocabulary, and truncation accounts for the special tokens a model adds. Python-based tokenization is often too slow on very large datasets, so the most recent release uses a Rust tokenization library by default.
Heads are wrapper classes that add an output layer and an optional loss on top of the transformer's contextual embeddings. Their names follow a pattern such as XXXForSequenceClassification, where XXX is the model name. Some heads, such as conditional generation, also support sampling and beam search. For pretrained models, the heads used during pretraining are released as well, so users can continue with the pretraining objectives.
Sharing and loading models through the Model Hub
DistilBERT, which was developed for the library, is now widely downloaded by the community. The hub stores the model under a canonical name, so anyone can download, cache and run it in two lines of code. The FlauBERT example loads both parts with AutoTokenizer.from_pretrained and AutoModel.from_pretrained on flaubert/flaubert_base_uncased.
Each uploaded model gets a landing page and an optional model card that lists training details, datasets, a citation and known biases. Because the hub only holds transformer models, a model page can run live inference on user text. Model pages also link to tools such as exBERT for visualizing the model.
What three community groups built on the hub
The authors group the users into architects, trainers and end-users, following Strobelt et al. (2017). AllenAI trained SciBERT on PubMed data as a masked language model for biomedical extraction, then distributed it through the hub.
NYU used the Transformers API as a generic front-end for its Jiant framework and fine-tuned many models. That work led to research on the structure of BERT. Plot.ly needed document summarization without any training, so it searched the hub and found DistilBART. It ran and deployed that model directly from the hub with no research or ML expertise.
Paths from training to production
Models are available in both PyTorch and TensorFlow, and a model saved in one framework can be reloaded in the other. This lets users switch frameworks at any point in the model lifetime, from training to serving.
In PyTorch, TorchScript gives an intermediate form that runs efficiently from Python or in a C++ environment. Fine-tuned models can then be served through TorchServing, and TensorFlow offers its own serving options.
The library also exports models to ONNX, a standard interoperable format. In experiments run with the ONNX team on BERT, RoBERTa and GPT-2, ONNX reached nearly a 4x speedup. The text does not break that speedup down by model.
The team is also testing JAX/XLA and TVM as intermediate formats. For phones, models can be converted to CoreML weights with adapters and embedded in an iOS application. The authors note that similar methods can apply to Android, but they report no results for it.
Limitations
The report focuses on the core modeling specifications and leaves out training and development tools. Its only speed experiment is the ONNX run on BERT, RoBERTa and GPT-2.
The three community cases describe how the library was used, not how well the models performed on the users' tasks. The CoreML and Android paths are described without experiments.
Paper info
- Authors
- Thomas Wolf, Lysandre Debut, Victor Sanh, Julien Chaumond, Clement Delangue, Anthony Moi, Pierric Cistac, Tim Rault, Rémi Louf, Morgan Funtowicz, Joe Davison, Sam Shleifer, Patrick von Platen, Clara Ma, Yacine Jernite, Julien Plu, Canwen Xu, Teven Le Scao, Sylvain Gugger, Mariama Drame, Quentin Lhoest, Alexander M. Rush
- Venue
- arXiv · Preprint
- First published
- 2019-10-9
- Added
- 2026-11-11
- Updated
- 2020-7-14
- Version
- v5
- arXiv
- 1910.03771
- Category
- cs.CL
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Thomas Wolf, Lysandre Debut, Victor Sanh, Julien Chaumond, Clement Delangue, Anthony Moi, Pierric Cistac, Tim Rault, Rémi Louf, Morgan Funtowicz, Joe Davison, Sam Shleifer, Patrick von Platen, Clara Ma, Yacine Jernite, Julien Plu, Canwen Xu, Teven Le Scao, Sylvain Gugger, Mariama Drame et al. (2019). HuggingFace's Transformers: State-of-the-art Natural Language Processing. arXiv:1910.03771v5. https://arxiv.org/abs/1910.03771v5
BibTeX
@misc{wolf2019huggingface,
title = {{HuggingFace's Transformers: State-of-the-art Natural Language Processing}},
author = {Thomas Wolf and Lysandre Debut and Victor Sanh and Julien Chaumond and Clement Delangue and Anthony Moi and Pierric Cistac and Tim Rault and Rémi Louf and Morgan Funtowicz and Joe Davison and Sam Shleifer and Patrick von Platen and Clara Ma and Yacine Jernite and Julien Plu and Canwen Xu and Teven Le Scao and Sylvain Gugger and Mariama Drame and Quentin Lhoest and Alexander M. Rush},
year = {2019},
eprint = {1910.03771},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1910.03771v5}
}