AQIT 0.1.0
Loading...
Searching...
No Matches
train.py
Go to the documentation of this file.
1# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2# This file is part of the Aquin Engine. Unauthorized copying, modification,
3# distribution, or use of this file, via any medium, is strictly prohibited.
4# Proprietary and confidential. See LICENSE for terms.
5
6"""Run a Recipe: capture DataRevision → train → Checkpoint → EvalGate."""
7
8from __future__ import annotations
9
10import time
11import uuid
12from pathlib import Path
13from typing import Any
14
15from aquin.recipe.load import load_recipe
16from aquin.recipe.schema import Recipe, RecipeError, RunRecord
17from aquin.recipe.store import capture_revision, run_dir, save_run
18from aquin.run import Run
19
20
21def train_recipe(
22 recipe: str | Path | Recipe,
23 *,
24 cwd: Path | None = None,
25 snapshot: bool | None = None,
26 dry_run: bool = False,
27) -> RunRecord:
28 cwd = cwd or Path.cwd()
29 rec = recipe if isinstance(recipe, Recipe) else load_recipe(recipe)
30 snap = bool(rec.data.get("snapshot")) if snapshot is None else snapshot
31 revision = capture_revision(rec.data["path"], snapshot=snap, cwd=cwd)
32 rec.data["path"] = revision.path
33
34 run_id = uuid.uuid4().hex[:12]
35 dest = run_dir(run_id, cwd=cwd)
36 dest.mkdir(parents=True, exist_ok=True)
37
38 record = RunRecord(
39 run_id=run_id,
40 name=rec.name,
41 family=rec.family,
42 status="planned" if dry_run else "running",
43 run_dir=str(dest),
44 recipe=rec.to_dict(),
45 revision=revision.to_dict(),
46 )
47 if dry_run:
48 record.status = "dry_run"
49 save_run(record)
50 return record
51
52 recorder = Run(
53 base_model=str(rec.train.get("base") or rec.family),
54 run_name=rec.name,
55 config=rec.to_dict(),
56 run_dir=dest,
57 )
58 t0 = time.time()
59 try:
60 if rec.family == "tabular":
61 from aquin.recipe.tabular import train_tabular
62
63 result = train_tabular(rec, recorder, dest)
64 elif rec.family == "llm":
65 from aquin.recipe.llm_lora import train_llm
66
67 result = train_llm(rec, recorder, dest)
68 else:
69 raise RecipeError(f"Unsupported family {rec.family!r}")
70 except Exception as exc:
71 record.status = "failed"
72 record.error = str(exc)
73 save_run(record)
74 recorder.finish()
75 raise
76
77 record.checkpoint = result["checkpoint"].to_dict()
78 record.gate = result["gate"].to_dict()
79 record.metrics = dict(result.get("metrics") or {})
80 record.inspect = dict(result.get("inspect") or {})
81 record.status = "passed" if result["gate"].passed else "failed_gate"
82 record.metrics["elapsed_s"] = round(time.time() - t0, 3)
83 save_run(record)
84 recorder.finish()
85 return record
RunRecord train_recipe(str|Path|Recipe recipe, *, Path|None cwd=None, bool|None snapshot=None, bool dry_run=False)
Definition train.py:31