-
Notifications
You must be signed in to change notification settings - Fork 1
/
executor.py
226 lines (197 loc) · 7.39 KB
/
executor.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
import time
import os
import json
import gc
import multiprocessing as mp
import pandas as pd
import collections
import infer_perf
import infer_perf.util
import bm_manager.client
TAG = '[BenchmarkExectutor]'
class Benchmark:
def __init__(self, data_size=256, warmup=1, rounds=1):
self.data_size = data_size
self.warmup = warmup
self.rounds = rounds
def execute(self, runner):
metric = infer_perf.util.simple_bench(runner,
data_size=self.data_size,
warmup=self.warmup,
rounds=self.rounds,
verbose=True)
return metric
def __str__(self):
return 'data_size:{} warmup:{} rounds:{}'.format(
self.data_size, self.warmup, self.rounds)
class Task:
def __init__(self, fe, optimizer, model, batch_size, device):
self.fe = fe
self.optimizer = optimizer
self.model = model
self.batch_size = batch_size
self.device = device
self.params = {}
def get_runner(self):
if self.optimizer == 'xla':
if self.fe == 'tf-train':
from infer_perf.tf_train import train_runner
return train_runner(self.model,
self.batch_size,
self.device,
xla=True)
else:
from to_xla import xla_runner
return xla_runner(self.fe,
self.model,
self.batch_size,
self.device,
xla=True)
elif self.optimizer == 'tvm':
from infer_perf.to_tvm import tvm_runner
return tvm_runner(self.fe, self.model, self.batch_size,
self.device)
elif self.optimizer == 'trt':
from infer_perf.to_trt import trt_runner
return trt_runner(self.fe, self.model, self.batch_size,
self.device)
elif self.optimizer == 'baseline':
if self.fe == "torch":
from infer_perf.to_torch import torch_runner
return torch_runner(self.model, self.batch_size, self.device)
elif self.fe == "tf":
from infer_perf.to_xla import xla_runner
return xla_runner(self.fe,
self.model,
self.batch_size,
self.device,
xla=False)
elif self.fe == 'tf-train':
from infer_perf.tf_train import train_runner
return train_runner(self.model,
self.batch_size,
self.device,
xla=False)
else:
return None
def get_info(self):
return {
"optimizer": self.optimizer,
"fe": self.fe,
"model": self.model,
"batch_size": self.batch_size,
"device": self.device,
}
def __str__(self):
return str(list(self.get_info().values()))
def validate_config(config):
if 'fe' not in config or len(config['fe']) == 0:
return 'Missing frontend', False
if 'optimizer' not in config or len(config['optimizer']) == 0:
return 'Missing optimizer', False
if 'model' not in config or len(config['model']) == 0:
return 'Missing models', False
if 'batch_size' not in config or len(config['batch_size']) == 0:
return "Missg batch_sizes", False
return '', True
def generate_tasks(config, batch=-1):
tasks = []
for model in config['model']:
for batch_size in config['batch_size']:
if batch != -1:
if batch_size != batch:
continue
for optimizer in config['optimizer']:
for device in config['device']:
for fe in config['fe']:
tasks.append(
Task(fe, optimizer, model, batch_size, device))
return tasks
def execute_worker(resultq, benchmark, task):
task_info = task.get_info()
print(TAG, 'Star process stask: {}'.format(task_info))
print(TAG, 'Try to get runner...')
runner = task.get_runner()
if runner is None:
print('Get invalid task: {}'.format(task_info))
return
print(TAG, 'Start to run benchmark')
metric = benchmark.execute(runner)
task_info['metric'] = metric
print(task_info)
resultq.put(task_info)
def execute_manager(config, file, warmup, rounds, data_size, batch, server, group):
msg, valid = validate_config(config)
if not valid:
raise Exception("Invlida benchmark config : {}".format(msg))
tasks = generate_tasks(config, batch)
print("Get Tasks:\n {}".format("\n".join([str(task) for task in tasks])))
resultq = mp.Queue()
benchmark = Benchmark(data_size=data_size, warmup=warmup, rounds=rounds)
print("Create benchmark: {}".format(benchmark))
for task in tasks:
try:
p = mp.Process(target=execute_worker,
args=(resultq, benchmark, task))
p.start()
p.join()
except Exception as e:
print("Got exception when run {}:{}".format(task, e))
result = collections.defaultdict(list)
while not resultq.empty():
for k, v in resultq.get().items():
result[k].append(v)
result_df = pd.DataFrame.from_dict(data=result)
if file != "":
result_df.to_csv(file, index=False, header=True)
if server != "":
bm_manager.client.update_df(result_df, server, group)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="benchmark task runner")
parser.add_argument("task_file", type=str, help="json file of tasks")
parser.add_argument("-f",
"--report",
type=str,
help="output file of results")
parser.add_argument("-w",
"--warmup",
default=5,
type=int,
help="warm up rounds")
parser.add_argument("-r",
"--rounds",
default=30,
type=int,
help="rounds to execute runner")
parser.add_argument("-s",
"--size",
default=256,
type=int,
help="size of test data size")
parser.add_argument(
"-b",
"--batch",
default=-1,
type=int,
help="specific batch size to run (-1 for without specification)")
parser.add_argument(
"--server",
default="",
type=str,
help="url of bm server")
parser.add_argument(
"--group",
default="test",
type=str,
help="group name of benchmark"
)
args = parser.parse_args()
with open(args.task_file) as f:
config = json.load(f)
try:
mp.set_start_method('spawn')
except RuntimeError:
pass
execute_manager(config, args.report, args.warmup, args.rounds,
args.size, args.batch, args.server, args.group)