"vscode:/vscode.git/clone" did not exist on "bc659f86adfb26d7cf86e67fa4600b89e63ac07c"
mobilebert.rst 7.02 KB
Newer Older
Vasily Shamporov's avatar
Vasily Shamporov committed
1
MobileBERT
Sylvain Gugger's avatar
Sylvain Gugger committed
2
-----------------------------------------------------------------------------------------------------------------------
Vasily Shamporov's avatar
Vasily Shamporov committed
3
4

Overview
Sylvain Gugger's avatar
Sylvain Gugger committed
5
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
6

Sylvain Gugger's avatar
Sylvain Gugger committed
7
8
9
10
The MobileBERT model was proposed in `MobileBERT: a Compact Task-Agnostic BERT for Resource-Limited Devices
<https://arxiv.org/abs/2004.02984>`__ by Zhiqing Sun, Hongkun Yu, Xiaodan Song, Renjie Liu, Yiming Yang, and Denny
Zhou. It's a bidirectional transformer based on the BERT model, which is compressed and accelerated using several
approaches.
Vasily Shamporov's avatar
Vasily Shamporov committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

The abstract from the paper is the following:

*Natural Language Processing (NLP) has recently achieved great success by using huge pre-trained models with hundreds
of millions of parameters. However, these models suffer from heavy model sizes and high latency such that they cannot
be deployed to resource-limited mobile devices. In this paper, we propose MobileBERT for compressing and accelerating
the popular BERT model. Like the original BERT, MobileBERT is task-agnostic, that is, it can be generically applied
to various downstream NLP tasks via simple fine-tuning. Basically, MobileBERT is a thin version of BERT_LARGE, while
equipped with bottleneck structures and a carefully designed balance between self-attentions and feed-forward
networks. To train MobileBERT, we first train a specially designed teacher model, an inverted-bottleneck incorporated
BERT_LARGE model. Then, we conduct knowledge transfer from this teacher to MobileBERT. Empirical studies show that
MobileBERT is 4.3x smaller and 5.5x faster than BERT_BASE while achieving competitive results on well-known
benchmarks. On the natural language inference tasks of GLUE, MobileBERT achieves a GLUEscore o 77.7
(0.6 lower than BERT_BASE), and 62 ms latency on a Pixel 4 phone. On the SQuAD v1.1/v2.0 question answering task,
MobileBERT achieves a dev F1 score of 90.0/79.2 (1.5/2.1 higher than BERT_BASE).*

Tips:

- MobileBERT is a model with absolute position embeddings so it's usually advised to pad the inputs on
  the right rather than the left.
- MobileBERT is similar to BERT and therefore relies on the masked language modeling (MLM) objective.
  It is therefore efficient at predicting masked tokens and at NLU in general, but is not optimal for
  text generation. Models trained with a causal language modeling (CLM) objective are better in that regard.

Sylvain Gugger's avatar
Sylvain Gugger committed
35
The original code can be found `here <https://github.com/google-research/mobilebert>`__.
Vasily Shamporov's avatar
Vasily Shamporov committed
36
37

MobileBertConfig
Sylvain Gugger's avatar
Sylvain Gugger committed
38
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
39
40
41
42
43
44

.. autoclass:: transformers.MobileBertConfig
    :members:


MobileBertTokenizer
Sylvain Gugger's avatar
Sylvain Gugger committed
45
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
46
47

.. autoclass:: transformers.MobileBertTokenizer
Sylvain Gugger's avatar
Sylvain Gugger committed
48
    :members:
Vasily Shamporov's avatar
Vasily Shamporov committed
49
50
51


MobileBertTokenizerFast
Sylvain Gugger's avatar
Sylvain Gugger committed
52
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
53
54
55
56
57

.. autoclass:: transformers.MobileBertTokenizerFast
    :members:


58
MobileBert specific outputs
Sylvain Gugger's avatar
Sylvain Gugger committed
59
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60

Sylvain Gugger's avatar
Sylvain Gugger committed
61
62
63
64
.. autoclass:: transformers.modeling_mobilebert.MobileBertForPreTrainingOutput
    :members:

.. autoclass:: transformers.modeling_tf_mobilebert.TFMobileBertForPreTrainingOutput
65
66
67
    :members:


Vasily Shamporov's avatar
Vasily Shamporov committed
68
MobileBertModel
Sylvain Gugger's avatar
Sylvain Gugger committed
69
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
70
71

.. autoclass:: transformers.MobileBertModel
Sylvain Gugger's avatar
Sylvain Gugger committed
72
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
73
74
75


MobileBertForPreTraining
Sylvain Gugger's avatar
Sylvain Gugger committed
76
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
77
78

.. autoclass:: transformers.MobileBertForPreTraining
Sylvain Gugger's avatar
Sylvain Gugger committed
79
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
80
81
82


MobileBertForMaskedLM
Sylvain Gugger's avatar
Sylvain Gugger committed
83
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
84
85

.. autoclass:: transformers.MobileBertForMaskedLM
Sylvain Gugger's avatar
Sylvain Gugger committed
86
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
87
88
89


MobileBertForNextSentencePrediction
Sylvain Gugger's avatar
Sylvain Gugger committed
90
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
91
92

.. autoclass:: transformers.MobileBertForNextSentencePrediction
Sylvain Gugger's avatar
Sylvain Gugger committed
93
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
94
95
96


MobileBertForSequenceClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
97
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
98
99

.. autoclass:: transformers.MobileBertForSequenceClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
100
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
101
102
103


MobileBertForMultipleChoice
Sylvain Gugger's avatar
Sylvain Gugger committed
104
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
105
106

.. autoclass:: transformers.MobileBertForMultipleChoice
Sylvain Gugger's avatar
Sylvain Gugger committed
107
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
108
109
110


MobileBertForTokenClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
111
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
112
113

.. autoclass:: transformers.MobileBertForTokenClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
114
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
115
116
117


MobileBertForQuestionAnswering
Sylvain Gugger's avatar
Sylvain Gugger committed
118
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
119
120

.. autoclass:: transformers.MobileBertForQuestionAnswering
Sylvain Gugger's avatar
Sylvain Gugger committed
121
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
122
123
124


TFMobileBertModel
Sylvain Gugger's avatar
Sylvain Gugger committed
125
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
126
127

.. autoclass:: transformers.TFMobileBertModel
Sylvain Gugger's avatar
Sylvain Gugger committed
128
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
129
130
131


TFMobileBertForPreTraining
Sylvain Gugger's avatar
Sylvain Gugger committed
132
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
133
134

.. autoclass:: transformers.TFMobileBertForPreTraining
Sylvain Gugger's avatar
Sylvain Gugger committed
135
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
136
137
138


TFMobileBertForMaskedLM
Sylvain Gugger's avatar
Sylvain Gugger committed
139
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
140
141

.. autoclass:: transformers.TFMobileBertForMaskedLM
Sylvain Gugger's avatar
Sylvain Gugger committed
142
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
143
144
145


TFMobileBertForNextSentencePrediction
Sylvain Gugger's avatar
Sylvain Gugger committed
146
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
147
148

.. autoclass:: transformers.TFMobileBertForNextSentencePrediction
Sylvain Gugger's avatar
Sylvain Gugger committed
149
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
150
151
152


TFMobileBertForSequenceClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
153
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
154
155

.. autoclass:: transformers.TFMobileBertForSequenceClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
156
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
157
158
159


TFMobileBertForMultipleChoice
Sylvain Gugger's avatar
Sylvain Gugger committed
160
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
161
162

.. autoclass:: transformers.TFMobileBertForMultipleChoice
Sylvain Gugger's avatar
Sylvain Gugger committed
163
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
164
165
166


TFMobileBertForTokenClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
167
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
168
169

.. autoclass:: transformers.TFMobileBertForTokenClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
170
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
171
172
173


TFMobileBertForQuestionAnswering
Sylvain Gugger's avatar
Sylvain Gugger committed
174
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
175
176

.. autoclass:: transformers.TFMobileBertForQuestionAnswering
Sylvain Gugger's avatar
Sylvain Gugger committed
177
    :members: call