pytorch tf bert question-answering en dataset:squad transformers license:mit
.gitattributes README.md config.json model_info.json pytorch_model.bin special_tokens_map.json tf_model.h5 tokenizer_config.json vocab.txt eval/eval_metrics.json eval/evaluate_timing.json eval/nbest_predictions.json.tgz eval/predictions.json eval/sparsity_report.json eval/speed_report.json model_card/density_info.js model_card/pruning_info.js model_card/images/layer_0_attention_output_dense.png model_card/images/layer_0_attention_self_key.png model_card/images/layer_0_attention_self_query.png model_card/images/layer_0_attention_self_value.png model_card/images/layer_0_intermediate_dense.png model_card/images/layer_0_output_dense.png model_card/images/layer_10_attention_output_dense.png model_card/images/layer_10_attention_self_key.png model_card/images/layer_10_attention_self_query.png model_card/images/layer_10_attention_self_value.png model_card/images/layer_10_intermediate_dense.png model_card/images/layer_10_output_dense.png model_card/images/layer_11_attention_output_dense.png model_card/images/layer_11_attention_self_key.png model_card/images/layer_11_attention_self_query.png model_card/images/layer_11_attention_self_value.png model_card/images/layer_11_intermediate_dense.png model_card/images/layer_11_output_dense.png model_card/images/layer_1_attention_output_dense.png model_card/images/layer_1_attention_self_key.png model_card/images/layer_1_attention_self_query.png model_card/images/layer_1_attention_self_value.png model_card/images/layer_1_intermediate_dense.png model_card/images/layer_1_output_dense.png model_card/images/layer_2_attention_output_dense.png model_card/images/layer_2_attention_self_key.png model_card/images/layer_2_attention_self_query.png model_card/images/layer_2_attention_self_value.png model_card/images/layer_2_intermediate_dense.png model_card/images/layer_2_output_dense.png model_card/images/layer_3_attention_output_dense.png model_card/images/layer_3_attention_self_key.png model_card/images/layer_3_attention_self_query.png model_card/images/layer_3_attention_self_value.png model_card/images/layer_3_intermediate_dense.png model_card/images/layer_3_output_dense.png model_card/images/layer_4_attention_output_dense.png model_card/images/layer_4_attention_self_key.png model_card/images/layer_4_attention_self_query.png model_card/images/layer_4_attention_self_value.png model_card/images/layer_4_intermediate_dense.png model_card/images/layer_4_output_dense.png model_card/images/layer_5_attention_output_dense.png model_card/images/layer_5_attention_self_key.png model_card/images/layer_5_attention_self_query.png model_card/images/layer_5_attention_self_value.png model_card/images/layer_5_intermediate_dense.png model_card/images/layer_5_output_dense.png model_card/images/layer_6_attention_output_dense.png model_card/images/layer_6_attention_self_key.png model_card/images/layer_6_attention_self_query.png model_card/images/layer_6_attention_self_value.png model_card/images/layer_6_intermediate_dense.png model_card/images/layer_6_output_dense.png model_card/images/layer_7_attention_output_dense.png model_card/images/layer_7_attention_self_key.png model_card/images/layer_7_attention_self_query.png model_card/images/layer_7_attention_self_value.png model_card/images/layer_7_intermediate_dense.png model_card/images/layer_7_output_dense.png model_card/images/layer_8_attention_output_dense.png model_card/images/layer_8_attention_self_key.png model_card/images/layer_8_attention_self_query.png model_card/images/layer_8_attention_self_value.png model_card/images/layer_8_intermediate_dense.png model_card/images/layer_8_output_dense.png model_card/images/layer_9_attention_output_dense.png model_card/images/layer_9_attention_self_key.png model_card/images/layer_9_attention_self_query.png model_card/images/layer_9_attention_self_value.png model_card/images/layer_9_intermediate_dense.png model_card/images/layer_9_output_dense.png training/data_args.json training/model_args.json training/sparse_args.json training/training_args.bin
---
language: en
thumbnail:
license: mit
tags:
- question-answering
- 
- 
datasets:
- squad
metrics:
- squad
widget:
- text: "Where is the Eiffel Tower located?"
  context: "The Eiffel Tower is a wrought-iron lattice tower on the Champ de Mars in Paris, France. It is named after the engineer Gustave Eiffel, whose company designed and built the tower."
- text: "Who is Frederic Chopin?"
  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."
---

## BERT-base uncased model fine-tuned on SQuAD v1

This model was created using the [nn_pruning](https://github.com/huggingface/nn_pruning) python library: the **linear layers contains 8.0%** of the original  weights.



The model contains **28.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 **1.16x 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.

<div class="graph"><script src="/madlag/bert-base-uncased-squadv1-x1.16-f88.1-d8-unstruct-v1/raw/main/model_card/density_info.js" id="c60d09ec-81ff-4d6f-b616-c3ef09b2175d"></script></div>

In terms of accuracy, its **F1 is 88.11**, compared with 88.5 for bert-base-uncased, a **F1 drop of 0.39**.

## Fine-Pruning details
This model was fine-tuned from the HuggingFace [model](https://huggingface.co/bert-base-uncased) checkpoint on [SQuAD1.1](https://rajpurkar.github.io/SQuAD-explorer), and distilled from the model [bert-large-uncased-whole-word-masking-finetuned-squad](https://huggingface.co/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: 22 heads were removed on a total of 144 (15.3%).
Here is a detailed view on how the remaining heads are distributed in the network after pruning.
<div class="graph"><script src="/madlag/bert-base-uncased-squadv1-x1.16-f88.1-d8-unstruct-v1/raw/main/model_card/pruning_info.js" id="55528c8b-d5f5-46a5-a35a-dad93725f7e5"></script></div>

## 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:

```CPU: Intel(R) Core(TM) i7-6700K CPU
Memory: 64 GiB
GPUs: 1 GeForce GTX 3090, with 24GiB memory
GPU driver: 455.23.05, CUDA: 11.1
```

### Results

**Pytorch model file size**: `398MB` (original BERT: `420MB`)

| Metric | # Value   | # Original ([Table 2](https://www.aclweb.org/anthology/N19-1423.pdf))| Variation |
| ------ | --------- | --------- | --------- |
| **EM** | **80.94** | **80.8** | **+0.14**|
| **F1** | **88.11** | **88.5** | **-0.39**|

## 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.

```python
from transformers import pipeline
from nn_pruning.inference_model_patcher import optimize_model

qa_pipeline = pipeline(
    "question-answering",
    model="madlag/bert-base-uncased-squadv1-x1.16-f88.1-d8-unstruct-v1",
    tokenizer="madlag/bert-base-uncased-squadv1-x1.16-f88.1-d8-unstruct-v1"
)

print("bert-base-uncased parameters: 152.0M")
print(f"Parameters count (includes only head pruning, not feed forward pruning)={int(qa_pipeline.model.num_parameters() / 1E6)}M")
qa_pipeline.model = optimize_model(qa_pipeline.model, "dense")

print(f"Parameters count after complete optimization={int(qa_pipeline.model.num_parameters() / 1E6)}M")
predictions = qa_pipeline({
    '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.",
    'question': "Who is Frederic Chopin?",
})
print("Predictions", predictions)
```