|
| 1 | +""" |
| 2 | +To compare the op perf diff |
| 3 | +# usage |
| 4 | +python op_perf_comparison.py --xpu_file /path/to/xpu/performance/result/dir/forward.csv --baseline_file /path/to/baselineence/dir/baseline.csv |
| 5 | +
|
| 6 | +""" |
| 7 | + |
| 8 | +import pandas as pd |
| 9 | +import argparse |
| 10 | +import os |
| 11 | +from ast import literal_eval |
| 12 | +from tabulate import tabulate |
| 13 | + |
| 14 | +def preprocess_row(row): |
| 15 | + processed = {} |
| 16 | + for col, val in row.items(): |
| 17 | + if pd.isna(val): |
| 18 | + processed[col] = "NULL" |
| 19 | + else: |
| 20 | + try: |
| 21 | + processed[col] = literal_eval(str(val)) |
| 22 | + except (ValueError, SyntaxError): |
| 23 | + processed[col] = val |
| 24 | + return processed |
| 25 | + |
| 26 | +def display_row(record): |
| 27 | + formatted = {} |
| 28 | + for key, value in record.items(): |
| 29 | + if isinstance(value, (list, tuple, dict)): |
| 30 | + formatted[key] = str(value) |
| 31 | + elif value == "NULL": |
| 32 | + formatted[key] = "NULL" |
| 33 | + else: |
| 34 | + formatted[key] = value |
| 35 | + return formatted |
| 36 | + |
| 37 | +def write_to_github_summary(content): |
| 38 | + github_step_summary = os.getenv('GITHUB_STEP_SUMMARY') |
| 39 | + if github_step_summary: |
| 40 | + with open(github_step_summary, 'a') as f: |
| 41 | + f.write(content + "\n") |
| 42 | + |
| 43 | +def display_comparison(results, threshold): |
| 44 | + if results.empty: |
| 45 | + print(f"\n No outlier exceeding ({threshold:.0%})") |
| 46 | + write_to_github_summary(f"## No outlier exceeding ({threshold:.0%})") |
| 47 | + return |
| 48 | + |
| 49 | + regression = results[results['change'] == '↓'] |
| 50 | + improvement = results[results['change'] == '↑'] |
| 51 | + |
| 52 | + if not regression.empty: |
| 53 | + print("\n🔴 Regression:") |
| 54 | + display_records = [] |
| 55 | + for _, row in regression.iterrows(): |
| 56 | + record = display_row(row) |
| 57 | + display_records.append({ |
| 58 | + **{k: v for k, v in record.items() if k not in ['time_xpu_file', 'time_baseline_file', 'difference', 'change']}, |
| 59 | + 'Current Time(us)': record['time_xpu_file'], |
| 60 | + 'Baseline Time(us)': record['time_baseline_file'], |
| 61 | + 'Difference': record['difference'] |
| 62 | + }) |
| 63 | + |
| 64 | + print(tabulate( |
| 65 | + display_records, |
| 66 | + headers="keys", |
| 67 | + tablefmt='grid', |
| 68 | + showindex=False, |
| 69 | + floatfmt=".2f" |
| 70 | + )) |
| 71 | + |
| 72 | + if not improvement.empty: |
| 73 | + print("\n🟢 Improvement:") |
| 74 | + display_records = [] |
| 75 | + for _, row in improvement.iterrows(): |
| 76 | + record = display_row(row) |
| 77 | + display_records.append({ |
| 78 | + **{k: v for k, v in record.items() if k not in ['time_xpu_file', 'time_baseline_file', 'difference', 'change']}, |
| 79 | + 'Current Time(us)': record['time_xpu_file'], |
| 80 | + 'Baseline Time(us)': record['time_baseline_file'], |
| 81 | + 'Difference': record['difference'] |
| 82 | + }) |
| 83 | + |
| 84 | + print(tabulate( |
| 85 | + display_records, |
| 86 | + headers="keys", |
| 87 | + tablefmt='grid', |
| 88 | + showindex=False, |
| 89 | + floatfmt=".2f" |
| 90 | + )) |
| 91 | + # Print Summary on Github Action Summary |
| 92 | + summary_output = "## Performance Comparison Results\n" |
| 93 | + if not regression.empty: |
| 94 | + summary_output += "\n### 🔴 Regression\n" |
| 95 | + display_records = [] |
| 96 | + for _, row in regression.iterrows(): |
| 97 | + record = display_row(row) |
| 98 | + display_records.append({ |
| 99 | + **{k: v for k, v in record.items() if k not in ['time_xpu_file', 'time_baseline_file', 'difference', 'change']}, |
| 100 | + 'Current Time(us)': record['time_xpu_file'], |
| 101 | + 'Baseline Time(us)': record['time_baseline_file'], |
| 102 | + 'Difference': record['difference'] |
| 103 | + }) |
| 104 | + |
| 105 | + summary_output += tabulate( |
| 106 | + display_records, |
| 107 | + headers="keys", |
| 108 | + tablefmt='github', |
| 109 | + showindex=False, |
| 110 | + floatfmt=".2f" |
| 111 | + ) + "\n" |
| 112 | + |
| 113 | + if not improvement.empty: |
| 114 | + summary_output += "\n### 🟢 Improvement\n" |
| 115 | + display_records = [] |
| 116 | + for _, row in improvement.iterrows(): |
| 117 | + record = display_row(row) |
| 118 | + display_records.append({ |
| 119 | + **{k: v for k, v in record.items() if k not in ['time_xpu_file', 'time_baseline_file', 'difference', 'change']}, |
| 120 | + 'Current Time(us)': record['time_xpu_file'], |
| 121 | + 'Baseline Time(us)': record['time_baseline_file'], |
| 122 | + 'Difference': record['difference'] |
| 123 | + }) |
| 124 | + |
| 125 | + summary_output += tabulate( |
| 126 | + display_records, |
| 127 | + headers="keys", |
| 128 | + tablefmt='github', |
| 129 | + showindex=False, |
| 130 | + floatfmt=".2f" |
| 131 | + ) + "\n" |
| 132 | + |
| 133 | + write_to_github_summary(summary_output) |
| 134 | + |
| 135 | +def compare_op_time_values(xpu_file, baseline_file, threshold=0.05, output_file=None): |
| 136 | + df_xpu = pd.read_csv(xpu_file, sep=';') |
| 137 | + df_baseline = pd.read_csv(baseline_file, sep=';') |
| 138 | + |
| 139 | + records_xpu = [preprocess_row(row) for _, row in df_xpu.iterrows()] |
| 140 | + records_baseline = [preprocess_row(row) for _, row in df_baseline.iterrows()] |
| 141 | + |
| 142 | + dict_xpu = { |
| 143 | + tuple((k, str(v)) for k, v in record.items() if k != 'time(us)'): |
| 144 | + record['time(us)'] |
| 145 | + for record in records_xpu |
| 146 | + } |
| 147 | + dict_baseline = { |
| 148 | + tuple((k, str(v)) for k, v in record.items() if k != 'time(us)'): |
| 149 | + record['time(us)'] |
| 150 | + for record in records_baseline |
| 151 | + } |
| 152 | + common_keys = set(dict_xpu.keys()) & set(dict_baseline.keys()) |
| 153 | + results = [] |
| 154 | + |
| 155 | + for key in common_keys: |
| 156 | + time_xpu = dict_xpu[key] |
| 157 | + time_baseline = dict_baseline[key] |
| 158 | + |
| 159 | + # Skip comparison if time_xpu is 0 |
| 160 | + if time_xpu == 0: |
| 161 | + continue |
| 162 | + |
| 163 | + diff = (time_baseline - time_xpu) / time_xpu |
| 164 | + # Compare Time, Lower is better |
| 165 | + if abs(diff) > threshold: |
| 166 | + record = dict(key) |
| 167 | + print(record) |
| 168 | + record.update({ |
| 169 | + 'time_xpu_file': time_xpu, |
| 170 | + 'time_baseline_file': time_baseline, |
| 171 | + 'difference': f"{diff:.2%}", |
| 172 | + 'change': "↑" if diff > 0 else "↓" |
| 173 | + }) |
| 174 | + results.append(record) |
| 175 | + |
| 176 | + result_df = pd.DataFrame(results) if results else pd.DataFrame() |
| 177 | + display_comparison(result_df, threshold) |
| 178 | + |
| 179 | + |
| 180 | +def main(): |
| 181 | + parser = argparse.ArgumentParser(description='Compare time values between two CSV files') |
| 182 | + parser.add_argument('-x', '--xpu_file', required=True, help='XPU OP performance result csv files dir') |
| 183 | + parser.add_argument('-b', '--baseline_file', required=True, help="XPU OP baseline result csv files dir") |
| 184 | + parser.add_argument('-t', '--threshold', type=float, default=0.10, |
| 185 | + help='Threshold for time difference (default: 0.10 for 10%)') |
| 186 | + args = parser.parse_args() |
| 187 | + |
| 188 | + print(f" Compared file: {args.xpu_file} 和 {args.baseline_file}") |
| 189 | + print(f" Threshold: {args.threshold:.0%}") |
| 190 | + write_to_github_summary("## Performance Comparison Set") |
| 191 | + write_to_github_summary(f"- Threshold: {args.threshold:.0%}") |
| 192 | + |
| 193 | + compare_op_time_values( |
| 194 | + xpu_file=args.xpu_file, |
| 195 | + baseline_file=args.baseline_file, |
| 196 | + threshold=args.threshold, |
| 197 | + ) |
| 198 | + |
| 199 | + |
| 200 | +if __name__ == "__main__": |
| 201 | + main() |
0 commit comments