-
Notifications
You must be signed in to change notification settings - Fork 45.7k
/
Copy pathgated_feedforward_test.py
120 lines (104 loc) · 4.39 KB
/
gated_feedforward_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
# Copyright 2024 The TensorFlow 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.
"""Tests for Keras-based gated feedforward layer."""
from absl.testing import parameterized
import numpy as np
import tensorflow as tf, tf_keras
from official.nlp.modeling.layers import gated_feedforward
class GatedFeedforwardTest(tf.test.TestCase, parameterized.TestCase):
def tearDown(self):
super(GatedFeedforwardTest, self).tearDown()
tf_keras.mixed_precision.set_global_policy("float32")
@parameterized.parameters(
(True, 1, "after_residual", "float32"),
(True, 1, "after_residual", "mixed_float16"),
(False, 4, "before_residual", "float32"),
(False, 4, "before_residual", "mixed_float16"),
(True, 4, "after_residual", "float32"),
(True, 4, "after_residual", "mixed_float16"),
(False, 1, "before_residual", "float32"),
(False, 1, "before_residual", "mixed_float16"),
)
def test_layer_creation(self, use_gate, num_blocks, dropout_position, dtype):
tf_keras.mixed_precision.set_global_policy(dtype)
kwargs = dict(
inner_dim=128,
inner_activation="relu",
dropout=0.1,
use_gate=use_gate,
num_blocks=num_blocks,
dropout_position=dropout_position,
kernel_initializer="glorot_uniform",
bias_initializer="zeros")
test_layer = gated_feedforward.GatedFeedforward(**kwargs)
sequence_length = 64
width = 128
# Create a 3-dimensional input (the first dimension is implicit).
data_tensor = tf_keras.Input(shape=(sequence_length, width))
output_tensor = test_layer(data_tensor)
# The default output of a transformer layer should be the same as the input.
self.assertEqual(data_tensor.shape.as_list(), output_tensor.shape.as_list())
@parameterized.parameters(
(True, 1, "after_residual", "float32"),
(True, 1, "after_residual", "mixed_float16"),
(False, 4, "before_residual", "float32"),
(False, 4, "before_residual", "mixed_float16"),
(True, 4, "after_residual", "float32"),
(True, 4, "after_residual", "mixed_float16"),
(False, 1, "before_residual", "float32"),
(False, 1, "before_residual", "mixed_float16"),
)
def test_layer_invocation(self, use_gate, num_blocks, dropout_position,
dtype):
tf_keras.mixed_precision.set_global_policy(dtype)
kwargs = dict(
inner_dim=16,
inner_activation="relu",
dropout=0.1,
use_gate=use_gate,
num_blocks=num_blocks,
dropout_position=dropout_position,
kernel_initializer="glorot_uniform",
bias_initializer="zeros")
test_layer = gated_feedforward.GatedFeedforward(**kwargs)
sequence_length = 16
width = 32
# Create a 3-dimensional input (the first dimension is implicit).
data_tensor = tf_keras.Input(shape=(sequence_length, width))
output_tensor = test_layer(data_tensor)
# Create a model from the test layer.
model = tf_keras.Model(data_tensor, output_tensor)
# Invoke the model on test data.
batch_size = 6
input_data = 10 * np.random.random_sample(
(batch_size, sequence_length, width))
output_data = model.predict(input_data)
self.assertEqual(output_data.shape, (batch_size, sequence_length, width))
def test_serialize_deserialize(self):
kwargs = dict(
inner_dim=16,
inner_activation="relu",
dropout=0.1,
use_gate=False,
num_blocks=4,
dropout_position="after_residual",
kernel_initializer="glorot_uniform",
bias_initializer="zeros")
test_layer = gated_feedforward.GatedFeedforward(**kwargs)
new_layer = gated_feedforward.GatedFeedforward.from_config(
test_layer.get_config())
# If the serialization was successful, the new config should match the old.
self.assertAllEqual(test_layer.get_config(), new_layer.get_config())
if __name__ == "__main__":
tf.test.main()