| """ |
| Build a mixed continued-pretraining dataset for a code LM. |
| |
| Sources (streamed from the Hub — no full download): |
| - UKPLab/SLTrans (LLVM IR <-> source pairs; primary IRCoder signal) |
| - allenai/peS2o (open scientific text) |
| - bigcode/the-stack (permissively licensed source code) |
| |
| Mixing target (token-weighted): 70 / 15 / 15. |
| |
| Output: JSONL shards under OUT_DIR. Each line: |
| {"text": "...", "source": "sltrans" | "pes2o" | "the_stack", "meta": {...}} |
| |
| The token budget is approximate — we use a fast whitespace token estimate by default |
| to avoid pulling a heavy tokenizer into the streaming loop. Swap in a real tokenizer |
| (see TOKENIZER section) if you want exact counts against your model's vocab. |
| |
| Usage: |
| pip install "datasets>=2.18" huggingface_hub tqdm |
| huggingface-cli login # SLTrans + the-stack are gated; you must accept their terms |
| python build_pretrain_dataset.py |
| """ |
|
|
| from __future__ import annotations |
|
|
| import json |
| import os |
| import random |
| import sys |
| import time |
| from concurrent.futures import ThreadPoolExecutor |
| from dataclasses import dataclass |
| from pathlib import Path |
| from typing import Callable, Iterator |
|
|
| import socket |
|
|
| from datasets import interleave_datasets, load_dataset |
| from tqdm import tqdm |
|
|
| |
| |
| os.environ.setdefault("HF_HUB_ENABLE_HF_TRANSFER", "1") |
| |
| os.environ.setdefault("HF_HUB_DOWNLOAD_TIMEOUT", "120") |
|
|
| |
| |
| |
| socket.setdefaulttimeout(90) |
|
|
| |
| |
| |
|
|
| @dataclass |
| class SourceSpec: |
| name: str |
| hf_path: str |
| hf_config: str | None |
| split: str |
| target_fraction: float |
| text_fn: Callable[[dict], str] |
| meta_fn: Callable[[dict], dict] |
| |
| |
| subsets: list[str] | None = None |
|
|
|
|
| |
| TOTAL_TOKEN_BUDGET = 1_500_000_000 |
|
|
| |
| MIN_TOKENS_PER_RECORD = 32 |
| MAX_TOKENS_PER_RECORD = 8192 |
|
|
| |
| OUT_DIR = Path("./pretrain_mix") |
| SHARD_RECORDS = 50_000 |
| SEED = 17 |
|
|
| |
| |
| |
| |
| KEEP_PROB = { |
| "sltrans": 1.0, |
| "pes2o": 1.0, |
| "the_stack": 0.5, |
| } |
|
|
| |
| |
| THE_STACK_LANGS = [ |
| "python", "c", "cpp", "rust", "go", "java", "javascript", "typescript", |
| ] |
|
|
| |
| SLTRANS_SUBSETS = [ |
| f"{lang}/{split}" |
| for lang in ["C", "C++", "D", "Fortran", "Go", "Haskell", "Nim", "Objective-C", "Python", "Rust", "Swift"] |
| for split in ["Perf_Optimized", "Size_Optimized"] |
| ] |
|
|
| |
| |
| |
| |
| |
|
|
| def _first_present(row: dict, keys: list[str], default: str = "") -> str: |
| for k in keys: |
| if k in row and row[k]: |
| return row[k] |
| return default |
|
|
|
|
| def sltrans_text(row: dict) -> str: |
| """SLTrans pairs source code with its LLVM IR. Concatenate with a marker so |
| the model learns to associate them — IRCoder-style.""" |
| src = _first_present(row, ["source", "code", "src", "input"]) |
| ir = _first_present(row, ["ir", "llvm_ir", "llvm", "target", "output"]) |
| if not src or not ir: |
| return "" |
| return f"<source>\n{src}\n</source>\n<llvm_ir>\n{ir}\n</llvm_ir>" |
|
|
|
|
| def sltrans_meta(row: dict) -> dict: |
| return { |
| "lang": _first_present(row, ["language", "lang", "source_lang"]), |
| } |
|
|
|
|
| def pes2o_text(row: dict) -> str: |
| return _first_present(row, ["text", "content"]) |
|
|
|
|
| def pes2o_meta(row: dict) -> dict: |
| return { |
| "id": _first_present(row, ["id", "doc_id"]), |
| "source": _first_present(row, ["source", "venue"]), |
| } |
|
|
|
|
| def the_stack_text(row: dict) -> str: |
| return _first_present(row, ["content", "text", "code"]) |
|
|
|
|
| def the_stack_meta(row: dict) -> dict: |
| return { |
| "lang": _first_present(row, ["lang", "language"]), |
| "repo": _first_present(row, ["max_stars_repo_name", "repo_name"]), |
| "license": _first_present(row, ["license"]), |
| } |
|
|
|
|
| |
| |
| |
|
|
| SOURCES: list[SourceSpec] = [ |
| SourceSpec( |
| name="sltrans", |
| hf_path="UKPLab/SLTrans", |
| hf_config=None, |
| split="train", |
| target_fraction=0.70, |
| text_fn=sltrans_text, |
| meta_fn=sltrans_meta, |
| subsets=SLTRANS_SUBSETS, |
| ), |
| SourceSpec( |
| name="pes2o", |
| hf_path="allenai/peS2o", |
| hf_config="v2", |
| split="train", |
| target_fraction=0.15, |
| text_fn=pes2o_text, |
| meta_fn=pes2o_meta, |
| ), |
| SourceSpec( |
| name="the_stack", |
| hf_path="bigcode/the-stack", |
| hf_config=None, |
| |
| split="train", |
| target_fraction=0.15, |
| text_fn=the_stack_text, |
| meta_fn=the_stack_meta, |
| subsets=THE_STACK_LANGS, |
| ), |
| ] |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| def estimate_tokens(text: str) -> int: |
| return int(len(text.split()) * 1.5) |
|
|
|
|
| |
| |
| |
|
|
| def open_stream(spec: SourceSpec, subset: str | None): |
| """Return an IterableDataset for a (source, subset) pair, or None if unavailable.""" |
| kwargs = {"split": spec.split, "streaming": True} |
| if spec.hf_config is not None: |
| kwargs["name"] = spec.hf_config |
|
|
| |
| if spec.hf_path == "bigcode/the-stack" and subset is not None: |
| kwargs["data_dir"] = f"data/{subset}" |
|
|
| |
| if spec.hf_path == "UKPLab/SLTrans" and subset is not None: |
| lang, slt_split = subset.rsplit("/", 1) |
| kwargs["name"] = lang |
| kwargs["split"] = slt_split |
|
|
| _TRANSIENT = ("ssl", "timeout", "handshake", "connection", "timed out") |
| for attempt in range(5): |
| try: |
| return load_dataset(spec.hf_path, **kwargs) |
| except ValueError as e: |
| if "Bad split" in str(e): |
| return None |
| raise |
| except Exception as e: |
| if attempt < 4 and any(k in str(e).lower() for k in _TRANSIENT): |
| time.sleep(2 ** attempt) |
| continue |
| raise |
| return None |
|
|
|
|
| def round_robin(spec: SourceSpec) -> Iterator[dict]: |
| """Yield rows from the source, interleaving across subsets if any. |
| |
| Data-file resolution (the HF Hub HTTP round-trips that show as |
| 'resolving data files') is parallelised across subsets so all |
| metadata fetches happen concurrently instead of one-by-one. |
| """ |
| if not spec.subsets: |
| ds = open_stream(spec, None) |
| if ds is not None: |
| yield from ds |
| return |
|
|
| |
| |
| with ThreadPoolExecutor(max_workers=min(4, len(spec.subsets))) as pool: |
| results = list(pool.map(open_stream, [spec] * len(spec.subsets), spec.subsets)) |
|
|
| datasets = [ds for ds in results if ds is not None] |
| if not datasets: |
| return |
|
|
| yield from interleave_datasets(datasets, stopping_strategy="all_exhausted") |
|
|
|
|
| |
| |
| |
|
|
| class ShardWriter: |
| def __init__(self, out_dir: Path, prefix: str, records_per_shard: int): |
| self.out_dir = out_dir |
| self.prefix = prefix |
| self.records_per_shard = records_per_shard |
| self.out_dir.mkdir(parents=True, exist_ok=True) |
| self._shard_idx = 0 |
| self._records_in_shard = 0 |
| self._fh = None |
| self._open_new_shard() |
|
|
| def _open_new_shard(self) -> None: |
| if self._fh is not None: |
| self._fh.close() |
| path = self.out_dir / f"{self.prefix}-{self._shard_idx:05d}.jsonl" |
| self._fh = path.open("w", encoding="utf-8") |
| self._records_in_shard = 0 |
| self._shard_idx += 1 |
|
|
| def write(self, record: dict) -> None: |
| self._fh.write(json.dumps(record, ensure_ascii=False) + "\n") |
| self._records_in_shard += 1 |
| if self._records_in_shard >= self.records_per_shard: |
| self._open_new_shard() |
|
|
| def close(self) -> None: |
| if self._fh is not None: |
| self._fh.close() |
| self._fh = None |
|
|
|
|
| |
| |
| |
|
|
| def sample_source(spec: SourceSpec, target_tokens: int, writer: ShardWriter, |
| rng: random.Random) -> int: |
| """Stream `spec` until ~target_tokens have been written. Returns tokens written.""" |
| keep_prob = KEEP_PROB.get(spec.name, 1.0) |
| tokens_written = 0 |
| records_written = 0 |
| rows_seen = 0 |
| rows_skipped_filter = 0 |
| rows_skipped_subsample = 0 |
| rows_skipped_empty = 0 |
| started = time.time() |
|
|
| |
| |
| bar = tqdm( |
| total=target_tokens, |
| unit="tok", |
| unit_scale=True, |
| desc=f"{spec.name:>10}", |
| dynamic_ncols=True, |
| smoothing=0.05, |
| ) |
|
|
| def _refresh_postfix() -> None: |
| elapsed = max(time.time() - started, 1e-6) |
| bar.set_postfix({ |
| "records": f"{records_written:,}", |
| "rows": f"{rows_seen:,}", |
| "tok/s": f"{tokens_written/elapsed:,.0f}", |
| "skip": f"{rows_skipped_filter+rows_skipped_subsample+rows_skipped_empty:,}", |
| }, refresh=False) |
|
|
| try: |
| for row in round_robin(spec): |
| rows_seen += 1 |
|
|
| if keep_prob < 1.0 and rng.random() > keep_prob: |
| rows_skipped_subsample += 1 |
| continue |
|
|
| try: |
| text = spec.text_fn(row) |
| except Exception as e: |
| if rows_seen <= 3: |
| bar.write(f"[{spec.name}] text_fn error on row {rows_seen}: {e!r}") |
| rows_skipped_empty += 1 |
| continue |
|
|
| if not text: |
| rows_skipped_empty += 1 |
| continue |
|
|
| n_tok = estimate_tokens(text) |
| if n_tok < MIN_TOKENS_PER_RECORD or n_tok > MAX_TOKENS_PER_RECORD: |
| rows_skipped_filter += 1 |
| continue |
|
|
| record = { |
| "text": text, |
| "source": spec.name, |
| "meta": spec.meta_fn(row), |
| "est_tokens": n_tok, |
| } |
| writer.write(record) |
| tokens_written += n_tok |
| records_written += 1 |
|
|
| |
| bar.update(min(n_tok, target_tokens - bar.n)) |
|
|
| |
| if records_written % 1_000 == 0: |
| _refresh_postfix() |
|
|
| if tokens_written >= target_tokens: |
| break |
|
|
| _refresh_postfix() |
| finally: |
| bar.close() |
|
|
| elapsed = time.time() - started |
| print(f"[{spec.name}] DONE: {records_written:,} records, " |
| f"{tokens_written:,} tokens, {rows_seen:,} rows seen, " |
| f"skipped(filter={rows_skipped_filter:,} subsample={rows_skipped_subsample:,} " |
| f"empty={rows_skipped_empty:,}), {elapsed:.0f}s") |
| return tokens_written |
|
|
|
|
| def main() -> None: |
| rng = random.Random(SEED) |
| OUT_DIR.mkdir(parents=True, exist_ok=True) |
|
|
| |
| total_frac = sum(s.target_fraction for s in SOURCES) |
| if abs(total_frac - 1.0) > 1e-3: |
| print(f"WARN: source fractions sum to {total_frac}, not 1.0", file=sys.stderr) |
|
|
| |
| print("=" * 64) |
| print(f"Building mixed pretrain corpus → {OUT_DIR.resolve()}") |
| print(f"Total token budget: {TOTAL_TOKEN_BUDGET:,}") |
| print(f"Per-record range: {MIN_TOKENS_PER_RECORD}–{MAX_TOKENS_PER_RECORD} tokens") |
| for s in SOURCES: |
| target = int(TOTAL_TOKEN_BUDGET * s.target_fraction) |
| kp = KEEP_PROB.get(s.name, 1.0) |
| subs = f", subsets={s.subsets}" if s.subsets else "" |
| print(f" - {s.name:>10}: {s.target_fraction:>5.0%} → {target:>15,} tok " |
| f"[keep_prob={kp}{subs}]") |
| print("=" * 64) |
|
|
| summary = {} |
| for spec in SOURCES: |
| target = int(TOTAL_TOKEN_BUDGET * spec.target_fraction) |
| writer = ShardWriter(OUT_DIR, prefix=spec.name, |
| records_per_shard=SHARD_RECORDS) |
| try: |
| written = sample_source(spec, target, writer, rng) |
| finally: |
| writer.close() |
| summary[spec.name] = {"target": target, "written": written} |
|
|
| |
| manifest = { |
| "total_token_budget": TOTAL_TOKEN_BUDGET, |
| "seed": SEED, |
| "sources": [ |
| {"name": s.name, "hf_path": s.hf_path, |
| "fraction": s.target_fraction, "subsets": s.subsets} |
| for s in SOURCES |
| ], |
| "tokens_written": summary, |
| } |
| (OUT_DIR / "manifest.json").write_text(json.dumps(manifest, indent=2)) |
|
|
| print("\n=== SUMMARY ===") |
| grand_total = sum(v["written"] for v in summary.values()) |
| for name, v in summary.items(): |
| pct = 100 * v["written"] / grand_total if grand_total else 0 |
| print(f" {name:>10}: {v['written']:>15,} tokens ({pct:5.1f}%)") |
| print(f" {'TOTAL':>10}: {grand_total:>15,} tokens") |
| print(f"\nOutput: {OUT_DIR.resolve()}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |