File size: 2,853 Bytes
21419ae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import argparse,os,json,pathlib,importlib
from common import *
p=argparse.ArgumentParser();p.add_argument('--artifact');p.add_argument('--setting',choices=['A','B']);p.add_argument('--method',choices=sorted({a['family']for a in experiments()}));p.add_argument('--seed',type=int,choices=[42,2024,3407],default=42);p.add_argument('--phase',choices=['train','evaluate'],default='train');p.add_argument('--gpu',default='0');p.add_argument('--data-root');p.add_argument('--path-map');p.add_argument('--cache-dir');p.add_argument('--checkpoint-root',help='Optional local checkpoint store, SETTING/METHOD_seedN.pth plus .json training record');p.add_argument('--run-tag',default='reproduction');p.add_argument('--check-dependencies',action='store_true');p.add_argument('--execute',action='store_true');args=p.parse_args()
if args.artifact:
if args.setting or args.method:p.error('Use either --artifact or --setting/--method')
a=artifact(args.artifact)
else:
if not args.setting or not args.method:p.error('--setting and --method required')
a=artifact(f'{args.setting}/{args.method}/seed{args.seed}')
plan=build_plan(a['setting'],a['family'],a['seed'],args.run_tag,args.checkpoint_root,resolve=args.check_dependencies or args.execute and args.phase=='train');j=plan['jobs'][0];out=run_directory(a['id'],args.run_tag)
print(json.dumps({'artifact':a['id'],'execute':args.execute,'output':str(out),'dependencies':plan['dependencies']}))
if not args.execute:raise SystemExit(0)
if not args.data_root and not args.path_map:p.error('Specify --data-root or --path-map')
if args.phase=='train'and a['kind']in ['single','dual']and not args.cache_dir:p.error('Specify --cache-dir; first run data/extract_cache.py with the same --run-tag/--checkpoint-root')
os.environ['CUDA_VISIBLE_DEVICES']=args.gpu
if args.data_root:os.environ['BINARY_DATA_ROOT']=args.data_root
if args.path_map:os.environ['BINARY_PATH_MAP']=pathlib.Path(args.path_map).read_text()
module=importlib.import_module(a['training_module']);module.ROOT=out;module.HERE=out;module.PLAN=plan;out.mkdir(exist_ok=True,parents=True)
for name in ['logs','checkpoints']:(out/name).mkdir(exist_ok=True)
if args.cache_dir:module.CACHE_ROOT=pathlib.Path(args.cache_dir).resolve()
if args.phase=='train':
module.train(plan,j)
weight=export_checkpoint(a['id'],out/'checkpoints'/j['run_name']/'best_model_state.pth',out/'logs'/j['run_name']/'training_complete.json',args.run_tag);print(json.dumps({'exported_checkpoint':weight}))
else:
paths=[out/'logs'/j['run_name']/'training_complete.json',out/'checkpoints'/j['run_name']/'best_model_state.pth'];assert all(x.exists()for x in paths);frozen={'files':{str(x):sha(x)for x in paths}}
for name in ['frozen_training_results.json',j['id']+'.frozen_training_results.json']:(out/name).write_text(json.dumps(frozen))
module.evaluate(plan,j)
|