BERT-base uncased model fine-tuned on SQuAD v1
This model was created using the
nn_pruning python library: the
linear layers contains 15.0% of the original weights.
The model contains 34.0% of the original weights overall (the embeddings account for a significant part of the model, and they are not pruned by this method).
With a simple resizing of the linear matrices it ran 2.32x as fast as bert-base-uncased on the evaluation.
This is possible because the pruning method lead to structured matrices: to visualize them, hover below on the plot to see the non-zero/zero parts of each matrix.
In terms of accuracy, its F1 is 86.64, compared with 88.5 for bert-base-uncased, a F1 drop of 1.86.
Fine-Pruning details
This model was fine-tuned from the HuggingFace
model checkpoint on
SQuAD1.1, and distilled from the model
bert-large-uncased-whole-word-masking-finetuned-squad
This model is case-insensitive: it does not make a difference between english and English.
A side-effect of the block pruning is that some of the attention heads are completely removed: 63 heads were removed on a total of 144 (43.8%).
Here is a detailed view on how the remaining heads are distributed in the network after pruning.
Details of the SQuAD1.1 dataset
| Dataset | Split | # samples |
|---|
| SQuAD1.1 | train | 90.6K |
| SQuAD1.1 | eval | 11.1k |
Fine-tuning
-
Python: 3.8.5
-
Machine specs:
1Memory: 64 GiB
2GPUs: 1 GeForce GTX 3090, with 24GiB memory
3GPU driver: 455.23.05, CUDA: 11.1
Results
Pytorch model file size: 368MB (original BERT: 420MB)
| Metric | # Value | # Original (Table 2) | Variation |
|---|
| EM | 78.77 | 80.8 | -2.03 |
| F1 | 86.64 | 88.5 | -1.86 |
Example Usage
Install nn_pruning: it contains the optimization script, which just pack the linear layers into smaller ones by removing empty rows/columns.
pip install nn_pruning
Then you can use the transformers library almost as usual: you just have to call optimize_model when the pipeline has loaded.
1from transformers import pipeline
2from nn_pruning.inference_model_patcher import optimize_model
3
4qa_pipeline = pipeline(
5 "question-answering",
6 model="madlag/bert-base-uncased-squadv1-x2.32-f86.6-d15-hybrid-v1",
7 tokenizer="madlag/bert-base-uncased-squadv1-x2.32-f86.6-d15-hybrid-v1"
8)
9
10print("bert-base-uncased parameters: 165.0M")
11print(f"Parameters count (includes only head pruning, not feed forward pruning)={int(qa_pipeline.model.num_parameters() / 1E6)}M")
12qa_pipeline.model = optimize_model(qa_pipeline.model, "dense")
13
14print(f"Parameters count after complete optimization={int(qa_pipeline.model.num_parameters() / 1E6)}M")
15predictions = qa_pipeline({
16 'context': "Frédéric François Chopin, born Fryderyk Franciszek Chopin (1 March 1810 – 17 October 1849), was a Polish composer and virtuoso pianist of the Romantic era who wrote primarily for solo piano.",
17 'question': "Who is Frederic Chopin?",
18})
19print("Predictions", predictions)