MCPcopy Create free account
hub / github.com/NVIDIA/FasterTransformer / attention_layer

Function attention_layer

examples/tensorflow/bert/utils/bert.py:38–156  ·  view source on GitHub ↗
(from_tensor,
                    to_tensor,
                    attention_mask=None,
                    num_attention_heads=1,
                    size_per_head=512,
                    query_act=None,
                    key_act=None,
                    value_act=None,
                    attention_probs_dropout_prob=0.0,
                    initializer_range=0.02,
                    do_return_2d_tensor=False,
                    batch_size=None,
                    from_seq_length=None,
                    to_seq_length=None,
                    tf_datatype=tf.float32)

Source from the content-addressed store, hash-verified

36
37
38def attention_layer(from_tensor,
39 to_tensor,
40 attention_mask=None,
41 num_attention_heads=1,
42 size_per_head=512,
43 query_act=None,
44 key_act=None,
45 value_act=None,
46 attention_probs_dropout_prob=0.0,
47 initializer_range=0.02,
48 do_return_2d_tensor=False,
49 batch_size=None,
50 from_seq_length=None,
51 to_seq_length=None,
52 tf_datatype=tf.float32):
53
54 def transpose_for_scores(input_tensor, batch_size, num_attention_heads,
55 seq_length, width):
56 output_tensor = tf.reshape(
57 input_tensor, [batch_size, seq_length, num_attention_heads, width])
58
59 output_tensor = tf.transpose(output_tensor, [0, 2, 1, 3])
60 return output_tensor
61
62 from_shape = get_shape_list(from_tensor, expected_rank=[2, 3])
63 to_shape = get_shape_list(to_tensor, expected_rank=[2, 3])
64
65 if len(from_shape) != len(to_shape):
66 raise ValueError(
67 "The rank of `from_tensor` must match the rank of `to_tensor`.")
68
69 if len(from_shape) == 3:
70 batch_size = from_shape[0]
71 from_seq_length = from_shape[1]
72 to_seq_length = to_shape[1]
73 elif len(from_shape) == 2:
74 if (batch_size is None or from_seq_length is None or to_seq_length is None):
75 raise ValueError(
76 "When passing in rank 2 tensors to attention_layer, the values "
77 "for `batch_size`, `from_seq_length`, and `to_seq_length` "
78 "must all be specified.")
79
80 from_tensor_2d = reshape_to_matrix(from_tensor)
81 to_tensor_2d = reshape_to_matrix(to_tensor)
82
83 # `query_layer` = [B*F, N*H]
84 query_layer = tf.layers.dense(
85 from_tensor_2d,
86 num_attention_heads * size_per_head,
87 activation=query_act,
88 name="query",
89 use_bias=True,
90 bias_initializer=create_initializer(initializer_range, tf_datatype),
91 kernel_initializer=create_initializer(initializer_range, tf_datatype))
92
93 # `key_layer` = [B*T, N*H]
94 key_layer = tf.layers.dense(
95 to_tensor_2d,

Callers 1

tf_bertFunction · 0.70

Calls 4

create_initializerFunction · 0.90
get_shape_listFunction · 0.70
reshape_to_matrixFunction · 0.70
transpose_for_scoresFunction · 0.70

Tested by

no test coverage detected