add data prep script
Browse files- prepare_data.py +114 -0
prepare_data.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
One-time data preparation for the 3B-token HelixLM run.
|
| 4 |
+
|
| 5 |
+
Produces and pushes two Hub datasets:
|
| 6 |
+
|
| 7 |
+
1. david-thrower/HelixLM-pretrain-1500Mt-2048-gpt2
|
| 8 |
+
The existing 1.5B-token corpus, tokenised with GPT-2 and cut into
|
| 9 |
+
non-overlapping 2048-token windows in the layout PretrainTrainer expects
|
| 10 |
+
(manifest.json + samples-00000.u16). 2 epochs over it = 3B tokens.
|
| 11 |
+
Mount/download it to skip the ~55-minute compile on the GPU box.
|
| 12 |
+
|
| 13 |
+
2. david-thrower/HelixLM-s512-instruct-smollm-dolly-replay
|
| 14 |
+
SFT mix: smollm-instruct + Dolly + 8% pretrain replay. OpenHermes removed.
|
| 15 |
+
|
| 16 |
+
Run as a CPU job: hf jobs uv run --flavor cpu-basic --timeout 3h prepare_data.py
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
import json
|
| 20 |
+
import os
|
| 21 |
+
import sys
|
| 22 |
+
import tarfile
|
| 23 |
+
import urllib.request
|
| 24 |
+
|
| 25 |
+
REPO_ARCHIVE = "https://github.com/david-thrower/HelixLM/archive/refs/heads/65-staging-for-pr-64-r2.tar.gz"
|
| 26 |
+
WORKDIR = "/tmp/HelixLM-65-staging-for-pr-64-r2"
|
| 27 |
+
|
| 28 |
+
PRETRAIN_DATASET = "david-thrower/HelixLM-medium-1500.0Mt-2988750pt-20260528"
|
| 29 |
+
PRETRAIN_SPLIT = "pretrain_train"
|
| 30 |
+
PRETRAIN_STORE_REPO = "david-thrower/HelixLM-pretrain-1500Mt-2048-gpt2"
|
| 31 |
+
SEQ_LEN = 2048
|
| 32 |
+
|
| 33 |
+
SFT_REPO = "david-thrower/HelixLM-s512-instruct-smollm-dolly-replay"
|
| 34 |
+
INSTRUCT_DATA = [
|
| 35 |
+
("david-thrower/HelixLM-s512-instruct-smollm-stock-corpus-1-7m-token", "train", "formattedconversation"),
|
| 36 |
+
("david-thrower/databricks-dolly-14K-samples-1489K-tokens-512-seq", "train", "formattedconversation"),
|
| 37 |
+
]
|
| 38 |
+
EVAL_DATA = [
|
| 39 |
+
("david-thrower/HelixLM-s512-instruct-smollm-stock-corpus-1-7m-token", "eval", "formattedconversation"),
|
| 40 |
+
("david-thrower/databricks-dolly-14K-samples-1489K-tokens-512-seq", "val", "formattedconversation"),
|
| 41 |
+
]
|
| 42 |
+
REPLAY_RATIO = 0.08
|
| 43 |
+
|
| 44 |
+
if not os.path.isdir(WORKDIR):
|
| 45 |
+
urllib.request.urlretrieve(REPO_ARCHIVE, "/tmp/helix.tar.gz")
|
| 46 |
+
with tarfile.open("/tmp/helix.tar.gz") as tar:
|
| 47 |
+
tar.extractall("/tmp")
|
| 48 |
+
sys.path.insert(0, WORKDIR)
|
| 49 |
+
|
| 50 |
+
from datasets import Dataset, load_dataset
|
| 51 |
+
from huggingface_hub import HfApi
|
| 52 |
+
from helix_lm import HelixTokenizer
|
| 53 |
+
from helix_lm.pretrain_data import PretrainSampleCompiler
|
| 54 |
+
|
| 55 |
+
api = HfApi()
|
| 56 |
+
|
| 57 |
+
# ---------------------------------------------------------------- SFT dataset
|
| 58 |
+
def read(entries):
|
| 59 |
+
rows = []
|
| 60 |
+
for repo, split, column in entries:
|
| 61 |
+
for r in load_dataset(repo, split=split, streaming=True):
|
| 62 |
+
rows.append({"text": r[column], "source": repo.split("/")[-1],
|
| 63 |
+
"tokencount": int(r.get("tokencount") or 0)})
|
| 64 |
+
return rows
|
| 65 |
+
|
| 66 |
+
print("building SFT mix", flush=True)
|
| 67 |
+
train_rows = read(INSTRUCT_DATA)
|
| 68 |
+
eval_rows = read(EVAL_DATA)
|
| 69 |
+
num_replay = int(len(train_rows) * REPLAY_RATIO)
|
| 70 |
+
replay = []
|
| 71 |
+
for r in load_dataset(PRETRAIN_DATASET, split=PRETRAIN_SPLIT, streaming=True):
|
| 72 |
+
if len(replay) >= num_replay:
|
| 73 |
+
break
|
| 74 |
+
replay.append({"text": r["text"], "source": "pretrain_replay",
|
| 75 |
+
"tokencount": int(r.get("tokencount") or 0)})
|
| 76 |
+
train_rows.extend(replay)
|
| 77 |
+
|
| 78 |
+
seen, deduped = set(), []
|
| 79 |
+
for row in train_rows:
|
| 80 |
+
key = hash(row["text"])
|
| 81 |
+
if key not in seen:
|
| 82 |
+
seen.add(key)
|
| 83 |
+
deduped.append(row)
|
| 84 |
+
|
| 85 |
+
print(f" train {len(deduped):,} rows (replay {len(replay):,}) | eval {len(eval_rows):,} rows", flush=True)
|
| 86 |
+
Dataset.from_list(deduped).push_to_hub(SFT_REPO, config_name="default", split="train", private=False)
|
| 87 |
+
Dataset.from_list(eval_rows).push_to_hub(SFT_REPO, config_name="default", split="eval", private=False)
|
| 88 |
+
print(f" pushed https://huggingface.co/datasets/{SFT_REPO}", flush=True)
|
| 89 |
+
|
| 90 |
+
# ----------------------------------------------------------- pretrain windows
|
| 91 |
+
print("compiling 2048-token pretrain windows (single pass, ~1 hour)", flush=True)
|
| 92 |
+
tokenizer = HelixTokenizer("gpt2")
|
| 93 |
+
source = {
|
| 94 |
+
"dataset": PRETRAIN_DATASET,
|
| 95 |
+
"revision": None,
|
| 96 |
+
"split": PRETRAIN_SPLIT,
|
| 97 |
+
"text_column": "text",
|
| 98 |
+
"tokenizer": "gpt2",
|
| 99 |
+
"seq_len": SEQ_LEN,
|
| 100 |
+
}
|
| 101 |
+
store_dir = "/tmp/pretrain_store_2048" # compiler creates this directory itself
|
| 102 |
+
compiler = PretrainSampleCompiler(tokenizer, SEQ_LEN, store_dir, source=source)
|
| 103 |
+
compiler.compile((r["text"] for r in load_dataset(
|
| 104 |
+
PRETRAIN_DATASET, split=PRETRAIN_SPLIT, streaming=True)))
|
| 105 |
+
|
| 106 |
+
manifest = json.load(open(os.path.join(store_dir, "manifest.json")))
|
| 107 |
+
print(f" samples: {manifest.get('sample_count'):,} | "
|
| 108 |
+
f"tokens: {manifest.get('causal_target_count', 0):,}", flush=True)
|
| 109 |
+
|
| 110 |
+
api.create_repo(PRETRAIN_STORE_REPO, repo_type="dataset", exist_ok=True)
|
| 111 |
+
api.upload_folder(folder_path=store_dir, repo_id=PRETRAIN_STORE_REPO,
|
| 112 |
+
repo_type="dataset", commit_message="2048-token GPT-2 windows")
|
| 113 |
+
print(f" pushed https://huggingface.co/datasets/{PRETRAIN_STORE_REPO}", flush=True)
|
| 114 |
+
print("done")
|