-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_layers.py
More file actions
180 lines (157 loc) · 6.61 KB
/
Copy pathcustom_layers.py
File metadata and controls
180 lines (157 loc) · 6.61 KB
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
import numpy as np
import tensorflow as tf
from tensorflow import keras
import string
import re
import warnings
warnings.filterwarnings("ignore")
from tensorflow.keras.utils import register_keras_serializable
@register_keras_serializable()
class PositionalEmbedding(keras.layers.Layer):
def __init__(self, sequence_length, input_dim, output_dim, **kwargs):
super().__init__(**kwargs)
self.token_embeddings = keras.layers.Embedding(input_dim=input_dim, output_dim=output_dim)
self.position_embeddings = keras.layers.Embedding(input_dim=sequence_length, output_dim=output_dim)
self.sequence_length = sequence_length
self.input_dim = input_dim
self.output_dim = output_dim
def call(self, inputs):
embedded_tokens = self.token_embeddings(inputs)
length = tf.shape(inputs)[-1]
positions = tf.range(start=0, limit=length, delta=1)
embedded_positions = self.position_embeddings(positions)
return embedded_tokens + embedded_positions
def compute_mask(self, inputs, mask=None):
return keras.ops.not_equal(inputs, 0)
def get_config(self):
config = super(PositionalEmbedding, self).get_config()
config.update({
"input_dim": self.input_dim,
"output_dim": self.output_dim,
"sequence_length": self.sequence_length,
})
return config
@register_keras_serializable()
class TransformerEncoder(keras.layers.Layer):
def __init__(self, embed_dim, dense_dim, num_heads, **kwargs):
super().__init__(**kwargs)
self.embed_dim = embed_dim
self.dense_dim = dense_dim
self.num_heads = num_heads
self.layer_norm_1 = keras.layers.LayerNormalization()
self.layer_norm_2 = keras.layers.LayerNormalization()
self.global_self_attention = MultiHeadAttention(embed_dim=embed_dim, h=num_heads)
self.feed_forward = keras.Sequential(
[keras.layers.Dense(dense_dim, activation="relu"),
keras.layers.Dense(embed_dim),]
)
def call(self, x):
# Post layer normalization + residual connections
x = self.layer_norm_1(x + self.global_self_attention(q=x, k=x, v=x))
x = self.layer_norm_2(x + self.feed_forward(x))
return x
def get_config(self):
config = super().get_config()
config.update({
"embed_dim": self.embed_dim,
"dense_dim": self.dense_dim,
"num_heads": self.num_heads,
})
return config
@register_keras_serializable()
class TransformerDecoder(keras.layers.Layer):
def __init__(self, embed_dim, dense_dim, num_heads, **kwargs):
super().__init__(**kwargs)
self.embed_dim = embed_dim
self.dense_dim = dense_dim
self.num_heads = num_heads
self.causal_self_attention = MultiHeadAttention(embed_dim=embed_dim, h=num_heads)
self.cross_attention = MultiHeadAttention(embed_dim=embed_dim, h=num_heads)
self.feed_forward = keras.Sequential(
[keras.layers.Dense(dense_dim, activation="relu"),
keras.layers.Dense(embed_dim),]
)
self.layer_norm_1 = keras.layers.LayerNormalization()
self.layer_norm_2 = keras.layers.LayerNormalization()
self.layer_norm_3 = keras.layers.LayerNormalization()
def get_config(self):
config = super().get_config()
config.update({
"embed_dim": self.embed_dim,
"dense_dim": self.dense_dim,
"num_heads": self.num_heads,
})
return config
def call(self, x, context):
# Post layer normalization + residual connections
x = self.layer_norm_1(x + self.causal_self_attention(q=x, k=x, v=x, use_causal_mask=True))
x = self.layer_norm_2(x + self.cross_attention(q=x, k=context, v=context))
x = self.layer_norm_3(x + self.feed_forward(x))
return x
def shape_list(x):
"""Deal with dynamic shape in tensorflow cleanly."""
static = x.shape.as_list()
dynamic = tf.shape(x)
return [dynamic[i] if s is None else s for i, s in enumerate(static)]
def attention_mask(nd, ns, *, dtype):
"""1's in the lower triangle, counting from the lower right corner.
Same as tf.matrix_band_part(tf.ones([nd, ns]), -1, ns-nd), but doesn't produce garbage on TPUs.
"""
i = tf.range(nd)[:,None]
j = tf.range(ns)
m = i >= j - ns + nd
return tf.cast(m, dtype)
def mask_attn_weights(w):
_, _, nd, ns = shape_list(w)
b = attention_mask(nd, ns, dtype=w.dtype)
b = tf.reshape(b, [1, 1, nd, ns])
w = w*b - tf.cast(1e10, w.dtype)*(1-b)
return w
def scaled_dot_product_attention(q, k, v, use_causal_mask=False):
d_k = tf.cast(tf.shape(k)[-1], tf.float32)
scores = tf.matmul(q, k, transpose_b=True)
scaled_scores = scores / tf.math.sqrt(d_k)
if use_causal_mask:
scaled_scores = mask_attn_weights(scaled_scores)
weights = tf.nn.softmax(scaled_scores, axis=-1)
output = tf.matmul(weights, v)
return output
@register_keras_serializable()
class MultiHeadAttention(keras.layers.Layer):
def __init__(self, embed_dim, h, **kwargs):
super().__init__(**kwargs)
self.embed_dim = embed_dim
self.h = h
if embed_dim % h != 0:
raise ValueError(
f"dimension of the embedding space = {embed_dim} should be divisible by number of heads = {h}"
)
self.q_linear = keras.layers.Dense(embed_dim)
self.k_linear = keras.layers.Dense(embed_dim)
self.v_linear = keras.layers.Dense(embed_dim)
self.concat_linear = keras.layers.Dense(embed_dim)
def split_heads(self, x, batch_size):
x = tf.reshape(x, shape=(batch_size, -1, self.h, self.embed_dim // self.h))
return tf.transpose(x, perm=[0, 2, 1, 3])
def concat_heads(self, x, batch_size):
x = tf.transpose(x, perm=[0, 2, 1, 3])
return tf.reshape(x, (batch_size, -1, self.embed_dim))
def call(self, q, k, v, use_causal_mask=False):
batch_size = tf.shape(k)[0]
q = self.q_linear(q)
k = self.k_linear(k)
v = self.v_linear(v)
q = self.split_heads(q, batch_size)
k = self.split_heads(k, batch_size)
v = self.split_heads(v, batch_size)
attention = scaled_dot_product_attention(q, k, v, use_causal_mask)
concat = self.concat_heads(attention, batch_size)
concat = self.concat_linear(concat)
return concat
def get_config(self):
config = super(MultiHeadAttention, self).get_config()
config.update({
"embed_dim": self.embed_dim,
"h": self.h,
})
return config