|
| 1 | +""" |
| 2 | +Generate a torchbench test report from a file containing the PR body. |
| 3 | +Currently, only supports running tests on specified model names |
| 4 | +
|
| 5 | +Testing environment: |
| 6 | +- Intel Xeon 8259CL @ 2.50 GHz, 24 Cores with disabled Turbo and HT |
| 7 | +- Nvidia Tesla T4 |
| 8 | +- Nvidia Driver 450.51.06 |
| 9 | +- Python 3.7 |
| 10 | +- CUDA 10.2 |
| 11 | +""" |
| 12 | +# Known issues: |
| 13 | +# 1. Does not reuse the build artifact in other CI workflows |
| 14 | +# 2. CI jobs are serialized because there is only one worker |
| 15 | +import os |
| 16 | +import pathlib |
| 17 | +import argparse |
| 18 | +import subprocess |
| 19 | + |
| 20 | +from typing import List |
| 21 | + |
| 22 | +CUDA_VERSION = "cu102" |
| 23 | +PYTHON_VERSION = "3.7" |
| 24 | +TORCHBENCH_CONFIG_NAME = "config.yaml" |
| 25 | +MAGIC_PREFIX = "RUN_TORCHBENCH:" |
| 26 | +ABTEST_CONFIG_TEMPLATE = """# This config is automatically generated by run_torchbench.py |
| 27 | +start: {control} |
| 28 | +end: {treatment} |
| 29 | +threshold: 100 |
| 30 | +direction: decrease |
| 31 | +timeout: 60 |
| 32 | +tests:""" |
| 33 | + |
| 34 | +def gen_abtest_config(control: str, treatment: str, models: List[str]): |
| 35 | + d = {} |
| 36 | + d["control"] = control |
| 37 | + d["treatment"] = treatment |
| 38 | + config = ABTEST_CONFIG_TEMPLATE.format(**d) |
| 39 | + for model in models: |
| 40 | + config = f"{config}\n - {model}" |
| 41 | + config = config + "\n" |
| 42 | + return config |
| 43 | + |
| 44 | +def deploy_torchbench_config(output_dir: str, config: str): |
| 45 | + # Create test dir if needed |
| 46 | + pathlib.Path(output_dir).mkdir(exist_ok=True) |
| 47 | + # TorchBench config file name |
| 48 | + config_path = os.path.join(output_dir, TORCHBENCH_CONFIG_NAME) |
| 49 | + with open(config_path, "w") as fp: |
| 50 | + fp.write(config) |
| 51 | + |
| 52 | +def extract_models_from_pr(torchbench_path: str, prbody_file: str) -> List[str]: |
| 53 | + model_list = [] |
| 54 | + with open(prbody_file, "r") as pf: |
| 55 | + lines = map(lambda x: x.strip(), pf.read().splitlines()) |
| 56 | + magic_lines = list(filter(lambda x: x.startswith(MAGIC_PREFIX), lines)) |
| 57 | + if magic_lines: |
| 58 | + # Only the first magic line will be respected. |
| 59 | + model_list = list(map(lambda x: x.strip(), magic_lines[0][len(MAGIC_PREFIX):].split(","))) |
| 60 | + # Sanity check: make sure all the user specified models exist in torchbench repository |
| 61 | + full_model_list = os.listdir(os.path.join(torchbench_path, "torchbenchmark", "models")) |
| 62 | + for m in model_list: |
| 63 | + if m not in full_model_list: |
| 64 | + print(f"The model {m} you specified does not exist in TorchBench suite. Please double check.") |
| 65 | + return [] |
| 66 | + return model_list |
| 67 | + |
| 68 | +def run_torchbench(pytorch_path: str, torchbench_path: str, output_dir: str): |
| 69 | + # Copy system environment so that we will not override |
| 70 | + env = dict(os.environ) |
| 71 | + command = ["python", "bisection.py", "--work-dir", output_dir, |
| 72 | + "--pytorch-src", pytorch_path, "--torchbench-src", torchbench_path, |
| 73 | + "--config", os.path.join(output_dir, "config.yaml"), |
| 74 | + "--output", os.path.join(output_dir, "result.txt")] |
| 75 | + subprocess.check_call(command, cwd=torchbench_path, env=env) |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + parser = argparse.ArgumentParser(description='Run TorchBench tests based on PR') |
| 79 | + parser.add_argument('--pr-num', required=True, type=str, help="The Pull Request number") |
| 80 | + parser.add_argument('--pr-base-sha', required=True, type=str, help="The Pull Request base hash") |
| 81 | + parser.add_argument('--pr-head-sha', required=True, type=str, help="The Pull Request head hash") |
| 82 | + parser.add_argument('--pr-body', required=True, help="The file that contains body of a Pull Request") |
| 83 | + parser.add_argument('--pytorch-path', required=True, type=str, help="Path to pytorch repository") |
| 84 | + parser.add_argument('--torchbench-path', required=True, type=str, help="Path to TorchBench repository") |
| 85 | + args = parser.parse_args() |
| 86 | + |
| 87 | + output_dir: str = os.path.join(os.environ["HOME"], ".torchbench", "bisection", f"pr{args.pr_num}") |
| 88 | + # Identify the specified models and verify the input |
| 89 | + models = extract_models_from_pr(args.torchbench_path, args.pr_body) |
| 90 | + if not models: |
| 91 | + print("Can't parse the model filter from the pr body. Currently we only support allow-list.") |
| 92 | + exit(1) |
| 93 | + print(f"Ready to run TorchBench with benchmark. Result will be saved in the directory: {output_dir}.") |
| 94 | + # Run TorchBench with the generated config |
| 95 | + torchbench_config = gen_abtest_config(args.pr_base_sha, args.pr_head_sha, models) |
| 96 | + deploy_torchbench_config(output_dir, torchbench_config) |
| 97 | + run_torchbench(pytorch_path=args.pytorch_path, torchbench_path=args.torchbench_path, output_dir=output_dir) |
0 commit comments