File size: 14,151 Bytes
2facf1f | 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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | import base64
import dataclasses
import textwrap
from typing import List
from libkernelbot import consts
from libkernelbot.run_eval import CompileResult, EvalResult, FullResult, RunResult, SystemInfo
from libkernelbot.utils import format_time, limit_length
@dataclasses.dataclass
class Text:
"""
Text represents markdown-formatted text to be added to the report.
"""
text: str
@dataclasses.dataclass
class Log:
"""
Log represents a potentially extensive log of some operation, such as
stdout/stderr of the compiler or tester script.
Logs will be automatically wrapped in code blocks, and prefixed with
the given header. If `content` is too long to fit into a single discord
message, it can be broken up automatically (and reasonably) into multiple
smaller messages.
"""
header: str
content: str
@dataclasses.dataclass
class Link:
"""
Link represents a link in the profiling report, to result data
which can be downloaded by clicking it.
"""
title: str
text: str
url: str
@dataclasses.dataclass
class File:
"""
Link represents a file that gets attached to the report.
"""
name: str
message: str
content: bytes
class RunResultReport:
def __init__(self, data=None):
self.data: List[Text | Log | Link | File] = data or []
def add_text(self, section: str):
self.data.append(Text(section))
def add_log(self, header: str, log: str):
self.data.append(Log(header, log))
def add_link(self, title: str, text: str, url: str):
self.data.append(Link(title, text, url))
def add_file(self, name: str, message: str, content: bytes):
self.data.append(File(name, message, content))
def __repr__(self):
return f"RunResultReport(data={self.data})"
def _generate_compile_report(reporter: "RunResultReport", comp: CompileResult):
message = ""
if not comp.nvcc_found:
message += "# Compilation failed\nNVCC could not be found.\n"
message += "This indicates a bug in the runner configuration, _not in your code_.\n"
message += "Please notify the server admins of this problem"
reporter.add_text(message)
return
# ok, we found nvcc
message += "# Compilation failed\n"
message += "Command "
message += f"```bash\n>{limit_length(comp.command, 1000)}```\n"
message += f"exited with code **{comp.exit_code}**."
reporter.add_text(message)
reporter.add_log("Compiler stderr", comp.stderr.strip())
if len(comp.stdout.strip()) > 0:
reporter.add_log("Compiler stdout", comp.stdout.strip())
def _generate_crash_report(reporter: "RunResultReport", run: RunResult):
message = "# Running failed\n"
message += "Command "
message += f"```bash\n{limit_length(run.command, 1000)}```\n"
if run.exit_code == consts.ExitCode.TIMEOUT_EXPIRED:
message += f"**timed out** after {float(run.duration):.2f} seconds."
else:
message += (
f"exited with error code **{run.exit_code}** after {float(run.duration):.2f} seconds."
)
reporter.add_text(message)
if len(run.stderr.strip()) > 0:
reporter.add_log("Program stderr", run.stderr.strip())
if len(run.stdout.strip()) > 0:
reporter.add_log("Program stdout", run.stdout.strip())
def _generate_test_report(reporter: "RunResultReport", run: RunResult):
message = "# Testing failed\n"
message += "Command "
message += f"```bash\n{limit_length(run.command, 1000)}```\n"
message += f"ran successfully in {run.duration:.2f} seconds, but did not pass all tests.\n"
reporter.add_text(message)
# Generate a test
reporter.add_log("Test log", make_test_log(run))
if len(run.stderr.strip()) > 0:
reporter.add_log("Program stderr", run.stderr.strip())
if len(run.stdout.strip()) > 0:
reporter.add_log("Program stdout", run.stdout.strip())
def _short_fail_reason(run: RunResult):
"""
Translate the exit code of `run` into a short error identifier.
"""
if run.exit_code == consts.ExitCode.TIMEOUT_EXPIRED:
return " (timeout)"
elif run.exit_code == consts.ExitCode.CUDA_FAIL:
return " (cuda api error)"
elif run.exit_code != consts.ExitCode.VALIDATE_FAIL:
return f" (internal error {run.exit_code})"
else:
return ""
def make_short_report(runs: dict[str, EvalResult], full=True) -> list[str]: # noqa: C901
"""
Creates a minimalistic report for `runs`,
returned as a list of status strings
"""
any_compile = False
result = []
for r in runs.values():
if r.compilation is not None:
any_compile = True
if not r.compilation.success:
return ["β Compilation failed"]
if any_compile:
result.append("β
Compilation successful")
if "test" in runs:
test_run = runs["test"].run
if not test_run.success:
result.append("β Running tests failed" + _short_fail_reason(test_run))
return result
elif not test_run.passed:
result.append("β Testing failed")
return result
else:
result.append("β
Testing successful")
elif full:
result.append("β Tests missing")
if "benchmark" in runs:
bench_run = runs["benchmark"].run
if not bench_run.success:
result.append("β Running benchmarks failed" + _short_fail_reason(bench_run))
return result
elif not bench_run.passed:
result.append("β Benchmarking failed")
return result
else:
result.append("β
Benchmarking successful")
elif full:
result.append("β Benchmarks missing")
profile_runs = [v for k, v in runs.items() if k.startswith("profile")]
if len(profile_runs) > 0:
for prof_run in profile_runs:
bench_run = prof_run.run
if not bench_run.success:
result.append("β Running profile failed" + _short_fail_reason(bench_run))
return result
elif not bench_run.passed:
result.append("β Profiling failed")
return result
else:
result.append("β
Profiling successful")
if "leaderboard" in runs:
lb_run = runs["leaderboard"].run
if not lb_run.success:
result.append("β Running leaderboard failed" + _short_fail_reason(lb_run))
elif not lb_run.passed:
result.append("β Leaderboard run failed")
else:
result.append("β
Leaderboard run successful")
elif full:
result.append("β Leaderboard missing")
return result
def make_test_log(run: RunResult) -> str:
test_log = []
for i in range(len(run.result)):
status = run.result.get(f"test.{i}.status", None)
spec = run.result.get(f"test.{i}.spec", "<Error>")
if status is None:
break
if status == "pass":
test_log.append(f"β
{spec}")
msg = run.result.get(f"test.{i}.message", None)
if msg:
test_log.append(f"> {msg.replace('\\n', '\n')}")
elif status == "fail":
test_log.append(f"β {spec}")
error = run.result.get(f"test.{i}.error", "No error information available")
if error:
test_log.append(f"> {error.replace('\\n', '\n')}")
if len(test_log) > 0:
return str.join("\n", test_log)
else:
return "β Could not find any test cases"
def make_benchmark_log(run: RunResult) -> str:
num_bench = int(run.result.get("benchmark-count", 0))
def log_one(base_name):
status = run.result.get(f"{base_name}.status")
spec = run.result.get(f"{base_name}.spec")
if status == "fail":
bench_log.append(f"β {spec} failed testing:\n")
bench_log.append(run.result.get(f"{base_name}.error"))
return
mean = run.result.get(f"{base_name}.mean")
err = run.result.get(f"{base_name}.err")
best = run.result.get(f"{base_name}.best")
worst = run.result.get(f"{base_name}.worst")
bench_log.append(f"{spec}")
bench_log.append(f" β± {format_time(mean, err)}")
if best is not None and worst is not None:
bench_log.append(f" β‘ {format_time(best)} π {format_time(worst)}")
bench_log = []
for i in range(num_bench):
log_one(f"benchmark.{i}")
bench_log.append("")
if len(bench_log) > 0:
return "\n".join(bench_log)
else:
return "β Could not find any benchmarks"
def make_profile_log(run: RunResult) -> str:
num_bench = int(run.result.get("benchmark-count", 0))
def log_one(base_name):
report: str = run.result.get(f"{base_name}.report")
report = base64.b64decode(report.encode("utf-8"), b"+*").decode("utf-8")
report = textwrap.indent(report, " ")
bench_log.append(report)
bench_log = []
for i in range(num_bench):
log_one(f"benchmark.{i}")
bench_log.append("")
if len(bench_log) > 0:
return "\n".join(bench_log)
else:
return "β Could not find any profiling data"
def generate_system_info(system: SystemInfo):
return f"""
Running on:
* GPU: `{system.gpu}`
* CPU: `{system.cpu}`
* Device count: `{system.device_count}`
* Requeues: `{system.requeues}`
* Runtime: `{system.runtime}`
* Platform: `{system.platform}`
* Torch: `{system.torch}`
* Hostname: `{system.hostname}`
"""
def _handle_crash_report(report: RunResultReport, run_result: EvalResult):
if run_result.compilation is not None and not run_result.compilation.success:
_generate_compile_report(report, run_result.compilation)
return True
if not run_result.run.success:
_generate_crash_report(report, run_result.run)
return True
return False
def _shortname(spec: str):
return spec.replace(": ", "=").replace("; ", "_")
def generate_report(result: FullResult, extra_text: str = "") -> RunResultReport: # noqa: C901
runs = result.runs
report = RunResultReport()
if extra_text and len(extra_text.strip()) > 0:
report.add_text(extra_text)
report.add_text(generate_system_info(result.system))
if "test" in runs:
test_run = runs["test"]
if _handle_crash_report(report, test_run):
return report
test_run = test_run.run
if not test_run.passed:
_generate_test_report(report, test_run)
return report
else:
num_tests = int(test_run.result.get("test-count", 0))
report.add_log(f"β
Passed {num_tests}/{num_tests} tests", make_test_log(test_run))
if "benchmark" in runs:
bench_run = runs["benchmark"]
if _handle_crash_report(report, bench_run):
return report
report.add_log(
"Benchmarks",
make_benchmark_log(bench_run.run),
)
profile_runs = [v for k, v in runs.items() if k.startswith("profile")]
if len(profile_runs) > 0:
for prof_run in profile_runs:
if _handle_crash_report(report, prof_run):
return report
if prof_run.profile.trace is not None:
report.add_log(
f"Profiling {prof_run.run.result.get('benchmark.0.spec')}",
make_profile_log(prof_run.run),
)
if prof_run.profile.download_url is not None:
report.add_link(
f"{prof_run.profile.profiler} profiling output",
"Download from GitHub",
prof_run.profile.download_url,
)
for prof_run in profile_runs:
if prof_run.profile is not None:
if prof_run.profile.trace is not None:
report.add_file(
f"profile-{_shortname(prof_run.run.result.get('benchmark.0.spec'))}.zip",
f"{prof_run.profile.profiler} report - "
+ prof_run.run.result.get("benchmark.0.spec"),
base64.b64decode(prof_run.profile.trace),
)
if "leaderboard" in runs:
bench_run = runs["leaderboard"]
if _handle_crash_report(report, bench_run):
return report
bench_run = bench_run.run
report.add_log(
"Ranked Benchmark",
make_benchmark_log(bench_run),
)
if len(runs) == 1:
run = next(iter(runs.values()))
if len(run.run.stderr.strip()) > 0:
report.add_log("Program stderr", run.run.stderr.strip())
if len(run.run.stdout.strip()) > 0:
report.add_log("Program stdout", run.run.stdout.strip())
return report
class MultiProgressReporter:
async def show(self, title: str):
raise NotImplementedError()
def add_run(self, title: str) -> "RunProgressReporter":
raise NotImplementedError()
def make_message(self):
raise NotImplementedError()
class RunProgressReporter:
def __init__(self, title: str):
# short report
self.title = title
self.lines = []
async def push(self, content: str | list[str]):
if isinstance(content, str):
self.lines.append(f"> {content}")
else:
for line in content:
self.lines.append(f"> {line}")
await self._update_message()
async def update(self, new_content: str):
self.lines[-1] = f"> {new_content}"
await self._update_message()
async def update_title(self, new_title):
self.title = new_title
await self._update_message()
def get_message(self):
return str.join("\n", [f"**{self.title}**"] + self.lines)
async def display_report(self, title: str, report: RunResultReport):
raise NotImplementedError()
async def _update_message(self):
raise NotImplementedError()
|