|
| 1 | +""" |
| 2 | +PyTorch Regression Detector |
| 3 | +Used toghether with TritonParse bisector for automatic bisection. |
| 4 | +
|
| 5 | +Envs to control the behavior: |
| 6 | +
|
| 7 | +- FUNCTIONAL: Detect performance or functional regression. |
| 8 | +- REPRO_CMDLINE: The repro command line to run. |
| 9 | +- BASELINE_LOG: The baseline log file to compare with. |
| 10 | +- REGRESSION_THRESHOLD: The regression threshold, default to 10%. |
| 11 | +
|
| 12 | +Example usage: |
| 13 | +
|
| 14 | +REPRO_CMDLINE="python benchmarks/dynamo/timm_models.py --performance --amp --training --cudagraphs --only inception_v3 --inductor" \ |
| 15 | +BASELINE_LOG="$PWD/bisect_logs/baseline.log" REGRESSION_THRESHOLD="0.1" \ |
| 16 | +tritonparseoss bisect --triton-dir $HOME/local/pytorch --test-script $PWD/.ci/bisect/regression_detector.py \ |
| 17 | +--good 34cdf49 --bad 9d49044 |
| 18 | +
|
| 19 | +""" |
| 20 | + |
| 21 | +import os |
| 22 | +import subprocess |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +# the default regression threshold is 10% |
| 26 | +REGRESSION_THRESHOLD = float(os.environ.get("REGRESSION_THRESHOLD", 10.0)) / 100.0 |
| 27 | +# functional or performance regression |
| 28 | +FUNCTIONAL = bool(int(os.environ["FUNCTIONAL"])) |
| 29 | +# repro command line |
| 30 | +REPRO_CMDLINE = os.environ.get("REPRO_CMDLINE", None) |
| 31 | +# baseline log file |
| 32 | +BASELINE_LOG = os.environ.get("BASELINE_LOG", None) |
| 33 | +# pytorch root dir |
| 34 | +TORCH_SRC_DIR = os.environ["PYTORCH_SRC_DIR"] |
| 35 | + |
| 36 | + |
| 37 | +def get_baseline(baseline_log) -> float: |
| 38 | + with open(baseline_log, "r") as f: |
| 39 | + last_line = f.readlines()[-1].strip() |
| 40 | + if last_line.endswith("x"): |
| 41 | + last_line = last_line[:-1] |
| 42 | + return float(last_line) |
| 43 | + |
| 44 | + |
| 45 | +def get_current_value(stdout_lines) -> float: |
| 46 | + last_line = stdout_lines[-1].strip() |
| 47 | + if last_line.endswith("x"): |
| 48 | + last_line = last_line[:-1] |
| 49 | + return float(last_line) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + assert REPRO_CMDLINE is not None, "REPRO_CMDLINE is not set." |
| 54 | + cmdline = REPRO_CMDLINE.split() |
| 55 | + |
| 56 | + # functional regression |
| 57 | + if FUNCTIONAL: |
| 58 | + try: |
| 59 | + subprocess.check_call(cmdline, cwd=TORCH_SRC_DIR) |
| 60 | + except subprocess.CalledProcessError as e: |
| 61 | + print(f"cmd line {cmdline} failed: {e}") |
| 62 | + exit(e.returncode) |
| 63 | + exit(0) |
| 64 | + |
| 65 | + assert BASELINE_LOG and os.path.exists(BASELINE_LOG), ( |
| 66 | + f"BASELINE_LOG is not set or to a non-exist location: {BASELINE_LOG}." |
| 67 | + ) |
| 68 | + baseline_signal = get_baseline(BASELINE_LOG) |
| 69 | + p = subprocess.Popen(cmdline, cwd=TORCH_SRC_DIR, stdout=subprocess.PIPE, stderr=None) |
| 70 | + assert p.stdout is not None |
| 71 | + stdout_lines = [] |
| 72 | + for line in p.stdout: |
| 73 | + decoded_line = line.decode("utf-8").strip() |
| 74 | + print(decoded_line) |
| 75 | + stdout_lines.append(decoded_line) |
| 76 | + rc = p.wait() |
| 77 | + # if subprocess failed, exit with the return code |
| 78 | + if not rc == 0: |
| 79 | + exit(rc) |
| 80 | + # otherwise, check for the perf regression or accuracy regression |
| 81 | + current_value = get_current_value(stdout_lines) |
| 82 | + if current_value == 0 and "accuracy" in REPRO_CMDLINE: |
| 83 | + print("Accuracy test failed, exit with 1.") |
| 84 | + exit(1) |
| 85 | + smaller_value = min(baseline_signal, current_value) |
| 86 | + larger_value = max(baseline_signal, current_value) |
| 87 | + assert smaller_value > 0, "smaller_value should be positive, got zero." |
| 88 | + ratio = (larger_value - smaller_value) / smaller_value * 100 |
| 89 | + if larger_value > smaller_value * (1 + REGRESSION_THRESHOLD): |
| 90 | + print( |
| 91 | + f"Regression detected: current value {current_value}, {larger_value} / {smaller_value} - 1 == {ratio}% , threshold {REGRESSION_THRESHOLD * 100}%)" |
| 92 | + ) |
| 93 | + exit(1) |
| 94 | + else: |
| 95 | + print( |
| 96 | + f"No regression detected: current value {current_value}, {larger_value} / {smaller_value} - 1 == {ratio}%, threshold {REGRESSION_THRESHOLD * 100}%)" |
| 97 | + ) |
| 98 | + exit(0) |
0 commit comments