|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | +from dataclasses import dataclass, asdict |
| 6 | + |
| 7 | +from utils.aggregate import SimpleMedian |
| 8 | +from utils.validate import Validate |
| 9 | +from utils.result import Result, BenchmarkRun |
| 10 | +from options import options |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class BenchmarkHistoricAverage: |
| 14 | + # Name of benchmark as defined in Benchmark class definition |
| 15 | + name: str |
| 16 | + |
| 17 | + # Measure of central tendency used to compute "average" |
| 18 | + average_type: str |
| 19 | + # TODO replace this with Compare enum? |
| 20 | + # However, compare enum's use in the history is ambiguous, perhaps a new enum |
| 21 | + # should replace both |
| 22 | + |
| 23 | + # Value recorded from the benchmark |
| 24 | + value: float |
| 25 | + # TODO "value" in compute_benchmark assumes median, what about tracking e.g. |
| 26 | + # standard deviation through this process? |
| 27 | + |
| 28 | + # Arguments used to call the benchmark executable. |
| 29 | + # |
| 30 | + # This exists to ensure benchmarks called using different arguments are not |
| 31 | + # compared together. |
| 32 | + command_args: set[str] |
| 33 | + # TODO Ensure ONEAPI_DEVICE_SELECTOR? GPU name itself? |
| 34 | + |
| 35 | +class Compare: |
| 36 | + |
| 37 | + @staticmethod |
| 38 | + def get_hist_avg( |
| 39 | + result_name: str, result_dir: str, cutoff: str, aggregator=SimpleMedian, |
| 40 | + exclude: list[str] = [] |
| 41 | + ) -> dict[str, BenchmarkHistoricAverage]: |
| 42 | + |
| 43 | + def get_timestamp(f: str) -> str: |
| 44 | + """Extract timestamp from result filename""" |
| 45 | + return str(f)[-len("YYYYMMDD_HHMMSS.json") : -len(".json")] |
| 46 | + |
| 47 | + def get_result_paths() -> list[str]: |
| 48 | + """ |
| 49 | + Get a list of all results matching result_name in result_dir that is |
| 50 | + newer than the timestamp specified by cutoff |
| 51 | + """ |
| 52 | + cache_dir = Path(f"{result_dir}") |
| 53 | + |
| 54 | + # List is sorted by filename: given our timestamp format, the |
| 55 | + # timestamps are sorted from oldest to newest |
| 56 | + return sorted( |
| 57 | + filter( |
| 58 | + lambda f: f.is_file() |
| 59 | + and Validate.timestamp(get_timestamp(f)) |
| 60 | + and get_timestamp(f) > cutoff |
| 61 | + # Result file is not excluded |
| 62 | + and f.stem not in exclude, |
| 63 | + # Assumes format is <name>_YYYYMMDD_HHMMSS.json |
| 64 | + cache_dir.glob(f"{result_name}_*_*.json") |
| 65 | + ) |
| 66 | + ) |
| 67 | + |
| 68 | + # key: name of the benchmark test result |
| 69 | + # value: { command_args: set[str], aggregate: Aggregator } |
| 70 | + # |
| 71 | + # This is then used to build a dict[BenchmarkHistoricAverage] used |
| 72 | + # to find historic averages. |
| 73 | + average_aggregate: dict[str, dict] = dict() |
| 74 | + |
| 75 | + for result_path in get_result_paths(): |
| 76 | + with result_path.open('r') as result_f: |
| 77 | + result = BenchmarkRun.from_json(json.load(result_f)) |
| 78 | + |
| 79 | + if result.name != result_name: |
| 80 | + print(f"Warning: Result file {result_path} has mismatching name {result.name}. Skipping file.") |
| 81 | + continue |
| 82 | + |
| 83 | + for test_run in result.results: |
| 84 | + def reset_aggregate() -> dict: |
| 85 | + return { |
| 86 | + "command_args": set(test_run.command[1:]), |
| 87 | + # The assumption here is that "value" is median |
| 88 | + # TODO standardization should happen here on what "value" |
| 89 | + # really is |
| 90 | + "aggregate": aggregator(starting_elements=[test_run.value]) |
| 91 | + } |
| 92 | + |
| 93 | + # Add every benchmark run to average_aggregate: |
| 94 | + if test_run.name not in average_aggregate: |
| 95 | + average_aggregate[test_run.name] = reset_aggregate() |
| 96 | + else: |
| 97 | + # Check that we are comparing runs with the same cmd args: |
| 98 | + if set(test_run.command[1:]) == average_aggregate[test_run.name]["command_args"]: |
| 99 | + average_aggregate[test_run.name]["aggregate"].add(test_run.value) |
| 100 | + else: |
| 101 | + # If the command args used between runs are different, |
| 102 | + # discard old run data and prefer new command args |
| 103 | + # |
| 104 | + # This relies on the fact that paths from get_result_paths() |
| 105 | + # is sorted from older to newer |
| 106 | + print(f"Warning: Command args for {test_run.name} from {result_path} is different from prior runs.") |
| 107 | + print("DISCARDING older data and OVERRIDING with data using new arg.") |
| 108 | + average_aggregate[test_run.name] = reset_aggregate() |
| 109 | + |
| 110 | + return { |
| 111 | + name: BenchmarkHistoricAverage( |
| 112 | + name=name, |
| 113 | + average_type=stats["aggregate"].get_type(), |
| 114 | + value=stats["aggregate"].get_avg(), |
| 115 | + command_args=stats["command_args"] |
| 116 | + ) |
| 117 | + for name, stats in average_aggregate.items() |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + def to_hist_avg( |
| 122 | + hist_avg: dict[str, BenchmarkHistoricAverage], compare_file: str |
| 123 | + ) -> tuple: |
| 124 | + with open(compare_file, 'r') as compare_f: |
| 125 | + compare_result = BenchmarkRun.from_json(json.load(compare_f)) |
| 126 | + |
| 127 | + improvement = [] |
| 128 | + regression = [] |
| 129 | + |
| 130 | + for test in compare_result.results: |
| 131 | + if test.name not in hist_avg: |
| 132 | + continue |
| 133 | + if hist_avg[test.name].command_args != set(test.command[1:]): |
| 134 | + print(f"Warning: skipped {test.name} due to command args mismatch.") |
| 135 | + continue |
| 136 | + |
| 137 | + delta = 1 - ( |
| 138 | + test.value / hist_avg[test.name].value |
| 139 | + if test.lower_is_better else |
| 140 | + hist_avg[test.name].value / test.value |
| 141 | + ) |
| 142 | + |
| 143 | + def perf_diff_entry() -> dict: |
| 144 | + res = asdict(test) |
| 145 | + res["delta"] = delta |
| 146 | + res["hist_avg"] = hist_avg[test.name].value |
| 147 | + res["avg_type"] = hist_avg[test.name].average_type |
| 148 | + return res |
| 149 | + |
| 150 | + if delta > options.regression_threshold: |
| 151 | + improvement.append(perf_diff_entry()) |
| 152 | + elif delta < -options.regression_threshold: |
| 153 | + regression.append(perf_diff_entry()) |
| 154 | + |
| 155 | + return improvement, regression |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + def to_hist( |
| 161 | + avg_type: str, result_name: str, compare_name: str, result_dir: str, cutoff: str, |
| 162 | + |
| 163 | + ) -> tuple: |
| 164 | + """ |
| 165 | + This function generates a historic average from results named result_name |
| 166 | + in result_dir and compares it to the results in compare_file |
| 167 | +
|
| 168 | + Parameters: |
| 169 | + result_name (str): Save name of the result |
| 170 | + compare_name (str): Result file name to compare historic average against |
| 171 | + result_dir (str): Directory to look for results in |
| 172 | + cutoff (str): Timestamp (in YYYYMMDD_HHMMSS) indicating the oldest |
| 173 | + result included in the historic average calculation |
| 174 | + avg_type (str): Type of "average" (measure of central tendency) to |
| 175 | + use in historic "average" calculation |
| 176 | + """ |
| 177 | + |
| 178 | + if avg_type != "median": |
| 179 | + print("Only median is currently supported: refusing to continue.") |
| 180 | + exit(1) |
| 181 | + |
| 182 | + # TODO call validator on cutoff timestamp |
| 183 | + hist_avg = Compare.get_hist_avg(result_name, result_dir, cutoff, exclude=[compare_name]) |
| 184 | + return Compare.to_hist_avg(hist_avg, f"{result_dir}/{compare_name}.json") |
| 185 | + |
| 186 | + |
| 187 | +res = Compare.to_hist("median", "Baseline_PVC_L0", "Baseline_PVC_L0_20250314_170754", "./", "00000000_000000") |
| 188 | +print(res) |
0 commit comments