-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathstip_original.py
282 lines (222 loc) · 9.43 KB
/
stip_original.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""
reference: https://towardsdatascience.com/build-your-own-transformer-from-scratch-using-pytorch-84c850470dcb
"""
import torch
import torch.nn as nn
import math
import numpy as np
import copy
import torch.nn.functional as F
class MultiHeadAttention(nn.Module):
"""
"""
def __init__(self, d_model, num_heads):
super(MultiHeadAttention, self).__init__()
assert d_model % num_heads == 0
self.d_model = d_model
self.num_heads = num_heads
self.d_k = d_model // num_heads
self.w_q = nn.Linear(d_model, d_model, bias=True)
self.w_k = nn.Linear(d_model, d_model, bias=True)
self.w_v = nn.Linear(d_model, d_model, bias=True)
self.w_o = nn.Linear(d_model, d_model, bias=True)
def split_heads(self, x):
batch_size, seq_length, d_model = x.size()
# d_model = num_heads * d_k
return x.view(batch_size, seq_length, self.num_heads, self.d_k).transpose(1, 2)
def scaled_dot_product_attention(self, q, k, v, mask):
attention_scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.d_k)
# shape [BS, NUM_HEADS, SEQ_LEN, SEQ_LEN]
if mask is not None:
attention_scores = attention_scores.masked_fill(mask == 0, -1e9)
attention_probs = torch.softmax(attention_scores, dim=-1)
return torch.matmul(attention_probs, v)
def combine_heads(self, x):
batch_size, num_heads, seq_length, d_k = x.size()
return x.transpose(1, 2).contiguous().view(batch_size, seq_length, self.d_model)
def forward(self, q, k, v, mask):
q = self.split_heads(self.w_q(q))
k = self.split_heads(self.w_k(k))
v = self.split_heads(self.w_v(v))
attention_output = self.scaled_dot_product_attention(q, k, v, mask)
attention_output = self.combine_heads(attention_output)
return self.w_o(attention_output)
class FeedForward(nn.Module):
"""
"""
def __init__(self, d_model, d_ff):
super(FeedForward, self).__init__()
self.fc1 = nn.Linear(d_model, d_ff, bias=True)
self.fc2 = nn.Linear(d_ff, d_model, bias=True)
self.relu = nn.ReLU()
def forward(self, x):
return self.fc2(self.relu(self.fc1(x)))
class EncoderBlock(nn.Module):
"""
"""
def __init__(self, d_model, num_heads, d_ff):
super(EncoderBlock, self).__init__()
self.attention_layer = MultiHeadAttention(d_model, num_heads)
self.ff_layer = FeedForward(d_model, d_ff)
self.ln1 = nn.LayerNorm(d_model)
self.ln2 = nn.LayerNorm(d_model)
def forward(self, x, mask=None):
attention_output = self.attention_layer(x, x, x, mask)
x0 = x + attention_output
x1 = self.ln1(x0)
ff_output = self.ff_layer(x1)
x2 = self.ln2(x1 + ff_output)
return x2
class DecoderBlock(nn.Module):
"""
"""
def __init__(self, d_model, num_heads, d_ff):
super(DecoderBlock, self).__init__()
self.self_attention = MultiHeadAttention(d_model, num_heads)
self.cross_attention = MultiHeadAttention(d_model, num_heads)
self.ff_layer = FeedForward(d_model, d_ff)
self.ln1 = nn.LayerNorm(d_model)
self.ln2 = nn.LayerNorm(d_model)
self.ln3 = nn.LayerNorm(d_model)
def forward(self, x, enc_output, src_mask, tgt_mask):
self_attention_output = self.self_attention(x, x, x, tgt_mask)
x = self.ln1(x + self_attention_output)
cross_attention_output = self.cross_attention(x, enc_output, enc_output, src_mask)
x = self.ln2(x + cross_attention_output)
ff_output = self.ff_layer(x)
x = self.ln3(x + ff_output)
return x
class Transformer(nn.Module):
"""
"""
def __init__(self, d_model, num_heads, d_ff, num_layers, output_dim):
super(Transformer, self).__init__()
self.enc_layers = nn.ModuleList([EncoderBlock(d_model, num_heads, d_ff) for _ in range(num_layers)])
self.dec_layers = nn.ModuleList([DecoderBlock(d_model, num_heads, d_ff) for _ in range(num_layers)])
self.fc = nn.Linear(d_model, output_dim)
def generate_mask(self, src, tgt):
src_mask = (src != 0).unsqueeze(1).unsqueeze(2)
tgt_mask = (tgt != 0).unsqueeze(1).unsqueeze(3)
seq_length = tgt.size(1)
nopeak_mask = (1 - torch.triu(torch.ones(1, seq_length, seq_length), diagonal=1)).bool()
tgt_mask = tgt_mask & nopeak_mask
return src_mask, tgt_mask
def forward(self, x, target):
src_mask, tgt_mask = self.generate_mask(x, target)
enc_output = x
for enc_l in self.enc_layers:
enc_output = enc_l(enc_output, src_mask)
dec_output = target
for dec_l in self.dec_layers:
dec_output = dec_l(dec_output, enc_output, src_mask, tgt_mask)
output = self.fc(dec_output)
return output
def inv_permutation(p):
inv_p = [0]*len(p)
for old_idx, new_idx in enumerate(p):
inv_p[new_idx] = old_idx
return inv_p
def permute_attention(attn_layer, p):
inv_p = inv_permutation(p)
print(inv_p)
p_attn_layer = copy.deepcopy(attn_layer)
with torch.no_grad():
for name, para in p_attn_layer.named_parameters():
if name in ["w_q.weight", "w_k.weight", "w_v.weight"]:
para.data = para.data[:, p]
if name in ["w_o.weight", "w_o.bias"]:
para.data = para.data[p]
return p_attn_layer
def permute_feedforward(ffn_layer, p):
inv_p = inv_permutation(p)
p_ffn_layer = copy.deepcopy(ffn_layer)
with torch.no_grad():
for name, para in p_ffn_layer.named_parameters():
# print(name)
if name in ["fc1.weight"]:
para.data = para.data[:, p]
if name in ["fc2.weight", "fc2.bias"]:
para.data = para.data[p]
return p_ffn_layer
def permute_block(blk, p):
inv_p = inv_permutation(p)
p_blk = copy.deepcopy(blk)
with torch.no_grad():
for name, para in p_blk.named_parameters():
if name in ["attention_layer.w_q.weight",
"attention_layer.w_k.weight",
"attention_layer.w_v.weight",
"ff_layer.fc1.weight"]:
para.data = para.data[:, p]
if name in ["attention_layer.w_o.weight",
"attention_layer.w_o.bias",
"ff_layer.fc2.weight",
"ff_layer.fc2.bias"]:
para.data = para.data[p]
return p_blk
if __name__ == "__main__":
BS = 2
SEQLEN = 3
DMODEL = 4
DFF = 8
NHEADS = 2
TEST_ATT = 0
TEST_FFN = 0
TEST_BLK = 1
if TEST_ATT:
x = torch.from_numpy(np.random.rand(BS, SEQLEN, DMODEL)).float()
attention_layer = MultiHeadAttention(d_model=DMODEL, num_heads=NHEADS)
mask = (1 - torch.triu(torch.ones(1, SEQLEN, SEQLEN), diagonal=1)).bool()
with torch.no_grad():
y = attention_layer(x, x, x, None)
y_mask = attention_layer(x, x, x, mask)
p = np.random.permutation(x.shape[2])
print(p)
xp = x[:, :, p]
p_attention_layer = permute_attention(attention_layer, p)
with torch.no_grad():
print("Without mask:")
yp = p_attention_layer(xp, xp, xp, None)
diff = np.abs(y[:, :, p] - yp).sum()
print("Original reslut:\n", y, "\nNew result:\n", yp)
print("Diff=", diff)
print("With mask:")
yp_mask = p_attention_layer(xp, xp, xp, mask)
diff_mask = np.abs(y_mask[:, :, p] - yp_mask).sum()
print("Original reslut:\n", y_mask, "\nNew result:\n", yp_mask)
print("Diff=", diff_mask)
if TEST_FFN:
x = torch.from_numpy(np.random.rand(BS, SEQLEN, DMODEL)).float()
ffn_layer = FeedForward(DMODEL, DFF)
p = np.random.permutation(DMODEL)
print(f"Permutation={p}")
xp = x[:, :, p]
p_ffn_layer = permute_feedforward(ffn_layer, p)
with torch.no_grad():
print("Feedforward Layer:")
y = ffn_layer(x)
yp = p_ffn_layer(xp)
diff = np.abs(y[:, :, p] - yp).sum()
print("Original reslut:\n", y, "\nNew result:\n", yp)
print("Diff=", diff)
if TEST_BLK:
x = torch.from_numpy(np.random.rand(BS, SEQLEN, DMODEL)).float()
block = EncoderBlock(DMODEL, NHEADS, DFF)
p = np.random.permutation(DMODEL)
print(f"Permutation={p}")
xp = x[:, :, p]
p_block = permute_block(block, p)
mask = (1 - torch.triu(torch.ones(1, SEQLEN, SEQLEN), diagonal=1)).bool()
with torch.no_grad():
print("Encoder Block:")
y = block(x, None)
yp = p_block(xp, None)
diff = np.abs(y[:, :, p] - yp).sum()
print("Original reslut:\n", y, "\nNew result:\n", yp)
print("Diff=", diff)
print("Decoder Block:")
y = block(x, mask)
yp = p_block(xp, mask)
diff = np.abs(y[:, :, p] - yp).sum()
print("Original reslut:\n", y, "\nNew result:\n", yp)
print("Diff=", diff)