mobilebert.rst 7.65 KB
Newer Older
Sylvain Gugger's avatar
Sylvain Gugger committed
1
2
3
4
5
6
7
8
9
10
11
12
.. 
    Copyright 2020 The HuggingFace Team. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
    the License. You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
    an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
    specific language governing permissions and limitations under the License.

Vasily Shamporov's avatar
Vasily Shamporov committed
13
MobileBERT
Sylvain Gugger's avatar
Sylvain Gugger committed
14
-----------------------------------------------------------------------------------------------------------------------
Vasily Shamporov's avatar
Vasily Shamporov committed
15
16

Overview
Sylvain Gugger's avatar
Sylvain Gugger committed
17
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
18

Sylvain Gugger's avatar
Sylvain Gugger committed
19
20
21
22
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
23
24
25
26
27
28

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
Sylvain Gugger's avatar
Sylvain Gugger committed
29
30
31
32
33
34
35
36
37
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).*
Vasily Shamporov's avatar
Vasily Shamporov committed
38
39
40

Tips:

Sylvain Gugger's avatar
Sylvain Gugger committed
41
42
43
44
45
- 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.
Vasily Shamporov's avatar
Vasily Shamporov committed
46

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

MobileBertConfig
Sylvain Gugger's avatar
Sylvain Gugger committed
50
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
51
52
53
54
55
56

.. autoclass:: transformers.MobileBertConfig
    :members:


MobileBertTokenizer
Sylvain Gugger's avatar
Sylvain Gugger committed
57
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
58
59

.. autoclass:: transformers.MobileBertTokenizer
Sylvain Gugger's avatar
Sylvain Gugger committed
60
    :members:
Vasily Shamporov's avatar
Vasily Shamporov committed
61
62
63


MobileBertTokenizerFast
Sylvain Gugger's avatar
Sylvain Gugger committed
64
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
65
66
67
68
69

.. autoclass:: transformers.MobileBertTokenizerFast
    :members:


70
MobileBert specific outputs
Sylvain Gugger's avatar
Sylvain Gugger committed
71
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72

Sylvain Gugger's avatar
Sylvain Gugger committed
73
.. autoclass:: transformers.models.mobilebert.modeling_mobilebert.MobileBertForPreTrainingOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
74
75
    :members:

Sylvain Gugger's avatar
Sylvain Gugger committed
76
.. autoclass:: transformers.models.mobilebert.modeling_tf_mobilebert.TFMobileBertForPreTrainingOutput
77
78
79
    :members:


Vasily Shamporov's avatar
Vasily Shamporov committed
80
MobileBertModel
Sylvain Gugger's avatar
Sylvain Gugger committed
81
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
82
83

.. autoclass:: transformers.MobileBertModel
Sylvain Gugger's avatar
Sylvain Gugger committed
84
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
85
86
87


MobileBertForPreTraining
Sylvain Gugger's avatar
Sylvain Gugger committed
88
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
89
90

.. autoclass:: transformers.MobileBertForPreTraining
Sylvain Gugger's avatar
Sylvain Gugger committed
91
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
92
93
94


MobileBertForMaskedLM
Sylvain Gugger's avatar
Sylvain Gugger committed
95
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
96
97

.. autoclass:: transformers.MobileBertForMaskedLM
Sylvain Gugger's avatar
Sylvain Gugger committed
98
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
99
100
101


MobileBertForNextSentencePrediction
Sylvain Gugger's avatar
Sylvain Gugger committed
102
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
103
104

.. autoclass:: transformers.MobileBertForNextSentencePrediction
Sylvain Gugger's avatar
Sylvain Gugger committed
105
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
106
107
108


MobileBertForSequenceClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
109
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
110
111

.. autoclass:: transformers.MobileBertForSequenceClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
112
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
113
114
115


MobileBertForMultipleChoice
Sylvain Gugger's avatar
Sylvain Gugger committed
116
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
117
118

.. autoclass:: transformers.MobileBertForMultipleChoice
Sylvain Gugger's avatar
Sylvain Gugger committed
119
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
120
121
122


MobileBertForTokenClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
124
125

.. autoclass:: transformers.MobileBertForTokenClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
126
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
127
128
129


MobileBertForQuestionAnswering
Sylvain Gugger's avatar
Sylvain Gugger committed
130
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
131
132

.. autoclass:: transformers.MobileBertForQuestionAnswering
Sylvain Gugger's avatar
Sylvain Gugger committed
133
    :members: forward
Vasily Shamporov's avatar
Vasily Shamporov committed
134
135
136


TFMobileBertModel
Sylvain Gugger's avatar
Sylvain Gugger committed
137
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
138
139

.. autoclass:: transformers.TFMobileBertModel
Sylvain Gugger's avatar
Sylvain Gugger committed
140
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
141
142
143


TFMobileBertForPreTraining
Sylvain Gugger's avatar
Sylvain Gugger committed
144
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
145
146

.. autoclass:: transformers.TFMobileBertForPreTraining
Sylvain Gugger's avatar
Sylvain Gugger committed
147
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
148
149
150


TFMobileBertForMaskedLM
Sylvain Gugger's avatar
Sylvain Gugger committed
151
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
152
153

.. autoclass:: transformers.TFMobileBertForMaskedLM
Sylvain Gugger's avatar
Sylvain Gugger committed
154
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
155
156
157


TFMobileBertForNextSentencePrediction
Sylvain Gugger's avatar
Sylvain Gugger committed
158
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
159
160

.. autoclass:: transformers.TFMobileBertForNextSentencePrediction
Sylvain Gugger's avatar
Sylvain Gugger committed
161
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
162
163
164


TFMobileBertForSequenceClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
165
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
166
167

.. autoclass:: transformers.TFMobileBertForSequenceClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
168
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
169
170
171


TFMobileBertForMultipleChoice
Sylvain Gugger's avatar
Sylvain Gugger committed
172
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
173
174

.. autoclass:: transformers.TFMobileBertForMultipleChoice
Sylvain Gugger's avatar
Sylvain Gugger committed
175
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
176
177
178


TFMobileBertForTokenClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
179
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
180
181

.. autoclass:: transformers.TFMobileBertForTokenClassification
Sylvain Gugger's avatar
Sylvain Gugger committed
182
    :members: call
Vasily Shamporov's avatar
Vasily Shamporov committed
183
184
185


TFMobileBertForQuestionAnswering
Sylvain Gugger's avatar
Sylvain Gugger committed
186
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasily Shamporov's avatar
Vasily Shamporov committed
187
188

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