Skip to content

Commit ab94d4e

Browse files
committed
Add dump
1 parent 111c7d8 commit ab94d4e

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

sharktuner/dispatch_tuner/dispatch_tuner.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,11 @@ def main() -> None:
159159
print(path_config.run_log.resolve())
160160
print("Check the summary in:")
161161
print(summary_log_file.resolve())
162+
163+
output_csv_name = (
164+
f"tuning_{args.dispatch_file.stem.removesuffix('_benchmark')}.csv"
165+
)
166+
csv_path = libtuner.candidate_tuning_records.export_record_to_csv(
167+
dispatch_tuner.tuning_records, path_config.base_dir, output_csv_name
168+
)
169+
print(f"Wrote tuning records CSV: {csv_path}")

sharktuner/sharktuner/candidate_tuning_records.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import os
2+
import csv
13
from typing import Optional
24
from dataclasses import dataclass
5+
from pathlib import Path
36

47
from . import common, candidate_tuning_records
58

@@ -20,9 +23,14 @@ class TuningRecord:
2023
benchmark_speedup: Optional[float] = None
2124
benchmark_rank_order: Optional[int] = None
2225

23-
def init_tuning_records(knobs: list[Optional[common.KnobAssignment]], sorted_order: list[int]) -> list[TuningRecord]:
26+
27+
def init_tuning_records(
28+
knobs: list[Optional[common.KnobAssignment]], sorted_order: list[int]
29+
) -> list[TuningRecord]:
2430
tuning_records: list[TuningRecord] = []
25-
tuning_records.append(TuningRecord(gen_id=0, candidate_id=0, to_compile=True, to_benchmark=True))
31+
tuning_records.append(
32+
TuningRecord(gen_id=0, candidate_id=0, to_compile=True, to_benchmark=True)
33+
)
2634

2735
for can_idx, gen_idx in enumerate(sorted_order, start=1):
2836
tr = TuningRecord(
@@ -32,4 +40,42 @@ def init_tuning_records(knobs: list[Optional[common.KnobAssignment]], sorted_ord
3240
)
3341
tuning_records.append(tr)
3442

35-
return tuning_records
43+
return tuning_records
44+
45+
46+
def export_record_to_csv(
47+
objects: list[TuningRecord], dest_dir: Path, filename: str = "export.csv"
48+
) -> Path:
49+
if not objects:
50+
return None
51+
52+
rows = []
53+
headers = []
54+
55+
for obj in objects:
56+
row = {}
57+
for k, v in vars(obj).items():
58+
if hasattr(v, "__dict__"):
59+
nested = vars(v)
60+
if nested: # only if it has attrs
61+
for nk, nv in nested.items():
62+
key = f"{k}.{nk}"
63+
row[key] = nv
64+
if key not in headers:
65+
headers.append(key)
66+
else:
67+
# skip empty nested object entirely
68+
continue
69+
else:
70+
row[k] = v
71+
if k not in headers:
72+
headers.append(k)
73+
rows.append(row)
74+
75+
path = os.path.join(dest_dir, filename)
76+
with open(path, "w", newline="", encoding="utf-8") as f:
77+
writer = csv.DictWriter(f, fieldnames=headers)
78+
writer.writeheader()
79+
writer.writerows(rows)
80+
81+
return path

0 commit comments

Comments
 (0)