-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.py
154 lines (128 loc) · 4.68 KB
/
utils.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
import torch
import torch.nn.functional as F
import numpy as np
def regularized_nll_loss(args, model, output, target):
index = 0
loss = F.nll_loss(output, target)
if args.l2:
for name, param in model.named_parameters():
if name.split('.')[-1] == "weight":
loss += args.alpha * param.norm()
index += 1
return loss
def admm_loss(args, device, model, Z, U, output, target):
idx = 0
loss = F.nll_loss(output, target)
for name, param in model.named_parameters():
if name.split('.')[-1] == "weight":
u = U[idx].to(device)
z = Z[idx].to(device)
loss += args.rho / 2 * (param - z + u).norm()
if args.l2:
loss += args.alpha * param.norm()
idx += 1
return loss
def initialize_Z_and_U(model):
Z = ()
U = ()
for name, param in model.named_parameters():
if name.split('.')[-1] == "weight":
Z += (param.detach().cpu().clone(),)
U += (torch.zeros_like(param).cpu(),)
return Z, U
def update_X(model):
X = ()
for name, param in model.named_parameters():
if name.split('.')[-1] == "weight":
X += (param.detach().cpu().clone(),)
return X
def update_Z(X, U, args):
new_Z = ()
idx = 0
for x, u in zip(X, U):
z = x + u
pcen = np.percentile(abs(z), 100*args.percent[idx])
under_threshold = abs(z) < pcen
z.data[under_threshold] = 0
new_Z += (z,)
idx += 1
return new_Z
def update_Z_l1(X, U, args):
new_Z = ()
delta = args.alpha / args.rho
for x, u in zip(X, U):
z = x + u
new_z = z.clone()
if (z > delta).sum() != 0:
new_z[z > delta] = z[z > delta] - delta
if (z < -delta).sum() != 0:
new_z[z < -delta] = z[z < -delta] + delta
if (abs(z) <= delta).sum() != 0:
new_z[abs(z) <= delta] = 0
new_Z += (new_z,)
return new_Z
def update_U(U, X, Z):
new_U = ()
for u, x, z in zip(U, X, Z):
new_u = u + x - z
new_U += (new_u,)
return new_U
def prune_weight(weight, device, percent):
# to work with admm, we calculate percentile based on all elements instead of nonzero elements.
weight_numpy = weight.detach().cpu().numpy()
pcen = np.percentile(abs(weight_numpy), 100*percent)
under_threshold = abs(weight_numpy) < pcen
weight_numpy[under_threshold] = 0
mask = torch.Tensor(abs(weight_numpy) >= pcen).to(device)
return mask
def prune_l1_weight(weight, device, delta):
weight_numpy = weight.detach().cpu().numpy()
under_threshold = abs(weight_numpy) < delta
weight_numpy[under_threshold] = 0
mask = torch.Tensor(abs(weight_numpy) >= delta).to(device)
return mask
def apply_prune(model, device, args):
# returns dictionary of non_zero_values' indices
print("Apply Pruning based on percentile")
dict_mask = {}
idx = 0
for name, param in model.named_parameters():
if name.split('.')[-1] == "weight":
mask = prune_weight(param, device, args.percent[idx])
param.data.mul_(mask)
# param.data = torch.Tensor(weight_pruned).to(device)
dict_mask[name] = mask
idx += 1
return dict_mask
def apply_l1_prune(model, device, args):
delta = args.alpha / args.rho
print("Apply Pruning based on percentile")
dict_mask = {}
idx = 0
for name, param in model.named_parameters():
if name.split('.')[-1] == "weight":
mask = prune_l1_weight(param, device, delta)
param.data.mul_(mask)
dict_mask[name] = mask
idx += 1
return dict_mask
def print_convergence(model, X, Z):
idx = 0
print("normalized norm of (weight - projection)")
for name, _ in model.named_parameters():
if name.split('.')[-1] == "weight":
x, z = X[idx], Z[idx]
print("({}): {:.4f}".format(name, (x-z).norm().item() / x.norm().item()))
idx += 1
def print_prune(model):
prune_param, total_param = 0, 0
for name, param in model.named_parameters():
if name.split('.')[-1] == "weight":
print("[at weight {}]".format(name))
print("percentage of pruned: {:.4f}%".format(100 * (abs(param) == 0).sum().item() / param.numel()))
print("nonzero parameters after pruning: {} / {}\n".format((param != 0).sum().item(), param.numel()))
total_param += param.numel()
prune_param += (param != 0).sum().item()
print("total nonzero parameters after pruning: {} / {} ({:.4f}%)".
format(prune_param, total_param,
100 * (total_param - prune_param) / total_param))