diff --git a/corpora/pretrain-mix-v3.manifest.json b/corpora/pretrain-mix-v3.manifest.json new file mode 100644 index 0000000..4d6e98e --- /dev/null +++ b/corpora/pretrain-mix-v3.manifest.json @@ -0,0 +1,38 @@ +{ + "output": { + "path": "corpora/pretrain-mix-v3.jsonl", + "words": 166522724, + "records": 39124 + }, + "sources": { + "english": { + "records": 27349, + "words": 158852759, + "share_of_words": 0.9539 + }, + "kubernetes-docs-train": { + "records": 3726, + "words": 4264035, + "repeats": 3, + "share_of_words": 0.0256 + }, + "kubernetes-openapi-train": { + "records": 5397, + "words": 725532, + "repeats": 3, + "share_of_words": 0.0044 + }, + "deployment-docs-train": { + "records": 2652, + "words": 2680398, + "repeats": 3, + "share_of_words": 0.0161 + } + }, + "inputs": [ + "corpora/english-train.jsonl", + "corpora/kubernetes-docs-train.jsonl", + "corpora/kubernetes-openapi-train.jsonl", + "corpora/deployment-docs-train.jsonl" + ] +} diff --git a/scripts/build-pretrain-mix.py b/scripts/build-pretrain-mix.py new file mode 100644 index 0000000..7dddaee --- /dev/null +++ b/scripts/build-pretrain-mix.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +"""Concatenate English prose and Kubernetes text into one pretraining file. + +Kubernetes records are repeated --repeats times so they are a small but real share of +the words. The file is not shuffled: kubertrainer's Trainer shuffles each epoch, so +sharding order is not needed here. The manifest records the word share of each source. +Held-out eval documents must already be excluded from the English input. +""" +import argparse +import json +import sys +from pathlib import Path + +def copy_lines(source, target, repeats): + """Copy records repeats times, returning (records, words) written.""" + records = words = 0 + for _ in range(repeats): + with source.open(encoding="utf-8") as stream: + for line in stream: + target.write(line) + records += 1 + words += len(json.loads(line)["text"].split()) + return records, words + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--english", required=True, type=Path) + parser.add_argument("--kubernetes", action="append", required=True, type=Path, + help="Kubernetes text file; repeat for each source") + parser.add_argument("--repeats", type=int, default=3, + help="Times to repeat each Kubernetes record (default 3)") + parser.add_argument("--output-file", required=True, type=Path) + parser.add_argument("--manifest-file", required=True, type=Path) + args = parser.parse_args() + if args.repeats < 1: + parser.error("--repeats must be at least 1") + for path in (args.output_file, args.manifest_file): + if path.exists(): + parser.error(f"refusing to overwrite: {path}") + + sources = {} + with args.output_file.open("x", encoding="utf-8") as target: + records, words = copy_lines(args.english, target, 1) + sources["english"] = {"records": records, "words": words} + for path in args.kubernetes: + records, words = copy_lines(path, target, args.repeats) + sources[path.stem] = {"records": records, "words": words, "repeats": args.repeats} + + total_words = sum(source["words"] for source in sources.values()) + for name, source in sources.items(): + source["share_of_words"] = round(source["words"] / total_words, 4) + manifest = { + "output": {"path": str(args.output_file), "words": total_words, + "records": sum(s["records"] for s in sources.values())}, + "sources": sources, + "inputs": [str(args.english), *map(str, args.kubernetes)], + } + args.manifest_file.write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8") + print(json.dumps(manifest["sources"], indent=2)) + print(f"total words: {total_words}") + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/split-technical-docs.py b/scripts/split-technical-docs.py new file mode 100644 index 0000000..d99713f --- /dev/null +++ b/scripts/split-technical-docs.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +"""Hold out a fixed share of technical documents from training, by path hash. + +A document is held out when the first byte of sha256(source path) is below a threshold, +so the split is reproducible and does not depend on file order. Held-out documents go to +eval/technical_heldout.jsonl and are removed from the training corpus files written here. +""" +import argparse +import hashlib +import json +import sys +from pathlib import Path + + +def held_out(source, share): + digest = hashlib.sha256(source.encode("utf-8")).digest() + return digest[0] < int(share * 256) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--inputs", nargs="+", required=True, type=Path) + parser.add_argument("--share", type=float, default=0.1) + parser.add_argument("--train-dir", type=Path, default=Path("corpora")) + parser.add_argument("--eval-file", type=Path, default=Path("eval/technical_heldout.jsonl")) + args = parser.parse_args() + if args.eval_file.exists(): + parser.error(f"refusing to overwrite: {args.eval_file}") + args.eval_file.parent.mkdir(parents=True, exist_ok=True) + summary = {} + with args.eval_file.open("x", encoding="utf-8") as heldout: + for path in args.inputs: + kept = removed = 0 + out_path = args.train_dir / (path.stem + "-train.jsonl") + if out_path.exists(): + parser.error(f"refusing to overwrite: {out_path}") + with path.open(encoding="utf-8") as source, out_path.open("x", encoding="utf-8") as train: + for line in source: + record = json.loads(line) + if held_out(record["source"], args.share): + heldout.write(json.dumps({**record, "from": path.name}, ensure_ascii=False) + "\n") + removed += 1 + else: + train.write(line) + kept += 1 + summary[path.name] = {"train_records": kept, "heldout_records": removed, + "train_file": str(out_path)} + print(json.dumps(summary, indent=2)) + + +if __name__ == "__main__": + sys.exit(main())