Download scripts/run_experiment.py from SPARK-LAB/ST-DTKD: direct link, hf CLI and curl.
- Browser
- Download file 2.85 kB
-
https://huggingface.co/SPARK-LAB/ST-DTKD/resolve/main/scripts/run_experiment.py
- Command line
-
hf download hf://SPARK-LAB/ST-DTKD/scripts/run_experiment.py
-
curl -L -o run_experiment.py https://huggingface.co/SPARK-LAB/ST-DTKD/resolve/main/scripts/run_experiment.py
2.85 kB
| 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) | |