-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
357 lines (315 loc) · 14.3 KB
/
Copy pathmodel.py
File metadata and controls
357 lines (315 loc) · 14.3 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
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
"""
Author: Jonathan Svirsky, 2026
"""
import torch
import torch.nn as nn
import math
import torch.nn.functional as F
from torch.nn.parameter import Parameter
from torch.nn import init
SQRT_2 = math.sqrt(2)
class GatesVector(nn.Module):
def __init__(self, size, sparsity_lambda=1, dtype=None, target_sparsity=1):
super().__init__()
self.dtype = dtype if dtype is not None else torch.float32
self._gates_logits = torch.nn.Parameter(torch.ones(size).type(self.dtype))
torch.nn.init.normal_(self._gates_logits, mean=1., std=0.1).type(self.dtype)
self.sparsity_lambda = sparsity_lambda
self.target_sparsity = target_sparsity
self.noise_mean = 0.
self.noise_std = 0.1
self.register_buffer("noise", torch.empty_like(self._gates_logits))
@property
def gates(self):
if self.training:
self.noise.normal_(self.noise_mean, self.noise_std)
return self.hard_sigmoid(self.mu + self.noise)
else:
return self.eval_gates
@property
def eval_gates(self):
return self.hard_sigmoid(self.mu)
@property
def mu(self):
return torch.tanh(self._gates_logits)
def sparsity_loss(self):
return 0.5 - 0.5 * torch.erf((-0.5 - self.mu) / (0.5 * SQRT_2))
@staticmethod
def hard_sigmoid(x):
return torch.clamp(x + 0.5, 0.0, 1.0)
class SparseLayer(nn.Linear):
def __init__(
self,
in_features: int,
out_features: int,
fan_in_fan_out: bool = False,
original_layer=None,
target_sparsity=0,
dtype='float32',
kurt=False,
kurt_tau=100,
**kwargs
):
if original_layer == None:
nn.Linear.__init__(self, in_features, out_features, **kwargs)
nn.Linear.requires_grad = False
self.flag_pretrained = False
else:
nn.Linear.__init__(self, in_features, out_features)
self.weight.data = original_layer.weight.data.clone().contiguous()
if original_layer.bias is not None:
self.bias.data = original_layer.bias.data.clone().contiguous()
else:
self.bias = Parameter(torch.empty(out_features))
fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
bound = 1 / math.sqrt(fan_in) if fan_in > 0 else 0
init.uniform_(self.bias, -bound, bound)
nn.Linear.requires_grad = False
self.flag_pretrained = True
self.fan_in_fan_out = fan_in_fan_out
if 'dtype' in kwargs:
self.weight_ = self.weight.data.type(getattr(torch, kwargs['dtype']))
else:
self.weight_ = self.weight.data
self.weight = None
self.target_sparsity = target_sparsity
self.gates_columns = GatesVector(
(1, self.weight_.size(1)),
dtype=getattr(torch, dtype),
target_sparsity=target_sparsity)
self.gates_rows = GatesVector(
(1, self.weight_.size(0)),
dtype=getattr(torch, dtype),
target_sparsity=target_sparsity)
self.register_buffer(
"target_keep",
torch.tensor(1.0 - self.target_sparsity, dtype=self.weight_.dtype)
)
# we count only weight params and its reduction
self.initial_params = torch.numel(self.weight_)
if kurt:
self.kurtosis_weights = torch.tensor(0)
self.kurt_tau = kurt_tau
else:
self.kurtosis_weights = None
def T(self, w):
return w.transpose(0, 1) if self.fan_in_fan_out else w
def number_compressed_parameters(self):
gates_columns = self.gates_columns.eval_gates.reshape(-1)
indices_columns = torch.nonzero(gates_columns > 0, as_tuple=True)[0].long()
gates_rows = self.gates_rows.eval_gates.reshape(-1)
indices_rows = torch.nonzero(gates_rows > 0, as_tuple=True)[0].long()
current = torch.numel(torch.index_select(
torch.index_select(self.weight_.to(indices_columns.device), 1, indices_columns),
0,
indices_rows))
return self.initial_params - current
def train(self, mode: bool = True):
nn.Linear.train(self, mode)
self.gates_columns.train(mode)
self.gates_rows.train(mode)
def forward(self, x: torch.Tensor):
if self.weight_.device != x.device:
self.weight_ = self.weight_.to(x.device)
if self.kurtosis_weights is not None:
x_reduced = x.mean(0).mean(0).unsqueeze(0)
rows_gates_eval = self.gates_rows.eval_gates.reshape(-1)
cols_gates_eval = self.gates_columns.eval_gates.reshape(-1)
positive_rows = torch.nonzero(rows_gates_eval > 0, as_tuple=True)[0]
positive_columns = torch.nonzero(cols_gates_eval > 0, as_tuple=True)[0]
activations = x_reduced * self.weight_
# very slow
# activations = x.mean(dim=1, keepdim = True) * gated_weight.unsqueeze(0)
self.kurtosis_weights = (
self.compute_kurtosis_weights(activations[positive_rows][:, positive_columns]),
positive_rows,
positive_columns
)
y = F.linear(x * self.gates_columns.gates, self.weight_, None)
if self.bias is not None:
y = y + self.bias
return y * self.gates_rows.gates
def prepare_for_inference(self, device='cpu'):
with torch.no_grad():
rows_gates_train = self.gates_rows.gates.to(device).reshape(-1)
cols_gates_train = self.gates_columns.gates.to(device).reshape(-1)
self.eval_rows_index = torch.nonzero(rows_gates_train > 0, as_tuple=True)[0].long()
self.eval_cols_index = torch.nonzero(cols_gates_train > 0, as_tuple=True)[0].long()
self.weight_eval = self.T(torch.index_select(rows_gates_train.reshape(-1, 1) * self.weight_.to(device) * cols_gates_train.reshape(1, -1), -1, self.eval_cols_index)).to(device)
self.bias_eval = self.bias.to(device) * rows_gates_train
def target_loss(self, val, target):
return (val - target).abs()
def sparsity_loss(self):
loss_vec_rows = self.gates_rows.sparsity_loss().reshape(-1)
loss_vec_cols = self.gates_columns.sparsity_loss().reshape(-1)
if self.kurtosis_weights is not None:
(sparse_weight_rows, sparse_weight_cols), positive_rows, positive_columns = self.kurtosis_weights
means = torch.stack((
(loss_vec_rows[positive_rows] * sparse_weight_rows).sum(),
(loss_vec_cols[positive_columns] * sparse_weight_cols).sum(),
))
else:
means = torch.stack((loss_vec_rows.mean(), loss_vec_cols.mean()))
return (means - self.target_keep).abs().mean()
@torch.no_grad()
def kurtosis(self, matrix, dim=0):
std = torch.std(matrix, dim, keepdim=True)
mu = torch.mean(matrix, dim, keepdim=True)
# Compute the centered values
centered = matrix - mu
# Compute the zscore
# Set zscores to 0 where std is 0
zscores = centered / std
zscores = torch.where(torch.isnan(zscores), torch.zeros_like(zscores), zscores)
kurt = torch.mean(zscores.pow(4), dim)
return kurt.sqrt()
@torch.no_grad()
def compute_kurtosis_weights(self, activations):
"""Compute the kurtosis (Pearson) of a distribution.
Kurtosis is the fourth central moment divided by the square of the
variance.
"""
columns_kurt = self.kurtosis(activations, 0)
columns_kurt_weights = torch.softmax(-columns_kurt / self.kurt_tau, dim=0)
rows_kurt = self.kurtosis(activations.T, 0)
rows_kurt_weights = torch.softmax(-rows_kurt / self.kurt_tau, dim=0)
return rows_kurt_weights, columns_kurt_weights
class SparseLoRALayer(SparseLayer):
def __init__(
self,
in_features: int,
out_features: int,
lora_rank=4,
lora_dropout=0,
fan_in_fan_out: bool = False,
original_layer=None,
target_sparsity=0,
dtype='float32',
kurt=False,
kurt_tau=100,
**kwargs
):
super().__init__(
in_features,
out_features,
fan_in_fan_out,
original_layer,
target_sparsity,
dtype,
kurt,
kurt_tau,
**kwargs
)
self.fan_in_fan_out = fan_in_fan_out
self.lora_A = nn.Parameter(self.weight_.new_zeros((lora_rank, in_features)))
self.lora_B = nn.Parameter(self.weight_.new_zeros((out_features, lora_rank)))
self.reset_parameters()
if fan_in_fan_out:
self.weight_.data = self.weight_.data.transpose(0, 1)
if lora_dropout > 0.:
self.lora_dropout = nn.Dropout(p=lora_dropout)
else:
self.lora_dropout = lambda x: x
def train(self, mode: bool = True):
nn.Linear.train(self, mode)
self.gates_columns.train(mode)
self.gates_rows.train(mode)
self.lora_dropout.train(mode)
self.lora_A.train(mode)
self.lora_B.train(mode)
def reset_parameters(self):
if hasattr(self, 'lora_A'):
# initialize B the same way as the default for nn.Linear and A to zero
# this is different than what is described in the paper but should not affect performance
nn.init.kaiming_uniform_(self.lora_A, a=math.sqrt(5))
nn.init.zeros_(self.lora_B)
def forward(self, x: torch.Tensor):
y = F.linear(x * self.gates_columns.gates, self.weight_, None)
if self.bias is not None:
y = y + self.bias
result = y * self.gates_rows.gates
result += self.gates_columns.gates * (self.lora_dropout(x) @ self.lora_A.transpose(0, 1) @ self.lora_B.transpose(0, 1)) * self.gates_rows.gates.reshape(-1,1)
return result
class SparseLayerPretrain(nn.Linear):
def __init__(
self,
in_features: int,
out_features: int,
fan_in_fan_out: bool = False,
original_layer=None,
target_sparsity=0,
dtype='float32',
**kwargs
):
if original_layer == None:
nn.Linear.__init__(self, in_features, out_features, **kwargs)
self.flag_pretrained = False
else:
# Create a new linear layer
nn.Linear.__init__(self, in_features, out_features)
# Copy weights and biases from the original layer to the new layer
self.weight.data = original_layer.weight.data.clone()
if original_layer.bias is not None:
self.bias.data = original_layer.bias.data.clone()
else:
self.bias = Parameter(torch.empty(out_features))
fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
bound = 1 / math.sqrt(fan_in) if fan_in > 0 else 0
init.uniform_(self.bias, -bound, bound)
self.flag_pretrained = True
self.fan_in_fan_out = fan_in_fan_out
self.target_sparsity = target_sparsity
self.gates_columns = GatesVector(
(1, self.weight.size(1)),
dtype=getattr(torch, dtype),
target_sparsity=target_sparsity)
self.gates_rows = GatesVector(
(1, self.weight.size(0)),
dtype=getattr(torch, dtype),
target_sparsity=target_sparsity)
# we count only weight params and its reduction
self.initial_params = torch.numel(self.weight)
self.register_buffer(
"target_keep",
torch.tensor(1.0 - self.target_sparsity, dtype=self.weight.dtype)
)
def T(self, w):
return w.transpose(0, 1) if self.fan_in_fan_out else w
def number_compressed_parameters(self):
gates_columns = self.gates_columns.eval_gates.reshape(-1)
indices_columns = torch.nonzero(gates_columns > 0, as_tuple=True)[0].long()
gates_rows = self.gates_rows.eval_gates.reshape(-1)
indices_rows = torch.nonzero(gates_rows > 0, as_tuple=True)[0].long()
current = torch.numel(
torch.index_select(
torch.index_select(
self.weight.to(indices_columns.device), 1, indices_columns),
0,
indices_rows))
return self.initial_params - current
def train(self, mode: bool = True):
nn.Linear.train(self, mode)
self.gates_columns.train(mode)
self.gates_rows.train(mode)
def forward(self, x: torch.Tensor):
if self.weight.device != x.device:
self.weight = self.weight.to(x.device)
y = F.linear(x * self.gates_columns.gates, self.weight, None)
if self.bias is not None:
y = y + self.bias
return y * self.gates_rows.gates
def prepare_for_inference(self, device='cpu'):
with torch.no_grad():
rows_gates_train = self.gates_rows.gates.to(device).reshape(-1)
cols_gates_train = self.gates_columns.gates.to(device).reshape(-1)
self.eval_rows_index = torch.nonzero(rows_gates_train > 0, as_tuple=True)[0].long()
self.eval_cols_index = torch.nonzero(cols_gates_train > 0, as_tuple=True)[0].long()
self.weight_eval = torch.index_select(rows_gates_train.reshape(-1, 1) * self.weight.to(device) * cols_gates_train.reshape(1, -1), -1, self.eval_cols_index)
self.bias_eval = self.bias.to(device) * rows_gates_train
def target_loss(self, val, target):
return (val - target).abs()
def sparsity_loss(self):
loss_vec_rows = self.gates_rows.sparsity_loss()
loss_vec_cols = self.gates_columns.sparsity_loss()
means = torch.stack((loss_vec_rows.mean(), loss_vec_cols.mean()))
return (means - self.target_keep).abs().mean()