Download mix/mix_frontload.py from laion/MV-FrontLoad-Reasoning-Instruct: direct link, hf CLI and curl.
- Browser
- Download file 3.17 kB
-
https://huggingface.co/datasets/laion/MV-FrontLoad-Reasoning-Instruct/resolve/main/mix/mix_frontload.py
- Command line
-
hf download hf://datasets/laion/MV-FrontLoad-Reasoning-Instruct/mix/mix_frontload.py
-
curl -L -o mix_frontload.py https://huggingface.co/datasets/laion/MV-FrontLoad-Reasoning-Instruct/resolve/main/mix/mix_frontload.py
3.17 kB
| #!/usr/bin/env python3 | |
| """Front-load MV-FrontLoad-Reasoning-Instruct into ANY web dataset. | |
| Streams both sources and interleaves them so that, in expectation, MV makes up | |
| `--mv_share` of the documents; inside MV, shards are sampled with the paper's | |
| per-shard token weights converted to document weights (see blend_weights.json). | |
| Example (reproduces the Comma 0.1 + MV-FrontLoad blend: 42.0573% MV / 57.9427% Comma): | |
| python mix_frontload.py --web_dataset common-pile/comma_v0.1_training_dataset \ | |
| --web_split train --out comma_plus_mv.jsonl.gz --max_docs 1000000 | |
| Any other web dataset works the same way, e.g. HuggingFaceFW/fineweb-edu: | |
| python mix_frontload.py --web_dataset HuggingFaceFW/fineweb-edu --web_name sample-10BT \ | |
| --mv_share 0.4205731 --out fw_plus_mv.jsonl.gz --max_docs 1000000 | |
| Output: JSONL.gz with a single "text" field (+ "source": "mv"|"web"). | |
| Requires: pip install datasets huggingface_hub | |
| """ | |
| import argparse, gzip, json, os, random | |
| from datasets import load_dataset | |
| from huggingface_hub import hf_hub_download | |
| MV_REPO = "laion/MV-FrontLoad-Reasoning-Instruct" | |
| def mv_stream(shard): | |
| ds = load_dataset(MV_REPO, data_files=shard["path"], split="train", streaming=True) | |
| for r in ds: | |
| yield r["text"] | |
| def cycle(make_iter): | |
| while True: # restart when exhausted (repeat epochs) | |
| for x in make_iter(): | |
| yield x | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--web_dataset", required=True, help="HF dataset id of the web corpus") | |
| ap.add_argument("--web_name", default=None) | |
| ap.add_argument("--web_split", default="train") | |
| ap.add_argument("--web_text_column", default="text") | |
| ap.add_argument("--mv_share", type=float, default=None, | |
| help="fraction of the final mix coming from MV (default 0.420573 = paper recipe)") | |
| ap.add_argument("--max_docs", type=int, default=100000) | |
| ap.add_argument("--seed", type=int, default=0) | |
| ap.add_argument("--out", required=True) | |
| a = ap.parse_args() | |
| bw = json.load(open(hf_hub_download(MV_REPO, "mix/blend_weights.json", repo_type="dataset"))) | |
| mv_share = a.mv_share if a.mv_share is not None else bw["mv_share"] | |
| shards = bw["shards"] | |
| # token weight / avg tokens-per-doc -> relative document sampling weight per shard | |
| docw = [s["mv_internal_weight"] / (s["tokens"] / s["docs"]) for s in shards] | |
| # NOTE: --mv_share is applied per DOCUMENT; set it from your own tokens/doc ratio if you need exact token shares. | |
| rng = random.Random(a.seed) | |
| mv_iters = [cycle(lambda s=s: mv_stream(s)) for s in shards] | |
| web_iter = cycle(lambda: (r[a.web_text_column] for r in load_dataset( | |
| a.web_dataset, a.web_name, split=a.web_split, streaming=True))) | |
| with gzip.open(a.out, "wt") as f: | |
| for _ in range(a.max_docs): | |
| if rng.random() < mv_share: | |
| i = rng.choices(range(len(shards)), weights=docw)[0] | |
| f.write(json.dumps({"text": next(mv_iters[i]), "source": "mv"}) + "\n") | |
| else: | |
| f.write(json.dumps({"text": next(web_iter), "source": "web"}) + "\n") | |
| if __name__ == "__main__": | |
| main() | |