mobilebert.rst 7.73 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

47
48
This model was contributed by `vshampor <https://huggingface.co/vshampor>`__. The original code can be found `here
<https://github.com/google-research/mobilebert>`__.
Vasily Shamporov's avatar
Vasily Shamporov committed
49
50

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

.. autoclass:: transformers.MobileBertConfig
    :members:


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

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


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

.. autoclass:: transformers.MobileBertTokenizerFast
    :members:


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

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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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