Bert-squeeze is a repository aiming to provide code to reduce the size of Transformer-based models or decrease their latency at inference time.
It gathers a non-exhaustive list of techniques such as distillation, pruning, quantization, early-exiting. The repo is written using PyTorch Lightning and Transformers.
As a heavy user of transformer-based models (which are truly amazing from my point of view) I always struggled to put
those heavy models in production while having a decent inference speed. There are of course a bunch of existing
libraries to optimize and compress transformer-based models (ONNX
, distiller, compressors
, KD_Lib, ... ).
I started this project because of the need to reduce the latency of models integrating transformers as subcomponents.
For this reason, this project aims at providing implementations to train various transformer-based models (and others)
using PyTorch Lightning but also to distill, prune, and quantize models.
I chose to write this repo with Lightning because of its growing trend, its flexibility, and the very few repositories
using it. It currently only handles sequence classification models, but support for other tasks and custom architectures
is planned.
First download the repository:
git clone https://github.com/JulesBelveze/bert-squeeze.git
and then install dependencies using uv:
uv venv
source .venv/bin/active
uv sync
You are all set!
You can find a bunch of examples on how to use the library to simply train models or perform optimization techniques (distillation, pruning, quantization) in the docs.
Disclaimer: I have not extensively tested all procedures and thus do not guarantee the performance of every implemented method.
If you never heard of it then I can only recommend you to read this amazing blog post and if you want to dig deeper there is this awesome lecture was given by Stanford available here.
The idea of distillation is to train a small network to mimic a big network by trying to replicate its outputs. The
repository provides the ability to transfer knowledge from any model to any other (if you need a model that is not
within the models
folder just write your own).
The repository also provides the possibility to perform soft-distillation or hard-distillation on an unlabeled dataset. In the soft case, we use the probabilities of the teacher as a target. In the hard one, we assume that the teacher's predictions are the actual label.
You can find these implementations under the distillation/
folder.
Neural network quantization is the process of reducing the weights precision in the neural network. The repo has two callbacks one for dynamic quantization and one for quantization-aware training (using the Lightning callback) .
You can find those implementations under the utils/callbacks/
folder.
Pruning neural networks consist of removing weights from trained models to compress them. This repo features various pruning implementations and methods such as head-pruning, layer dropping, and weights dropping.
You can find those implementations under the utils/callbacks/
folder.
If you are missing a feature that could be relevant to this repo, or a bug that you noticed feel free to open a PR or open an issue. As you can see in the roadmap there are a bunch more features to come 😃
Also, if you have any questions or suggestions feel free to ask!
- Alammar, J (2018). The Illustrated Transformer [Blog post]. Retrieved from https://jalammar.github.io/illustrated-transformer/
- stanfordonline (2021) Stanford CS224N NLP with Deep Learning | Winter 2021 | Lecture 9 - Self- Attention and Transformers. [online video] Available at: https://www.youtube.com/watch?v=ptuGllU5SQQ
- 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 Jamie Brew (2019). HuggingFace's Transformers: State-of-the-art Natural Language Processing
- Hassan Sajjad and Fahim Dalvi and Nadir Durrani and Preslav Nakov (2020). Poor Man's BERT Smaller and Faster Transformer Models
- Angela Fan and Edouard Grave and Armand Joulin (2019). Reducing Transformer Depth on Demand with Structured Dropout
- Paul Michel and Omer Levy and Graham Neubig (2019). Are Sixteen Heads Really Better than One?
- Fangxiaoyu Feng and Yinfei Yang and Daniel Cer and Naveen Arivazhagan and Wei Wang (2020). Language-agnostic BERT Sentence Embedding
- Weijie Liu and Peng Zhou and Zhe Zhao and Zhiruo Wang and Haotang Deng and Qi Ju (2020). FastBERT: a
Self-distilling {BERT} with Adaptive Inference Time.
Repository: https://github.com/BitVoyage/FastBERT - Xu, Canwen and Zhou, Wangchunshu and Ge, Tao and Wei, Furu and Zhou, Ming (2020). {BERT}-of-Theseus: Compressing {BERT} by Progressive Module Replacing