-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
278 lines (233 loc) · 11 KB
/
search.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
import argparse
import copy
import csv
import hashlib
import json
import numpy as np
import os
import pickle
import random
import time
import torch
from decomposition.utils import *
from evo_agent import EvolutionaryAgent
from utils import *
def search_main():
parser = argparse.ArgumentParser(description='Mixed-TD Search')
parser.add_argument('--gpu', default=None, type=int)
parser.add_argument('--batch_size', default='128', type=int)
parser.add_argument('--workers', default='4', type=int)
parser.add_argument('--output_path', default=None, type=str)
parser.add_argument('--model_name', default='resnet18', type=str,
choices=["resnet18", "repvgga0"])
parser.add_argument('--method', default=['SVD', 'CP'], type=str, nargs='+')
parser.add_argument('--perf_mode', default="fpgaconvnet_sample_predict", type=str,
choices=["fpgaconvnet_macs", "fpgaconvnet_run", "fpgaconvnet_sample_predict"])
args = parser.parse_args()
args.search_size = 50000
args.max_steps = 500
args.start_step = 0
if args.model_name == "resnet18":
args.required_compression_ratio = 0.65
if args.method == ["CP"]:
args.sample_bias = 0.2
args.macs_latency_bias = 0.3
else:
args.sample_bias = 0.1
args.macs_latency_bias = 0.23
elif args.model_name == "repvgga0":
args.required_compression_ratio = 0.75
if args.method == ["CP"]:
args.sample_bias = 0.20
args.macs_latency_bias = 0.35
else:
args.sample_bias = 0.15
args.macs_latency_bias = 0.23
if args.output_path == None:
args.output_path = os.getcwd() + "/output"
if not os.path.exists(args.output_path):
os.makedirs(args.output_path)
args.resume_checkpoint = None
if args.resume_checkpoint is not None:
with open(args.resume_checkpoint, 'rb') as f:
checkpoint = pickle.load(f)
args.start_step = checkpoint["step"] + 1
print(args)
data_begin = time.time()
agent_id = hashlib.md5(args.output_path.encode()).hexdigest()
print("Agent id: " + str(agent_id))
torch.manual_seed(0)
np.random.seed(0)
random.seed(0)
model = load_model(args.model_name)
random_input = torch.randn(model.input_size)
train_loader, search_loader, val_loader = prepare_dataloader(model, args.batch_size, args.workers, args.search_size)
if torch.cuda.is_available():
print("Using gpu " + str(args.gpu))
torch.cuda.set_device(args.gpu)
model.cuda()
random_input = random_input.cuda()
annoate_feature_map_size(model, random_input)
model_begin = time.time()
print("Data loading time: ", model_begin - data_begin)
agent_model = copy.deepcopy(model)
cand_mappings = {}
if torch.cuda.is_available():
agent_model.cuda()
wrapper_mapping = {}
lr_module_cache = {}
conv_layer_index = 0
for name, module in agent_model.named_modules():
if svd_target_module_filter(args.model_name, name, module):
candiate_wrappers, candidates = build_decomposition_candidates(args.model_name, agent_model, name, module, method=args.method)
for wrap, cand in zip(candiate_wrappers, candidates):
wrapper_mapping[cand] = wrap
cfg = EvolutionaryAgent.encode_cfg(None,wrap)
cfg = "_".join([str(x) for x in cfg])
lr_module_cache[(module, cfg)] = cand
cand_mappings[module] = (candiate_wrappers, candidates)
conv_layer_index += 1
search_begin = time.time()
print("Model loading time: ", search_begin - model_begin)
agent_model.eval()
model.eval()
agent = EvolutionaryAgent(agent_id, agent_model, args.model_name, args.perf_mode, args.required_compression_ratio, cand_mappings, search_loader, val_loader, args.macs_latency_bias)
agent.lr_module_cache = lr_module_cache
agent.wrapper_mapping = wrapper_mapping
if args.model_name == "resnet18":
agent.filter_candidates(tolerance=1.0, reverse=False)
elif args.model_name == "repvgga0":
agent.filter_candidates(tolerance=5.0, reverse=True)
init_prob = {}
for module, (candiate_wrappers, candidates) in agent.cand_mappings.items():
dist_mean = args.required_compression_ratio-args.sample_bias-args.macs_latency_bias
range_dict, range_prob = generate_random_sample(candidates, mean=dist_mean)
init_prob[module] = [0] * len(candidates)
for i, (k,v) in enumerate(range_dict.items()):
for j in v:
init_prob[module][j] = range_prob[i] / len(v)
children_size = 100
parents_size = 25
mutation_size = 50
mutate_prob = 0.2
crossover_size = 50
valid_cache = {}
assert children_size == mutation_size + crossover_size # new children per generation
best_top1 = -1
best_info = None
total_step_time = 0
for i in range(args.start_step, args.max_steps):
step_begin = time.time()
if args.perf_mode == "fpgaconvnet_sample_predict":
# schedule fpgaconvnet proxy mode
if i <= 2:
agent.perf_mode = "fpgaconvnet_run"
else:
agent.perf_mode = "fpgaconvnet_predict"
agent.build_predictor()
if args.resume_checkpoint is not None:
dummy_status = agent.report_status()
for k in dummy_status.keys():
setattr(agent, k, checkpoint["agent_status"][k])
parents = []
for j in range(parents_size):
sample = {}
module_count = 0
for module, (candidate_wrappers, candidates) in agent.cand_mappings.items():
for index in range(len(candidate_wrappers)):
cfg = agent.encode_cfg(candidate_wrappers[index])
if cfg == checkpoint["parent_status"][j][module_count]:
sample[module] = (cfg, candidates[index])
break
module_count += 1
assert len(sample) == len(agent.cand_mappings)
parents.append([sample, checkpoint["parent_status"][j][-2], checkpoint["parent_status"][j][-1]])
if i == 0:
population = []
for j in range(children_size):
print("First Generation: ", j, "/", children_size)
sample, accuracy, efficiency = agent.random_sample(weights=init_prob)
population.append([sample, accuracy, efficiency])
else:
for j in range(parents_size):
agent.apply_sample(parents[j][0])
parents[j][1] = agent.get_model_accuracy_proxy()
agent.revert_sample(parents[j][0])
children = []
for j in range(mutation_size):
parent = random.choice(parents)
sample, accuracy, efficiency = agent.mutate_sample(parent[0], mutate_prob)
children.append([sample, accuracy, efficiency])
for j in range(crossover_size):
parent1 = random.choice(parents)
parent2 = random.choice(parents)
sample, accuracy, efficiency = agent.crossover_sample(parent1[0], parent2[0])
children.append([sample, accuracy, efficiency])
population = parents + children
population = sorted(population, key=lambda x: x[1])[::-1]
if agent.perf_mode == "fpgaconvnet_predict":
reject_list = []
for j in range(len(population)):
agent.apply_sample(sample=population[j][0])
actual_efficiency = agent.get_model_performance(mode="fpgaconvnet_run", cfg=[x[0] for x in population[j][0].values()])
agent.revert_sample(sample=population[j][0])
if actual_efficiency <= agent.target_perf:
break
else:
reject_list.append(j)
print ("False prediction reject list: ", reject_list)
population = [population[j] for j in range(len(population)) if j not in reject_list]
if len(population) > parents_size:
parents = population[:parents_size]
else:
assert len(population) > 0
parents = population
else:
parents = population[:parents_size]
# evaluate
val_top1, val_top5 = agent.get_model_valid_accuracy(parents[0][0])
decompositon_config = [config_decoder(v[0]) for v in parents[0][0].values()]
with open(args.output_path + "/search_decomposition_config.json", 'w') as f:
json.dump(decompositon_config, f, indent=4)
info = "Step: {} Search Top1: {} Val Top1: {}".format(i, parents[0][1], val_top1)
if agent.perf_mode not in ["fpgaconvnet_predict", "fpgaconvnet_macs"]:
info += " Efficiency: {}".format(parents[0][2])
else:
agent.apply_sample(sample=parents[0][0])
actual_efficiency = agent.get_model_performance(mode="fpgaconvnet_run", cfg=[x[0] for x in parents[0][0].values()])
info += " Efficiency: {}".format(actual_efficiency)
agent.revert_sample(sample=parents[0][0])
agent.apply_sample(sample=parents[0][0])
info += " MACs: {}".format(agent.get_model_performance(mode="fpgaconvnet_macs", cfg=[x[0] for x in parents[0][0].values()]))
agent.revert_sample(sample=parents[0][0])
print(info)
if val_top1 > best_top1:
best_top1 = val_top1
best_info = info
with open(args.output_path + "/best_decomposition_config.json", 'w') as f:
json.dump(decompositon_config, f, indent=4)
print("Best ", best_info)
step_end = time.time()
total_step_time += step_end - step_begin
print("Step: {} Time: {} / {}".format(i, step_end - step_begin, total_step_time))
print("Acc Queries: ", agent.acc_query_count, "Perf Queries: ", agent.perf_query_count)
# save checkpoint
agent_status = agent.report_status()
parent_status = []
for p in parents:
parent_status.append([x[0] for x in p[0].values()] + [p[1], p[2]])
with open(args.output_path + "/checkpoint.pickle", 'wb') as f:
pickle.dump({"step": i, "agent_status": agent_status, "parent_status": parent_status}, f)
if agent.perf_mode == "fpgaconvnet_run":
np.save(args.output_path + "/X.npy", np.array(agent.pred_X))
np.save(args.output_path + "/Y.npy", np.array(agent.pred_Y))
csv_entry = [i]
csv_entry += decompositon_config
csv_entry += [val_top1, val_top5, parents[0][2]]
with open(args.output_path + "/nas_log.csv", mode='a') as f:
csv_writer = csv.writer(f)
csv_writer.writerow(csv_entry)
search_end = time.time()
print("Total time: ", search_end - data_begin)
if __name__ == "__main__":
search_main()