"""Fine-tune Laya on a task's training set with Laya's own `finetune`. The settings are Laya's defaults with the changes a task's module names, all fixed before any fine-tuned model of that task is scored: - `head_max_len` 512 instead of 192, so that every option keeps its own words. At 192 Laya cuts long option lists short until some of them read the same. - `target_error` 0.01 instead of 0.10. Laya fits a confidence threshold on training rows it held back, so that the answers above it are about 99% right. The rest are meant for a person. - `max_len`, `epochs`, `loss` and `dev_rows` only where the task's module says why. Laya holds back 400 training rows (its `calib_max`) to fit its temperature and that threshold, and trains on the rest. A task with `dev_rows` also keeps that many training rows out of the run, and every other row with the same text, so they are new to the model. They go to `dev.jsonl` in the run folder, and `layatasks.evaluate --dev` scores them. That judges a run without reading the test. Run it with `python -m layatasks.train --seed 0`. """ from __future__ import annotations import argparse import dataclasses import json import os import random import time from pathlib import Path from layatasks import task as tasks from layatasks.common import data_dir, normalise, read_jsonl, run_dir, sha256_file, write_json, write_jsonl BASE_REPO = "convaiinnovations/laya" BASE_REVISION = "85cc74ddae6109077ebe2853a9b3077fb6606a78" BASE_WEIGHTS_SHA256 = "891102d372688fc2a094dac56a384bc537b87c63f21f9f3dac0be2b7cbc8d86c" BASE_FILES = ["rl_agent_config.json", "model.safetensors", "tokenizer/*", "encoder/*"] def base_dir(path: str | None = None) -> str: """The pinned base checkpoint: downloaded from the Hub at `BASE_REVISION`, or a local copy of it. Either way its weights must match `BASE_WEIGHTS_SHA256`, so a local copy cannot quietly be another revision. A local copy can also be named by `LAYA_BASE`. """ path = path or os.environ.get("LAYA_BASE") or None if path is None: from huggingface_hub import snapshot_download path = snapshot_download(BASE_REPO, revision=BASE_REVISION, allow_patterns=BASE_FILES) got = sha256_file(Path(path) / "model.safetensors") if got != BASE_WEIGHTS_SHA256: raise RuntimeError("base weights in %s are %s, expected %s" % (path, got, BASE_WEIGHTS_SHA256)) return str(path) EFFECTIVE_BATCH = 64 DEV_SEED = 20261118 def split_dev(rows, n: int, seed: int = DEV_SEED): """`(train, dev)`: `n` rows drawn with a fixed seed, and training without any row whose text matches one of them. Contracts repeat boilerplate, so a dev row whose text stays in training would measure recall, not the model. """ order = list(range(len(rows))) random.Random(seed).shuffle(order) dev = [rows[i] for i in sorted(order[:n])] texts = {normalise(r["state"]) for r in dev} keep = set(order[n:]) train = [r for i, r in enumerate(rows) if i in keep and normalise(r["state"]) not in texts] return train, dev def config(task, seed: int, micro_batch: int = 8): """Laya's settings with the task's changes. `micro_batch` only splits each step of 64 rows into smaller pieces when the GPU runs out of memory; the step itself, and so the learning, stays the same size.""" from laya.train import TrainConfig if micro_batch < 1 or EFFECTIVE_BATCH % micro_batch: raise ValueError("micro_batch must divide %d, got %r" % (EFFECTIVE_BATCH, micro_batch)) return TrainConfig(seed=seed, epochs=task.epochs, max_len=task.max_len, head_max_len=task.head_max_len, target_error=task.target_error, loss=task.loss, micro_batch=micro_batch, grad_accum=EFFECTIVE_BATCH // micro_batch) def main() -> None: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("task", choices=tasks.TASKS) parser.add_argument("--seed", type=int, default=0) parser.add_argument("--name", default=None, help="run name (default: seed)") parser.add_argument("--base", default=None, help="a local copy of the base checkpoint (default: download it)") parser.add_argument("--micro-batch", type=int, default=8, help="rows a forward pass (default 8); lower it if the GPU runs out of memory") args = parser.parse_args() from laya.train import finetune task = tasks.load(args.task).TASK if task.test_sha256 is None: raise SystemExit("%s: the test file is not locked yet; run layatasks.data and pin its hash first" % task.name) name = args.name or "seed%d" % args.seed out = run_dir(task.name, name) out.mkdir(parents=True, exist_ok=True) cfg = config(task, args.seed, args.micro_batch) base = base_dir(args.base) data = data_dir(task.name) / "train.jsonl" dev = None if task.dev_rows: rows = read_jsonl(data) kept, dev_part = split_dev(rows, task.dev_rows) data = out / "train_rows.jsonl" write_jsonl(data, kept) write_jsonl(out / "dev.jsonl", dev_part) dev = {"rows": len(dev_part), "seed": DEV_SEED, "train_rows": len(kept), "dropped_same_text": len(rows) - len(kept) - len(dev_part)} print("dev: %d rows kept out, %d more dropped for sharing their text; training on %d" % (dev["rows"], dev["dropped_same_text"], dev["train_rows"])) t = time.perf_counter() summary = finetune(str(data), base, str(out / "model"), config=cfg) seconds = time.perf_counter() - t summary.pop("output_dir", None) write_json(out / "train.json", json.loads(json.dumps({ "task": task.name, "name": name, "seed": args.seed, "seconds": seconds, "dev": dev, "base": {"repo": BASE_REPO, "revision": BASE_REVISION, "weights_sha256": BASE_WEIGHTS_SHA256}, "config": dataclasses.asdict(cfg), "summary": summary}, default=str))) print("trained %s/%s in %.0f s; model in %s" % (task.name, name, seconds, out / "model")) if __name__ == "__main__": os.environ.setdefault("HF_HUB_DISABLE_SYMLINKS_WARNING", "1") main()