|
| 1 | +import glob |
| 2 | +import json |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +import matplotlib.pyplot as plt |
| 6 | +import re |
| 7 | +import argparse |
| 8 | + |
| 9 | +AT_REGEX = r"variant-AutoTunerBase-([\w-]+)-\w+" |
| 10 | + |
| 11 | + |
| 12 | +def load_dir(dir: str) -> pd.DataFrame: |
| 13 | + # Concatenate progress DFs |
| 14 | + df = pd.concat([pd.read_csv(fname) for fname in glob.glob(f"{dir}/*/progress.csv")]) |
| 15 | + |
| 16 | + # Concatenate params.json & metrics.json file |
| 17 | + params = [] |
| 18 | + for fname in glob.glob(f"{dir}/*/params.json"): |
| 19 | + try: |
| 20 | + with open(fname, "r") as f: |
| 21 | + _dict = json.load(f) |
| 22 | + _dict["trial_id"] = re.search(AT_REGEX, fname).group(1) |
| 23 | + with open(fname.replace("params.json", "metrics.json"), "r") as f: |
| 24 | + metrics = json.load(f) |
| 25 | + ws = metrics["finish"]["timing__setup__ws"] |
| 26 | + metrics["worst_slack"] = ws |
| 27 | + _dict.update(metrics) |
| 28 | + params.append(_dict) |
| 29 | + except Exception as e: |
| 30 | + print(f"Error in {fname}: {e}") |
| 31 | + continue |
| 32 | + tmp_df = pd.DataFrame(params) |
| 33 | + |
| 34 | + # Merge all dataframe |
| 35 | + df = df.merge(tmp_df, on="trial_id") |
| 36 | + return df |
| 37 | + |
| 38 | + |
| 39 | +def preprocess(df: pd.DataFrame) -> pd.DataFrame: |
| 40 | + cols_to_remove = [ |
| 41 | + "done", |
| 42 | + "training_iteration", |
| 43 | + "date", |
| 44 | + "pid", |
| 45 | + "hostname", |
| 46 | + "node_ip", |
| 47 | + "time_since_restore", |
| 48 | + "time_total_s", |
| 49 | + "iterations_since_restore", |
| 50 | + ] |
| 51 | + rename_dict = { |
| 52 | + "time_this_iter_s": "runtime", |
| 53 | + "_SDC_CLK_PERIOD": "clk_period", |
| 54 | + "minimum": "qor", |
| 55 | + } |
| 56 | + df = df.rename(columns=rename_dict) |
| 57 | + df = df.drop(columns=cols_to_remove) |
| 58 | + df = df[df["qor"] != 9e99] |
| 59 | + df["timestamp"] -= df["timestamp"].min() |
| 60 | + return df |
| 61 | + |
| 62 | + |
| 63 | +def plot(df: pd.DataFrame, key: str): |
| 64 | + # Plot box plot and time series plot for key |
| 65 | + fig, ax = plt.subplots(1, figsize=(15, 10)) |
| 66 | + ax.scatter(df["timestamp"], df[key]) |
| 67 | + ax.set_xlabel("Time (s)") |
| 68 | + ax.set_ylabel(key) |
| 69 | + ax.set_title(f"{key} vs Time") |
| 70 | + z = np.polyfit(df["timestamp"], df[key], 1) |
| 71 | + p = np.poly1d(z) |
| 72 | + ax.plot( |
| 73 | + df["timestamp"], p(df["timestamp"]), "r--", label=f"y={z[0]:.2f}x+{z[1]:.2f}" |
| 74 | + ) |
| 75 | + ax.legend() |
| 76 | + fig.savefig(f"images/{key}.png") |
| 77 | + |
| 78 | + plt.figure(figsize=(15, 10)) |
| 79 | + plt.boxplot(df[key]) |
| 80 | + plt.ylabel(key) |
| 81 | + plt.title(f"{key} Boxplot") |
| 82 | + plt.savefig(f"images/{key}-boxplot.png") |
| 83 | + |
| 84 | + |
| 85 | +def main(results_dir: str): |
| 86 | + df = load_dir(results_dir) |
| 87 | + df = preprocess(df) |
| 88 | + keys = ["qor", "runtime", "clk_period", "worst_slack"] |
| 89 | + for key in keys: |
| 90 | + plot(df, key) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + parser = argparse.ArgumentParser(description="Plot AutoTuner results.") |
| 95 | + parser.add_argument( |
| 96 | + "results_dir", |
| 97 | + type=str, |
| 98 | + help="Directory containing the results.", |
| 99 | + default="../../../../../flow/logs/asap7/gcd/test-tune-2024-09-17-12-00-44", |
| 100 | + ) |
| 101 | + args = parser.parse_args() |
| 102 | + main(args.results_dir) |
0 commit comments