File size: 3,965 Bytes
bbf915c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Build held-out code and text/math eval token files for PSSA-300M.

Each source is streamed past its training quota (the bytes the training corpus
consumed), so every eval token is unseen. Produces:
    data/code_eval.u16      codeparrot-clean + textbook (post-quota)
    data/textmath_eval.u16  open-web-math + fineweb-edu (post-quota)

Usage on the box:
    cd /marimo/pssa_300m && python3 eval_data.py
"""
import multiprocessing as mp
import time
from pathlib import Path

from datasets import load_dataset

from prep import encode_file, DATA

TOKEN = open("/marimo/.hf_token").read().strip()
OUT = Path("/marimo/pssa_300m/eval_src")
OUT.mkdir(parents=True, exist_ok=True)

# (name, repo, kwargs, field, train_quota_bytes, collect_bytes)
JOBS = [
    ("code", "codeparrot/codeparrot-clean", dict(split="train", streaming=True),
     "content", 1_800_000_000, 8_000_000),
    ("edu", "vikp/textbook_quality_programming", dict(split="train", streaming=True),
     "__edu__", 800_000_000, 8_000_000),
    ("math", "open-web-math/open-web-math", dict(split="train", streaming=True),
     "text", 600_000_000, 8_000_000),
    ("web", "HuggingFaceFW/fineweb-edu", dict(name="sample-10BT", split="train", streaming=True),
     "text", 800_000_000, 8_000_000),
]


def log(msg):
    print(f"[{time.strftime('%H:%M:%S')}] {msg}", flush=True)


def _s(v):
    if v is None:
        return ""
    if isinstance(v, str):
        return v
    if isinstance(v, (list, tuple)):
        return "\n".join(_s(x) for x in v if _s(x))
    if isinstance(v, dict):
        return "\n".join(f"{k}: {_s(val)}" for k, val in v.items())
    return str(v)


def collect(name, repo, kwargs, field, quota, collect_bytes):
    path = OUT / f"raw_{name}.txt"
    if path.exists() and path.stat().st_size >= collect_bytes:
        log(f"{name}: already {path.stat().st_size/1e6:.1f} MB")
        return path
    ds = load_dataset(repo, token=TOKEN, **kwargs)
    written, docs, got, t0 = 0, 0, 0, time.time()
    with open(path, "w", encoding="utf-8") as f:
        for row in ds:
            if field == "__edu__":
                text = "\n\n".join(p for p in [_s(row.get("topic")), _s(row.get("outline")),
                                               _s(row.get("markdown"))] if p)
            else:
                text = row.get(field) or ""
            if not text:
                continue
            blob = len(text.encode("utf-8")) + 20
            if written < quota:
                written += blob
                docs += 1
                if written >= quota:
                    log(f"{name}: passed quota {quota/1e9:.2f} GB after {docs} docs, collecting")
                continue
            f.write(text)
            f.write("\n\n<tool_call>\n\n")
            got += blob
            docs += 1
            if got >= collect_bytes:
                break
            if docs % 20000 == 0:
                log(f"{name}: skip {written/1e9:.2f} GB, got {got/1e6:.1f} MB")
    log(f"{name}: collected {got/1e6:.1f} MB after {docs} docs in {time.time()-t0:.0f}s")
    return path


def concat(names, out_name):
    out = OUT / out_name
    with open(out, "wb") as g:
        for n in names:
            with open(OUT / f"{n}.txt", "rb") as f:
                while True:
                    b = f.read(1 << 20)
                    if not b:
                        break
                    g.write(b)
    log(f"{out_name}: {out.stat().st_size/1e6:.1f} MB")
    return out


def main():
    paths = {}
    for job in JOBS:
        paths[job[0]] = collect(*job)
    code = concat(["raw_code", "raw_edu"], "code.txt")
    textmath = concat(["raw_math", "raw_web"], "textmath.txt")
    with mp.Pool(4) as pool:
        nc = encode_file(code, DATA / "code_eval.u16", 4, pool=pool)
        nm = encode_file(textmath, DATA / "textmath_eval.u16", 4, pool=pool)
    log(f"done: code_eval {nc/1e6:.1f}M tokens, textmath_eval {nm/1e6:.1f}M tokens")


if __name__ == "__main__":
    main()