-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunner.py
More file actions
362 lines (308 loc) · 13.2 KB
/
Copy pathrunner.py
File metadata and controls
362 lines (308 loc) · 13.2 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"""
TrainLoop evaluation runner logic.
──────────────────────────────
This module contains the core logic for discovering and running evaluation suites.
It's designed to be called by the `trainloop eval` CLI command.
Functions:
run_evaluations: Discovers suites, runs them, writes results, prints summary.
"""
from __future__ import annotations
import importlib
import json
import pkgutil
import sys
from collections import defaultdict, Counter
from dataclasses import asdict
from pathlib import Path
from typing import List, Optional, Dict, Set, Iterator, Tuple, Union, cast, Any
from datetime import datetime
import fsspec
from fsspec.spec import AbstractFileSystem
from .types import Result
# ANSI helpers for console output
OK = "\033[32m✓\033[0m"
BAD = "\033[31m✗\033[0m" # Red X
INFO_COLOR = "\033[94m" # Blue
HEADER_COLOR = "\033[95m" # Magenta
EMPHASIS_COLOR = "\033[93m" # Yellow
RESET_COLOR = "\033[0m" # Reset
EMOJI_ROCKET = "🚀"
EMOJI_FOLDER = "📂"
EMOJI_PLAY = "▶️"
EMOJI_SAVE = "💾"
EMOJI_INFO = "ℹ️"
def _discover_suites(
project_root_dir: Path,
suite_dir: Path,
filter_names: Optional[Set[str]] = None,
) -> Iterator[Tuple[str, List[Result]]]:
"""
Yields (suite_name, results_list) tuples.
A suite is any module under the project's eval/suites directory that defines `results`.
"""
if not suite_dir.exists():
print(f"Warning: Suite directory not found: {suite_dir}")
return
# Ensure project root is on Python path for imports within suites
original_sys_path = list(sys.path)
if str(project_root_dir) not in sys.path:
sys.path.insert(0, str(project_root_dir))
module_prefix = "eval.suites."
try:
for info in pkgutil.walk_packages([str(suite_dir)], module_prefix):
suite_module_name = info.name
# Extract the simple name for filtering (e.g., 'my_suite' from 'eval.suites.my_suite')
simple_name = suite_module_name.split(".")[-1]
if filter_names and simple_name not in filter_names:
continue
try:
print(
f"\n{EMOJI_PLAY} Processing suite: {EMPHASIS_COLOR}{simple_name}{RESET_COLOR}..."
)
module = importlib.import_module(suite_module_name)
if hasattr(module, "results"):
results = getattr(module, "results")
if isinstance(results, list) and all(
isinstance(r, Result) for r in results
):
yield simple_name, results # Use simple_name for reporting
else:
print(
f"Warning: 'results' in {suite_module_name} is not a list of Result objects."
)
else:
print(
f"Warning: No 'results' attribute found in {suite_module_name}."
)
except ImportError as e:
print(f"Error importing suite {suite_module_name}: {e}")
except Exception as e:
print(f"Error processing suite {suite_module_name}: {e}")
finally:
# Restore original Python path
sys.path = original_sys_path
def _write_results(
suite_name: str, results: List[Result], result_dir_for_run: Union[Path, str]
):
"""Writes results for a single suite to a JSONL file in the run-specific directory."""
# Convert Path to string for fsspec
if isinstance(result_dir_for_run, Path):
result_dir_str = str(result_dir_for_run)
out_file_str = str(result_dir_for_run / f"{suite_name}.jsonl")
else:
# Handle string paths (e.g., S3 URLs)
result_dir_str = result_dir_for_run
out_file_str = f"{result_dir_for_run}/{suite_name}.jsonl"
try:
# Ensure the directory exists using fsspec
fs_spec = fsspec.open(out_file_str, "a")
fs = cast(AbstractFileSystem, fs_spec.fs)
if fs:
fs.makedirs(result_dir_str, exist_ok=True)
# Write results using fsspec
with fsspec.open(out_file_str, "a", encoding="utf-8") as f:
for r in results:
f.write(json.dumps(asdict(r), default=str) + "\n") # type: ignore
print(f" {EMOJI_SAVE} {out_file_str}")
except IOError as e:
print(f"Error writing results for suite '{suite_name}' to {out_file_str}: {e}")
def _analyze_results_by_metric(
all_results: Dict[str, List[Result]],
) -> Dict[str, Dict[str, Dict[str, Any]]]:
"""Analyze results grouped by suite and then by metric."""
analysis = {}
for suite_name, results in all_results.items():
analysis[suite_name] = {}
# Group by metric
metrics = defaultdict(list)
for result in results:
metric_name = result.metric
metrics[metric_name].append(result)
# Analyze each metric
for metric_name, metric_results in metrics.items():
total = len(metric_results)
passed = sum(1 for r in metric_results if r.passed)
failed = total - passed
# Collect failure reasons
failure_reasons = defaultdict(int)
for r in metric_results:
if not r.passed:
reason = r.reason if r.reason is not None else "Unknown reason"
failure_reasons[reason] += 1
analysis[suite_name][metric_name] = {
"total": total,
"passed": passed,
"failed": failed,
"pass_rate": passed / total if total > 0 else 0,
"failure_reasons": dict(failure_reasons),
}
return analysis
def _print_metric_breakdown(analysis: Dict[str, Dict[str, Dict[str, Any]]]):
"""Print detailed per-metric breakdown."""
print(f"\n{HEADER_COLOR}--- Per-Metric Breakdown ---{RESET_COLOR}")
for suite_name, metrics in analysis.items():
print(f"\n{EMPHASIS_COLOR}🔍 {suite_name.upper()}{RESET_COLOR}")
print("-" * 60)
total_suite_passed = 0
total_suite_evaluations = 0
for metric_name, metric_data in metrics.items():
total = metric_data["total"]
passed = metric_data["passed"]
failed = metric_data["failed"]
pass_rate = metric_data["pass_rate"]
total_suite_passed += passed
total_suite_evaluations += total
# Status indicator
if passed == total:
status = OK
elif passed == 0:
status = BAD
else:
status = "⚠️" # Warning for mixed results
print(
f" {status} {metric_name:<35} {passed:>2}/{total:<2} ({pass_rate:>6.1%})"
)
# Show failure reasons if any
if failed > 0 and metric_data["failure_reasons"]:
for reason, count in metric_data["failure_reasons"].items():
if count > 1:
print(f" └─ {reason}")
print(f" ({count} instances)")
else:
print(f" └─ {reason}")
# Suite summary
suite_pass_rate = (
total_suite_passed / total_suite_evaluations
if total_suite_evaluations > 0
else 0
)
print(
f" 📊 Suite Summary: {total_suite_passed}/{total_suite_evaluations} ({suite_pass_rate:.1%})"
)
def _print_summary(all_results: Dict[str, List[Result]], no_breakdown: bool = False):
"""Prints a pass/fail summary to the console."""
print(f"\n{HEADER_COLOR}--- Evaluation Summary ---{RESET_COLOR}")
if not all_results:
print("No results to summarize.")
return
for suite_name, results in all_results.items():
total = len(results)
if total == 0:
print(
f"{EMPHASIS_COLOR}{suite_name:<30}{RESET_COLOR} {BAD} 0/0 (No results collected)"
)
continue
passed = sum(r.passed for r in results)
status = OK if passed == total else BAD
print(
f"{EMPHASIS_COLOR}{suite_name:<30}{RESET_COLOR} {status} {passed}/{total}"
)
if passed != total: # print grouped failures
failure_details: Dict[str, List[str]] = defaultdict(list)
for r in results:
if not r.passed:
reason_str = r.reason if r.reason is not None else "Unknown reason"
sample_tag_info = (
getattr(r.sample, "tag", "N/A") if r.sample else "N/A"
)
source_description = (r.metric, sample_tag_info)
failure_details[reason_str].append(source_description)
for reason, sources_list in failure_details.items():
print(
f" - {BAD}{reason}{RESET_COLOR}"
) # Print the unique error reason
if not sources_list:
continue
source_counts = Counter(sources_list)
for source_desc, count in source_counts.items():
metric, tag = source_desc
if count > 1:
print(
f" (for {metric} on {EMPHASIS_COLOR}{tag}{RESET_COLOR} [{count} instances])"
)
else:
print(
f" (for {metric} on {EMPHASIS_COLOR}{tag}{RESET_COLOR})"
)
print("------------------------")
# Add per-metric breakdown (unless disabled)
if not no_breakdown:
analysis = _analyze_results_by_metric(all_results)
_print_metric_breakdown(analysis)
def run_evaluations(
project_root_dir: Path,
suite_filter_names: Optional[List[str]] = None,
no_breakdown: bool = False,
) -> int:
"""
Main entry point for running evaluations.
Args:
project_root_dir: The absolute path to the root of the user's project.
suite_filter_names: An optional list of suite names to run. If None, all suites are run.
Returns:
0 if all suites pass, 1 otherwise.
"""
suite_dir = project_root_dir / "eval" / "suites"
result_dir_base = project_root_dir / "data" / "results"
# Create a timestamped directory for this specific run's results
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
result_dir_for_this_run = result_dir_base / timestamp
try:
# Use fsspec to create directory
result_dir_str = str(result_dir_for_this_run)
fs_spec = fsspec.open(result_dir_str + "/.placeholder", "w")
fs = cast(AbstractFileSystem, fs_spec.fs)
if fs:
fs.makedirs(result_dir_str, exist_ok=True)
except OSError as e:
print(f"Error creating results directory {result_dir_for_this_run}: {e}")
return 1 # Indicate failure
filter_set = set(suite_filter_names) if suite_filter_names else None
collected_results: Dict[str, List[Result]] = {}
any_suites_found = False
print(
f"\n{EMOJI_FOLDER} {HEADER_COLOR}--- Discovering Suites in {suite_dir} ---{RESET_COLOR}"
)
if filter_set:
print(f"Filtering for suites: {', '.join(filter_set)}")
for suite_name, results_list in _discover_suites(
project_root_dir, suite_dir, filter_set
):
any_suites_found = True
collected_results[suite_name] = results_list
if not results_list:
print(
f"No results collected for suite '{suite_name}'. It might be empty or misconfigured."
)
continue
_write_results(suite_name, results_list, result_dir_for_this_run)
if not any_suites_found:
if filter_set:
print(f"No suites found matching your filter: {', '.join(filter_set)}.")
print(f"Please check the names and ensure they exist in {suite_dir}.")
else:
print(f"No suites found in {suite_dir}.")
print(
"To create a suite, add a Python file to this directory defining a 'results' list."
)
# Consider if this should be an error (return 1) or not.
# For now, if no suites are found (e.g. new project), it's not an error itself.
_print_summary(
collected_results, no_breakdown
) # Will print 'No results to summarize'
return 0 # No suites run, so technically no failures.
_print_summary(collected_results, no_breakdown)
# Determine overall exit code: 0 if all metrics in all run suites passed, 1 otherwise.
if (
not collected_results
): # Should be caught by any_suites_found, but as a safeguard
return 0
all_passed_overall = True
for suite_name, results_list in collected_results.items():
if not results_list: # Suite ran but had no results (e.g. empty `results` list)
# This could be considered a partial failure or warning, but for now, doesn't make all_passed_overall false
continue
if not all(r.passed for r in results_list):
all_passed_overall = False
break
return 0 if all_passed_overall else 1