-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparamixer_demo.py
292 lines (244 loc) · 8.02 KB
/
paramixer_demo.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
# A demo to show how to use Paramixer for your classification task.
import math
import sys
import numpy as np
import torch
import itertools
from torch import nn
from typing import Union, List
from torch_sparse import spmm
def get_chord_indices_assym(n_vec, n_link):
"""
Generates position indicies, based on the Chord protocol (incl. itself).
:param n_vec: sequence length
:param n_link: number of links in the Chord protocol
:return: target indices in two lists, each is of size n_vec * n_link
"""
rows = list(
itertools.chain(
*[
[i for j in range(n_link)] for i in range(n_vec)
]
)
)
cols = list(
itertools.chain(
*[
[i] + [(i + 2 ** k) % n_vec for k in range(n_link - 1)] for i in range(n_vec)
]
)
)
return rows, cols
def get_dil_indices_assym(n_vec, n_link, n_layer):
"""
Generates the position indicies, based on the symmetric Chord protocol (incl. itself).
So n_link is an odd number
"""
dil_ws = []
for n in range(n_layer):
dilation = 2 ** n
half_link = int((n_link - 1) / 2)
rows = list(
itertools.chain(
*[
[r for _ in range(n_link)] for r in range(n_vec)
]
)
)
cols = list(
itertools.chain(
*[
[i] + [(i + k * dilation) % n_vec for k in range(1, 1 + half_link)] +
[(i - k * dilation) % n_vec for k in range(1, 1 + half_link)] for i in range(n_vec)
]
)
)
rc_tensor = torch.tensor([rows, cols])
rc_list = rc_tensor.tolist()
dil_ws.append(rc_list)
return dil_ws
def MakeMLP(cfg: List[Union[str, int]], in_channels: int, out_channels: int) -> nn.Sequential:
"""
Constructs an MLP based on a given structural config.
"""
layers: List[nn.Module] = []
for i in cfg:
if isinstance(i, int):
layers += [nn.Linear(in_channels, i)]
in_channels = i
else:
layers += [nn.GELU()]
layers += [nn.Linear(in_channels, out_channels)]
return nn.Sequential(*layers)
class MLPBlock(nn.Module):
"""
Constructs a MLP with the specified structure.
"""
def __init__(self, cfg, in_dim, out_dim):
super(MLPBlock, self).__init__()
self.network = MakeMLP(cfg, in_dim, out_dim)
def forward(self, data):
return self.network(data)
class Paramixer(nn.Module):
def __init__(self,
embedding_size,
max_seq_len,
protocol,
dropout1_p,
dropout2_p,
hidden_size
):
super(Paramixer, self).__init__()
self.embedding_size = embedding_size
self.max_seq_len = max_seq_len
self.n_W = int(np.log2(self.max_seq_len))
self.n_links = self.n_W + 1
self.protocol = protocol
self.embedding_each_head = int(embedding_size)
self.hidden_size = int(hidden_size)
self.Ws = [self.hidden_size, 'GELU']
self.V = [self.hidden_size, 'GELU']
self.dropout1_p = dropout1_p
self.dropout2_p = dropout2_p
if self.protocol == "dil":
self.n_links = 9
self.protocol_indicies = torch.tensor(
get_dil_indices_assym(self.max_seq_len, self.n_links, self.n_W)).cuda()
elif self.protocol == "chord":
self.protocol_indicies = torch.tensor(get_chord_indices_assym(self.max_seq_len, self.n_links)).cuda()
# Init Ws
self.fs = nn.ModuleList(
[
MLPBlock(
self.Ws,
self.embedding_size,
self.n_links
)
for _ in range(self.n_W)
]
)
# Init V
self.g = MLPBlock(
self.V,
self.embedding_size,
self.embedding_size
)
self.dropout1 = nn.Dropout(self.dropout1_p)
self.dropout2 = nn.Dropout(self.dropout2_p)
def forward(self, V, data):
# Apply the first dropout
#V = self.dropout1(V)
# Iterate over all heads
# Get V
V = self.g(V)
w_index = 0
for m in range(self.n_W):
# Init residual connection
res_conn = V
# Get W_m
# W = self.fs[h][m](data)
W = self.fs[m](data)
# print(W.shape)
# Multiply W_m and V, get new V
if self.protocol == "dil":
V = spmm(
self.protocol_indicies[w_index],
W.reshape(W.size(0), W.size(1) * W.size(2)),
self.max_seq_len,
self.max_seq_len,
V
)
else:
V = spmm(
self.protocol_indicies,
W.reshape(W.size(0), W.size(1) * W.size(2)),
self.max_seq_len,
self.max_seq_len,
V
)
w_index += 1
# Vs.append(V)
V = V + res_conn
V = self.dropout2(V)
return V
class Classifier(nn.Module):
def __init__(self,
vocab_size,
embedding_size,
max_seq_len,
n_layers,
n_class,
protocol,
dropout1_p,
dropout2_p,
init_embedding,
pos_embedding,
pooling_type,
hidden_size,
problem
):
super(Classifier, self).__init__()
self.vocab_size = vocab_size
self.embedding_size = embedding_size
self.hidden_size = hidden_size
self.max_seq_len = max_seq_len
self.n_layers = n_layers
self.n_class = n_class
self.protocol = protocol
self.dropout1_p = dropout1_p
self.dropout2_p = dropout2_p
self.problem = problem
# Init embedding layer
self.embedding = nn.Embedding(
self.vocab_size,
self.embedding_size,
padding_idx=self.vocab_size-2
)
self.dropout1 = nn.Dropout(self.dropout1_p)
# Init APC
self.apc_embedding = nn.Embedding(
self.max_seq_len,
self.embedding_size
)
self.attention = nn.ModuleList(
[
Paramixer(self.embedding_size, self.max_seq_len, self.protocol, self.dropout1_p, self.dropout2_p, self.hidden_size)
for _ in range(self.n_layers)
]
)
self.init_embedding = init_embedding
if self.init_embedding:
self.init_embed_weights()
self.pos_emb = pos_embedding
self.pooling_type = pooling_type
if self.pooling_type == "FLATTEN":
self.final = nn.Linear(
self.max_seq_len * self.embedding_size,
self.n_class
)
elif self.pooling_type == "CLS":
self.final = nn.Linear(
self.embedding_size,
self.n_class
)
def init_embed_weights(self):
initrange = 0.1
self.embedding.weight.data.uniform_(-initrange, initrange)
self.embedding.weight.requires_grad = True
def forward(self, data):
# Get embedding
data = self.embedding(data)
# Add APC
if self.pos_emb == 'APC':
positions = torch.arange(0, self.max_seq_len).expand(data.size(0), self.max_seq_len).cuda()
pos_embed = self.apc_embedding(positions)
data = data + pos_embed
# Iterate over layers
data = self.dropout1(data)
V = self.dropout1(data)
for l in range(self.n_layers):
V = self.attention[l](V, data)
if self.pooling_type == 'CLS':
V = V[:, 0, :]
V = self.final(V.view(V.size(0), -1))
return V