-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathbert_encoder_test.py
208 lines (182 loc) · 6.67 KB
/
bert_encoder_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Copyright 2019 The Texar Authors. 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.
"""
Unit tests for BERT encoders.
"""
import unittest
import torch
from texar.torch.modules.encoders.bert_encoder import BERTEncoder
from texar.torch.utils.test import pretrained_test
class BERTEncoderTest(unittest.TestCase):
r"""Tests :class:`~texar.torch.modules.BERTEncoder` class.
"""
def setUp(self) -> None:
self.batch_size = 2
self.max_length = 3
self.inputs = torch.zeros(
self.batch_size, self.max_length, dtype=torch.long)
@pretrained_test
def test_model_loading(self):
r"""Tests model loading functionality."""
for pretrained_model_name in BERTEncoder.available_checkpoints():
encoder = BERTEncoder(pretrained_model_name=pretrained_model_name)
_, _ = encoder(self.inputs)
@pretrained_test
def test_hparams(self):
r"""Tests the priority of the encoder arch parameter.
"""
# case 1: set "pretrained_mode_name" by constructor argument
hparams = {
"pretrained_model_name": "bert-large-uncased",
}
encoder = BERTEncoder(pretrained_model_name="bert-base-uncased",
hparams=hparams)
self.assertEqual(encoder.hparams.encoder.num_blocks, 12)
_, _ = encoder(self.inputs)
# case 2: set "pretrained_mode_name" by hparams
hparams = {
"pretrained_model_name": "bert-large-uncased",
"encoder": {
"num_blocks": 6,
}
}
encoder = BERTEncoder(hparams=hparams)
self.assertEqual(encoder.hparams.encoder.num_blocks, 24)
_, _ = encoder(self.inputs)
# case 3: set to None in both hparams and constructor argument
hparams = {
"pretrained_model_name": None,
"encoder": {
"num_blocks": 6,
},
}
encoder = BERTEncoder(hparams=hparams)
self.assertEqual(encoder.hparams.encoder.num_blocks, 6)
_, _ = encoder(self.inputs)
# case 4: using default hparams
encoder = BERTEncoder()
self.assertEqual(encoder.hparams.encoder.num_blocks, 12)
_, _ = encoder(self.inputs)
@pretrained_test
def test_trainable_variables(self):
r"""Tests the functionality of automatically collecting trainable
variables.
"""
# case 1: bert base
encoder = BERTEncoder()
self.assertEqual(len(encoder.trainable_variables), 3 + 2 + 12 * 16 + 2)
_, _ = encoder(self.inputs)
# case 2: bert large
hparams = {
"pretrained_model_name": "bert-large-uncased"
}
encoder = BERTEncoder(hparams=hparams)
self.assertEqual(len(encoder.trainable_variables), 3 + 2 + 24 * 16 + 2)
_, _ = encoder(self.inputs)
# case 3: self-designed bert
hparams = {
"encoder": {
"num_blocks": 6,
},
"pretrained_model_name": None,
}
encoder = BERTEncoder(hparams=hparams)
self.assertEqual(len(encoder.trainable_variables), 3 + 2 + 6 * 16 + 2)
_, _ = encoder(self.inputs)
def test_encode(self):
r"""Tests encoding.
"""
# case 1: bert base
hparams = {
"pretrained_model_name": None,
}
encoder = BERTEncoder(hparams=hparams)
inputs = torch.randint(30521, (self.batch_size, self.max_length))
outputs, pooled_output = encoder(inputs)
outputs_dim = encoder.hparams.encoder.dim
self.assertEqual(
outputs.shape,
torch.Size([self.batch_size, self.max_length, outputs_dim]))
self.assertEqual(
pooled_output.shape,
torch.Size([self.batch_size, encoder.output_size]))
# case 2: self-designed bert
hparams = {
'pretrained_model_name': None,
'embed': {
'dim': 96,
},
'segment_embed': {
'dim': 96,
},
'position_embed': {
'dim': 96,
},
'encoder': {
'dim': 96,
'multihead_attention': {
'num_units': 96,
'output_dim': 96,
},
'poswise_feedforward': {
'layers': [
{
'kwargs': {
'in_features': 96,
'out_features': 96 * 4,
'bias': True,
},
'type': 'Linear',
},
{"type": "BertGELU"},
{
'kwargs': {
'in_features': 96 * 4,
'out_features': 96,
'bias': True,
},
'type': 'Linear',
}
]
},
},
'hidden_size': 96,
}
encoder = BERTEncoder(hparams=hparams)
outputs, pooled_output = encoder(inputs)
outputs_dim = encoder.hparams.encoder.dim
self.assertEqual(
outputs.shape,
torch.Size([self.batch_size, self.max_length, outputs_dim]))
self.assertEqual(
pooled_output.shape,
torch.Size([self.batch_size, encoder.output_size]))
def test_soft_ids(self):
r"""Tests soft ids.
"""
hparams = {
"pretrained_model_name": None,
}
encoder = BERTEncoder(hparams=hparams)
inputs = torch.rand(self.batch_size, self.max_length, 30522)
outputs, pooled_output = encoder(inputs)
outputs_dim = encoder.hparams.encoder.dim
self.assertEqual(
outputs.shape,
torch.Size([self.batch_size, self.max_length, outputs_dim]))
self.assertEqual(
pooled_output.shape,
torch.Size([self.batch_size, encoder.output_size]))
if __name__ == "__main__":
unittest.main()