-
Notifications
You must be signed in to change notification settings - Fork 0
/
pNN_Power_Aware.py
401 lines (346 loc) · 15.7 KB
/
pNN_Power_Aware.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
import numpy as np
import torch
# ================================================================================================================================================
# ===================================================== Learnable Negative Weight Circuit ======================================================
# ================================================================================================================================================
class InvRT(torch.nn.Module):
def __init__(self, args):
super().__init__()
self.args = args
# R1n, k1, R3n, k2, R5n, Wn, k3
# be careful, k1, k2, k3 are not normalized
self.rt_ = torch.nn.Parameter(torch.tensor(
[args.NEG_R1n, args.NEG_k1, args.NEG_R3n, args.NEG_k2, args.NEG_R5n, args.NEG_Wn, args.NEG_Ln]), requires_grad=True)
# model
package = torch.load('./utils/neg_model_package')
self.eta_estimator = package['eta_estimator'].to(self.args.DEVICE)
self.eta_estimator.train(False)
for name, param in self.eta_estimator.named_parameters():
param.requires_grad = False
self.X_max = package['X_max'].to(self.args.DEVICE)
self.X_min = package['X_min'].to(self.args.DEVICE)
self.Y_max = package['Y_max'].to(self.args.DEVICE)
self.Y_min = package['Y_min'].to(self.args.DEVICE)
# load power model
package = torch.load('./utils/neg_power_model_package')
self.power_estimator = package['power_estimator'].to(self.args.DEVICE)
for name, param in self.power_estimator.named_parameters():
param.requires_grad = False
self.power_estimator.train(False)
self.pow_X_max = package['X_max'].to(self.args.DEVICE)
self.pow_X_min = package['X_min'].to(self.args.DEVICE)
self.pow_Y_max = package['Y_max'].to(self.args.DEVICE)
self.pow_Y_min = package['Y_min'].to(self.args.DEVICE)
@property
def RT_(self):
# keep values in (0,1)
rt_temp = torch.sigmoid(self.rt_)
# calculate normalized (only R1n, R3n, R5n, Wn, Ln)
RTn = torch.zeros([10]).to(self.args.DEVICE)
RTn[0] = rt_temp[0] # R1n
RTn[2] = rt_temp[2] # R3n
RTn[4] = rt_temp[4] # R5n
RTn[5] = rt_temp[5] # Wn
RTn[6] = rt_temp[6] # Ln
# denormalization
RT = RTn * (self.X_max - self.X_min) + self.X_min
# calculate R2, R4
R2 = RT[0] * rt_temp[1] # R2 = R1 * k1
R4 = RT[2] * rt_temp[3] # R4 = R3 * k2
# stack new variable: R1, R2, R3, R4, R5, W, L
RT_full = torch.stack([RT[0], R2, RT[2], R4, RT[4], RT[5], RT[6]])
return RT_full
@property
def RT(self):
# keep each component value in feasible range
RT_full = torch.zeros([10]).to(self.args.DEVICE)
RT_full[:7] = self.RT_.clone()
RT_full[RT_full > self.X_max] = self.X_max[RT_full > self.X_max] # clip
RT_full[RT_full < self.X_min] = self.X_min[RT_full < self.X_min] # clip
return RT_full[:7].detach() + self.RT_ - self.RT_.detach()
@property
def RT_extend(self):
# extend RT to 10 variables with k1 k2 and k3
R1 = self.RT[0]
R2 = self.RT[1]
R3 = self.RT[2]
R4 = self.RT[3]
R5 = self.RT[4]
W = self.RT[5]
L = self.RT[6]
k1 = R2 / R1
k2 = R4 / R3
k3 = L / W
return torch.hstack([R1, R2, R3, R4, R5, W, L, k1, k2, k3])
@property
def RTn_extend(self):
# normalize RT_extend
return (self.RT_extend - self.X_min) / (self.X_max - self.X_min)
@property
def eta(self):
# calculate eta
eta_n = self.eta_estimator(self.RTn_extend)
eta = eta_n * (self.Y_max - self.Y_min) + self.Y_min
return eta
@property
def power(self):
# calculate power
power_n = self.power_estimator(self.RTn_extend)
power = power_n * (self.pow_Y_max - self.pow_Y_min) + self.pow_Y_min
return power
def forward(self, z):
return - (self.eta[0] + self.eta[1] * torch.tanh((z - self.eta[2]) * self.eta[3]))
# ================================================================================================================================================
# ======================================================== Learnable Activation Circuit ========================================================
# ================================================================================================================================================
class TanhRT(torch.nn.Module):
def __init__(self, args):
super().__init__()
self.args = args
# R1n, R2n, W1n, L1n, W2n, L2n
self.rt_ = torch.nn.Parameter(
torch.tensor([args.ACT_R1n, args.ACT_R2n, args.ACT_W1n, args.ACT_L1n, args.ACT_W2n, args.ACT_L2n]), requires_grad=True)
# model
package = torch.load('./utils/act_model_package')
self.eta_estimator = package['eta_estimator'].to(self.args.DEVICE)
self.eta_estimator.train(False)
for n, p in self.eta_estimator.named_parameters():
p.requires_grad = False
self.X_max = package['X_max'].to(self.args.DEVICE)
self.X_min = package['X_min'].to(self.args.DEVICE)
self.Y_max = package['Y_max'].to(self.args.DEVICE)
self.Y_min = package['Y_min'].to(self.args.DEVICE)
# load power model
package = torch.load('./utils/act_power_model_package')
self.power_estimator = package['power_estimator'].to(self.args.DEVICE)
self.power_estimator.train(False)
for n, p in self.power_estimator.named_parameters():
p.requires_grad = False
self.pow_X_max = package['X_max'].to(self.args.DEVICE)
self.pow_X_min = package['X_min'].to(self.args.DEVICE)
self.pow_Y_max = package['Y_max'].to(self.args.DEVICE)
self.pow_Y_min = package['Y_min'].to(self.args.DEVICE)
@property
def RT(self):
# keep values in (0,1)
rt_temp = torch.sigmoid(self.rt_)
# denormalization
RTn = torch.zeros([9]).to(self.args.DEVICE)
RTn[0] = rt_temp[0] # R1n
RTn[1] = rt_temp[1] # R2n
RTn[2] = rt_temp[2] # W1n
RTn[3] = rt_temp[3] # L1n
RTn[4] = rt_temp[4] # W2n
RTn[5] = rt_temp[5] # L2n
RT = RTn * (self.X_max - self.X_min) + self.X_min
return RT[:6]
@property
def RT_extend(self):
# extend RT to 9 variables with k1 k2 and k3
R1 = self.RT[0]
R2 = self.RT[1]
W1 = self.RT[2]
L1 = self.RT[3]
W2 = self.RT[4]
L2 = self.RT[5]
k1 = R2 / R1
k2 = L1 / W1
k3 = L2 / W2
return torch.hstack([R1, R2, W1, L1, W2, L2, k1, k2, k3])
@property
def RTn_extend(self):
# normalize RT_extend
return (self.RT_extend - self.X_min) / (self.X_max - self.X_min)
@property
def eta(self):
# calculate eta
eta_n = self.eta_estimator(self.RTn_extend)
eta = eta_n * (self.Y_max - self.Y_min) + self.Y_min
return eta
@property
def power(self):
# calculate power
power_n = self.power_estimator(self.RTn_extend)
power = power_n * (self.pow_Y_max - self.pow_Y_min) + self.pow_Y_min
return power.flatten()
def forward(self, z):
return self.eta[0] + self.eta[1] * torch.tanh((z - self.eta[2]) * self.eta[3])
# ================================================================================================================================================
# =============================================================== Printed Layer ================================================================
# ================================================================================================================================================
class pLayer(torch.nn.Module):
def __init__(self, n_in, n_out, args, ACT, INV):
super().__init__()
self.args = args
# define nonlinear circuits
self.INV = INV
self.ACT = ACT
# initialize conductances for weights
theta = torch.rand([n_in + 2, n_out])/100. + args.gmin
theta[-1, :] = theta[-1, :] + args.gmax
theta[-2, :] = self.ACT.eta[2].detach().item() / \
(1.-self.ACT.eta[2].detach().item()) * \
(torch.sum(theta[:-2, :], axis=0)+theta[-1, :])
self.theta_ = torch.nn.Parameter(theta, requires_grad=True)
@property
def device(self):
return self.args.DEVICE
@property
def theta(self):
self.theta_.data.clamp_(-self.args.gmax, self.args.gmax)
theta_temp = self.theta_.clone()
theta_temp[theta_temp.abs() < self.args.gmin] = 0.
return theta_temp.detach() + self.theta_ - self.theta_.detach()
@property
def W(self):
return self.theta.abs() / torch.sum(self.theta.abs(), axis=0, keepdim=True)
def MAC(self, a):
# 0 and positive thetas are corresponding to no negative weight circuit
positive = self.theta.clone().to(self.device)
positive[positive >= 0] = 1.
positive[positive < 0] = 0.
negative = 1. - positive
a_extend = torch.cat([a,
torch.ones([a.shape[0], 1]).to(self.device),
torch.zeros([a.shape[0], 1]).to(self.device)], dim=1)
a_neg = self.INV(a_extend)
a_neg[:, -1] = 0.
z = torch.matmul(a_extend, self.W * positive) + \
torch.matmul(a_neg, self.W * negative)
return z
def forward(self, a_previous):
z_new = self.MAC(a_previous)
self.mac_power = self.MACPower(a_previous, z_new)
a_new = self.ACT(z_new)
self.act_power = self.ACT.power * a_new.shape[1]
return a_new
@property
def g_tilde(self):
# scaled conductances
g_initial = self.theta_.abs()
g_min = g_initial.min(dim=0, keepdim=True)[0]
scaler = self.args.pgmin / g_min
return g_initial * scaler
def MACPower(self, x, y):
x_extend = torch.cat([x,
torch.ones([x.shape[0], 1]).to(self.device),
torch.zeros([x.shape[0], 1]).to(self.device)], dim=1)
x_neg = self.INV(x_extend)
x_neg[:, -1] = 0.
E = x_extend.shape[0]
M = x_extend.shape[1]
N = y.shape[1]
positive = self.theta.clone().detach().to(self.device)
positive[positive >= 0] = 1.
positive[positive < 0] = 0.
negative = 1. - positive
Power = torch.tensor(0.).to(self.device)
for m in range(M):
for n in range(N):
Power += self.g_tilde[m, n] * (
(x_extend[:, m]*positive[m, n]+x_neg[:, m]*negative[m, n])-y[:, n]).pow(2.).sum()
Power = Power / E
return Power
@property
def neg_power(self):
# forward pass: power of neg * number of negative weights
positive = self.theta.clone().detach()[:-1,:]
positive[positive >= 0] = 1.
positive[positive < 0] = 0.
negative = 1. - positive
N_neg = negative.sum(1)
N_neg[N_neg>0] = 1.
N_neg = N_neg.sum()
power = self.INV.power * N_neg
# backward pass: power of neg * value of negative weights
soft_count = 1 - torch.sigmoid(self.theta[:-1,:])
soft_count = soft_count * negative
soft_count = soft_count.max(1)[0].sum()
# soft_N_neg = torch.nn.functional.relu(-self.theta_).sum()
power_relaxed = self.INV.power * soft_count
return power.detach() + power_relaxed - power_relaxed.detach()
@property
def power(self):
return self.mac_power + self.act_power + self.neg_power
def WeightAttraction(self):
mean = self.theta.mean(dim=0)
diff = self.theta - mean
return diff.pow(2.).mean()
def WeightDecay(self):
return self.theta.pow(2.).mean()
def SetParameter(self, name, value):
if name == 'args':
self.args = value
self.INV.args = value
self.ACT.args = value
# ================================================================================================================================================
# ============================================================== Printed Circuit ===============================================================
# ================================================================================================================================================
class pNN(torch.nn.Module):
def __init__(self, topology, args):
super().__init__()
self.args = args
# define nonlinear circuits
self.act = TanhRT(args)
self.inv = InvRT(args)
self.model = torch.nn.Sequential()
for i in range(len(topology)-1):
self.model.add_module(
f'{i}-th pLayer', pLayer(topology[i], topology[i+1], args, self.act, self.inv))
def forward(self, X):
return self.model(X)
@property
def device(self):
return self.args.DEVICE
def Power(self):
power = torch.tensor([0.]).to(self.device)
for l in self.model:
power += l.power
return power
def WeightModifier(self):
penalty = torch.tensor(0.)
for l in self.model:
penalty += l.WeightAttraction() / 2
penalty += l.WeightDecay() / 2
return penalty / len(self.model)
def GetParam(self, name):
if name == 'rt':
return [p for name, p in self.named_parameters() if name.endswith('.rt_')]
elif name == 'theta':
return [p for name, p in self.named_parameters() if name.endswith('.theta_')]
def SetParameter(self, name, value):
if name == 'args':
self.args = value
for m in self.model:
m.SetParameter('args', self.args)
# ================================================================================================================================================
# =============================================================== Loss function ================================================================
# ================================================================================================================================================
class Lossfunction(torch.nn.Module):
def __init__(self, args):
super().__init__()
self.args = args
def standard(self, prediction, label):
label = label.reshape(-1, 1)
fy = prediction.gather(1, label).reshape(-1, 1)
fny = prediction.clone()
fny = fny.scatter_(1, label, -10 ** 10)
fnym = torch.max(fny, axis=1).values.reshape(-1, 1)
l = torch.max(self.args.m + self.args.T - fy, torch.tensor(0)
) + torch.max(self.args.m + fnym, torch.tensor(0))
L = torch.mean(l)
return L
def PowerEstimator(self, nn, x):
_ = nn(x)
return nn.Power()
def WeightModifier(self, nn):
return nn.WeightModifier()
def forward(self, nn, x, label):
if self.args.powerestimator == 'attraction':
return (1. - self.args.powerbalance) * self.standard(nn(x), label) + self.args.powerbalance * self.WeightModifier(nn)
elif self.args.powerestimator == 'power':
return (1. - self.args.powerbalance) * self.standard(nn(x), label) + self.args.powerbalance * self.PowerEstimator(nn, x)
elif self.args.powerestimator == 'both':
return (1. - self.args.powerbalance) * self.standard(nn(x), label) + self.args.powerbalance * (self.args.estimatorbalance * self.PowerEstimator(nn, x) + (1-self.args.estimatorbalance) * self.WeightModifier(nn)) / 2
elif self.args.powerestimator == 'none':
return self.standard(nn(x), label)