Download src/macjev/train/cli.py from VTN-TokenFactory/Telasap-0.8B-v1.0: direct link, hf CLI and curl.
- Browser
- Download file 5.71 kB
-
https://huggingface.co/VTN-TokenFactory/Telasap-0.8B-v1.0/resolve/main/src/macjev/train/cli.py
- Command line
-
hf download hf://VTN-TokenFactory/Telasap-0.8B-v1.0/src/macjev/train/cli.py
-
curl -L -o cli.py https://huggingface.co/VTN-TokenFactory/Telasap-0.8B-v1.0/resolve/main/src/macjev/train/cli.py
5.71 kB
| """Training CLI (CONTRACTS section 8). | |
| python -m macjev.train.cli --config configs/macjev/<name>.json --run runs/macjev/<run> | |
| [--resume] [--max-tokens N] [--deadline-unix T] | |
| [--lr-scale X] [--stop-step N] [--copy-dir D] [--pool-dir D] [--no-r4-retry] | |
| ``--lr-scale`` multiplies the config's peak LR (e.g. 2.5 turns main.json's 2e-5 into the LR | |
| pilot's 5e-5 winner without editing the preregistered config); ``--stop-step`` ends training at | |
| that optimizer step without changing the LR horizon (refit reuses the main run's chosen step); | |
| ``--copy-dir`` mirrors every checkpoint file (e.g. to a network volume; not part of the config | |
| hash); ``--pool-dir`` reads ``<dir>/{train,dev}.jsonl`` fixture rows instead of ``macjev.pool``. | |
| Writes into the run directory: ``config.resolved.json``, ``progress.json``, ``dev-*.jsonl``, | |
| ``dev_history.jsonl``, ``state-*.pt`` / ``weights-*.safetensors`` / ``ema-*.safetensors`` | |
| (+ ``best-*.safetensors``), ``checkpoints.json``, ``best.json``, ``exposure.json``, ``train.log``. | |
| Plan rule R4: on a non-finite loss/gradient or a sustained loss spike (``spike_*`` config keys) | |
| the run is resumed once from the newest verified full state with 0.5x the peak LR (from scratch | |
| if no state exists yet); a second failure ends the run with exit code 2. The halving is recorded | |
| in ``r4.json`` and in the full state (``r4_factor``), so a later ``--resume`` (even with an explicit | |
| ``--lr-scale``) keeps it, and a run that R4 terminated refuses to resume (exit code 2). | |
| ``--deadline-unix`` makes the trainer save and exit cleanly (status ``deadline``, exit code 0) | |
| before the deadline; ``--max-tokens`` caps training tokens and the LR schedule horizon. | |
| Starting without ``--resume`` in a run directory that already holds a full state is refused | |
| (exit code 3) so two trajectories never mix in one directory. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import sys | |
| import time | |
| from pathlib import Path | |
| from .checkpoint import atomic_write_json | |
| from .config import config_hash, load_config, resolve_path | |
| EXIT = {"done": 0, "deadline": 0, "nonfinite": 2} | |
| def main(argv=None) -> int: | |
| ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| ap.add_argument("--config", required=True) | |
| ap.add_argument("--run", required=True) | |
| ap.add_argument("--resume", action="store_true") | |
| ap.add_argument("--max-tokens", type=int, default=None) | |
| ap.add_argument("--deadline-unix", type=float, default=None) | |
| ap.add_argument("--lr-scale", type=float, default=None) | |
| ap.add_argument("--stop-step", type=int, default=None) | |
| ap.add_argument("--copy-dir", default=None) | |
| ap.add_argument("--pool-dir", default=None) | |
| ap.add_argument("--no-r4-retry", action="store_true") | |
| args = ap.parse_args(argv) | |
| from .trainer import Trainer | |
| over: dict = {} | |
| if args.copy_dir: | |
| over["checkpoint"] = {"copy_dir": str(resolve_path(args.copy_dir))} | |
| if args.pool_dir: | |
| over["data"] = {"pool_dir": str(resolve_path(args.pool_dir))} | |
| cfg = load_config(args.config, over or None) | |
| run = resolve_path(args.run) | |
| run.mkdir(parents=True, exist_ok=True) | |
| resolved = run / "config.resolved.json" | |
| if resolved.exists() and args.resume: | |
| old = json.loads(resolved.read_text()) | |
| if old.get("config_hash") != config_hash(cfg): | |
| print(f"refusing to resume: {args.config} differs from the run's resolved config", file=sys.stderr) | |
| return 3 | |
| index = run / "checkpoints.json" | |
| if not args.resume and index.exists() and json.loads(index.read_text()).get("state"): | |
| print(f"refusing to start: {run} already holds a resumable state; pass --resume or use a new --run", | |
| file=sys.stderr) | |
| return 3 | |
| r4_path = run / "r4.json" | |
| r4 = json.loads(r4_path.read_text()) if (args.resume and r4_path.exists()) else None | |
| if r4 and r4.get("terminated"): | |
| print(f"refusing to resume: plan rule R4 terminated this run ({r4_path})", file=sys.stderr) | |
| return 2 | |
| atomic_write_json(resolved, {"config_hash": config_hash(cfg), "config": cfg, "argv": sys.argv[1:], | |
| "time": time.time()}) | |
| if args.deadline_unix is not None and args.deadline_unix <= time.time(): | |
| print("deadline already passed; nothing to do", file=sys.stderr) | |
| return 0 | |
| def make(r4_factor): | |
| return Trainer(cfg, run, max_tokens=args.max_tokens, deadline_unix=args.deadline_unix, | |
| lr_scale=args.lr_scale, stop_step=args.stop_step, r4_factor=r4_factor) | |
| trainer = make(r4["r4_factor"] if r4 else None) | |
| status = trainer.run(resume=args.resume) | |
| if status == "nonfinite" and r4 is None and not args.no_r4_retry: | |
| r4 = {"first_failure": trainer.progress, "r4_factor": 0.5, "terminated": False, "time": time.time()} | |
| atomic_write_json(r4_path, r4) | |
| trainer.log("R4: restarting once from the newest verified state with 0.5x peak LR") | |
| del trainer | |
| _free() | |
| trainer = make(0.5) | |
| status = trainer.run(resume=True) | |
| if status == "nonfinite" and r4 is not None: # second failure under R4 (not merely --no-r4-retry) | |
| r4 = dict(r4, terminated=True, final_failure=trainer.progress, time_terminated=time.time()) | |
| atomic_write_json(r4_path, r4) | |
| trainer.log("R4: second non-finite loss / sustained spike after the 0.5x LR retry; run terminated") | |
| return EXIT.get(status, 1) | |
| def _free() -> None: | |
| import gc | |
| import torch | |
| gc.collect() | |
| if torch.cuda.is_available(): | |
| torch.cuda.empty_cache() | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |