File size: 12,802 Bytes
fdc6474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Calibration corpus v3 (~15M tokens) for phase-2 full AQLM.

Writes /data/glm52-calib-v3/shard_NNNNN.npy (uint32 token ids, ~1M tokens
each) using the GLM tokenizer at /data/glm52. Deterministic (seed 42).

Mix (by tokens):
  ~40% code           (local vLLM sources + HF CodeFeedback + HF python code)
  ~25% tool-calling / agentic   (reused make_agentic_session, GLM chat template)
  ~15% instruction chat         (HF Alpaca + reused coding chat)
  ~10% medical        (MedQA textbook continuation + medical Q&A)
  ~10% prose          (vLLM docs markdown + HF Dolly general knowledge)

Reuses the generators in tools/collect_expert_stats_v2.py. Downloads three
ungated HF streaming datasets:
  - m-a-p/CodeFeedback-Filtered-Instruction   (code)
  - jtatman/python-code-dataset-500k          (code)
  - tatsu-lab/alpaca                          (instruction)
  - databricks/databricks-dolly-15k           (prose / general knowledge)

HELD-OUT EXCLUSIONS (these feed /data/glm52-heldout.txt; never include them):
  - do not read /data/glm52-heldout.txt
  - skip the last 25 vLLM docs/ markdown files (sorted)
  - skip the last 3 MedQA jsonl files (sorted)
  - skip vLLM python files at indices >= 400 of the seed-42 shuffle (the
    corpus builder's random.Random(42) order); we take only code_files[:400].
"""
import glob
import json
import os
import random
import sys

import numpy as np

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))
import collect_expert_stats_v2 as cs   # noqa: E402  reuse the generators

TOK_DIR = "/data/glm52"
OUT = "/data/glm52-calib-v3"
HELDOUT = "/data/glm52-heldout.txt"
SHARD_TOKENS = 1_000_000
TARGET = 15_000_000
SEED = 42

BUDGET = {                     # target tokens per category
    "code": int(0.40 * TARGET),
    "agentic": int(0.25 * TARGET),
    "instruction": int(0.15 * TARGET),
    "medical": int(0.10 * TARGET),
    "prose": int(0.10 * TARGET),
}

MEDICAL_QA = [
    "Explain the differential diagnosis approach for acute chest pain, "
    "covering cardiac, pulmonary, gastrointestinal, and musculoskeletal "
    "causes, and which investigations discriminate between them.",
    "Describe how to systematically read a chest X-ray, common findings "
    "(consolidation, pneumothorax, effusion, cardiomegaly, nodules), and "
    "typical pitfalls in interpretation.",
    "Explain the pharmacology of beta-blockers: mechanism, receptor "
    "selectivity, indications, contraindications, and interactions.",
    "Walk through the pathophysiology of type 2 diabetes from insulin "
    "resistance to complications, and the mechanism of each major drug "
    "class used to treat it.",
    "Explain how CT and MRI imaging work physically, when each is "
    "preferred clinically, and their contraindications.",
    "Describe the interpretation of a full blood count and common "
    "patterns: microcytic vs macrocytic anaemia, neutrophilia, "
    "lymphopenia, thrombocytopenia, and their differential causes.",
    "Explain the staging and grading of solid tumours, TNM notation, "
    "and how imaging and histopathology contribute to each.",
    "Describe the physiology of the cardiac cycle and how it maps to "
    "ECG waveforms, heart sounds, and common arrhythmia mechanisms.",
    "Explain sepsis: definitions, pathophysiology, early recognition "
    "criteria, and the evidence behind initial management bundles.",
    "Explain acid-base disturbances and how to interpret an arterial "
    "blood gas step by step, with compensated and mixed examples.",
    "Describe the mechanisms and comparative effectiveness of the major "
    "vaccine platforms: live attenuated, inactivated, subunit, mRNA, "
    "and viral vector.",
]
GENERAL_QA = [
    "the history of the transistor", "the economics of shipping",
    "how compilers optimize loops", "how DNS resolution works",
    "what causes inflation", "the water cycle",
    "the development of the internet", "how vaccines are manufactured",
    "the physics of climate", "how GPS positioning works",
]


class ShardWriter:
    def __init__(self, out, shard_tokens):
        self.out = out
        self.shard_tokens = shard_tokens
        os.makedirs(out, exist_ok=True)
        self.buf = []          # list of np.uint32 arrays
        self.buf_n = 0
        self.shard = 0
        self.total = 0
        self.by_cat = {}

    def add(self, ids, cat):
        if len(ids) == 0:
            return
        a = np.asarray(ids, dtype=np.uint32)
        self.buf.append(a)
        self.buf_n += len(a)
        self.total += len(a)
        self.by_cat[cat] = self.by_cat.get(cat, 0) + len(a)
        while self.buf_n >= self.shard_tokens:
            self._flush(self.shard_tokens)

    def _flush(self, n):
        cat = np.concatenate(self.buf)
        head, tail = cat[:n], cat[n:]
        p = os.path.join(self.out, f"shard_{self.shard:05d}.npy")
        np.save(p, head)
        assert head.size > 0, f"empty shard {p}"
        print(f"  wrote {p} ({head.size} tokens)", flush=True)
        self.shard += 1
        self.buf = [tail] if tail.size else []
        self.buf_n = tail.size

    def finalize(self):
        if self.buf_n > 0:
            cat = np.concatenate(self.buf)
            p = os.path.join(self.out, f"shard_{self.shard:05d}.npy")
            np.save(p, cat)
            assert cat.size > 0, f"empty shard {p}"
            print(f"  wrote {p} ({cat.size} tokens)", flush=True)
            self.shard += 1
            self.buf, self.buf_n = [], 0


# ---------------------------------------------------------------- generators
def gen_code_local(art):
    """Raw local code, restricted to the seed-42 shuffle's first 400 files
    (held-out uses vLLM python at indices >= 400)."""
    for p in art["code_files"][:400]:
        t = cs._read(p, 60000)
        if len(t) > 300:
            yield f"# ==== {os.path.relpath(p, ROOT)} ====\n{t}\n"


def gen_code_hf():
    """HF code: CodeFeedback (query+answer) and python-code-dataset."""
    from datasets import load_dataset
    cf = iter(load_dataset("m-a-p/CodeFeedback-Filtered-Instruction",
                           split="train", streaming=True))
    py = iter(load_dataset("jtatman/python-code-dataset-500k",
                           split="train", streaming=True))
    while True:
        got = False
        try:
            r = next(cf)
            yield f"### Task:\n{r['query']}\n\n### Solution:\n{r['answer']}\n"
            got = True
        except StopIteration:
            pass
        try:
            r = next(py)
            yield (f"# {r.get('instruction', '')}\n{r.get('output', '')}\n")
            got = True
        except StopIteration:
            pass
        if not got:
            return


def gen_agentic(rng, art, tok):
    while True:
        yield cs.make_agentic_session(rng, art, tok)


def gen_instruction(tok):
    from datasets import load_dataset
    ds = load_dataset("tatsu-lab/alpaca", split="train", streaming=True)
    for r in ds:
        instr = r["instruction"]
        if r.get("input"):
            instr = f"{instr}\n\n{r['input']}"
        msgs = [{"role": "user", "content": instr},
                {"role": "assistant", "content": r["output"]}]
        yield tok.apply_chat_template(msgs, tokenize=False)


def gen_coding_chat(rng, art, tok):
    while True:
        yield cs.make_coding_chat(rng, art, tok)


def gen_medical(rng, tok):
    # MedQA textbook continuation, excluding the last 3 (sorted) jsonl files
    files = sorted(glob.glob("/tmp/medqa/**/*.jsonl", recursive=True))
    kept = files[:-3] if len(files) > 3 else files
    buf = ""
    for f in kept:
        for line in open(f, errors="ignore"):
            try:
                buf += json.loads(line)["text"] + "\n\n"
            except (json.JSONDecodeError, KeyError):
                continue
            while len(buf) >= cs.MAX_TOKENS * 4:
                yield buf[: cs.MAX_TOKENS * 4]
                buf = buf[cs.MAX_TOKENS * 4:]
    if buf.strip():
        yield buf
    # medical Q&A prompts (chat-templated)
    for q in MEDICAL_QA:
        msgs = [{"role": "user", "content": q + " Be thorough and detailed."}]
        yield tok.apply_chat_template(msgs, tokenize=False,
                                      add_generation_prompt=True)


def gen_prose(tok):
    # vLLM docs markdown, excluding the last 25 (sorted) files
    md = sorted(glob.glob(f"{ROOT}/vllm/docs/**/*.md", recursive=True))
    kept = md[:-25] if len(md) > 25 else md
    for p in kept:
        t = cs._read(p, 60000)
        if len(t) > 200:
            yield t + "\n"
    for topic in GENERAL_QA:
        msgs = [{"role": "user", "content":
                 f"Give me a long, detailed explanation of {topic}, with "
                 "history, fundamentals, examples, and common misconceptions."}]
        yield tok.apply_chat_template(msgs, tokenize=False,
                                      add_generation_prompt=True)
    # HF Dolly general knowledge (prose contexts + responses)
    from datasets import load_dataset
    ds = load_dataset("databricks/databricks-dolly-15k", split="train",
                      streaming=True)
    for r in ds:
        parts = [r.get("instruction", ""), r.get("context", ""),
                 r.get("response", "")]
        yield "\n\n".join(p for p in parts if p) + "\n"


def drive(writer, tok, gen, cat, budget):
    """Pull texts from `gen` until the category token budget is met."""
    start = writer.by_cat.get(cat, 0)
    for text in gen:
        if not text:
            continue
        ids = tok.encode(text[:200000], add_special_tokens=False)
        writer.add(ids, cat)
        if writer.by_cat.get(cat, 0) - start >= budget:
            break
    got = writer.by_cat.get(cat, 0) - start
    print(f"[{cat}] {got} tokens (budget {budget})", flush=True)


def main():
    from transformers import AutoTokenizer
    tok = AutoTokenizer.from_pretrained(TOK_DIR, trust_remote_code=True)

    rng = random.Random(SEED)          # FIRST consumer -> matches seed-42 shuffle
    art = cs.gather_artifacts(rng)     # defines the code_files[:400] cutoff
    assert len(art["code_files"]) >= 400, "need >=400 code files for the cutoff"

    writer = ShardWriter(OUT, SHARD_TOKENS)

    # code: interleave local + HF sources round-robin to hit the budget
    def code_stream():
        loc = gen_code_local(art)
        hf = gen_code_hf()
        while True:
            emitted = False
            for g in (loc, hf, hf):    # weight HF ~2x (local is finite)
                try:
                    yield next(g)
                    emitted = True
                except StopIteration:
                    pass
            if not emitted:
                return

    drive(writer, tok, code_stream(), "code", BUDGET["code"])
    drive(writer, tok, gen_agentic(rng, art, tok), "agentic",
          BUDGET["agentic"])

    def instr_stream():
        al = gen_instruction(tok)
        cc = gen_coding_chat(rng, art, tok)
        while True:
            emitted = False
            for g in (al, al, cc):     # mostly alpaca, some coding chat
                try:
                    yield next(g)
                    emitted = True
                except StopIteration:
                    pass
            if not emitted:
                return

    drive(writer, tok, instr_stream(), "instruction", BUDGET["instruction"])
    drive(writer, tok, gen_medical(rng, tok), "medical", BUDGET["medical"])
    drive(writer, tok, gen_prose(tok), "prose", BUDGET["prose"])

    writer.finalize()

    # ---------------------------------------------------------- verification
    shards = sorted(glob.glob(os.path.join(OUT, "shard_*.npy")))
    assert shards, "no shards written"
    sizes = [np.load(s, mmap_mode="r").shape[0] for s in shards]
    assert all(sz > 0 for sz in sizes), "an empty shard was written"
    total = sum(sizes)
    print("\n==== calib-v3 summary ====")
    print(f"total tokens: {total}")
    print(f"shards: {len(shards)} (sizes {min(sizes)}..{max(sizes)})")
    print("composition (tokens / %):")
    for c, n in writer.by_cat.items():
        print(f"  {c:12s} {n:>10d}  {100*n/total:5.1f}%")

    # tokenizer round-trips a sample
    samp = np.load(shards[0])[1000:1300].tolist()
    txt = tok.decode(samp)
    re_ids = tok.encode(txt, add_special_tokens=False)
    print(f"\nround-trip sample decode ({len(txt)} chars): {txt[:160]!r}")
    print(f"round-trip re-encode matches: {re_ids == samp} "
          f"(len {len(re_ids)} vs {len(samp)})")
    # held-out guard
    assert not any(os.path.samefile(s, HELDOUT) for s in shards
                   if os.path.exists(HELDOUT) and os.path.exists(s))
    print("done.")


if __name__ == "__main__":
    main()