-
Notifications
You must be signed in to change notification settings - Fork 2
/
load_model.py
355 lines (287 loc) · 12 KB
/
load_model.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
from torch import nn
from helper import *
class LoRA(nn.Module):
def __init__(self, width, rank, std = .25):
super().__init__()
# initialize the parameters
self.lora_A = nn.Parameter(torch.randn(width, rank))
self.lora_A.data.fill_(0.00)
self.lora_A.requires_grad = True
self.lora_B = nn.Parameter(torch.randn(width, rank))
nn.init.normal_(self.lora_B, mean=0.0, std=std)
self.lora_B.requires_grad = True
def forward_matrix(self, x):
# x: (batch_size, width)
# lora_A: (width, rank)
# lora_B: (width, rank)
# lora_B @ lora_A^T: (width, width)
# x @ lora_B @ lora_A^T: (batch_size, width)
return torch.matmul(x, self.lora_B @ self.lora_A.T)
def forward_tensor(self, X):
# x: (batch_size, width, seq_length)
# lora_A: (width, rank)
# lora_B: (width, rank)
# lora_A @ lora_B^T: (width, width)
# matmul(self.lora_A @ self.lora_B.T, X): (batch_size, width, seq_length)
return torch.matmul(self.lora_A @ self.lora_B.T, X)
def forward(self, x):
# identify the shape of x
if x.dim() <= 2:
return self.forward_matrix(x)
else:
return self.forward_tensor(x)
class FNN(nn.Module):
def __init__(
self,
depth,
width,
rank,
std = .25,
use_bias = True,
apply_lora = True,
activation = 'relu',
rank_step = 0,
):
super().__init__()
self.depth = depth
self.width = width
self.rank = rank
if activation == 'relu':
self.activation = nn.ReLU()
elif activation == 'linear':
self.activation = lambda x: x
else:
raise NotImplementedError(f'We only support relu and linear activation, and {activation} is not supported.')
self.linearlist = nn.ModuleList([nn.Linear(width, width, bias=use_bias) for _ in range(depth)])
self.apply_lora = apply_lora
if self.apply_lora:
if rank_step == 0:
self.loralist = nn.ModuleList([LoRA(width, rank, std) for _ in range(depth)])
elif rank_step > 0:
self.loralist = nn.ModuleList([LoRA(width, min(rank + rank_step * l, width), std) for l in range(depth)])
else:
self.loralist = nn.ModuleList([LoRA(width, max(rank - rank_step * l, 0), std) for l in range(depth)])
def forward(self, x):
for l in range(self.depth):
linear_x = self.linearlist[l](x)
lora_x = self.loralist[l](x) if self.apply_lora else 0
x = linear_x + lora_x
x = self.activation(x)
return x
class MultiheadAttention(nn.Module):
"""
This class implment the multi-head self-attention layer defined in the paper
"The Expressive Power of Low-Rank Adaptation"
"""
# comments: I have tried to directly use the multihead attention
# layer in pytorch, but it seems that their implementation
# is weird and does not support square weight matrix for
# each head...
def __init__(
self,
embed_dim,
n_head,
rank,
std,
apply_lora,
):
super().__init__()
self.embed_dim = embed_dim
self.n_head = n_head
# Initialize the weight matrices for all heads
# Wq, Wk, Wv, Wo: (embed_dim, embed_dim) for each head
self.Wq = nn.ParameterList([nn.Parameter(torch.randn(embed_dim, embed_dim)) for _ in range(n_head)])
self.Wk = nn.ParameterList([nn.Parameter(torch.randn(embed_dim, embed_dim)) for _ in range(n_head)])
self.Wv = nn.ParameterList([nn.Parameter(torch.randn(embed_dim, embed_dim)) for _ in range(n_head)])
self.Wo = nn.ParameterList([nn.Parameter(torch.randn(embed_dim, embed_dim)) for _ in range(n_head)])
self.apply_lora = apply_lora
if self.apply_lora:
# for each head, we have adapter for Wq, Wk, Wv, Wo
self.loralist = nn.ModuleList([LoRA(embed_dim, rank, std) for _ in range(n_head*4)])
def forward_head(self, x, h):
if self.apply_lora:
Wq = self.Wq[h] + self.loralist[h*4].lora_A @ self.loralist[h*4].lora_B.T
Wk = self.Wk[h] + self.loralist[h*4+1].lora_A @ self.loralist[h*4+1].lora_B.T
Wv = self.Wv[h] + self.loralist[h*4+2].lora_A @ self.loralist[h*4+2].lora_B.T
Wo = self.Wo[h] + self.loralist[h*4+3].lora_A @ self.loralist[h*4+3].lora_B.T
else:
Wq = self.Wq[h]
Wk = self.Wk[h]
Wv = self.Wv[h]
Wo = self.Wo[h]
# compute the attention score for each head
# attn_score: (batch_size, seq_length, seq_length)
attn_score = torch.bmm(torch.matmul(Wk, x).permute(0,2,1), torch.matmul(Wq, x))
# compute the attention weights for each head
# softmax is applied column-wise
# attn_weights: (batch_size, seq_length, seq_length)
attn_weights = torch.softmax(attn_score, dim = 1)
# compute the output for each head
# attn_output: (batch_size, embed_dim, seq_length)
attn_output = torch.matmul(Wv, x) @ attn_weights
return Wo @ attn_output
def forward(self, x):
"""
Args:
x: (batch_size, embed_dim, seq_length)
Returns:
(batch_size, embed_dim, seq_length)
"""
result = torch.zeros_like(x)
# compute the output for each head
for h in range(self.n_head):
result = result + self.forward_head(x, h)
# batch_size * embed_dim * seq_length
return result
class TFB(nn.Module):
def __init__(
self,
embed_dim,
n_head,
rank,
std,
apply_lora,
is_last,
better_optim = 0,
dropout = 0.1,
):
super().__init__()
self.embed_dim = embed_dim
self.n_head = n_head
# initialize the multi-head attention and feed-forward network
self.attention = MultiheadAttention(
embed_dim,
n_head,
rank,
std,
apply_lora,
)
self.feed_forward = nn.ModuleList([
nn.Linear(embed_dim, embed_dim),
nn.Linear(embed_dim, embed_dim),
])
self.better_optim = better_optim
if self.better_optim:
self.norm = nn.LayerNorm(embed_dim)
self.dropout = nn.Dropout(dropout)
self.apply_lora = apply_lora
self.is_last = is_last
if self.apply_lora and self.is_last:
# we add lora adapter for the second feed-forward layer if it is the last block
self.W2_lora = LoRA(embed_dim, rank, std)
def forward_ff1(self, x):
"""Get the output of the first feedforward layer
Args:
x: (batch_size, embed_dim, seq_length)
Returns:
f1_output: (batch_size, embed_dim, seq_length)
"""
batch_size, seq_length = x.shape[0], x.shape[2]
# multi-head attention: (batch_size, embed_dim, seq_length)
attn_output = self.attention(x)
if self.better_optim:
# residual connection + dropout + layer normalization
attn_output = self.norm((x + self.dropout(attn_output)).permute(0, 2, 1)).permute(0, 2, 1)
# apply the first feedforward layer
# 1. reshape attn_output to (batch_size * seq_length, embed_dim)
attn_output_reshaped = attn_output.permute(0, 2, 1).reshape(-1, self.embed_dim)
# 2. apply the first feed-forward layer -> (batch_size * seq_length, embed_dim)
f1_output_reshaped = self.feed_forward[0](attn_output_reshaped)
# 3. permute it back to (batch_size, embed_dim, seq_length)
f1_output = f1_output_reshaped.reshape(batch_size, seq_length, self.embed_dim).permute(0, 2, 1)
# 4. apply relu
f1_output = torch.relu(f1_output)
return f1_output, attn_output
def forward_ff2(self, x):
"""Get the output of the second feedforward layer
Args:
f1_output: (batch_size, embed_dim, seq_length)
Returns:
f2_output: (batch_size, embed_dim, seq_length)
"""
batch_size, seq_length = x.shape[0], x.shape[2]
# get the output of the first feedforward layer
f1_output, attn_output = self.forward_ff1(x)
# apply the second feedforward layer
# 1. reshape f1_output to (batch_size * seq_length, embed_dim)
f1_output_reshaped = f1_output.permute(0, 2, 1).reshape(-1, self.embed_dim)
# 2. apply the second feed-forward layer -> (batch_size * seq_length, embed_dim)
f2_output_reshaped = self.feed_forward[1](f1_output_reshaped)
# 3. permute it back to (batch_size, embed_dim, seq_length)
f2_output = f2_output_reshaped.reshape(batch_size, seq_length, self.embed_dim).permute(0, 2, 1)
# 4. apply lora if this is the last block
if self.apply_lora and self.is_last:
# f2_output: (batch_size, embed_dim, seq_length)
f2_output = f2_output + self.W2_lora(f1_output)
if self.better_optim:
# residual connection + dropout + layer normalization
f2_output = self.norm((attn_output + self.dropout(f2_output)).permute(0, 2, 1)).permute(0, 2, 1)
return f2_output
def forward(self, x):
"""_summary_
Args:
x: (batch_size, embed_dim, seq_length)
Returns:
f2_output: (batch_size, embed_dim, seq_length)
"""
return self.forward_ff2(x)
class TFN(nn.Module):
def __init__(
self,
embed_dim,
n_head,
depth,
rank,
std,
apply_lora,
better_optim = 0,
dropout = 0.1,
):
super().__init__()
self.embed_dim = embed_dim
self.tfblist = nn.ModuleList([TFB(
embed_dim,
n_head,
rank,
std,
apply_lora,
is_last = False,
better_optim = better_optim,
dropout = dropout,
) for _ in range(depth-1)]
)
self.tfblist.append(TFB(
embed_dim,
n_head,
rank,
std,
apply_lora,
is_last = True,
better_optim = better_optim,
dropout = dropout,
))
self.output_layer = nn.Linear(embed_dim, embed_dim, bias = False)
self.apply_lora = apply_lora
if self.apply_lora:
# apply lora to the output layer
self.output_layer_lora = LoRA(embed_dim, rank, std)
def forward(self, x):
"""_summary_
Args:
x: (batch_size, embed_dim, seq_length)
Returns:
x: (batch_size, embed_dim, seq_length)
"""
batch_size, seq_length = x.shape[0], x.shape[2]
for block in self.tfblist:
x = block(x)
# apply the output layer
# 1. reshape x to (batch_size * seq_length, embed_dim)
x_reshaped = x.permute(0, 2, 1).reshape(-1, self.embed_dim)
# 2. apply the output layer -> (batch_size * seq_length, embed_dim)
output_reshaped = self.output_layer(x_reshaped)
# 3. permute it back to (batch_size, embed_dim, seq_length)
output = output_reshaped.reshape(batch_size, seq_length, self.embed_dim).permute(0, 2, 1)
if self.apply_lora:
output = output + self.output_layer_lora(x)
return output