-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.py
330 lines (289 loc) · 14.4 KB
/
run.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
from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.optim.lr_scheduler import StepLR
import numpy as np
import time
import json
import sys
import os
import scipy
from scipy.stats import norm
import matplotlib.pyplot as plt
from datetime import datetime
sys.path.append("code/python/")
from Utils import set_layer_mode, parse_args, dump_exp_data, create_exp_folder, store_exp_data, get_model_and_datasets, print_tikz_data, cuda_profiler
from QuantizedNN import QuantizedLinear, QuantizedConv2d, QuantizedActivation
from Models import FC, VGG3, VGG7, ResNet, BasicBlock
from Traintest_Utils import train, test, test_error, Clippy, Criterion, binary_hingeloss
import binarizePM1
import binarizePM1FI
import quantization
# from resnet18 import ResNet, BasicBlock
class SymmetricBitErrorsBinarizedPM1:
def __init__(self, method, p):
self.method = method
self.p = p
def updateErrorModel(self, p_updated):
self.p = p_updated
def resetErrorModel(self):
self.p = 0
def applyErrorModel(self, input):
return self.method(input, self.p, self.p)
class Quantization1:
def __init__(self, method):
self.method = method
def applyQuantization(self, input):
return self.method(input)
binarizepm1 = Quantization1(binarizePM1.binarize)
binarizepm1fi = SymmetricBitErrorsBinarizedPM1(binarizePM1FI.binarizeFI, 0.1)
# crit_train = Criterion(method=nn.CrossEntropyLoss(reduction="none"), name="CEL_train")
# crit_test = Criterion(method=nn.CrossEntropyLoss(reduction="none"), name="CEL_test")
crit_train = Criterion(binary_hingeloss, "MHL_train", param=128)
crit_test = Criterion(binary_hingeloss, "MHL_test", param=128)
q_train = True # quantization during training
q_eval = True # quantization during evaluation
#python3 run.py --model=FC --dataset=FMNIST --load-model="model_fc_test.pt" --mapping=mapping_example/mapping.npy --array-size=32 --an-sim=1
# capacitor model
# t = - tau * torch.log(1-(a/v_o))
# class Snn_RC:
# def __init__(self, method):
# self.r_l = method
# self.v_th = v_th
# self.
# self.c_mem = c_mem
# def applyQuantization(self, input):
# return self.method(input)
def main():
# Training settings
parser = argparse.ArgumentParser(description='Training Process')
parse_args(parser)
args = parser.parse_args()
use_cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
device = torch.device("cuda" if use_cuda else "cpu")
available_gpus = [i for i in range(torch.cuda.device_count())]
print("Available GPUs: ", available_gpus)
gpu_select = args.gpu_num
# change GPU that is being used
torch.cuda.set_device(gpu_select)
# which GPU is currently used
print("Currently used GPU: ", torch.cuda.current_device())
train_kwargs = {'batch_size': args.batch_size}
test_kwargs = {'batch_size': args.test_batch_size}
if use_cuda:
cuda_kwargs = {'num_workers': 1,
'pin_memory': True,
'shuffle': True}
train_kwargs.update(cuda_kwargs)
test_kwargs.update(cuda_kwargs)
nn_model, dataset1, dataset2 = get_model_and_datasets(args)
train_loader = torch.utils.data.DataLoader(dataset1,**train_kwargs)
test_loader = torch.utils.data.DataLoader(dataset2, **test_kwargs)
mac_mapping = None
mac_mapping_distr = None
sorted_mac_mapping_idx = None
if args.mapping is not None:
print("Mapping: ", args.mapping)
mac_mapping = torch.from_numpy(np.load(args.mapping)).float().cuda()
# print("mapping", mac_mapping)
if args.mapping_distr is not None:
# print("hello")
# print("Mapping distr.: ", args.mapping_distr)
sorted_mac_mapping_idx = torch.from_numpy(np.argsort(np.load(args.mapping_distr))).float().cuda().contiguous()
mac_mapping_distr = torch.from_numpy(np.load(args.mapping_distr)).float().cuda().contiguous()
# calculate cumulative distribution
# flag = 1
# a = []
# print("MAC mapping distr", mac_mapping_distr[2])
for i in range(mac_mapping_distr.shape[0]):
# flag = 1
for j in range(mac_mapping_distr.shape[1]):
# the first entry that is not zero needs to be left alone
# print("mac1", mac_mapping_distr[i][int(sorted_mac_mapping_idx[i][j])])
# print("mac2", mac_mapping_distr[i][int(sorted_mac_mapping_idx[i][j+1])])
# if ((mac_mapping_distr[i][int(sorted_mac_mapping_idx[i][j])] > 0) and (flag is None)):
# flag = None
# continue
if (mac_mapping_distr[i][int(sorted_mac_mapping_idx[i][j])] > 0):
mac_mapping_distr[i][int(sorted_mac_mapping_idx[i][j])] = mac_mapping_distr[i][int(sorted_mac_mapping_idx[i][j])] + mac_mapping_distr[i][int(sorted_mac_mapping_idx[i][j-1])]
# print("map", mac_mapping_distr[i][int(sorted_mac_mapping_idx[i][j])])
# print("MAC mapping distr", mac_mapping_distr[2])
# print sorted array
# for i in range(mac_mapping_distr.shape[1]):
# print(mac_mapping_distr[2][int(sorted_mac_mapping_idx[2][i])])
# print(mac_mapping_distr[2])
# print(sorted_mac_mapping_idx[2])
# use later: mapping[sorted[i]]
# print("Mapping from distr: ", mac_mapping_distr)
# print("Mapping from distr idx: ", sorted_mac_mapping_idx)
model = None
if args.model == "ResNet":
model = nn_model(BasicBlock, [2, 2, 2, 2], crit_train, crit_test, quantMethod=binarizepm1, an_sim=args.an_sim, array_size=args.array_size, mapping=mac_mapping, mapping_distr=mac_mapping_distr, sorted_mapping_idx=sorted_mac_mapping_idx, performance_mode=args.performance_mode, quantize_train=q_train, quantize_eval=q_eval, error_model=None, train_model=args.train_model, extract_absfreq=args.extract_absfreq).to(device)
else:
# model = nn_model().to(device)
model = nn_model(crit_train, crit_test, quantMethod=binarizepm1, an_sim=args.an_sim, array_size=args.array_size, mapping=mac_mapping, mapping_distr=mac_mapping_distr, sorted_mapping_idx=sorted_mac_mapping_idx, performance_mode=args.performance_mode, quantize_train=q_train, quantize_eval=q_eval, error_model=None, train_model=args.train_model, extract_absfreq=args.extract_absfreq).to(device)
# optimizer = optim.Adam(model.parameters(), lr=args.lr)
optimizer = Clippy(model.parameters(), lr=args.lr)
scheduler = StepLR(optimizer, step_size=args.step_size, gamma=args.gamma)
# load training state or create new model
if args.load_training_state is not None:
print("Loaded training state: ", args.load_training_state)
checkpoint = torch.load(args.load_training_state)
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
# epoch = checkpoint['epoch']
# print(model.name)
# create experiment folder and file
to_dump_path = create_exp_folder(model)
if not os.path.exists(to_dump_path):
open(to_dump_path, 'w').close()
if args.train_model is not None:
time_elapsed = 0
times = []
for epoch in range(1, args.epochs + 1):
torch.cuda.synchronize()
since = int(round(time.time()*1000))
#
train(args, model, device, train_loader, optimizer, epoch)
#
time_elapsed += int(round(time.time()*1000)) - since
print('Epoch training time elapsed: {}ms'.format(int(round(time.time()*1000)) - since))
# test(model, device, train_loader)
since = int(round(time.time()*1000))
#
test(model, device, test_loader)
#
time_elapsed += int(round(time.time()*1000)) - since
print('Test time elapsed: {}ms'.format(int(round(time.time()*1000)) - since))
# test(model, device, train_loader)
scheduler.step()
if args.save_model is not None:
torch.save(model.state_dict(), "model_{}.pt".format(args.save_model))
if args.save_training_state is not None:
path = "model_checkpoint_{}.pt".format(args.save_training_state)
torch.save({
'epoch': args.epochs,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
}, path)
# load model
if args.load_model_path is not None:
to_load = args.load_model_path
print("Loaded model: ", to_load)
model.load_state_dict(torch.load(to_load, map_location='cuda:0'))
if args.test_error is not None:
all_accuracies = test_error(model, device, test_loader)
to_dump_data = dump_exp_data(model, args, all_accuracies)
store_exp_data(to_dump_path, to_dump_data)
if args.test_error_distr is not None:
# perform repeated experiments and return in tikz format
acc_list = []
for i in range(args.test_error_distr):
acc_list.append(test(model, device, test_loader))
# print("acclist", acc_list)
print_tikz_data(acc_list)
if args.print_accuracy is not None:
print("Accuracy: ")
test(model, device, test_loader)
if args.profile_time is not None:
print("Measuring time: ")
times_list = []
for rep in range(args.profile_time):
profiled = cuda_profiler(test, model, device, test_loader, pr=None)
times_list.append(profiled)
print_tikz_data(times_list)
# Resnet absfreq extraction is different from VGG
if args.extract_absfreq_resnet is not None:
####
# abs freq test resnet
print(model)
# print("--")
# print((model.layer1[1]))
# iterate through resnet structure to access conv and linear layer data
for block in model.children():
# print("BLOCK---", block)
if isinstance(block, (QuantizedLinear)):
print("--h_l", block)
block.absfreq = torch.zeros(args.array_size+1, dtype=int).cuda()
if isinstance(block, nn.Sequential):
for layer in block.children():
# print("--LAYER", layer)
print("--new block")
for inst in layer.children():
if isinstance(inst, (QuantizedLinear, QuantizedConv2d)):
print("--INST", inst)
inst.absfreq = torch.zeros(args.array_size+1, dtype=int).cuda()
if isinstance(inst, nn.Sequential):
for shortcut_stuff in inst.children():
if isinstance(shortcut_stuff, (QuantizedLinear, QuantizedConv2d)):
print("--shortcut", shortcut_stuff)
shortcut_stuff.absfreq = torch.zeros(args.array_size+1, dtype=int).cuda()
# run train set
test(model, device, train_loader)
accumulated_counts_np = np.zeros(args.array_size+1, dtype=int)
# iterate again trough resnet structure and accumulare the counts
for block in model.children():
# print("BLOCK---", block)
if isinstance(block, (QuantizedLinear)):
# print("--h_l", block)
accumulated_counts_np += block.absfreq.cpu().numpy()
if isinstance(block, nn.Sequential):
for layer in block.children():
# print("--LAYER", layer)
# print("--new block")
for inst in layer.children():
if isinstance(inst, (QuantizedLinear, QuantizedConv2d)):
# print("--INST", inst)
accumulated_counts_np += inst.absfreq.cpu().numpy()
if isinstance(inst, nn.Sequential):
for shortcut_stuff in inst.children():
if isinstance(shortcut_stuff, (QuantizedLinear, QuantizedConv2d)):
# print("--shortcut", shortcut_stuff)
accumulated_counts_np += shortcut_stuff.absfreq.cpu().numpy()
# store accumulated counts to file and create pdf
with open('accumulated_counts_{}.npy'.format(args.dataset), 'wb') as mp:
np.save(mp, accumulated_counts_np)
print("accumulated", accumulated_counts_np)
bins_np_all = np.array([i for i in range(0,args.array_size+1)])
plt.bar(bins_np_all, accumulated_counts_np, color ='black', width = 0.5)
plt.savefig("abs_freq_accumualted_{}.pdf".format(args.dataset), format="pdf")
plt.clf()
if args.extract_absfreq is not None:
# return
# reset all stored data
for layer in model.children():
if isinstance(layer, (QuantizedLinear, QuantizedConv2d)):
layer.absfreq = torch.zeros(args.array_size+1, dtype=int).cuda()
# run train set
test(model, device, train_loader)
for idx, layer in enumerate(model.children()):
if isinstance(layer, (QuantizedLinear, QuantizedConv2d)):
print(idx)
print(layer.absfreq)
counts_np = layer.absfreq.cpu().numpy()
bins_np = np.array([i for i in range(0,args.array_size+1)])
plt.bar(bins_np, counts_np, color ='black', width = 0.5)
plt.savefig("abs_freq_{}_{}.pdf".format(args.dataset, idx), format="pdf")
plt.clf()
# export data to numpy array
accumulated_counts_np = np.zeros(args.array_size+1, dtype=int)
for idx, layer in enumerate(model.children()):
if isinstance(layer, (QuantizedLinear, QuantizedConv2d)):
# print(idx)
# print(layer.absfreq)
accumulated_counts_np += layer.absfreq.cpu().numpy()
# export data
with open('accumulated_counts_{}.npy'.format(args.dataset), 'wb') as mp:
np.save(mp, accumulated_counts_np)
print("accumulated", accumulated_counts_np)
bins_np_all = np.array([i for i in range(0,args.array_size+1)])
plt.bar(bins_np_all, accumulated_counts_np, color ='black', width = 0.5)
plt.savefig("abs_freq_accumualted_{}.pdf".format(args.dataset), format="pdf")
plt.clf()
if __name__ == '__main__':
main()