MCPcopy Create free account
hub / github.com/Turing-Project/WriteGPT / attention_layer

Function attention_layer

LanguageNetwork/GPT2/train/modeling.py:145–222  ·  view source on GitHub ↗

:param x_flat: Tensor input, should be [batch_size*seq_length, dim] :param attention_mask: Attention mask to use of size [seq_length, seq_length+cached_length] :param size_per_head: dim = size_per_head * num_attention_heads :param num_attention_heads: dim = size_per_head * num_atte

(x_flat, attention_mask, batch_size, seq_length, size_per_head=512, num_attention_heads=1, *,
                    cache=None,
                    initializer_range=0.02, hidden_dropout_prob=0.1,
                    attention_probs_dropout_prob=0.1, do_cache=False)

Source from the content-addressed store, hash-verified

143
144
145def attention_layer(x_flat, attention_mask, batch_size, seq_length, size_per_head=512, num_attention_heads=1, *,
146 cache=None,
147 initializer_range=0.02, hidden_dropout_prob=0.1,
148 attention_probs_dropout_prob=0.1, do_cache=False):
149 """
150 :param x_flat: Tensor input, should be [batch_size*seq_length, dim]
151 :param attention_mask: Attention mask to use of size [seq_length, seq_length+cached_length]
152 :param size_per_head: dim = size_per_head * num_attention_heads
153 :param num_attention_heads: dim = size_per_head * num_attention_heads
154 :param cache: Optionally some past (cached) things of size
155 [batch, 2, heads, sequence, features], where 2 is [k, v]
156 :param do_cache: True if we should return cache
157 :return: A new tensor of shape [batch_size, seq_length, dim]
158 as well as a new cache "cached_keys_and_values" that will be of size
159 [batch_size, 2, num_attention_heads, seq_length, dim]
160 """
161 batch_size_seq_length, dim = get_shape_list(x_flat, expected_rank=2)
162
163 if dim != size_per_head * num_attention_heads:
164 raise ValueError("passed in a tensor of shape {} when size_per_head={} and num_attention_heads={}".format(
165 (batch_size_seq_length, dim), size_per_head, num_attention_heads
166 ))
167
168 query = _attention_projection_and_transpose(x_flat, batch_size=batch_size, seq_length=seq_length,
169 num_attention_heads=num_attention_heads, size_per_head=size_per_head,
170 name='query_layer',
171 initializer_range=initializer_range)
172 key = _attention_projection_and_transpose(x_flat, batch_size=batch_size, seq_length=seq_length,
173 num_attention_heads=num_attention_heads, size_per_head=size_per_head,
174 name='key_layer',
175 initializer_range=initializer_range)
176
177 value = _attention_projection_and_transpose(x_flat, batch_size=batch_size, seq_length=seq_length,
178 num_attention_heads=num_attention_heads, size_per_head=size_per_head,
179 name='value_layer',
180 initializer_range=initializer_range)
181
182 # Add to cache
183 cached_keys_and_values = tf.stack([key, value], axis=1) if do_cache else None
184
185 # Things that were relevant from the cache
186 if cache is not None:
187 pk, pv = tf.unstack(cache, axis=1)
188 key = tf.concat([pk, key], axis=-2)
189 value = tf.concat([pv, value], axis=-2)
190
191 # Multiply [batch_size, num_attention_heads, seq_length, size_per_head] with
192 # [batch_size, num_attention_heads, size_per_head, seq_length+cached_length] ->
193 # [batch_size, num_attention_heads, seq_length, seq_length+cached_length]
194 attention_scores = tf.matmul(query, key, transpose_b=True)
195 attention_scores = tf.multiply(attention_scores,
196 1.0 / math.sqrt(float(size_per_head)))
197 attention_scores = mask_attention_for_ltr(attention_scores, attention_mask)
198 attention_probs = tf.nn.softmax(attention_scores)
199
200 # This is actually dropping out entire tokens to attend to, which might
201 # seem a bit unusual, but is taken from the original Transformer paper.
202 # NOPENOPENOPENOPE

Callers 1

__init__Method · 0.70

Calls 5

get_shape_listFunction · 0.90
dropoutFunction · 0.90
mask_attention_for_ltrFunction · 0.70
create_initializerFunction · 0.70

Tested by

no test coverage detected