Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
ResNet50_tensorflow
Commits
cfef35b1
Commit
cfef35b1
authored
Aug 01, 2022
by
Renjie Liu
Committed by
A. Unique TensorFlower
Aug 01, 2022
Browse files
Internal change
PiperOrigin-RevId: 464685466
parent
4647a7aa
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
9 deletions
+31
-9
official/nlp/configs/bert.py
official/nlp/configs/bert.py
+2
-0
official/nlp/modeling/layers/mobile_bert_layers.py
official/nlp/modeling/layers/mobile_bert_layers.py
+27
-9
official/projects/edgetpu/nlp/experiments/mobilebert_edgetpu_xxs.yaml
...jects/edgetpu/nlp/experiments/mobilebert_edgetpu_xxs.yaml
+1
-0
official/projects/edgetpu/nlp/modeling/model_builder.py
official/projects/edgetpu/nlp/modeling/model_builder.py
+1
-0
No files found.
official/nlp/configs/bert.py
View file @
cfef35b1
...
@@ -41,3 +41,5 @@ class PretrainerConfig(base_config.Config):
...
@@ -41,3 +41,5 @@ class PretrainerConfig(base_config.Config):
cls_heads
:
List
[
ClsHeadConfig
]
=
dataclasses
.
field
(
default_factory
=
list
)
cls_heads
:
List
[
ClsHeadConfig
]
=
dataclasses
.
field
(
default_factory
=
list
)
mlm_activation
:
str
=
"gelu"
mlm_activation
:
str
=
"gelu"
mlm_initializer_range
:
float
=
0.02
mlm_initializer_range
:
float
=
0.02
# Currently only used for mobile bert.
mlm_output_weights_use_proj
:
bool
=
False
official/nlp/modeling/layers/mobile_bert_layers.py
View file @
cfef35b1
...
@@ -447,6 +447,7 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
...
@@ -447,6 +447,7 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
activation
=
None
,
activation
=
None
,
initializer
=
'glorot_uniform'
,
initializer
=
'glorot_uniform'
,
output
=
'logits'
,
output
=
'logits'
,
output_weights_use_proj
=
False
,
**
kwargs
):
**
kwargs
):
"""Class initialization.
"""Class initialization.
...
@@ -457,6 +458,9 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
...
@@ -457,6 +458,9 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
uniform initializer.
uniform initializer.
output: The output style for this layer. Can be either `logits` or
output: The output style for this layer. Can be either `logits` or
`predictions`.
`predictions`.
output_weights_use_proj: Use projection instead of concating extra output
weights, this may reduce the MLM task accuracy but will reduce the model
params as well.
**kwargs: keyword arguments.
**kwargs: keyword arguments.
"""
"""
super
().
__init__
(
**
kwargs
)
super
().
__init__
(
**
kwargs
)
...
@@ -469,6 +473,7 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
...
@@ -469,6 +473,7 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
(
'Unknown `output` value "%s". `output` can be either "logits" or '
(
'Unknown `output` value "%s". `output` can be either "logits" or '
'"predictions"'
)
%
output
)
'"predictions"'
)
%
output
)
self
.
_output_type
=
output
self
.
_output_type
=
output
self
.
_output_weights_use_proj
=
output_weights_use_proj
def
build
(
self
,
input_shape
):
def
build
(
self
,
input_shape
):
self
.
_vocab_size
,
embedding_width
=
self
.
embedding_table
.
shape
self
.
_vocab_size
,
embedding_width
=
self
.
embedding_table
.
shape
...
@@ -480,11 +485,18 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
...
@@ -480,11 +485,18 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
name
=
'transform/dense'
)
name
=
'transform/dense'
)
if
hidden_size
>
embedding_width
:
if
hidden_size
>
embedding_width
:
self
.
extra_output_weights
=
self
.
add_weight
(
if
self
.
_output_weights_use_proj
:
'extra_output_weights'
,
self
.
extra_output_weights
=
self
.
add_weight
(
shape
=
(
self
.
_vocab_size
,
hidden_size
-
embedding_width
),
'output_weights_proj'
,
initializer
=
tf_utils
.
clone_initializer
(
self
.
initializer
),
shape
=
(
embedding_width
,
hidden_size
),
trainable
=
True
)
initializer
=
tf_utils
.
clone_initializer
(
self
.
initializer
),
trainable
=
True
)
else
:
self
.
extra_output_weights
=
self
.
add_weight
(
'extra_output_weights'
,
shape
=
(
self
.
_vocab_size
,
hidden_size
-
embedding_width
),
initializer
=
tf_utils
.
clone_initializer
(
self
.
initializer
),
trainable
=
True
)
elif
hidden_size
==
embedding_width
:
elif
hidden_size
==
embedding_width
:
self
.
extra_output_weights
=
None
self
.
extra_output_weights
=
None
else
:
else
:
...
@@ -509,10 +521,16 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
...
@@ -509,10 +521,16 @@ class MobileBertMaskedLM(tf.keras.layers.Layer):
if
self
.
extra_output_weights
is
None
:
if
self
.
extra_output_weights
is
None
:
lm_data
=
tf
.
matmul
(
lm_data
,
self
.
embedding_table
,
transpose_b
=
True
)
lm_data
=
tf
.
matmul
(
lm_data
,
self
.
embedding_table
,
transpose_b
=
True
)
else
:
else
:
lm_data
=
tf
.
matmul
(
if
self
.
_output_weights_use_proj
:
lm_data
,
lm_data
=
tf
.
matmul
(
tf
.
concat
([
self
.
embedding_table
,
self
.
extra_output_weights
],
axis
=
1
),
lm_data
,
self
.
extra_output_weights
,
transpose_b
=
True
)
transpose_b
=
True
)
lm_data
=
tf
.
matmul
(
lm_data
,
self
.
embedding_table
,
transpose_b
=
True
)
else
:
lm_data
=
tf
.
matmul
(
lm_data
,
tf
.
concat
([
self
.
embedding_table
,
self
.
extra_output_weights
],
axis
=
1
),
transpose_b
=
True
)
logits
=
tf
.
nn
.
bias_add
(
lm_data
,
self
.
bias
)
logits
=
tf
.
nn
.
bias_add
(
lm_data
,
self
.
bias
)
masked_positions_length
=
masked_positions
.
shape
.
as_list
()[
1
]
or
tf
.
shape
(
masked_positions_length
=
masked_positions
.
shape
.
as_list
()[
1
]
or
tf
.
shape
(
...
...
official/projects/edgetpu/nlp/experiments/mobilebert_edgetpu_xxs.yaml
View file @
cfef35b1
...
@@ -63,6 +63,7 @@ student_model:
...
@@ -63,6 +63,7 @@ student_model:
type
:
mobilebert
type
:
mobilebert
mlm_activation
:
relu
mlm_activation
:
relu
mlm_initializer_range
:
0.02
mlm_initializer_range
:
0.02
mlm_output_weights_use_proj
:
true
teacher_model
:
teacher_model
:
cls_heads
:
[]
cls_heads
:
[]
encoder
:
encoder
:
...
...
official/projects/edgetpu/nlp/modeling/model_builder.py
View file @
cfef35b1
...
@@ -85,6 +85,7 @@ def build_bert_pretrainer(pretrainer_cfg: params.PretrainerModelParams,
...
@@ -85,6 +85,7 @@ def build_bert_pretrainer(pretrainer_cfg: params.PretrainerModelParams,
activation
=
tf_utils
.
get_activation
(
pretrainer_cfg
.
mlm_activation
),
activation
=
tf_utils
.
get_activation
(
pretrainer_cfg
.
mlm_activation
),
initializer
=
tf
.
keras
.
initializers
.
TruncatedNormal
(
initializer
=
tf
.
keras
.
initializers
.
TruncatedNormal
(
stddev
=
pretrainer_cfg
.
mlm_initializer_range
),
stddev
=
pretrainer_cfg
.
mlm_initializer_range
),
output_weights_use_proj
=
pretrainer_cfg
.
mlm_output_weights_use_proj
,
name
=
'cls/predictions'
)
name
=
'cls/predictions'
)
pretrainer
=
edgetpu_pretrainer
.
MobileBERTEdgeTPUPretrainer
(
pretrainer
=
edgetpu_pretrainer
.
MobileBERTEdgeTPUPretrainer
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment