from dataclasses import dataclass import fcntl import hashlib import json from pathlib import Path import shutil import time from omegaconf import OmegaConf from rich import print from refined_v4 import study as runner from . import REVISION from .data import ROOT, TASKS RESULTS = runner.PROJECT / 'results/refined-math-v1' @dataclass class Config: action: str = 'watch' root: str = str(ROOT) results: str = str(RESULTS) capacity: int = 32 interval: int = 30 refresh: int = 120 def prepare(root: Path, results: Path, budget: int): root.mkdir(parents=True, exist_ok=True) runner.prepare_results(root, results) runtime = root / 'runtime-cu126' if not runtime.exists(): runtime.symlink_to(runner.ROOT / 'runtime-cu126', target_is_directory=True) source_plot = Path(__file__).parent / '.plotting/plot.py' (results / '.plotting').mkdir(exist_ok=True) shutil.copy2(source_plot, results / '.plotting/plot.py') if (root / 'plan.json').exists(): return runner.read(root / 'plan.json') cases = [] for model in runner.MODELS: for task in TASKS: cases.append(dict(index=len(cases), key=f'{model}__{task}', model=model, task=task, label=runner.LABELS[task], output=str(root / 'runs' / model / task))) value = dict(revision=REVISION, root=str(root), results=str(results), tasks=list(TASKS), models=runner.model_inventory(), cases=cases, sft_tokens=budget, rl_rollouts=65536, group_size=32, eval_queries=64, eval_samples=8, created=time.time(), hint_distribution='Uniform1..16 first, then an eligible question and explicit problem-complexity mixture.', initializer='Existing hinted-v2 initialization; preserved for the first data-only comparison.', wandb_project='tiny-rl-refined-math-v1', wandb_entity='david-heineman', format='Native v2 math formats with multiple examples per document; math-v1 episode eligibility.', partitions=runner.PARTITIONS) value['signature'] = hashlib.sha256(json.dumps(value, sort_keys=True).encode()).hexdigest() runner.write(root / 'plan.json', value) return value def settings(root: Path, results: Path, budget: int, capacity: int): return runner.Config(root=str(root), results=str(results), sft_tokens=budget, capacity=capacity, job_prefix='math1-') def watch(config: Config): from .report import update root, results = Path(config.root), Path(config.results) root.mkdir(parents=True, exist_ok=True) pilot = root / 'pilot-2m' plans = [prepare(pilot, results / 'pilot-2m', 2097152), prepare(root, results, 16777216)] stages = [settings(pilot, results / 'pilot-2m', 2097152, config.capacity), settings(root, results, 16777216, config.capacity)] if config.action == 'plan': print({key: value for key, value in plans[0].items() if key != 'models'}) return with (root / 'math-watch.lock').open('a') as lock: fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) refresh = 0. while True: runner.dispatch(stages[0]) pilot_started = all((Path(case['output']) / 'request.json').exists() for case in plans[0]['cases']) if pilot_started: runner.dispatch(stages[1]) complete = [sum((Path(case['output']) / 'complete.json').exists() for case in plan['cases']) for plan in plans] print(f'[cyan]Math-v1: pilot {complete[0]}/16 complete; full {complete[1]}/16 complete[/cyan]', flush=True) if time.monotonic() - refresh >= config.refresh: for stage in stages: update(Path(stage.root), Path(stage.results)) refresh = time.monotonic() if complete == [16, 16]: return time.sleep(config.interval) if __name__ == '__main__': watch(OmegaConf.to_object(OmegaConf.merge(OmegaConf.structured(Config), OmegaConf.from_cli())))