Download prepare_data.py from david-thrower/HelixLM-50M-poc-kit: direct link, hf CLI and curl.
- Browser
- Download file 6.83 kB
-
https://huggingface.co/datasets/david-thrower/HelixLM-50M-poc-kit/resolve/main/prepare_data.py
- Command line
-
hf download hf://datasets/david-thrower/HelixLM-50M-poc-kit/prepare_data.py
-
curl -L -o prepare_data.py https://huggingface.co/datasets/david-thrower/HelixLM-50M-poc-kit/resolve/main/prepare_data.py
6.83 kB
| #!/usr/bin/env python3 | |
| """ | |
| Build and push the two datasets the 50M PoC trains on. | |
| SFT david-thrower/HelixLM-s2048-instruct-dolly-cosmopedia-v2 | |
| all of Dolly + long Cosmopedia-v2 (prompt -> text) pairs, formatted | |
| with the same chat template as the existing instruct set. | |
| PRETRAIN david-thrower/HelixLM-fineweb-edu-dedup-99-openwebmath-1-3B | |
| 99% fineweb-edu-dedup + 1% open-web-math by document count, ~3B tokens. | |
| Run: HF_TOKEN=hf_... python prepare_data.py sft | |
| HF_TOKEN=hf_... python prepare_data.py pretrain | |
| HF_TOKEN=hf_... python prepare_data.py both | |
| """ | |
| import io | |
| import os | |
| import random | |
| import sys | |
| import pyarrow as pa | |
| import pyarrow.parquet as pq | |
| from datasets import Dataset, load_dataset | |
| from huggingface_hub import HfApi | |
| from transformers import AutoTokenizer | |
| STAGE = sys.argv[1] if len(sys.argv) > 1 else "both" | |
| SEED = 42 | |
| api = HfApi() | |
| tokenizer = AutoTokenizer.from_pretrained("gpt2") | |
| # ============================================================================= | |
| # SFT: all of Dolly + long Cosmopedia-v2 pairs | |
| # ============================================================================= | |
| SFT_REPO = "david-thrower/HelixLM-s2048-instruct-dolly-cosmopedia-v2" | |
| DOLLY_REPO = "david-thrower/databricks-dolly-14K-samples-1489K-tokens-512-seq" | |
| COSMO_DATASET = "HuggingFaceTB/smollm-corpus" | |
| COSMO_CONFIG = "cosmopedia-v2" | |
| N_COSMO = 90_000 # ~126M real tokens with Dolly and replay | |
| COSMO_PREFILTER = 900 # cheap prefilter on the published token_length | |
| MIN_FORMATTED_TOKENS = 1024 | |
| MAX_FORMATTED_TOKENS = 2048 # one conversation must fit one 2048 window | |
| CHAT = "<|im_start|>user\n{prompt}\n<|im_end|>\n<|im_start|>assistant\n{answer}\n<|im_end|>" | |
| def format_pair(prompt, answer): | |
| return CHAT.format(prompt=prompt.strip(), answer=answer.strip()) | |
| def build_sft(): | |
| print("SFT: reading Dolly", flush=True) | |
| dolly = [ | |
| {"formattedconversation": row["formattedconversation"], "source": "dolly", "tokencount": 0} | |
| for row in load_dataset(DOLLY_REPO, split="train", streaming=True) | |
| ] | |
| dolly_eval = [ | |
| {"formattedconversation": row["formattedconversation"], "source": "dolly", "tokencount": 0} | |
| for row in load_dataset(DOLLY_REPO, split="val", streaming=True) | |
| ] | |
| print(f"SFT: Dolly {len(dolly):,} train / {len(dolly_eval):,} eval", flush=True) | |
| print(f"SFT: streaming Cosmopedia, keeping {N_COSMO:,} long pairs", flush=True) | |
| taken, eval_rows, seen = [], [], 0 | |
| for row in load_dataset(COSMO_DATASET, COSMO_CONFIG, split="train", streaming=True): | |
| seen += 1 | |
| if (row["token_length"] or 0) < COSMO_PREFILTER: | |
| continue | |
| text = format_pair(row["prompt"], row["text"]) | |
| n = len(tokenizer.encode(text, add_special_tokens=False)) | |
| if not (MIN_FORMATTED_TOKENS <= n <= MAX_FORMATTED_TOKENS): | |
| continue | |
| item = {"formattedconversation": text, "source": "cosmopedia-v2", "tokencount": n} | |
| if len(eval_rows) < 200: | |
| eval_rows.append(item) | |
| else: | |
| taken.append(item) | |
| if len(taken) >= N_COSMO: | |
| break | |
| if seen % 20000 == 0: | |
| print(f" scanned {seen:,} rows -> kept {len(taken):,}", flush=True) | |
| train = dolly + taken | |
| random.Random(SEED).shuffle(train) | |
| print(f"SFT: train {len(train):,} rows (dolly {len(dolly):,} + cosmopedia {len(taken):,})", flush=True) | |
| print(f"SFT: eval {len(dolly_eval) + len(eval_rows):,} rows", flush=True) | |
| Dataset.from_list(train).push_to_hub(SFT_REPO, config_name="default", split="train", private=False) | |
| Dataset.from_list(dolly_eval + eval_rows).push_to_hub( | |
| SFT_REPO, config_name="default", split="eval", private=False) | |
| print(f"SFT: pushed https://huggingface.co/datasets/{SFT_REPO}", flush=True) | |
| # ============================================================================= | |
| # Pretrain: 99% fineweb-edu-dedup + 1% open-web-math | |
| # ============================================================================= | |
| PRETRAIN_REPO = "david-thrower/HelixLM-fineweb-edu-dedup-99-openwebmath-1-3B" | |
| TARGET_TOKENS = 3_000_000_000 | |
| FINEWEB_SHARDS = 5 # 2.4 GB each, ~800k docs each | |
| OPENWEB_SHARDS = 1 | |
| ROWS_PER_FILE = 50_000 | |
| WORK = "/tmp/pretrain_subset" | |
| FINEWEB = [f"fineweb-edu-dedup/train-{i:05d}-of-00234.parquet" | |
| for i in random.Random(SEED).sample(range(234), FINEWEB_SHARDS)] | |
| OPENWEB = api.list_repo_files("open-web-math/open-web-math", repo_type="dataset") | |
| OPENWEB = [f for f in OPENWEB if f.startswith("data/") and f.endswith(".parquet")] | |
| OPENWEB = random.Random(SEED).sample(OPENWEB, OPENWEB_SHARDS) | |
| def build_pretrain(): | |
| os.makedirs(WORK, exist_ok=True) | |
| fineweb = load_dataset( | |
| "parquet", data_files=[f"hf://datasets/HuggingFaceTB/smollm-corpus/{f}" for f in FINEWEB], | |
| split="train", streaming=True) | |
| openweb = load_dataset( | |
| "parquet", data_files=[f"hf://datasets/open-web-math/open-web-math/{f}" for f in OPENWEB], | |
| split="train", streaming=True) | |
| print(f"PRETRAIN: fineweb shards {FINEWEB}", flush=True) | |
| print(f"PRETRAIN: open-web-math shards {OPENWEB}", flush=True) | |
| openweb_iter = iter(openweb) | |
| rows, total_chars, file_idx, written = [], 0, 0, 0 | |
| for i, row in enumerate(fineweb): | |
| rows.append({"text": row["text"], "source": "fineweb-edu-dedup"}) | |
| total_chars += len(row["text"]) | |
| if i % 99 == 0: | |
| try: | |
| math_row = next(openweb_iter) | |
| rows.append({"text": math_row["text"], "source": "open-web-math"}) | |
| total_chars += len(math_row["text"]) | |
| except StopIteration: | |
| pass | |
| if len(rows) >= ROWS_PER_FILE: | |
| pq.write_table(pa.Table.from_pylist(rows), | |
| os.path.join(WORK, f"train-{file_idx:05d}.parquet")) | |
| written += len(rows) | |
| file_idx += 1 | |
| rows = [] | |
| print(f" {written:,} rows | ~{total_chars/4/1e9:.2f}B tokens", flush=True) | |
| if total_chars / 4 >= TARGET_TOKENS: | |
| break | |
| if rows: | |
| pq.write_table(pa.Table.from_pylist(rows), os.path.join(WORK, f"train-{file_idx:05d}.parquet")) | |
| written += len(rows) | |
| print(f"PRETRAIN: {written:,} rows, ~{total_chars/4/1e9:.2f}B tokens in {file_idx + 1} files", flush=True) | |
| api.create_repo(PRETRAIN_REPO, repo_type="dataset", exist_ok=True) | |
| api.upload_folder(folder_path=WORK, repo_id=PRETRAIN_REPO, repo_type="dataset", | |
| commit_message=f"99/1 fineweb-edu-dedup + open-web-math, ~{TARGET_TOKENS/1e9:.0f}B tokens") | |
| print(f"PRETRAIN: pushed https://huggingface.co/datasets/{PRETRAIN_REPO}", flush=True) | |
| if STAGE in ("sft", "both"): | |
| build_sft() | |
| if STAGE in ("pretrain", "both"): | |
| build_pretrain() | |
| print("done") | |