-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrewire_testing.py
383 lines (308 loc) · 10.8 KB
/
rewire_testing.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import numpy as np
import torch
import torch.nn as nn
from copy import deepcopy
from transformers import (
AdamW,
)
from torch.nn import MSELoss as mse
from pprint import pprint
class BackHook:
def __init__(self):
self.grad_input = {}
self.grad_output = {}
self.layer_num = 0
def __call__(self, module, grad_in, grad_out):
print(module)
print(grad_out)
grad_out = torch.abs(grad_out[0])
if not hasattr(module, "name"):
setattr(module, "name", self.layer_num)
print("updating")
self.grad_output[self.layer_num] = grad_out
self.layer_num += 1
else:
print("calculating mean")
# take mean along batch dimension
layer_num = getattr(module, "name")
self.grad_output[layer_num] = torch.mean(
torch.stack([self.grad_output[layer_num], grad_out]), dim=0
)
def print(self):
print(len(self.grad_output))
pprint(self.grad_output)
def seed_everything(seed: int):
import random, os
import numpy as np
import torch
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
bhookfn = BackHook()
seed_everything(101)
def assert_array_equal(tens_a, tens_b):
# check if they are fully equal
print("Fully Equal: ", torch.all(tens_a.eq(tens_b)))
print("Outputs without permuting weights: ")
print(tens_a.detach().numpy())
print("Outputs with permuting weights:")
print(tens_b.detach().numpy())
# check if they are close to each other upto tolerance of 1e-08
assert torch.all(torch.isclose(tens_a, tens_b))
def inverse_permutation(permutation_order):
inv = torch.empty_like(permutation_order)
inv[permutation_order] = torch.arange(
permutation_order.size(0), device=permutation_order.device
)
return inv
def permute_linear(W, permutation, dim="col", permute_weight=False, permute_bias=False):
"""
Permute linear layer
:param W: weight matrix
:param permutation: permutation order for the weights
:param dim: 'row' or 'col'
:param permute_bias: whether to permute the bias
"""
_W = deepcopy(W)
if permute_bias:
# print("Bias: ")
# print(_W.bias.shape)
# print(permutation.shape)
# print(dim)
_W.bias.data.copy_(_W.bias[permutation])
if permute_weight:
# print("Weights: ")
# print(_W.weight.shape)
# print(permutation.shape)
# print(dim)
if dim == "col":
# permute columns
_W.weight.data.copy_(_W.weight[:, permutation])
elif dim == "row":
# permute rows
_W.weight.data.copy_(_W.weight[permutation, :])
else:
raise NotImplementedError
return _W
def get_permutation_order(dim):
# get random permutation order
# this is used for testing
return torch.randperm(dim)
def transformer_layer(
_input, W_att, W_o, W_1, W_2, permute=False, residual=True, return_all_outputs=True
):
"""
W_* -> linear layer with weights and bias
:param _input: input of size batch, emb_dim
:param W_att: linear layer of weight: (emb_dim, attn_dim), bias: (attn_dim,)
:param W_o: linear layer of weight: (attn_dim, emb_dim), bias: (emb_dim,)
:param W_1: linear layer of weight: (emb_dim, intermediate_dim), bias: (intermediate_dim,)
:param W_2: linear layer of weight: (intermediate_dim, emb_dim), bias: (emb_dim,)
:param permute: Whether to permute weights
:param residual: whether to use residual connection
"""
final_outputs = {
"output": None,
"per_layer_outputs": None,
"permutation_orders": None,
}
if permute:
_, emb_dim = _input.size()
permutation_order_1 = get_permutation_order(emb_dim)
# permuting according to embedding dim
# in our code with embeddings matrix, we will permute the emb matrix and not the actual inputs (like we do here)
_input = _input[:, permutation_order_1]
# print("permuting W_att")
W_att = permute_linear(
W_att,
permutation_order_1,
dim="col",
permute_weight=True,
permute_bias=False,
)
# since emb_dim is destroyed in prev layer, we recompute the permutation order here
permutation_order_2 = get_permutation_order(emb_dim)
# print("permuting W_o")
W_o = permute_linear(
W_o, permutation_order_2, dim="row", permute_weight=True, permute_bias=True
)
# print("permuting W_1")
W_1 = permute_linear(
W_1, permutation_order_2, dim="col", permute_weight=True, permute_bias=False
)
# since emb_dim is destroyed in prev layer, we recompute the permutation order here
permutation_order_3 = get_permutation_order(emb_dim)
# print("permuting W_2")
W_2 = permute_linear(
W_2, permutation_order_3, dim="row", permute_weight=True, permute_bias=True
)
outputs = []
out1 = W_att(_input)
out2 = W_o(out1)
outputs.append(out1)
outputs.append(out2)
if residual:
if permute:
inv_permutation_order_1 = inverse_permutation(permutation_order_1)
_input = _input[:, inv_permutation_order_1]
_input = _input[:, permutation_order_2]
out2 = _input + out2
outputs.append(out2)
out3 = W_1(out2)
out4 = W_2(out3)
outputs.append(out3)
outputs.append(out4)
if residual:
if permute:
inv_permutation_order_2 = inverse_permutation(permutation_order_2)
out2 = out2[:, inv_permutation_order_2]
out2 = out2[:, permutation_order_3]
out4 = out4 + out2
outputs.append(out4)
if permute:
inv_permutation_order_3 = inverse_permutation(permutation_order_3)
out4 = out4[:, inv_permutation_order_3]
if return_all_outputs:
final_outputs["per_layer_outputs"] = outputs
if permute:
permutation_orders = [
permutation_order_1,
permutation_order_2,
None,
permutation_order_3,
]
if residual:
permutation_orders = [
permutation_order_1,
permutation_order_2,
None,
None,
permutation_order_3,
None,
]
final_outputs["permutation_orders"] = permutation_orders
final_outputs["output"] = out4
return final_outputs
if __name__ == "__main__":
lr = 5e-5
batch_size = 1
emb_dim = 5
attn_dim = 6
intermediate_dim = 7
output_size = 10
n_layers = 1
n_epochs = 2
loss = mse(reduction="mean")
parameters = []
W_atts = []
W_os = []
W_1s = []
W_2s = []
for _ in range(n_layers):
W_att = nn.Linear(emb_dim, attn_dim)
W_o = nn.Linear(attn_dim, emb_dim)
W_1 = nn.Linear(emb_dim, intermediate_dim)
W_2 = nn.Linear(intermediate_dim, emb_dim)
W_att.register_backward_hook(bhookfn)
W_o.register_backward_hook(bhookfn)
W_1.register_backward_hook(bhookfn)
W_2.register_backward_hook(bhookfn)
W_atts.append(W_att)
W_os.append(W_o)
W_1s.append(W_1)
W_2s.append(W_2)
parameters += W_att.parameters()
parameters += W_o.parameters()
parameters += W_1.parameters()
parameters += W_2.parameters()
W_output = nn.Linear(emb_dim, output_size)
W_output.register_backward_hook(bhookfn)
parameters += W_output.parameters()
optimizer = AdamW(parameters, lr=lr)
for epoch in range(n_epochs):
_input = torch.randn(batch_size, emb_dim, requires_grad=False)
labels = torch.randn((batch_size, output_size))
prev_layer_output1 = _input
prev_layer_output2 = _input
for idx in range(n_layers):
W_att, W_o, W_1, W_2 = W_atts[idx], W_os[idx], W_1s[idx], W_2s[idx]
# W_att.zero_grad()
# W_o.zero_grad()
# W_1.zero_grad()
# W_2.zero_grad()
# exp1 = transformer_layer(
# prev_layer_output1, W_att, W_o, W_1, W_2, permute=False, residual=False
# )
exp2 = transformer_layer(
prev_layer_output2,
W_att,
W_o,
W_1,
W_2,
permute=False,
residual=False,
)
# prev_layer_output1 = exp1["output"]
prev_layer_output2 = exp2["output"]
logits = W_output(prev_layer_output2)
loss_val = loss(logits, labels)
loss_val.backward()
bhookfn.print()
strings = ["W_att_output: ", "W_o_output: ", "W_1_output: ", "W_2_output: "]
# for _string, exp1_out, exp2_out, permutation_order in zip(
# strings,
# exp1["per_layer_outputs"],
# exp2["per_layer_outputs"],
# exp2["permutation_orders"],
# ):
# print(_string)
# print(exp1_out)
# print(exp2_out)
# print(permutation_order)
# print()
print("Testing without residual connections: ")
# assert_array_equal(exp1["output"], exp2["output"])
# print(_input)
# prev_layer_output1 = _input
# prev_layer_output2 = _input
# for _ in range(n_layers):
# W_att = nn.Linear(emb_dim, attn_dim)
# W_o = nn.Linear(attn_dim, emb_dim)
# W_1 = nn.Linear(emb_dim, intermediate_dim)
# W_2 = nn.Linear(intermediate_dim, emb_dim)
# with torch.no_grad():
# exp1 = transformer_layer(
# _input, W_att, W_o, W_1, W_2, permute=False, residual=True
# )
# exp2 = transformer_layer(
# _input, W_att, W_o, W_1, W_2, permute=True, residual=True
# )
# logits = W_output(exp1["output"])
# prev_layer_output1 = exp1["output"]
# prev_layer_output2 = exp2["output"]
# # strings = [
# # "W_att_output: ",
# # "W_o_output: ",
# # "Residual1: ",
# # "W_1_output: ",
# # "W_2_output: ",
# # "Residual2: ",
# # ]
# # for _string, exp1_out, exp2_out, permutation_order in zip(
# # strings,
# # exp1["per_layer_outputs"],
# # exp2["per_layer_outputs"],
# # exp2["permutation_orders"],
# # ):
# # print(_string)
# # print(exp1_out)
# # print(exp2_out)
# # print(permutation_order)
# # print()
# print()
# print("Testing with residual connections: ")
# assert_array_equal(exp1["output"], exp2["output"])