#!/usr/bin/env python3 """Build a training mix from openly licensed datasets (no teacher endpoint needed). python scripts/build_open_mix.py --out data/open_mix.jsonl --eval-out data/open_mix_eval.jsonl Sources (all permit training use; see each dataset card): selfoss bigcode/self-oss-instruct-sc2-exec-filter-50k ODC-BY execution-verified Python nem_code nvidia/Nemotron-Post-Training-Dataset-v1 [code] CC-BY-4.0 DeepSeek-R1 reasoning; only the ~6% of traces whose *complete* answer fits in --max-total-tokens are kept tulu_if allenai/tulu-3-sft-personas-instruction-following ODC-BY instruction following with constraints (Nemotron's chat split is NOT used: its user prompts are stripped and some answers carry another model's identity) nem_stem nvidia/Nemotron-Post-Training-Dataset-v1 [stem] CC-BY-4.0 short STEM reasoning Records use the chat-JSONL format of `looped_qwen_moe.training.data`. """ from __future__ import annotations import argparse import itertools import json import random import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) def nemotron_record(e: dict, src: str) -> dict | None: msgs = [m for m in e["messages"] if m["role"] in ("user", "assistant", "system")] if not msgs or msgs[-1]["role"] != "assistant": return None ctx = [m for m in msgs[:-1] if m["role"] != "system" or m["content"].strip()] ans = msgs[-1]["content"] rec = {"messages": [{"role": m["role"], "content": m["content"]} for m in ctx] + [{"role": "assistant", "content": ans}], "source": src, "license": e.get("license", "cc-by-4.0")} if e.get("reasoning") == "off": rec["enable_thinking"] = False rec["messages"][-1]["content"] = ans.split("")[-1].strip() return rec def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--out", required=True) ap.add_argument("--eval-out", required=True) ap.add_argument("--selfoss", type=int, default=600) ap.add_argument("--nem-code", type=int, default=300) ap.add_argument("--tulu-if", type=int, default=300) ap.add_argument("--nem-stem", type=int, default=150) ap.add_argument("--n-eval-each", type=int, default=6) ap.add_argument("--max-total-tokens", type=int, default=2048) ap.add_argument("--allow-prefix", action="store_true", help="(not recommended) keep over-long traces as truncated prefixes; default keeps only " "complete answers that fit in --max-total-tokens") ap.add_argument("--seed", type=int, default=0) args = ap.parse_args() from datasets import load_dataset from transformers import AutoTokenizer from looped_qwen_moe.training.data import encode_record tok = AutoTokenizer.from_pretrained("ramachandrajoshi/Phai-A1-35B-1M-FP8") rng = random.Random(args.seed) train, evals = [], [] def take(name: str, it, n: int, prefix_ok: bool = False): got = 0 for rec in it: if rec is None: continue ex = encode_record(rec, tok, args.max_total_tokens, drop_truncated=True) if ex is None and prefix_ok: ex = encode_record(rec, tok, args.max_total_tokens, drop_truncated=False) if ex is None or len(ex.input_ids) - ex.prompt_len < 512: continue rec["prefix"] = True if ex is None: continue rec["n_tokens"] = len(ex.input_ids) (evals if got < args.n_eval_each else train).append(rec) got += 1 if got >= n + args.n_eval_each: break print(f"[open-mix] {name}: {got} records ({min(got, args.n_eval_each)} eval)", flush=True) ds = load_dataset("bigcode/self-oss-instruct-sc2-exec-filter-50k", split="train", streaming=True).shuffle(seed=args.seed) take("selfoss", ({"messages": [{"role": "user", "content": e["instruction"]}, {"role": "assistant", "content": e["response"]}], "enable_thinking": False, "source": "selfoss", "license": "odc-by"} for e in ds), args.selfoss) ds = load_dataset("allenai/tulu-3-sft-personas-instruction-following", split="train", streaming=True) ds = ds.shuffle(seed=args.seed, buffer_size=5000) def tulu(e): msgs = [m for m in e["messages"] if m["content"].strip()] if len(msgs) < 2 or msgs[-1]["role"] != "assistant" or msgs[0]["role"] != "user": return None return {"messages": msgs, "enable_thinking": False, "source": "tulu3_personas_if", "license": "odc-by"} take("tulu_if", (tulu(e) for e in ds), args.tulu_if) for split, n, pref in (("code", args.nem_code, args.allow_prefix), ("stem", args.nem_stem, False)): ds = load_dataset("nvidia/Nemotron-Post-Training-Dataset-v1", split=split, streaming=True) ds = ds.shuffle(seed=args.seed, buffer_size=2000) take(f"nem_{split}", (nemotron_record(e, f"nemotron_{split}") for e in itertools.islice(ds, 60000)), n, pref) rng.shuffle(train) for path, rows in ((args.out, train), (args.eval_out, evals)): with open(path, "w", encoding="utf-8") as f: for r in rows: f.write(json.dumps(r, ensure_ascii=False) + "\n") print(f"[open-mix] wrote {len(rows)} -> {path}") if __name__ == "__main__": main()