-
Notifications
You must be signed in to change notification settings - Fork 879
/
Copy pathauto_benchmark.py
332 lines (275 loc) · 10.9 KB
/
auto_benchmark.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
import argparse
import datetime
import os
import shlex
import shutil
from subprocess import Popen
import ruamel.yaml
from utils import gen_md_report, gen_metrics_json, gen_model_config_json
CWD = os.getcwd()
MODEL_JSON_CONFIG_PATH = CWD + "/model_json_config"
BENCHMARK_TMP_PATH = "/tmp/benchmark"
BENCHMARK_REPORT_PATH = "/tmp/ts_benchmark"
TS_LOGS_PATH = CWD + "/logs"
MODEL_STORE = "/tmp/model_store"
WF_STORE = "/tmp/wf_store"
class BenchmarkConfig:
def __init__(self, yaml_dict, skip_ts_install, skip_upload):
self.yaml_dict = yaml_dict
self.skip_ts_install = skip_ts_install
self.skip_upload = skip_upload
self.bm_config = {}
yesterday = datetime.date.today() - datetime.timedelta(days=1)
self.bm_config["version"] = "torchserve-nightly=={}.{}.{}".format(
yesterday.year, yesterday.month, yesterday.day
)
self.bm_config["hardware"] = "cpu"
def ts_version(self, version):
for k, v in version.items():
if k == "branch":
self.bm_config["version"] = v
elif k == "nightly":
self.bm_config["version"] = "torchserve-nightly=={}".format(v)
elif k == "release":
self.bm_config["version"] = "torchserve=={}".format(v)
break
def models(self, model_files):
self.bm_config["models"] = model_files
def hardware(self, hw):
self.bm_config["hardware"] = hw
def metrics_cmd(self, cmd):
cmd_options = []
for key_value in cmd:
for k, v in key_value.items():
if k == "cmd":
cmd_options.append(v)
elif k == "--namespace":
cmd_options.append(k)
cmd_options.append("".join(v))
else:
cmd_options.append(k)
cmd_options.append(v)
break
self.bm_config["metrics_cmd"] = " ".join(cmd_options)
def report_cmd(self, cmd):
cmd_options = []
for key_value in cmd:
for k, v in key_value.items():
if k == "cmd":
cmd_options.append(v)
elif k == "dest":
for i in range(len(v)):
if v[i] == "today()":
today = datetime.date.today()
v[i] = "{}-{}-{}".format(today.year, today.month, today.day)
break
cmd_options.append(
"{}/{}".format("/".join(v), self.bm_config["version"])
)
else:
cmd_options.append(v)
break
self.bm_config["report_cmd"] = " ".join(cmd_options)
def load_config(self):
report_cmd = None
for k, v in self.yaml_dict.items():
if k == "ts_version":
self.ts_version(v)
elif k == "models":
self.models(v)
elif k == "hardware":
self.hardware(v)
elif k == "metrics_cmd" and not self.skip_upload:
self.metrics_cmd(v)
elif k == "report_cmd" and not self.skip_upload:
report_cmd = v
self.bm_config["model_config_path"] = (
"{}/{}".format(MODEL_JSON_CONFIG_PATH, self.bm_config["hardware"])
if self.bm_config["hardware"] in ["cpu", "gpu", "neuron", "neuronx"]
else "{}/cpu".format(MODEL_JSON_CONFIG_PATH)
)
if self.skip_ts_install:
self.bm_config["version"] = get_torchserve_version()
if report_cmd:
self.report_cmd(report_cmd)
for k, v in self.bm_config.items():
print("{}={}".format(k, v))
def load_benchmark_config(bm_config_path, skip_ts_install, skip_upload):
yaml = ruamel.yaml.YAML()
with open(bm_config_path, "r") as f:
yaml_dict = yaml.load(f)
benchmark_config = BenchmarkConfig(yaml_dict, skip_ts_install, skip_upload)
benchmark_config.load_config()
return benchmark_config.bm_config
def benchmark_env_setup(bm_config, skip_ts_install, nightly):
install_torchserve(
skip_ts_install, bm_config["hardware"], bm_config["version"], nightly
)
setup_benchmark_path(bm_config["model_config_path"])
build_model_json_config(bm_config["models"])
enable_launcher_with_logical_core(bm_config["hardware"])
def install_torchserve(skip_ts_install, hw, ts_version, nightly):
if skip_ts_install:
return
# git checkout branch if it is needed
cmd = "git checkout master && git reset --hard && git clean -dffx . && git pull --rebase"
execute(cmd, wait=True)
print("successfully reset git")
ts_install_cmd = None
if ts_version.startswith("torchserve==") or ts_version.startswith(
"torchserve-nightly=="
):
ts_install_cmd = "pip install {}".format(ts_version)
else:
cmd = "git checkout {}".format(ts_version)
execute(cmd, wait=True)
# install_dependencies.py
if hw == "gpu":
cmd = "python ts_scripts/install_dependencies.py --environment dev --cuda cu121"
elif hw == "neuronx":
cmd = "python ts_scripts/install_dependencies.py --environment dev --neuronx"
else:
cmd = "python ts_scripts/install_dependencies.py --environment dev"
if nightly:
cmd += " --nightly_torch"
execute(cmd, wait=True)
print("successfully install install_dependencies.py")
# install torchserve
if ts_install_cmd is None:
ts_install_cmd = "python ts_scripts/install_from_src.py"
execute(ts_install_cmd, wait=True)
print("successfully install torchserve")
def setup_benchmark_path(model_config_path):
benchmark_path_list = [BENCHMARK_TMP_PATH, BENCHMARK_REPORT_PATH, model_config_path]
for benchmark_path in benchmark_path_list:
shutil.rmtree(benchmark_path, ignore_errors=True)
os.makedirs(benchmark_path, exist_ok=True)
print("successfully setup benchmark_path={}".format(benchmark_path))
def build_model_json_config(models):
for model in models:
if model.startswith("/"):
input_file = model
else:
input_file = CWD + "/benchmarks/models_config/{}".format(model)
gen_model_config_json.convert_yaml_to_json(input_file, MODEL_JSON_CONFIG_PATH)
def enable_launcher_with_logical_core(hw):
if hw == "cpu":
with open("./benchmarks/config.properties", "a") as f:
f.write("cpu_launcher_enable=true\n")
f.write("cpu_launcher_args=--use_logical_core\n")
def run_benchmark(bm_config):
files = os.listdir(bm_config["model_config_path"])
files.sort()
for model_json_config in files:
if model_json_config.endswith(".json"):
# call benchmark-ab.py
shutil.rmtree(TS_LOGS_PATH, ignore_errors=True)
shutil.rmtree(BENCHMARK_TMP_PATH, ignore_errors=True)
cmd = (
"python ./benchmarks/benchmark-ab.py --tmp_dir /tmp --report_location /tmp --config_properties "
"./benchmarks/config.properties --config {}/{}".format(
bm_config["model_config_path"], model_json_config
)
)
execute(cmd, wait=True)
# generate stats metrics from ab_report.csv
bm_model = model_json_config[0 : -len(".json")]
gen_metrics_json.gen_metric(
"{}/ab_report.csv".format(BENCHMARK_TMP_PATH),
"{}/logs/stats_metrics.json".format(BENCHMARK_TMP_PATH),
)
# load stats metrics to remote metrics storage
if "metrics_cmd" in bm_config:
execute(bm_config["metrics_cmd"], wait=True)
# cp benchmark logs to local
bm_model_log_path = "{}/{}".format(BENCHMARK_REPORT_PATH, bm_model)
os.makedirs(bm_model_log_path, exist_ok=True)
csv_file = "{}/ab_report.csv".format(BENCHMARK_TMP_PATH)
if os.path.exists(csv_file):
shutil.move(csv_file, bm_model_log_path)
cmd = "tar -cvzf {}/benchmark.tar.gz {}".format(
bm_model_log_path, BENCHMARK_TMP_PATH
)
execute(cmd, wait=True)
cmd = "tar -cvzf {}/logs.tar.gz {}".format(bm_model_log_path, TS_LOGS_PATH)
execute(cmd, wait=True)
print("finish benchmark {}".format(bm_model))
# generate final report
gen_md_report.iterate_subdir(
BENCHMARK_REPORT_PATH,
"{}/report.md".format(BENCHMARK_REPORT_PATH),
bm_config["hardware"],
bm_config["version"],
)
print("report.md is generated")
# load logs to remote storage
if "report_cmd" in bm_config:
execute(bm_config["report_cmd"], wait=True)
def clean_up_benchmark_env(bm_config):
shutil.rmtree(BENCHMARK_TMP_PATH, ignore_errors=True)
shutil.rmtree(MODEL_JSON_CONFIG_PATH, ignore_errors=True)
shutil.rmtree(MODEL_STORE, ignore_errors=True)
shutil.rmtree(WF_STORE, ignore_errors=True)
def execute(command, wait=False, stdout=None, stderr=None, shell=True):
print("execute: {}".format(command))
# Split the command into a list of arguments
if isinstance(command, str):
command = shlex.split(command)
cmd = Popen(
command,
close_fds=True,
stdout=stdout,
stderr=stderr,
universal_newlines=True,
)
if wait:
cmd.wait()
return cmd
def get_torchserve_version():
# fetch the torchserve version from version.txt file
with open(os.path.join(CWD, "ts", "version.txt"), "r") as file:
version = file.readline().rstrip()
return version
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--input",
action="store",
help="benchmark config yaml file path",
)
parser.add_argument(
"--skip",
action="store",
help="true: skip torchserve installation. default: true",
)
parser.add_argument(
"--skip_upload",
help="true: skip uploading commands. default: false",
)
parser.add_argument(
"--nightly",
help="true: install nightly version of torch package. default: false",
)
arguments = parser.parse_args()
skip_ts_config = (
False
if arguments.skip is not None and arguments.skip.lower() == "false"
else True
)
skip_upload = (
True
if arguments.skip_upload is not None and arguments.skip_upload.lower() == "true"
else False
)
nightly = (
True
if arguments.nightly is not None and arguments.nightly.lower() == "true"
else False
)
bm_config = load_benchmark_config(arguments.input, skip_ts_config, skip_upload)
benchmark_env_setup(bm_config, skip_ts_config, nightly)
run_benchmark(bm_config)
clean_up_benchmark_env(bm_config)
print("benchmark_serving.sh finished successfully.")
if __name__ == "__main__":
main()