forked from pytorch/benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bisection.py
588 lines (548 loc) · 25.3 KB
/
bisection.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
"""bisection.py
Runs bisection to determine PRs that cause performance change.
It assumes that the pytorch, torchbench, torchtext and torchvision repositories provided are all clean with the latest code.
By default, the torchvision and torchtext package version will be fixed to the latest commit on the pytorch commit date.
Usage:
python bisection.py --work-dir <WORK-DIR> \
--pytorch-src <PYTORCH_SRC_DIR> \
--torchbench-src <TORCHBENCH_SRC_DIR> \
--config <BISECT_CONFIG> --output <OUTPUT_FILE_PATH>
"""
import os
import json
import shutil
import yaml
import argparse
from tabulate import tabulate
import re
import subprocess
from datetime import datetime
from typing import Optional, List, Dict, Tuple
from torchbenchmark.util import gitutils
from utils.cuda_utils import prepare_cuda_env, DEFAULT_CUDA_VERSION
TORCH_GITREPO="https://github.com/pytorch/pytorch.git"
TORCHBENCH_GITREPO="https://github.com/pytorch/benchmark.git"
TORCHBENCH_DEPS = {
"torchtext": (os.path.expandvars("${HOME}/text"), "main"),
"torchvision": (os.path.expandvars("${HOME}/vision"), "main"),
}
def exist_dir_path(string):
if os.path.isdir(string):
return string
else:
raise NotADirectoryError(string)
# Translates test name to filter
# For example, ["test_eval[yolov3-cpu-eager]", "test_train[yolov3-gpu-eager]"]
# -> "((eval and yolov3 and cpu and eager) or (train and yolov3 and gpu and eager))"
# If targets is None, run everything except slomo
def targets_to_bmfilter(targets: List[str], models: List[str]) -> str:
bmfilter_names = []
if targets == None or len(targets) == 0:
return "(not slomo)"
for test in targets:
regex = re.compile("test_(train|eval)\[([a-zA-Z0-9_]+)-([a-z]+)-([a-z]+)\]")
m = regex.match(test)
if not m:
if test in models:
partial_name = test
else:
print(f"Cannot recognize the TorchBench filter: {test}. Exit.")
exit(1)
else:
partial_name = " and ".join(m.groups())
bmfilter_names.append(f"({partial_name})")
return "(" + " or ".join(bmfilter_names) + ")"
# Find the latest non-empty json file in the directory
def find_latest_json_file(result_dir: str):
json_files = list(filter(lambda x: x.endswith(".json"), os.listdir(result_dir)))
json_files.sort(reverse=True)
for f in json_files:
# Return the first non-empty json file
json_path = os.path.join(result_dir, f)
if os.path.exists(json_path) and os.stat(json_path).st_size:
return json_path
print(f"Can't find non-empty json files in path: {result_dir}")
return str()
def get_delta_str(reference: float, current: float) -> str:
delta_num = ((current - reference) / current * 100)
delta_str = "{:+3f}".format(delta_num) + "%"
if (abs(delta_num) >= 5):
delta_str = delta_str + "*"
return delta_str
def get_means(data):
rc = dict()
for param in data["benchmarks"]:
name = param["name"]
mean = param["stats"]["mean"]
rc[name] = mean
return rc
def analyze_abtest_result_dir(result_dir: str):
dirs = [ os.path.join(result_dir, name) for name in os.listdir(result_dir) if os.path.isdir(os.path.join(result_dir, name)) ]
delta = False
json_files = list(filter(len, map(find_latest_json_file, dirs)))
out = [['Benchmark']]
assert json_files, f"Don't find benchmark result files in {result_dir}."
# If there are only two json files, we believe it is an abtest, so print delta of the mean
if len(json_files) == 2:
delta = True
with open(json_files[0], "r") as fp:
cur_result = json.load(fp)
means = get_means(cur_result)
for key in means:
out.append([])
out[-1].append(key)
for index, json_file in enumerate(json_files):
with open(json_file, "r") as fp:
jsonobj = json.load(fp)
header = f"Run {os.path.basename(os.path.dirname(json_file))}"
out[0].append(header)
means = get_means(jsonobj)
if delta and index == 0:
reference = means
for key_index, key in enumerate(means):
out[key_index+1].append(means[key])
if delta and index == 1:
out[0].append("Delta")
out[key_index+1].append(get_delta_str(reference[key], means[key]))
out_str = tabulate(out, headers='firstrow')
return out_str
class Commit:
sha: str
ctime: str
digest: Dict[str, float]
def __init__(self, sha, ctime):
self.sha = sha
self.ctime = ctime
self.digest = None
def __str__(self):
return self.sha
class TorchSource:
srcpath: str
build_lazy: bool
commits: List[Commit]
build_env: os._Environ
# Map from commit SHA to index in commits
commit_dict: Dict[str, int]
def __init__(self, srcpath: str, build_lazy: bool):
self.srcpath = srcpath
self.build_lazy = build_lazy
self.commits = []
self.commit_dict = dict()
def prep(self, build_env: os._Environ) -> bool:
repo_origin_url = gitutils.get_git_origin(self.srcpath)
if not repo_origin_url == TORCH_GITREPO:
print(f"WARNING: Unmatched repo origin url: {repo_origin_url} with standard {TORCH_GITREPO}")
self.update_repos()
# Clean up the existing packages
self.cleanup()
self.build_env = build_env
return True
# Update pytorch, torchtext, and torchvision repo
def update_repos(self):
repos = [(self.srcpath, "master")]
repos.extend(TORCHBENCH_DEPS.values())
for (repo, branch) in repos:
gitutils.clean_git_repo(repo)
assert gitutils.update_git_repo(repo, branch), f"Failed to update {branch} branch of repository {repo}."
# Get all commits between start and end, save them in self.commits
def init_commits(self, start: str, end: str, abtest: bool) -> bool:
if not abtest:
commits = gitutils.get_git_commits(self.srcpath, start, end)
else:
commits = [start, end]
if not commits or len(commits) < 2:
print(f"Failed to retrieve commits from {start} to {end} in {self.srcpath}.")
return False
for count, commit in enumerate(commits):
ctime = gitutils.get_git_commit_date(self.srcpath, commit)
self.commits.append(Commit(sha=commit, ctime=ctime))
self.commit_dict[commit] = count
return True
def get_mid_commit(self, left: Commit, right: Commit) -> Optional[Commit]:
left_index = self.commit_dict[left.sha]
right_index = self.commit_dict[right.sha]
if right_index == left_index + 1:
return None
else:
return self.commits[int((left_index + right_index) / 2)]
def setup_build_env(self, env) -> Dict[str, str]:
env["USE_CUDA"] = "1"
env["BUILD_CAFFE2_OPS"] = "0"
# Do not build the test
env["BUILD_TEST"] = "0"
env["USE_MKLDNN"] = "1"
env["USE_MKL"] = "1"
env["USE_CUDNN"] = "1"
env["CMAKE_PREFIX_PATH"] = env["CONDA_PREFIX"]
return env
# Checkout the last commit of dependencies on date
def checkout_deps(self, cdate: datetime):
for pkg in TORCHBENCH_DEPS:
pkg_path, branch = TORCHBENCH_DEPS[pkg]
gitutils.checkout_git_branch(pkg_path, branch)
dep_commit = gitutils.get_git_commit_on_date(pkg_path, cdate)
print(f"Checking out {pkg} commit {dep_commit} ...", end="", flush=True)
assert dep_commit, "Failed to find the commit on {cdate} of {pkg}"
assert gitutils.checkout_git_commit(pkg_path, dep_commit), "Failed to checkout commit {commit} of {pkg}"
print("done.")
# Install dependencies such as torchtext and torchvision
def build_install_deps(self, build_env):
# Build torchvision
print(f"Building torchvision ...", end="", flush=True)
command = "python setup.py install"
subprocess.check_call(command, cwd=TORCHBENCH_DEPS["torchvision"][0], env=build_env, shell=True)
print("done")
# Build torchtext
print(f"Building torchtext ...", end="", flush=True)
command = "python setup.py clean install"
subprocess.check_call(command, cwd=TORCHBENCH_DEPS["torchtext"][0], env=build_env, shell=True)
print("done")
def _build_lazy_tensor(self, commit: Commit, build_env: Dict[str, str]):
if self.build_lazy:
print(f"Building pytorch lazy tensor on {commit.sha} ...", end="", flush=True)
lazy_tensor_path = os.path.join(self.srcpath, "lazy_tensor_core")
command = "./scripts/apply_patches.sh"
subprocess.check_call(command, cwd=self.lazy_tensor_path, env=build_env, shell=True)
command = "python setup.py install"
subprocess.check_call(command, cwd=self.lazy_tensor_path, env=build_env, shell=True)
print("done")
def build(self, commit: Commit):
# checkout pytorch commit
print(f"Checking out pytorch commit {commit.sha} ...", end="", flush=True)
gitutils.checkout_git_commit(self.srcpath, commit.sha)
print("done.")
# checkout pytorch deps commit
ctime = datetime.strptime(commit.ctime.split(" ")[0], "%Y-%m-%d")
self.checkout_deps(ctime)
# setup environment variables
build_env = self.setup_build_env(self.build_env)
# build pytorch
print(f"Building pytorch commit {commit.sha} ...", end="", flush=True)
# Check if version.py exists, if it does, remove it.
# This is to force pytorch update the version.py file upon incremental compilation
version_py_path = os.path.join(self.srcpath, "torch/version.py")
if os.path.exists(version_py_path):
os.remove(version_py_path)
try:
command = "python setup.py install"
subprocess.check_call(command, cwd=self.srcpath, env=build_env, shell=True)
command_testbuild = "python -c 'import torch'"
subprocess.check_call(command_testbuild, cwd=os.environ["HOME"], env=build_env, shell=True)
except subprocess.CalledProcessError:
# Remove the build directory, then try build it again
build_path = os.path.join(self.srcpath, "build")
if os.path.exists(build_path):
shutil.rmtree(build_path)
subprocess.check_call(command, cwd=self.srcpath, env=build_env, shell=True)
print("done")
# build pytorch lazy tensor if needed
self._build_lazy_tensor(commit, build_env)
self.build_install_deps(build_env)
def cleanup(self):
packages = ["torch", "torchtext", "torchvision"]
CLEANUP_ROUND = 5
# Clean up multiple times to make sure the packages are all uninstalled
for _ in range(CLEANUP_ROUND):
command = "pip uninstall -y " + " ".join(packages) + " || true"
subprocess.check_call(command, shell=True)
print("done")
class TorchBench:
srcpath: str # path to pytorch/benchmark source code
branch: str
timelimit: int # timeout limit in minutes
workdir: str
models: List[str]
first_time: bool
torch_src: TorchSource
bench_env: os._Environ
def __init__(self, srcpath: str,
torch_src: TorchSource,
timelimit: int,
workdir: str):
self.srcpath = srcpath
self.torch_src = torch_src
self.timelimit = timelimit
self.workdir = workdir
self.first_time = True
self.models = list()
def prep(self, bench_env) -> bool:
self.bench_env = bench_env
# Verify the code in srcpath is pytorch/benchmark
repo_origin_url = gitutils.get_git_origin(self.srcpath)
if not repo_origin_url == TORCHBENCH_GITREPO:
print(f"WARNING: Unmatched repo origin url: {repo_origin_url} with standard {TORCHBENCH_GITREPO}")
# get the name of current branch
self.branch = gitutils.get_current_branch(self.srcpath)
# get list of models
self.models = [ model for model in os.listdir(os.path.join(self.srcpath, "torchbenchmark", "models"))
if os.path.isdir(os.path.join(self.srcpath, "torchbenchmark", "models", model)) ]
return True
def _install_benchmark(self):
"Install and build TorchBench dependencies"
command = ["python", "install.py"]
subprocess.check_call(command, cwd=self.srcpath, env=self.bench_env, shell=False)
def run_benchmark(self, commit: Commit, targets: List[str]) -> str:
# Return the result json file path
output_dir = os.path.join(self.workdir, commit.sha)
# If the directory already exists, clear its contents
if os.path.exists(output_dir):
assert os.path.isdir(output_dir), "Must specify output directory: {output_dir}"
filelist = [ f for f in os.listdir(output_dir) ]
for f in filelist:
os.remove(os.path.join(output_dir, f))
else:
os.mkdir(output_dir)
bmfilter = targets_to_bmfilter(targets, self.models)
# If the first time to run benchmark, install the dependencies first
if self.first_time:
self._install_benchmark()
self.first_time = False
print(f"Running TorchBench for commit: {commit.sha}, filter {bmfilter} ...", end="", flush=True)
command = f"""bash .github/scripts/run.sh "{output_dir}" "{bmfilter}" 2>&1 | tee {output_dir}/benchmark.log"""
try:
subprocess.check_call(command, cwd=self.srcpath, env=self.bench_env, shell=True, timeout=self.timelimit * 60)
except subprocess.TimeoutExpired:
print(f"Benchmark timeout for {commit.sha}. Result will be None.")
return output_dir
print("done.")
return output_dir
def gen_digest(self, result_dir: str, targets: List[str]) -> Dict[str, float]:
filelist = [ f for f in os.listdir(result_dir) if f.endswith(".json") ]
out = dict()
if not len(filelist):
print(f"Empty directory or json file in {result_dir}. Return empty digest.")
return out
# Use the first json as the benchmark data file
data_file = os.path.join(result_dir, filelist[0])
if not os.stat(data_file).st_size:
print(f"Empty json file {filelist[0]} in {result_dir}. Return empty digest.")
return out
with open(data_file, "r") as df:
data = json.load(df)
# Fill in targets if it is None
if targets == None:
targets = list()
for each in data["benchmarks"]:
targets.append(each["name"])
old_targets = targets.copy()
for t in filter(lambda x: x in self.models, old_targets):
targets.remove(t)
names = filter(lambda y: t in y, map(lambda x: x["name"], data["benchmarks"]))
targets.extend(list(names))
for each in data["benchmarks"]:
if each["name"] in targets:
out[each["name"]] = each["stats"]["mean"]
# Make sure all target tests are available
for target in targets:
assert out[target], f"Don't find benchmark result of {target} in {filelist[0]}."
return out
def get_digest(self, commit: Commit, targets: List[str], debug: bool) -> Dict[str, float]:
# digest is cached
if commit.digest is not None:
return commit.digest
# if debug mode, skip the build and benchmark run
if debug:
result_dir = os.path.join(self.workdir, commit.sha)
if os.path.isdir(result_dir):
filelist = [ f for f in os.listdir(result_dir) if f.endswith(".json") ]
if len(filelist):
data_file = os.path.join(result_dir, filelist[0])
if os.stat(data_file).st_size:
commit.digest = self.gen_digest(result_dir, targets)
return commit.digest
# Build pytorch and its dependencies
self.torch_src.build(commit)
# Run benchmark
result_dir = self.run_benchmark(commit, targets)
commit.digest = self.gen_digest(result_dir, targets)
print(f"Cleaning up packages from commit {commit.sha} ...", end="", flush=True)
self.torch_src.cleanup()
return commit.digest
class TorchBenchBisection:
workdir: str
start: str
end: str
threshold: float
direction: str
targets: List[str]
# left commit, right commit, targets to test
bisectq: List[Tuple[Commit, Commit, List[str]]]
result: List[Tuple[Commit, Commit]]
torch_src: TorchSource
bench: TorchBench
output_json: str
debug: bool
abtest: bool
def __init__(self,
workdir: str,
torch_src: str,
bench_src: str,
start: str,
end: str,
threshold: float,
direction: str,
timeout: int,
targets: List[str],
output_json: str,
build_lazy: bool = False,
debug: bool = False):
self.workdir = workdir
self.start = start
self.end = end
self.threshold = threshold
self.direction = direction
self.targets = targets
self.bisectq = list()
self.result = list()
self.torch_src = TorchSource(srcpath = torch_src, build_lazy=build_lazy)
self.bench = TorchBench(srcpath = bench_src,
torch_src = self.torch_src,
timelimit = timeout,
workdir = self.workdir)
self.output_json = output_json
self.debug = debug
# Special treatment for abtest
self.abtest = False
if self.threshold == 100.0 and self.direction == "decrease":
self.abtest = True
# Left: older commit; right: newer commit
# Return: List of targets that satisfy the regression rule: <threshold, direction>
def regression(self, left: Commit, right: Commit, targets: List[str]) -> List[str]:
# If uncalculated, commit.digest will be None
assert left.digest, "Commit {left.sha} must have a digest"
assert right.digest, "Commit {right.sha} must have a digest"
out = []
for target in targets:
# digest could be empty if benchmark timeout
left_mean = left.digest[target] if len(left.digest) else 0
right_mean = right.digest[target] if len(right.digest) else 0
# If either left or right timeout, diff is 100. Otherwise use the min mean value to calculate diff.
diff = abs(left_mean - right_mean) / min(left_mean, right_mean) * 100 if min(left_mean, right_mean) else 100
# If both timeout, diff is zero percent
diff = 0 if not max(left_mean, right_mean) else diff
print(f"Target {target}: left commit {left.sha} mean {left_mean} vs. right commit {right.sha} mean {right_mean}. Diff: {diff}.")
if diff >= self.threshold:
if self.direction == "increase" and left_mean < right_mean:
# Time increase == performance regression
out.append(target)
elif self.direction == "decrease" and left_mean > right_mean:
# Time decrease == performance optimization
out.append(target)
elif self.direction == "both":
out.append(target)
return out
def prep(self) -> bool:
base_build_env = prepare_cuda_env(cuda_version=DEFAULT_CUDA_VERSION)
if not self.torch_src.prep(base_build_env):
return False
if not self.torch_src.init_commits(self.start, self.end, self.abtest):
return False
if not self.bench.prep(base_build_env):
return False
left_commit = self.torch_src.commits[0]
right_commit = self.torch_src.commits[-1]
self.bisectq.append((left_commit, right_commit, self.targets))
return True
def run(self):
while len(self.bisectq):
(left, right, targets) = self.bisectq.pop(0)
self.bench.get_digest(left, targets, self.debug)
self.bench.get_digest(right, targets, self.debug)
if targets == None and len(left.digest):
targets = left.digest.keys()
if targets == None and len(right.digest):
targets = right.digest.keys()
updated_targets = self.regression(left, right, targets)
if len(updated_targets):
mid = self.torch_src.get_mid_commit(left, right)
if mid == None:
self.result.append((left, right))
else:
self.bisectq.append((left, mid, updated_targets))
self.bisectq.append((mid, right, updated_targets))
def output(self):
json_obj = dict()
json_obj["start"] = self.start
json_obj["end"] = self.end
json_obj["threshold"] = self.threshold
json_obj["timeout"] = self.bench.timelimit
json_obj["torchbench_branch"] = self.bench.branch
json_obj["result"] = []
for res in self.result:
r = dict()
r["commit1"] = res[0].sha
r["commit1_time"] = res[0].ctime
r["commit1_digest"] = res[0].digest if len(res[0].digest) else "timeout"
r["commit2"] = res[1].sha
r["commit2_time"] = res[1].ctime
r["commit2_digest"] = res[1].digest if len(res[1].digest) else "timeout"
json_obj["result"].append(r)
with open(self.output_json, 'w') as outfile:
json.dump(json_obj, outfile, indent=2)
def output_abtest_result(self):
abtest_result = analyze_abtest_result_dir(self.workdir)
with open(self.output_json, 'w') as outfile:
outfile.write(abtest_result)
print(abtest_result)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--work-dir",
help="bisection working directory",
type=exist_dir_path)
parser.add_argument("--pytorch-src",
help="the directory of pytorch source code git repository",
type=exist_dir_path)
parser.add_argument("--torchbench-src",
help="the directory of torchbench source code git repository",
type=exist_dir_path)
parser.add_argument("--config",
help="the bisection configuration in YAML format")
parser.add_argument("--output",
help="the output json file")
parser.add_argument("--analyze-result",
help="specify the output result directory to analyze")
# by default, do not build lazy tensor
parser.add_argument("--build-lazy",
action='store_true',
help="build lazy tensor feature in PyTorch")
# by default, debug mode is disabled
parser.add_argument("--debug",
help="run in debug mode, if the result json exists, use it directly",
action='store_true')
args = parser.parse_args()
# If this is to print the overview of a test result, don't need to run the actual execution
if args.analyze_result:
print(analyze_abtest_result_dir(args.analyze_result))
exit(0)
with open(args.config, "r") as f:
bisect_config = yaml.full_load(f)
# sanity checks
valid_directions = ["increase", "decrease", "both"]
assert("start" in bisect_config), "Illegal bisection config, must specify start commit SHA."
assert("end" in bisect_config), "Illegal bisection config, must specify end commit SHA."
assert("threshold" in bisect_config), "Illegal bisection config, must specify threshold."
assert("direction" in bisect_config), "Illegal bisection config, must specify direction."
assert(bisect_config["direction"] in valid_directions), "We only support increase, decrease, or both directions"
assert("timeout" in bisect_config), "Illegal bisection config, must specify timeout."
targets = None
if "tests" in bisect_config:
targets = bisect_config["tests"]
bisection = TorchBenchBisection(workdir=args.work_dir,
torch_src=args.pytorch_src,
bench_src=args.torchbench_src,
start=bisect_config["start"],
end=bisect_config["end"],
threshold=bisect_config["threshold"],
direction=bisect_config["direction"],
timeout=bisect_config["timeout"],
targets=targets,
output_json=args.output,
build_lazy=args.build_lazy,
debug=args.debug)
assert bisection.prep(), "The working condition of bisection is not satisfied."
print("Preparation steps ok. Commit to bisect: " + " ".join([str(x) for x in bisection.torch_src.commits]))
bisection.run()
if bisection.abtest:
bisection.output_abtest_result()
else:
bisection.output()