Repository navigation
Expand file tree
/
Copy pathlogger.py
More file actions
200 lines (167 loc) · 6.71 KB
/
Copy pathlogger.py
File metadata and controls
200 lines (167 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
Logger class for the Caesar state machine that saves progress and results to JSON files.
"""
import json
import os
from typing import Dict, Any, Optional
from pathlib import Path
from dataclasses import asdict
from pydra import Config
from states import WorkArgs
from utils import ensure_json_serializable
from KernelBenchInternal.src import eval as kernel_eval
class CaesarLogger:
def __init__(
self,
log_dir: str,
config: Config,
work: WorkArgs,
verbose: bool = False,
log_name="log.json"
):
"""
Initialize the logger with a directory to save logs.
Args:
log_dir: Directory path where logs will be saved
"""
self.log_dir = Path(log_dir)
self.log_dir.mkdir(parents=True, exist_ok=True)
self.log_file = self.log_dir / log_name
self.verbose = verbose
self.turn = 1
self.current_log = {}
self.setup_logging_dir(config.to_dict())
metadata = asdict(work)
metadata["run_name"] = config.run_name
metadata["num_rounds"] = config.max_k
self.log_metadata(metadata)
def setup_logging_dir(self, config: dict) -> None:
config_path = self.log_dir / "config.json"
with open(config_path, 'w') as f:
json.dump(ensure_json_serializable(config), f, indent=2)
def log_metadata(self, info: dict) -> None:
"""
Log metadata. May assert that info has
the right keys.
"""
required_keys = ["run_name", "num_rounds"]
for key in required_keys: assert(key in info)
self.current_log["metadata"] = info
def update_turn(self, turn: int) -> None:
self.turn = turn
self.current_log[self.turn] = {}
self._save_log()
def clean_log(self) -> None:
"""
Hacky: Only call when finished!
"""
if self.current_log[self.turn] == {}:
del self.current_log[self.turn]
self._save_log()
def log_on_turn(self, key: str, content):
"""
Hacky function to add to current turn.
"""
if self.turn in self.current_log:
self.current_log[self.turn][key] = content
# self._save_log()
def log_turn(self,
turn: int,
context: Optional[str] = None,
model_response: Optional[str] = None,
kernel_code: Optional[str] = None,
feedback: Optional[str] = None,
eval_result: Optional[str] = None,
profiler_result: Optional[str] = None,
last: bool = False
) -> None:
"""
Log the data for a specific turn.
Args:
turn: Turn number to log
context: Optional context string for this turn
model_response: Optional model response string for this turn
kernel_code: Optional kernel code string for this turn
feedback: Optional feedback string for this turn
"""
if last: turn = "result" # Python checking is fine
if turn not in self.current_log:
self.current_log[turn] = {
"context": "",
"model_response": "",
"kernel_code": "",
"feedback": "",
"eval_result": "",
"profiler_result": "",
}
if context is not None:
self.current_log[turn]["context"] = context
if model_response is not None:
self.current_log[turn]["model_response"] = model_response
if kernel_code is not None:
self.current_log[turn]["kernel_code"] = kernel_code
if feedback is not None:
self.current_log[turn]["feedback"] = feedback
if eval_result is not None:
self.current_log[turn]["eval_result"] = eval_result
if profiler_result is not None:
self.current_log[turn]["profiler_result"] = profiler_result
if self.verbose:
print(f"Saved turn {turn} info to {self.log_file}")
dump_file = os.path.join(self.log_dir, f"context_dump_{turn}.txt")
with open(dump_file, 'w') as f:
f.write(context)
self._save_log()
def _save_log(self) -> None:
"""Save the current log to a JSON file."""
with open(self.log_file, 'w') as f:
json.dump(ensure_json_serializable(self.current_log), f, indent=2)
def _load_log(self) -> None:
"""Load existing log data from the log file if it exists."""
if self.log_file.exists():
with open(self.log_file, 'r') as f:
self.current_log = json.load(f)
# Find highest numeric turn
numeric_turns = [int(k) for k in self.current_log.keys() if str(k).isdigit()]
for turn in numeric_turns:
if "eval_result" in self.current_log[turn]:
self.current_log[turn] = self.exec_log_to_obj(self.current_log[turn])
self.turn = max(numeric_turns) if numeric_turns else 1
def exec_log_to_obj(self, saved_dict: dict | str) -> kernel_eval.KernelExecResult | None:
if isinstance(saved_dict, kernel_eval.KernelExecResult):
return saved_dict
if saved_dict == "":
# return None
# SHOULD NOT HAPPEN
raise ValueError("[Logger] exec_log_to_obj: saved_dict is empty, should not happen")
kernel_eval_result = kernel_eval.KernelExecResult(
compiled=saved_dict.get("compiled", None),
correctness=saved_dict.get("correctness", None),
metadata=saved_dict.get("metadata", {}),
runtime=saved_dict.get("runtime", -1.0),
runtime_stats=saved_dict.get("runtime_stats", {})
)
return kernel_eval_result
def _save_timeout_eval(self, msg: str) -> None:
"""
Special case for handling eval timeouts on GPU.
"""
self._load_log()
self.current_log[str(self.turn)]["eval_result"] = kernel_eval.KernelExecResult(
compiled=False,
correctness=False,
metadata={"timeout_error": msg,
"hardware": "", # TODO: if necessary, add
"device": ""
}
)
self._save_log()
def get_turn_data(self, turn: int) -> Dict[str, str]:
"""
Get the logged data for a specific turn.
Args:
turn: Turn number to retrieve
Returns:
Dictionary containing the turn's logged data
"""
return self.current_log.get(turn)