attention_test.py 2.13 KB
Newer Older
Frederick Liu's avatar
Frederick Liu committed
1
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
Hongkun Yu's avatar
Hongkun Yu committed
2
3
4
5
6
7
8
9
10
11
12
13
#
# 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.
Frederick Liu's avatar
Frederick Liu committed
14

Hongkun Yu's avatar
Hongkun Yu committed
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
"""Tests for official.nlp.projects.bigbird.attention."""

import tensorflow as tf

from official.nlp.projects.bigbird import attention


class BigbirdAttentionTest(tf.test.TestCase):

  def test_attention(self):
    num_heads = 12
    key_dim = 64
    seq_length = 1024
    batch_size = 2
    block_size = 64
    mask_layer = attention.BigBirdMasks(block_size=block_size)
    encoder_inputs_mask = tf.zeros((batch_size, seq_length), dtype=tf.int32)
    test_layer = attention.BigBirdAttention(
        num_heads=num_heads,
        key_dim=key_dim,
        from_block_size=block_size,
        to_block_size=block_size,
        seed=0)
    query = tf.random.normal(
        shape=(batch_size, seq_length, key_dim))
Frederick Liu's avatar
Frederick Liu committed
40
    masks = mask_layer(query, tf.cast(encoder_inputs_mask, dtype=tf.float64))
Hongkun Yu's avatar
Hongkun Yu committed
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
    value = query
    output = test_layer(
        query=query,
        value=value,
        attention_mask=masks)
    self.assertEqual(output.shape, [batch_size, seq_length, key_dim])

  def test_config(self):
    num_heads = 12
    key_dim = 64
    block_size = 64
    test_layer = attention.BigBirdAttention(
        num_heads=num_heads,
        key_dim=key_dim,
        from_block_size=block_size,
        to_block_size=block_size,
        seed=0)
    print(test_layer.get_config())
    new_layer = attention.BigBirdAttention.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()