new SFT and 99/1 pretrain subsets
Browse files- prepare_data.py +148 -99
prepare_data.py
CHANGED
|
@@ -1,114 +1,163 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 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 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
Run as a CPU job: hf jobs uv run --flavor cpu-basic --timeout 3h prepare_data.py
|
| 17 |
"""
|
| 18 |
|
| 19 |
-
import
|
| 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
|
| 53 |
-
from helix_lm.pretrain_data import PretrainSampleCompiler
|
| 54 |
|
|
|
|
|
|
|
| 55 |
api = HfApi()
|
| 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 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
print("done")
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
Build and push the two datasets the 50M PoC trains on.
|
| 4 |
|
| 5 |
+
SFT david-thrower/HelixLM-s2048-instruct-dolly-cosmopedia-v2
|
| 6 |
+
all of Dolly + long Cosmopedia-v2 (prompt -> text) pairs, formatted
|
| 7 |
+
with the same chat template as the existing instruct set.
|
| 8 |
|
| 9 |
+
PRETRAIN david-thrower/HelixLM-fineweb-edu-dedup-99-openwebmath-1-3B
|
| 10 |
+
99% fineweb-edu-dedup + 1% open-web-math by document count, ~3B tokens.
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
Run: HF_TOKEN=hf_... python prepare_data.py sft
|
| 13 |
+
HF_TOKEN=hf_... python prepare_data.py pretrain
|
| 14 |
+
HF_TOKEN=hf_... python prepare_data.py both
|
|
|
|
| 15 |
"""
|
| 16 |
|
| 17 |
+
import io
|
| 18 |
import os
|
| 19 |
+
import random
|
| 20 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
import pyarrow as pa
|
| 23 |
+
import pyarrow.parquet as pq
|
| 24 |
from datasets import Dataset, load_dataset
|
| 25 |
from huggingface_hub import HfApi
|
| 26 |
+
from transformers import AutoTokenizer
|
|
|
|
| 27 |
|
| 28 |
+
STAGE = sys.argv[1] if len(sys.argv) > 1 else "both"
|
| 29 |
+
SEED = 42
|
| 30 |
api = HfApi()
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 32 |
+
|
| 33 |
+
# =============================================================================
|
| 34 |
+
# SFT: all of Dolly + long Cosmopedia-v2 pairs
|
| 35 |
+
# =============================================================================
|
| 36 |
+
|
| 37 |
+
SFT_REPO = "david-thrower/HelixLM-s2048-instruct-dolly-cosmopedia-v2"
|
| 38 |
+
DOLLY_REPO = "david-thrower/databricks-dolly-14K-samples-1489K-tokens-512-seq"
|
| 39 |
+
COSMO_DATASET = "HuggingFaceTB/smollm-corpus"
|
| 40 |
+
COSMO_CONFIG = "cosmopedia-v2"
|
| 41 |
+
N_COSMO = 90_000 # ~126M real tokens with Dolly and replay
|
| 42 |
+
COSMO_PREFILTER = 900 # cheap prefilter on the published token_length
|
| 43 |
+
MIN_FORMATTED_TOKENS = 1024
|
| 44 |
+
MAX_FORMATTED_TOKENS = 2048 # one conversation must fit one 2048 window
|
| 45 |
+
|
| 46 |
+
CHAT = "<|im_start|>user\n{prompt}\n<|im_end|>\n<|im_start|>assistant\n{answer}\n<|im_end|>"
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def format_pair(prompt, answer):
|
| 50 |
+
return CHAT.format(prompt=prompt.strip(), answer=answer.strip())
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def build_sft():
|
| 54 |
+
print("SFT: reading Dolly", flush=True)
|
| 55 |
+
dolly = [
|
| 56 |
+
{"formattedconversation": row["formattedconversation"], "source": "dolly", "tokencount": 0}
|
| 57 |
+
for row in load_dataset(DOLLY_REPO, split="train", streaming=True)
|
| 58 |
+
]
|
| 59 |
+
dolly_eval = [
|
| 60 |
+
{"formattedconversation": row["formattedconversation"], "source": "dolly", "tokencount": 0}
|
| 61 |
+
for row in load_dataset(DOLLY_REPO, split="val", streaming=True)
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
print(f"SFT: Dolly {len(dolly):,} train / {len(dolly_eval):,} eval", flush=True)
|
| 65 |
+
print(f"SFT: streaming Cosmopedia, keeping {N_COSMO:,} long pairs", flush=True)
|
| 66 |
+
taken, eval_rows, seen = [], [], 0
|
| 67 |
+
for row in load_dataset(COSMO_DATASET, COSMO_CONFIG, split="train", streaming=True):
|
| 68 |
+
seen += 1
|
| 69 |
+
if (row["token_length"] or 0) < COSMO_PREFILTER:
|
| 70 |
+
continue
|
| 71 |
+
text = format_pair(row["prompt"], row["text"])
|
| 72 |
+
n = len(tokenizer.encode(text, add_special_tokens=False))
|
| 73 |
+
if not (MIN_FORMATTED_TOKENS <= n <= MAX_FORMATTED_TOKENS):
|
| 74 |
+
continue
|
| 75 |
+
item = {"formattedconversation": text, "source": "cosmopedia-v2", "tokencount": n}
|
| 76 |
+
if len(eval_rows) < 200:
|
| 77 |
+
eval_rows.append(item)
|
| 78 |
+
else:
|
| 79 |
+
taken.append(item)
|
| 80 |
+
if len(taken) >= N_COSMO:
|
| 81 |
+
break
|
| 82 |
+
if seen % 20000 == 0:
|
| 83 |
+
print(f" scanned {seen:,} rows -> kept {len(taken):,}", flush=True)
|
| 84 |
+
|
| 85 |
+
train = dolly + taken
|
| 86 |
+
random.Random(SEED).shuffle(train)
|
| 87 |
+
print(f"SFT: train {len(train):,} rows (dolly {len(dolly):,} + cosmopedia {len(taken):,})", flush=True)
|
| 88 |
+
print(f"SFT: eval {len(dolly_eval) + len(eval_rows):,} rows", flush=True)
|
| 89 |
+
|
| 90 |
+
Dataset.from_list(train).push_to_hub(SFT_REPO, config_name="default", split="train", private=False)
|
| 91 |
+
Dataset.from_list(dolly_eval + eval_rows).push_to_hub(
|
| 92 |
+
SFT_REPO, config_name="default", split="eval", private=False)
|
| 93 |
+
print(f"SFT: pushed https://huggingface.co/datasets/{SFT_REPO}", flush=True)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# =============================================================================
|
| 97 |
+
# Pretrain: 99% fineweb-edu-dedup + 1% open-web-math
|
| 98 |
+
# =============================================================================
|
| 99 |
+
|
| 100 |
+
PRETRAIN_REPO = "david-thrower/HelixLM-fineweb-edu-dedup-99-openwebmath-1-3B"
|
| 101 |
+
TARGET_TOKENS = 3_000_000_000
|
| 102 |
+
FINEWEB_SHARDS = 5 # 2.4 GB each, ~800k docs each
|
| 103 |
+
OPENWEB_SHARDS = 1
|
| 104 |
+
ROWS_PER_FILE = 50_000
|
| 105 |
+
WORK = "/tmp/pretrain_subset"
|
| 106 |
+
|
| 107 |
+
FINEWEB = [f"fineweb-edu-dedup/train-{i:05d}-of-00234.parquet"
|
| 108 |
+
for i in random.Random(SEED).sample(range(234), FINEWEB_SHARDS)]
|
| 109 |
+
OPENWEB = api.list_repo_files("open-web-math/open-web-math", repo_type="dataset")
|
| 110 |
+
OPENWEB = [f for f in OPENWEB if f.startswith("data/") and f.endswith(".parquet")]
|
| 111 |
+
OPENWEB = random.Random(SEED).sample(OPENWEB, OPENWEB_SHARDS)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def build_pretrain():
|
| 115 |
+
os.makedirs(WORK, exist_ok=True)
|
| 116 |
+
fineweb = load_dataset(
|
| 117 |
+
"parquet", data_files=[f"hf://datasets/HuggingFaceTB/smollm-corpus/{f}" for f in FINEWEB],
|
| 118 |
+
split="train", streaming=True)
|
| 119 |
+
openweb = load_dataset(
|
| 120 |
+
"parquet", data_files=[f"hf://datasets/open-web-math/open-web-math/{f}" for f in OPENWEB],
|
| 121 |
+
split="train", streaming=True)
|
| 122 |
+
|
| 123 |
+
print(f"PRETRAIN: fineweb shards {FINEWEB}", flush=True)
|
| 124 |
+
print(f"PRETRAIN: open-web-math shards {OPENWEB}", flush=True)
|
| 125 |
+
|
| 126 |
+
openweb_iter = iter(openweb)
|
| 127 |
+
rows, total_chars, file_idx, written = [], 0, 0, 0
|
| 128 |
+
for i, row in enumerate(fineweb):
|
| 129 |
+
rows.append({"text": row["text"], "source": "fineweb-edu-dedup"})
|
| 130 |
+
total_chars += len(row["text"])
|
| 131 |
+
if i % 99 == 0:
|
| 132 |
+
try:
|
| 133 |
+
math_row = next(openweb_iter)
|
| 134 |
+
rows.append({"text": math_row["text"], "source": "open-web-math"})
|
| 135 |
+
total_chars += len(math_row["text"])
|
| 136 |
+
except StopIteration:
|
| 137 |
+
pass
|
| 138 |
+
if len(rows) >= ROWS_PER_FILE:
|
| 139 |
+
pq.write_table(pa.Table.from_pylist(rows),
|
| 140 |
+
os.path.join(WORK, f"train-{file_idx:05d}.parquet"))
|
| 141 |
+
written += len(rows)
|
| 142 |
+
file_idx += 1
|
| 143 |
+
rows = []
|
| 144 |
+
print(f" {written:,} rows | ~{total_chars/4/1e9:.2f}B tokens", flush=True)
|
| 145 |
+
if total_chars / 4 >= TARGET_TOKENS:
|
| 146 |
+
break
|
| 147 |
+
|
| 148 |
+
if rows:
|
| 149 |
+
pq.write_table(pa.Table.from_pylist(rows), os.path.join(WORK, f"train-{file_idx:05d}.parquet"))
|
| 150 |
+
written += len(rows)
|
| 151 |
+
print(f"PRETRAIN: {written:,} rows, ~{total_chars/4/1e9:.2f}B tokens in {file_idx + 1} files", flush=True)
|
| 152 |
+
|
| 153 |
+
api.create_repo(PRETRAIN_REPO, repo_type="dataset", exist_ok=True)
|
| 154 |
+
api.upload_folder(folder_path=WORK, repo_id=PRETRAIN_REPO, repo_type="dataset",
|
| 155 |
+
commit_message=f"99/1 fineweb-edu-dedup + open-web-math, ~{TARGET_TOKENS/1e9:.0f}B tokens")
|
| 156 |
+
print(f"PRETRAIN: pushed https://huggingface.co/datasets/{PRETRAIN_REPO}", flush=True)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
if STAGE in ("sft", "both"):
|
| 160 |
+
build_sft()
|
| 161 |
+
if STAGE in ("pretrain", "both"):
|
| 162 |
+
build_pretrain()
|
| 163 |
print("done")
|