summaryrefslogtreecommitdiff
path: root/scripts/prepare_wordprob.py
blob: cfa0cd67023ec2ba3a845f0540e87f58bd759d4f (plain)
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
"""Public word-problem datasets -> Boris token shards (2026-10-10).

    python probe/prepare_wordprob.py tinygsm  --tokens 1.5e9
    python probe/prepare_wordprob.py elem

tinygsm: TinyGSM/TinyGSM (MIT) -- question, then its Python solution.
elem:    MU-NLPC Calc-ape210k and Calc-mawps, training splits only (MIT) -- question, then
         "Answer: <equation> = <result>".
Every document goes through the same 13-gram filter as the rest of the corpus (the ban
set covers HellaSwag, ARC, PIQA and every ArithMark-3 item). Writes data/<name>/ with a
provenance file.
"""
import argparse, hashlib, json, sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from datasets import load_dataset
import prepare
from tokenizer import BorisTokenizer


def tinygsm():
    for r in load_dataset("TinyGSM/TinyGSM", split="train", streaming=True):
        yield r["question"].strip() + "\n\n" + r["code"].strip()


def elem():
    # Training splits only: ASDiv-A and SVAMP exist only as test sets and are left out.
    for name in ("MU-NLPC/Calc-ape210k", "MU-NLPC/Calc-mawps"):
        for split in ("train",):
            for r in load_dataset(name, split=split):
                q = r["question"].strip()
                eq = (r.get("equation") or "").replace("x =", "").strip()
                res = str(r.get("result") or "").strip()
                yield f"{q}\nAnswer: {eq} = {res}" if eq and res and eq != res else f"{q}\nAnswer: {res}"


def _raw(repo, fn):
    from huggingface_hub import hf_hub_download
    return json.load(open(hf_hub_download(repo, fn, repo_type="dataset")))


def metamath_gsm():
    # GSM8K-derived rows only (rephrasings, self-verification, FOBAR); the MATH rows are competition level.
    for r in _raw("meta-math/MetaMathQA", "MetaMathQA-395K.json"):
        if r["type"].startswith("GSM"):
            yield r["query"].strip() + "\n" + r["response"].strip()


def gsm8k_aug():
    import re
    # Training file only; the repo also ships gsm8k_test.json, which is never read.
    d = _raw("zen-E/GSM8k-Aug", "gsm8k_aug_train.json")       # columnar: {question: [...], cot: [...], answer: [...]}
    for r in (dict(zip(d, vals)) for vals in zip(*d.values())):
        steps = re.findall(r"<<([^=<>]+)=([^<>]+)>>", r.get("cot", ""))
        work = "\n".join(f"{a} = {b}" for a, b in steps)
        yield r["question"].strip() + ("\n" + work if work else "") + f"\nAnswer: {str(r['answer']).strip()}"


def grade_lessons():
    for r in _raw("ajibawa-2023/Maths-Grade-School", "Maths-Grade-School-0-Final.json"):
        t = (r.get("output") or "").strip()
        if t:
            yield t


def omi2_gsm():
    # Full OpenMathInstruct-2 (CC-BY-4.0), grade-school rows only: gsm8k + augmented_gsm8k (~2.6M problems).
    from huggingface_hub import hf_hub_download
    import pyarrow.parquet as pq
    for k in range(32):
        t = pq.read_table(hf_hub_download("nvidia/OpenMathInstruct-2", f"data/train-{k:05d}-of-00032.parquet", repo_type="dataset"),
                          columns=["problem", "generated_solution", "problem_source"])
        for pr, so, src in zip(*(t.column(c).to_pylist() for c in ("problem", "generated_solution", "problem_source"))):
            if "gsm" in src:
                yield pr.strip() + "\n" + so.strip()


def libremath():
    # LibreMath v1 (K2-Horizon, Apache-2.0): single-part and two-part items generated so far.
    import glob
    for d in ("v1", "v1_pairs", "v1_pairs_key2"):
        for f in sorted(glob.glob(f"/media/joseph/Media/libremath/{d}/items-*.jsonl")):
            for l in open(f):
                r = json.loads(l)
                yield r["problem"].strip() + "\n" + r["solution"].strip()


def libremath_tpl():
    # LibreMath v2: problems filled from K2-Horizon-written templates (both keys), every value computed in code.
    import glob
    for d in ("v2_templates_key1", "v2_templates_key2"):
        for f in sorted(glob.glob(f"/media/joseph/Media/libremath/{d}/items-*.jsonl")):
            for l in open(f):
                r = json.loads(l)
                yield r["problem"].strip() + "\n" + r["solution"].strip()


def _hs_banned_titles():
    import re
    norm = lambda t: re.sub(r"[^a-z0-9 ]", "", t.lower().replace("how to", "")).split()
    banned = set()
    for sp in ("validation", "test"):
        for r in load_dataset("Rowan/hellaswag", split=sp):
            m = re.match(r"\[header\] (.*?) \[", r["ctx_a"] + " [")
            if r["source_id"].startswith("wikihow") and m: banned.add(" ".join(norm(m.group(1))))
    return banned, norm


def cosmo1_how_tf():
    # Cosmopedia v1 "wikihow" (the set used in Preview-1/2), now also dropping any article whose title
    # matches a HellaSwag WikiHow item, as for cosmo2_how.
    import re
    from huggingface_hub import hf_hub_download
    import pyarrow.parquet as pq
    banned, norm = _hs_banned_titles(); dropped = 0
    for k in range(2):
        t = pq.read_table(hf_hub_download("HuggingFaceTB/cosmopedia", f"data/wikihow/train-0000{k}-of-00002.parquet", repo_type="dataset"),
                          columns=["prompt", "text"])
        for pr, tx in zip(t.column("prompt").to_pylist(), t.column("text").to_pylist()):
            m = re.search(r'tutorial on "(.+?)"', pr)
            if m and " ".join(norm(m.group(1))) in banned:
                dropped += 1; continue
            yield tx.strip()
    print(f"  {dropped:,} dropped for a HellaSwag title", flush=True)


def cosmo2_how():
    # Cosmopedia v2 (SmolLM-Corpus, ODC-BY; Mixtral-8x7B generations), "wikihow" format only.
    # HellaSwag draws on WikiHow, so every document goes through the 13-gram eval filter below.
    # Stricter than the 13-gram filter: some articles were prompted with real WikiHow titles, so any article
    # whose title matches the title of a HellaSwag WikiHow item (validation or test) is dropped outright.
    import re
    norm = lambda t: re.sub(r"[^a-z0-9 ]", "", t.lower().replace("how to", "")).split()
    banned = set()
    for sp in ("validation", "test"):
        for r in load_dataset("Rowan/hellaswag", split=sp):
            m = re.match(r"\[header\] (.*?) \[", r["ctx_a"] + " [")
            if r["source_id"].startswith("wikihow") and m: banned.add(" ".join(norm(m.group(1))))
    print(f"  {len(banned):,} HellaSwag WikiHow titles banned", flush=True)
    dropped = 0
    import os
    start = int(os.environ.get("COSMO2_START_SHARD", "0"))   # resume after a network failure: shards are read in order
    files = [f"hf://datasets/HuggingFaceTB/smollm-corpus/cosmopedia-v2/train-{k:05d}-of-00104.parquet" for k in range(start, 104)]
    for r in load_dataset("parquet", data_files=files, split="train", streaming=True):
        if r["format"] != "wikihow": continue
        m = re.search(r'"([^"]+)" in the style of WikiHow', r["prompt"]) or re.match(r"\s*Title:\s*(.*)", r["text"])
        if m and " ".join(norm(m.group(1))) in banned:
            dropped += 1
            if dropped % 1000 == 0: print(f"  {dropped:,} dropped for a HellaSwag title", flush=True)
            continue
        yield r["text"].strip()


GEN = {"tinygsm": (tinygsm, "tinygsm"), "elem": (elem, "elem_wordprob"), "metamath_gsm": (metamath_gsm, "metamath_gsm"),
       "gsm8k_aug": (gsm8k_aug, "gsm8k_aug"), "grade_lessons": (grade_lessons, "grade_lessons"),
       "omi2_gsm": (omi2_gsm, "omi2_gsm"), "libremath": (libremath, "libremath_v1"), "cosmo2_how": (cosmo2_how, "cosmo2_how"), "libremath_tpl": (libremath_tpl, "libremath_tpl"), "cosmo1_how_tf": (cosmo1_how_tf, "cosmo_how_v1_tf")}


def main():
    ap = argparse.ArgumentParser(); ap.add_argument("name", choices=list(GEN))
    ap.add_argument("--tokens", type=float, default=1e12)
    a = ap.parse_args()
    fn, outname = GEN[a.name]
    out = prepare.DATA_DIR / outname
    tok = BorisTokenizer.load(prepare.DATA_DIR / "tokenizer"); ban = prepare.eval_ngrams()
    import os
    w = prepare.ShardWriter(out, tok.eos_id); assert w.n_tokens == 0 or os.environ.get("COSMO2_START_SHARD"), f"{out} not empty"
    kept = dropped = toks = 0; batch = []
    gen = fn()
    for t in gen:
        if prepare.contaminated(t, ban):
            dropped += 1; continue
        batch.append(t)
        if len(batch) == 1000:
            toks += w.write(tok.encode_batch(batch)); kept += len(batch); batch = []
            if kept % 200000 == 0: print(f"  {kept:,} docs {toks/1e6:.0f}M tokens {dropped} dropped", flush=True)
            if toks >= a.tokens: break
    if batch: toks += w.write(tok.encode_batch(batch)); kept += len(batch)
    w.close()
    sha = hashlib.sha256(open(prepare.DATA_DIR / "tokenizer" / "tokenizer.json", "rb").read()).hexdigest()
    json.dump({"source": a.name, "documents": kept, "new_tokens": toks, "dropped_contaminated": dropped,
               "new_tokenizer_sha256": sha}, open(out / "retokenize_0920.json", "w"), indent=2)
    print(f"{out.name}: {kept:,} docs, {toks:,} tokens, {dropped} dropped as contaminated")


if __name__ == "__main__":
    main()