forked from IST-DASLab/sparsegpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparsegpt.py
689 lines (562 loc) · 25.6 KB
/
sparsegpt.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
import math
import time
import torch
import torch.nn as nn
import transformers
from quant import *
DEBUG = False
torch.backends.cuda.matmul.allow_tf32 = False
torch.backends.cudnn.allow_tf32 = False
from utils import *
from snip.snip import *
from snip.train import *
from sparse_op import *
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
from net_utils import admm_solve, faster_admm_solve
class SparseGPT:
def __init__(self, layer):
self.layer = layer
self.dev = self.layer.weight.device
W = layer.weight.data.clone()
if isinstance(self.layer, nn.Conv2d):
W = W.flatten(1)
if isinstance(self.layer, transformers.Conv1D):
W = W.t()
self.rows = W.shape[0]
self.columns = W.shape[1]
self.H = torch.zeros((self.columns, self.columns), device=self.dev)
self.nsamples = 0
def add_batch(self, inp, out, blocksize=1024):
if DEBUG:
self.inp1 = inp
self.out1 = out
return
if len(inp.shape) == 2:
inp = inp.unsqueeze(0)
tmp = inp.shape[0]
if isinstance(self.layer, nn.Linear) or isinstance(self.layer, transformers.Conv1D):
if len(inp.shape) == 3:
inp = inp.reshape((-1, inp.shape[-1]))
inp = inp.t()
self.H *= self.nsamples / (self.nsamples + tmp)
self.nsamples += tmp
inp = math.sqrt(2 / self.nsamples) * inp.float()
self.H += inp.matmul(inp.t())
def fasterprune(
self, sparsity, prunen=0, prunem=0, blocksize=128, percdamp=.01
):
W = self.layer.weight.data.clone()
if isinstance(self.layer, nn.Conv2d):
W = W.flatten(1)
if isinstance(self.layer, transformers.Conv1D):
W = W.t()
W = W.float()
if hasattr(self, 'quantizer'):
if not self.quantizer.ready():
self.quantizer.find_params(W, weight=True)
tick = time.time()
H = self.H
del self.H
dead = torch.diag(H) == 0
H[dead, dead] = 1
W[:, dead] = 0
Losses = torch.zeros(self.rows, device=self.dev)
damp = percdamp * torch.mean(torch.diag(H))
diag = torch.arange(self.columns, device=self.dev)
H[diag, diag] += damp
H = torch.linalg.cholesky(H)
H = torch.cholesky_inverse(H)
H = torch.linalg.cholesky(H, upper=True)
Hinv = H
mask = None
for i1 in range(0, self.columns, blocksize):
i2 = min(i1 + blocksize, self.columns)
count = i2 - i1
W1 = W[:, i1:i2].clone()
Q1 = torch.zeros_like(W1)
Err1 = torch.zeros_like(W1)
Losses1 = torch.zeros_like(W1)
Hinv1 = Hinv[i1:i2, i1:i2]
if prunen == 0:
if mask is not None:
mask1 = mask[:, i1:i2]
else:
tmp = W1 ** 2 / (torch.diag(Hinv1).reshape((1, -1))) ** 2
thresh = torch.sort(tmp.flatten())[0][int(tmp.numel() * sparsity)]
mask1 = tmp <= thresh
else:
mask1 = torch.zeros_like(W1) == 1
for i in range(count):
w = W1[:, i]
d = Hinv1[i, i]
if prunen != 0 and i % prunem == 0:
tmp = W1[:, i:(i + prunem)] ** 2 / (torch.diag(Hinv1)[i:(i + prunem)].reshape((1, -1))) ** 2
mask1.scatter_(1, i + torch.topk(tmp, prunen, dim=1, largest=False)[1], True)
q = w.clone()
q[mask1[:, i]] = 0
if hasattr(self, 'quantizer'):
q = quantize(
q.unsqueeze(1), self.quantizer.scale, self.quantizer.zero, self.quantizer.maxq
).flatten()
Q1[:, i] = q
Losses1[:, i] = (w - q) ** 2 / d ** 2
err1 = (w - q) / d
W1[:, i:] -= err1.unsqueeze(1).matmul(Hinv1[i, i:].unsqueeze(0))
Err1[:, i] = err1
W[:, i1:i2] = Q1
Losses += torch.sum(Losses1, 1) / 2
W[:, i2:] -= Err1.matmul(Hinv[i1:i2, i2:])
if DEBUG:
self.layer.weight.data[:, :i2] = W[:, :i2]
self.layer.weight.data[:, i2:] = W[:, i2:]
print(torch.sum((self.layer(self.inp1) - self.out1) ** 2))
print(torch.sum(Losses))
torch.cuda.synchronize()
print('time %.2f' % (time.time() - tick))
print('error', torch.sum(Losses).item())
if isinstance(self.layer, transformers.Conv1D):
W = W.t()
self.layer.weight.data = W.reshape(self.layer.weight.shape).to(self.layer.weight.data.dtype)
if DEBUG:
print(torch.sum((self.layer(self.inp1) - self.out1) ** 2))
def admmprune(
self, sparsity, prunen=0, prunem=0, blocksize=128, percdamp=.01
):
W = self.layer.weight.data.clone()
if isinstance(self.layer, nn.Conv2d):
W = W.flatten(1)
if isinstance(self.layer, transformers.Conv1D):
W = W.t()
W = W.float()
if hasattr(self, 'quantizer'):
if not self.quantizer.ready():
self.quantizer.find_params(W, weight=True)
tick = time.time()
del self.H
N, M = 2, 4
s = admm_solve(W, N, M)
# def admm_solve(z, N, M, rho=1, max_iter=1000, tol=1e-4)
self.layer.weight.data = s.to(dtype=self.layer.weight.data.dtype)
if DEBUG:
print('error for admm:')
print(torch.sum((self.layer(self.inp1) - self.out1) ** 2))
def faster_admm_prune(
self, sparsity, prunen=0, prunem=0, blocksize=128, percdamp=.01
):
W = self.layer.weight.data.clone()
if isinstance(self.layer, nn.Conv2d):
W = W.flatten(1)
if isinstance(self.layer, transformers.Conv1D):
W = W.t()
W = W.float()
if hasattr(self, 'quantizer'):
if not self.quantizer.ready():
self.quantizer.find_params(W, weight=True)
tick = time.time()
del self.H
# apply mask from pgd
dtype = self.layer.weight.data.dtype
out_features, in_features = self.layer.weight.shape
model = nn.Linear(in_features=in_features, out_features=out_features, bias=False).to(self.dev)
model.weight.data = self.layer.weight.data.clone()
input = self.inp1.clone().squeeze(0)
output = self.out1.clone().squeeze(0)
# input = self.input.clone().squeeze(0)
# output = self.output.clone().squeeze(0)
input = input.to(torch.float32) # Convert data to Float
output = output.to(torch.float32) # Now output has shape [2048, 768]
model = model.to(torch.float32) # Convert model parameters to Float
from torch.utils.data import TensorDataset, DataLoader
dataset = TensorDataset(input, output)
train_loader = DataLoader(dataset, batch_size=len(dataset), shuffle=True)
# Define your hyperparameter grids
lr_values = [0.001, 0.01, 0.1]
rho_values = [0.001, 0.01, 0.1]
max_iter_values = [10, 100]
# Initialize variables to store the best hyperparameters and the corresponding minimum loss
best_lr = None
best_rho = None
best_max_iter = None
min_loss = float('inf')
# Grid search
for lr in lr_values:
for rho in rho_values:
for max_iter in max_iter_values:
# Copy the model for each iteration to avoid cumulative training effects
temp_model = copy.deepcopy(model)
temp_model.train()
with torch.enable_grad():
w = faster_admm_solve(temp_model, train_loader, W, lr=lr, rho=rho, max_iter=max_iter, tol=1e-4)
# Calculate loss
# temp_model.weight.data = temp_model.weight.data.to(self.layer.weight.data.dtype)
temp_model.weight.data = w.to(self.layer.weight.data.dtype)
current_loss = torch.sum((temp_model(self.inp1) - self.out1) ** 2).item()
# Update best hyperparameters if current loss is lower
if current_loss < min_loss:
min_loss = current_loss
best_lr = lr
best_rho = rho
best_max_iter = max_iter
self.layer.weight.data = temp_model.weight.data
# Print the best hyperparameters and the corresponding loss
print(f"Best lr: {best_lr}, Best rho: {best_rho}, Best max_iter: {best_max_iter}, Minimum Loss: {min_loss}")
print(f'self.layer.weight.data:{self.layer.weight.data}')
del model
del dataset
del train_loader
if DEBUG:
print(torch.sum((self.layer(self.inp1) - self.out1) ** 2))
def faster_snip_prune(
self, sparsity, prunen=0, prunem=0, blocksize=128, percdamp=.01
):
print('-' * 64)
# apply mask from snip
origin_dtype = self.layer.weight.dtype
model = copy.deepcopy(self.layer)
input = self.inp1.clone().squeeze(0)
output = self.out1.clone().squeeze(0)
input = input.to(torch.float32) # Convert data to Float
output = output.to(torch.float32) # Now output has shape [2048, 768]
model = model.to(torch.float32) # Convert model parameters to Float
from torch.utils.data import TensorDataset, DataLoader
dataset = TensorDataset(input, output)
train_loader = DataLoader(dataset, batch_size=len(dataset), shuffle=True)
# Define your hyperparameter grids
lr_values = [0.001, 0.01, 0.1]
rho_values = [0.001, 0.01, 0.1 ,1, 10]
max_iter_values = [10, 50, 100]
lr_values = [0.001]
rho_values = [0.001]
max_iter_values = [300]
# Initialize variables to store the best hyperparameters and the corresponding minimum loss
best_lr = None
best_rho = None
best_max_iter = None
min_loss = float('inf')
best_mask = None
# Grid search
for lr in lr_values:
for rho in rho_values:
for max_iter in max_iter_values:
# Copy the model for each iteration to avoid cumulative training effects
temp_model = copy.deepcopy(model)
temp_model.train()
with torch.enable_grad():
_, mask = SNIP_solve(temp_model, train_loader, lr, max_iter, rho, 0.001)
temp_model.weight_mask.data = mask.data
# Calculate loss
if best_mask is None:
best_mask = mask
current_loss = torch.sum((temp_model(self.inp1.to(torch.float32)) - self.out1.to(torch.float32) ** 2)).item()
# Update best hyperparameters if current loss is lower
if current_loss < min_loss:
min_loss = current_loss
best_lr = lr
best_rho = rho
best_max_iter = max_iter
best_mask = mask
print(f'self.layer.weight.data:{self.layer.weight.data}')
print(f'best_mask:{best_mask}')
# self.layer.weight.data = best_mask.to(origin_dtype).data * self.layer.weight.data
self.layer.weight.data[~best_mask.bool()] = 0
print(f'self.layer.weight.data:{self.layer.weight.data}')
# Print the best hyperparameters and the corresponding loss
print(f"Best lr: {best_lr}, Best rho: {best_rho}, Best max_iter: {best_max_iter}, Minimum Loss: {min_loss}")
print(f'self.layer.weight.data:{self.layer.weight.data}')
del model
del dataset
del train_loader
if DEBUG:
print('diff:')
print(torch.sum((self.layer(self.inp1) - self.out1) ** 2))
def faster_pgd_prune(
self, sparsity, prunen=0, prunem=0, blocksize=128, percdamp=.01
):
W = self.layer.weight.data.clone()
if isinstance(self.layer, nn.Conv2d):
W = W.flatten(1)
if isinstance(self.layer, transformers.Conv1D):
W = W.t()
W = W.float()
if hasattr(self, 'quantizer'):
if not self.quantizer.ready():
self.quantizer.find_params(W, weight=True)
tick = time.time()
H = self.H
del self.H
dead = torch.diag(H) == 0
H[dead, dead] = 1
W[:, dead] = 0
Losses = torch.zeros(self.rows, device=self.dev)
damp = percdamp * torch.mean(torch.diag(H))
diag = torch.arange(self.columns, device=self.dev)
H[diag, diag] += damp
H = torch.linalg.cholesky(H)
H = torch.cholesky_inverse(H)
H = torch.linalg.cholesky(H, upper=True)
Hinv = H
mask = None
# apply mask from pgd
out_features, in_features = self.layer.weight.shape
model = None
model.weight_old = self.layer.weight.data
# nn.init.kaiming_normal_(model.weight, mode='fan_out')
# nn.init.uniform_(model.weight, 0, 1)
input = self.inp1.clone().squeeze(0)
output = self.out1.clone().squeeze(0)
input = input.to(torch.float32) # Convert data to Float
output = output.to(torch.float32) # Now output has shape [2048, 768]
model = model.to(torch.float32) # Convert model parameters to Float
from torch.utils.data import TensorDataset, DataLoader
dataset = TensorDataset(input, output)
train_loader = DataLoader(dataset, batch_size=32, shuffle=True)
with torch.enable_grad():
model.train()
mask = PGD(model, 0.5, train_loader, self.dev)
# print(f'shape1 {torch.sum(mask) / (model.weight_mask.shape[0] * model.weight_mask.shape[1])}')
self.layer.weight.data[model.weight] = 0
del model
del dataset
del train_loader
return
for i1 in range(0, self.columns, blocksize):
i2 = min(i1 + blocksize, self.columns)
count = i2 - i1
W1 = W[:, i1:i2].clone()
Q1 = torch.zeros_like(W1)
Err1 = torch.zeros_like(W1)
Losses1 = torch.zeros_like(W1)
Hinv1 = Hinv[i1:i2, i1:i2]
if prunen == 0:
if mask is not None:
mask1 = mask[:, i1:i2]
else:
tmp = W1 ** 2 / (torch.diag(Hinv1).reshape((1, -1))) ** 2
thresh = torch.sort(tmp.flatten())[0][int(tmp.numel() * sparsity)]
mask1 = tmp <= thresh
else:
mask1 = torch.zeros_like(W1) == 1
for i in range(count):
w = W1[:, i]
d = Hinv1[i, i]
if prunen != 0 and i % prunem == 0:
tmp = W1[:, i:(i + prunem)] ** 2 / (torch.diag(Hinv1)[i:(i + prunem)].reshape((1, -1))) ** 2
mask1.scatter_(1, i + torch.topk(tmp, prunen, dim=1, largest=False)[1], True)
q = w.clone()
q[mask1[:, i]] = 0
if hasattr(self, 'quantizer'):
q = quantize(
q.unsqueeze(1), self.quantizer.scale, self.quantizer.zero, self.quantizer.maxq
).flatten()
Q1[:, i] = q
Losses1[:, i] = (w - q) ** 2 / d ** 2
err1 = (w - q) / d
W1[:, i:] -= err1.unsqueeze(1).matmul(Hinv1[i, i:].unsqueeze(0))
Err1[:, i] = err1
W[:, i1:i2] = Q1
Losses += torch.sum(Losses1, 1) / 2
W[:, i2:] -= Err1.matmul(Hinv[i1:i2, i2:])
if DEBUG:
self.layer.weight.data[:, :i2] = W[:, :i2]
self.layer.weight.data[:, i2:] = W[:, i2:]
print(torch.sum((self.layer(self.inp1) - self.out1) ** 2))
print(torch.sum(Losses))
torch.cuda.synchronize()
print('time %.2f' % (time.time() - tick))
print('error', torch.sum(Losses).item())
if isinstance(self.layer, transformers.Conv1D):
W = W.t()
self.layer.weight.data = W.reshape(self.layer.weight.shape).to(self.layer.weight.data.dtype)
if DEBUG:
print(torch.sum((self.layer(self.inp1) - self.out1) ** 2))
def faster_vrpge_prune(
self, sparsity, prunen=0, prunem=0, blocksize=128, percdamp=.01
):
W = self.layer.weight.data.clone()
if isinstance(self.layer, nn.Conv2d):
W = W.flatten(1)
if isinstance(self.layer, transformers.Conv1D):
W = W.t()
W = W.float()
if hasattr(self, 'quantizer'):
if not self.quantizer.ready():
self.quantizer.find_params(W, weight=True)
tick = time.time()
del self.H
mask = None
# apply mask from pgd
dtype = self.layer.weight.data.dtype
out_features, in_features = self.layer.weight.shape
# model = VRPGE(in_features=in_features, out_features=out_features, bias=True).to(self.dev)
model = VRPGE(
in_features, out_features, kernel_size=1, stride=1, bias=False
).to(self.dev)
# Clone and reshape the input
input = self.inp1.clone().squeeze(0)
input = input.view(-1, in_features, 1, 1) # Reshape to (2048, 768, 1, 1)
# Clone and prepare the output
output = self.out1.clone().squeeze(0)
input = input.to(torch.float32) # Convert data to Float
output = output.to(torch.float32) # Now output has shape [2048, 768]
model = model.to(torch.float32) # Convert model parameters to Float
from torch.utils.data import TensorDataset, DataLoader
dataset = TensorDataset(input, output)
train_loader = DataLoader(dataset, batch_size=32, shuffle=True)
with torch.enable_grad():
model.train()
# print(f'orign subnet:{model.scores}')
VRPGE_solve(model, 0.5, train_loader, self.dev)
# print(f'final subnet:{model.scores}')
# print(f'ratio:{torch.sum(model.subnet)/ model.subnet.nelement()}')
# self.layer.weight.data = model.weight.data.clone().to(dtype)
# self.layer.weight[~model.subnet.data.bool()] = 0
del model
del dataset
del train_loader
# if isinstance(self.layer, transformers.Conv1D):
# W = W.t()
# self.layer.weight.data = W.reshape(self.layer.weight.shape).to(self.layer.weight.data.dtype)
if DEBUG:
print(torch.sum((model(input).view(self.out1.shape) - self.out1) ** 2))
def faster_probmask_prune(
self, sparsity, prunen=0, prunem=0, blocksize=128, percdamp=.01
):
W = self.layer.weight.data.clone()
if isinstance(self.layer, nn.Conv2d):
W = W.flatten(1)
if isinstance(self.layer, transformers.Conv1D):
W = W.t()
W = W.float()
if hasattr(self, 'quantizer'):
if not self.quantizer.ready():
self.quantizer.find_params(W, weight=True)
tick = time.time()
del self.H
# apply mask from pgd
dtype = self.layer.weight.data.dtype
out_features, in_features = self.layer.weight.shape
# model = VRPGE(in_features=in_features, out_features=out_features, bias=True).to(self.dev)
model = ProbMaskLinear(in_features, out_features, bias=False).to(self.dev)
model.weight.data = self.layer.weight.data
# Clone and reshape the input
input = self.inp1.clone().squeeze(0)
output = self.out1.clone().squeeze(0)
input = input.to(torch.float32) # Convert data to Float
output = output.to(torch.float32) # Now output has shape [2048, 768]
model = model.to(torch.float32) # Convert model parameters to Float
from torch.utils.data import TensorDataset, DataLoader
dataset = TensorDataset(input, output)
train_loader = DataLoader(dataset, batch_size=256, shuffle=True)
# Define your hyperparameter grids
lr_values = [0.001, 0.01, 0.1]
weight_lr_values = [0.01, 0.1]
max_iter_values = [300]
# Initialize variables to store the best hyperparameters and the corresponding minimum loss
best_lr = None
best_weight_lr = None
best_max_iter = None
min_loss = float('inf')
# Grid search
for lr in lr_values:
for weight_lr in weight_lr_values:
for max_iter in max_iter_values:
# Copy the model for each iteration to avoid cumulative training effects
temp_model = copy.deepcopy(model)
temp_model.train()
with torch.enable_grad():
temp_model = Probmask_solve(temp_model, 0.5, train_loader, self.dev, lr = lr, epochs=max_iter)
# Calculate loss
# temp_model.weight.data = temp_model.weight.data.to(self.layer.weight.data.dtype)
current_loss = torch.sum((temp_model(self.inp1.to(torch.float32)) - self.out1.to(torch.float32)) ** 2).item()
if best_lr is None:
temp_model.fix_subnet()
self.layer.weight.data = (temp_model.subnet * temp_model.weight.data).to(dtype)
# Update best hyperparameters if current loss is lower
if current_loss < min_loss:
min_loss = current_loss
best_lr = lr
best_weight_lr = weight_lr
best_max_iter = max_iter
self.layer.weight.data = (temp_model.subnet * temp_model.weight.data).to(dtype)
# Print the best hyperparameters and the corresponding loss
print(f"Best lr: {best_lr}, Best best_weight_lr: {best_weight_lr}, Best max_iter: {best_max_iter}, Minimum Loss: {min_loss}")
if DEBUG:
print(torch.sum((self.layer(self.inp1) - self.out1) ** 2))
del model
del dataset
del train_loader
def faster_mask_prune(
self, sparsity, prunen=0, prunem=0, blocksize=128, percdamp=.01
):
W = self.layer.weight.data.clone()
if isinstance(self.layer, nn.Conv2d):
W = W.flatten(1)
if isinstance(self.layer, transformers.Conv1D):
W = W.t()
W = W.float()
if hasattr(self, 'quantizer'):
if not self.quantizer.ready():
self.quantizer.find_params(W, weight=True)
tick = time.time()
del self.H
# apply mask from pgd
dtype = self.layer.weight.data.dtype
out_features, in_features = self.layer.weight.shape
# model = VRPGE(in_features=in_features, out_features=out_features, bias=True).to(self.dev)
model = copy.deepcopy(self.layer)
model.bias = None
# Clone and reshape the input
input = self.inp1.clone().squeeze(0)
output = self.out1.clone().squeeze(0)
input = input.to(torch.float32) # Convert data to Float
output = output.to(torch.float32) # Now output has shape [2048, 768]
model = model.to(torch.float32) # Convert model parameters to Float
from torch.utils.data import TensorDataset, DataLoader
dataset = TensorDataset(input, output)
train_loader = DataLoader(dataset, batch_size=len(dataset), shuffle=True)
# Define your hyperparameter grids
lr_values = [0.1]
weight_lr_values = [0.01]
max_iter_values = [300]
# Initialize variables to store the best hyperparameters and the corresponding minimum loss
best_lr = None
best_weight_lr = None
best_max_iter = None
min_loss = float('inf')
# Grid search
for lr in lr_values:
for weight_lr in weight_lr_values:
for max_iter in max_iter_values:
# Copy the model for each iteration to avoid cumulative training effects
temp_model = copy.deepcopy(model)
temp_model.train()
with torch.enable_grad():
temp_model = mask_solve(temp_model,train_loader, self.dev)
# Calculate loss
current_loss = torch.sum((temp_model(self.inp1.to(torch.float32)) - self.out1.to(torch.float32)) ** 2).item()
self.layer.weight.data = (temp_model.weight_mask.data * temp_model.weight.data).to(dtype)
# Update best hyperparameters if current loss is lower
# if current_loss < min_loss:
# min_loss = current_loss
# best_lr = lr
# best_weight_lr = weight_lr
# best_max_iter = max_iter
# self.layer.weight.data = (temp_model.weight_mask.data * temp_model.weight.data).to(dtype)
# Print the best hyperparameters and the corresponding loss
print(f"Best lr: {best_lr}, Best best_weight_lr: {best_weight_lr}, Best max_iter: {best_max_iter}, Minimum Loss: {min_loss}")
if DEBUG:
print(f'self.layer.weight:{self.layer.weight}')
print(f'temp_model.weight_mask.data:{temp_model.weight_mask.data}')
print(f'delta:{torch.sum((self.layer(self.inp1) - self.out1) ** 2)}')
del model
del dataset
del train_loader
def free(self):
if DEBUG:
self.inp1 = None
self.out1 = None
self.H = None
torch.cuda.empty_cache()