File size: 3,711 Bytes
b48c9f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Turn bench result files into the Hugging Face dataset folder.

    python3 build_dataset.py OUT_DIR results/*.jsonl

Writes, under OUT_DIR:
  data/cases.jsonl   one row per case-run (the table most people want)
  data/calls.jsonl   one row per model request
  data/runs.jsonl    one row per bench invocation (model, settings, versions)
  raw/<label>.jsonl  the bench's own files, unchanged except for scrubbed local paths
  summary/           summarize.py's tables over everything
  code/              the bench, the summarizer, the Kaggle runner and this script

Nested fields (input, expected, output, mismatches, path, ...) are stored as JSON strings in
data/, so every row of a table has the same columns and types. Hugging Face's viewer and
`datasets.load_dataset` both need that; the raw/ files keep the original shapes.
"""

import json
import os
import re
import shutil
import subprocess
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
KAGGLE_RUNNER = os.path.join(HERE, "kaggle_runner.py")

# Anything that looks like a path on the machine that ran the bench. The Kaggle paths are
# kept: /kaggle/working is the same for everyone and says where a run happened.
LOCAL_PATH = re.compile(r"/(?:Users|home)/[^\"\s]+")

NESTED = {
    "case": ("input", "expected", "output", "mismatches", "path", "gates", "escalations",
             "rescued_expected"),
    "call": ("output",),
    "run": ("arms", "packs", "extra_body", "setup", "pack_digests", "stepmold", "bench"),
}


def scrub(text):
    return LOCAL_PATH.sub("<local checkout>", text)


def flatten(record):
    row = dict(record)
    for key in NESTED.get(record.get("kind"), ()):
        if key in row and row[key] is not None and not isinstance(row[key], str):
            row[key] = json.dumps(row[key], sort_keys=True)
    return row


def main(out, files):
    for sub in ("data", "raw", "summary", "code"):
        os.makedirs(os.path.join(out, sub), exist_ok=True)
    tables = {"case": [], "call": [], "run": []}
    for path in files:
        label = os.path.splitext(os.path.basename(path))[0]
        with open(path) as handle, open(os.path.join(out, "raw", label + ".jsonl"), "w") as raw:
            for line in handle:
                line = scrub(line)
                raw.write(line)
                record = json.loads(line)
                if record.get("kind") in tables:
                    tables[record["kind"]].append(flatten(record))
    names = {"case": "cases", "call": "calls", "run": "runs"}
    for kind, rows in tables.items():
        with open(os.path.join(out, "data", names[kind] + ".jsonl"), "w") as handle:
            for row in rows:
                handle.write(json.dumps(row, sort_keys=True) + "\n")
        print("%-6s %6d rows" % (names[kind], len(rows)))

    subprocess.run([sys.executable, os.path.join(HERE, "summarize.py")] + list(files), check=True,
                   stdout=subprocess.DEVNULL)
    for name in ("summary.md", "summary.csv"):
        shutil.move(os.path.join(os.path.dirname(files[0]), name), os.path.join(out, "summary", name))
    for name in ("bench.py", "summarize.py", "build_dataset.py", "README.md"):
        shutil.copy(os.path.join(HERE, name), os.path.join(out, "code", name))
    if os.path.exists(KAGGLE_RUNNER):
        shutil.copy(KAGGLE_RUNNER, os.path.join(out, "code", "kaggle_runner.py"))

    leaks = [p for p in (os.path.join(dp, f) for dp, _, fs in os.walk(out) for f in fs)
             if LOCAL_PATH.search(open(p, errors="ignore").read()) and "/code/" not in p]
    print("local paths left outside code/:", leaks or "none")


if __name__ == "__main__":
    if len(sys.argv) < 3:
        sys.exit(__doc__)
    main(sys.argv[1], sys.argv[2:])