Spaces:
Running
Running
Download python/borderland/cli.py from FatUnc6969/borderland-rl: direct link, hf CLI and curl.
- Browser
- Download file 13.5 kB
-
https://huggingface.co/spaces/FatUnc6969/borderland-rl/resolve/main/python/borderland/cli.py
- Command line
-
hf download hf://spaces/FatUnc6969/borderland-rl/python/borderland/cli.py
-
curl -L -o cli.py https://huggingface.co/spaces/FatUnc6969/borderland-rl/resolve/main/python/borderland/cli.py
13.5 kB
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import os | |
| from pathlib import Path | |
| import sys | |
| def _print(value): | |
| print(json.dumps(value, indent=2, ensure_ascii=False, allow_nan=False)) | |
| def _output(value, directory, filename): | |
| if directory: | |
| target = Path(directory) | |
| target.mkdir(parents=True, exist_ok=True) | |
| (target / filename).write_text(json.dumps(value, indent=2, ensure_ascii=False, allow_nan=False) + '\n') | |
| def _suite_summary(suite): | |
| return {'core_pass': suite['core_pass'], | |
| 'configuration': suite['configuration'], | |
| 'public_reference_pass': suite['public_reference_pass'], | |
| 'certificate_status': suite['certificate_status'], | |
| 'privileged_reference_status': suite['privileged_reference']['status'], | |
| 'policies': [{key: row[key] for key in ('name', 'scope', 'dependency_metrics', 'notebook_quality', 'denominator', 'escapes', 'escape_rate', 'categories')} | |
| for row in suite['policies']], | |
| 'certificates': [{'pass': row.get('pass', False), 'scope': row.get('scope'), 'failures': row.get('failures', [])} | |
| for row in suite['certificates']], | |
| 'limitations': suite.get('limitations', [])} | |
| def _training_summary(result): | |
| def metrics(value): | |
| if isinstance(value, dict): | |
| return {key: metrics(item) for key, item in value.items() if key != 'episodes'} | |
| return value | |
| summary = {key: result[key] for key in ('learner', 'compressor', 'initial_checksum', 'final_checksum', | |
| 'rl_updates', 'nonzero_rl_updates', 'imitation_updates', 'limits')} | |
| for key in ('full_campaign', 'valve_checkpoint_curriculum'): | |
| section = result[key] | |
| summary[key] = {name: value for name, value in section.items() if name not in {'before', 'after', 'training'}} | |
| for name in ('before', 'after'): | |
| summary[key][name] = metrics(section[name]) | |
| return summary | |
| def _hierarchical_training_summary(result): | |
| summary = {key: result[key] for key in ('learner', 'compressor', 'training_seed', 'initial_checksum', | |
| 'final_checksum', 'rl_updates', 'nonzero_rl_updates', | |
| 'imitation_updates', 'configuration_hash', 'run_configuration', | |
| 'run_configuration_hash', 'split_hashes', 'source_hash', 'engine_hash', 'limits')} | |
| for name in ('before', 'after'): | |
| summary[name] = {key: value for key, value in result[name].items() if key != 'episodes'} | |
| summary['ablations'] = {phase: {key: value for key, value in metrics.items() if key != 'episodes'} | |
| for phase, metrics in result['ablations'].items()} | |
| return summary | |
| def _play(seed, archive_workload, generation_version=3, notebook_bytes=768, memory_profile='static'): | |
| from borderland import Campaign | |
| campaign = Campaign.generated(seed, archive_workload=archive_workload, | |
| generation_version=generation_version, notebook_bytes=notebook_bytes, memory_profile=memory_profile) | |
| transition = campaign.reset() | |
| while True: | |
| data = transition.to_dict() | |
| observation = data['observation'] | |
| print(f"\nGame {observation['phase']} | charge {observation['charge']} | tick {observation['tick']} | {data['outcome']}") | |
| for rule in observation['rules']: | |
| print(rule) | |
| _print(observation['board']) | |
| if transition.done: | |
| return 0 if transition.outcome == 'escaped' else 1 | |
| actions = data['legal_actions'] | |
| for index, action in enumerate(actions, 1): | |
| print(f"{index}. {action['tag']} {json.dumps(action['arguments'], ensure_ascii=False)}") | |
| try: | |
| command = input('Action number, notes TEXT, or quit: ').strip() | |
| except (EOFError, KeyboardInterrupt): | |
| print() | |
| return 0 | |
| if command == 'quit': | |
| return 0 | |
| if command.startswith('notes '): | |
| transition = campaign.replace_notebook(command[6:], expected_revision=transition.revision) | |
| elif command.isdigit() and 1 <= int(command) <= len(actions): | |
| transition = campaign.step(actions[int(command) - 1], expected_revision=transition.revision) | |
| else: | |
| print('Choose a displayed action number.') | |
| continue | |
| if transition.error: | |
| print(transition.to_dict()['error']['message']) | |
| def main(argv=None): | |
| arguments = sys.argv[1:] if argv is None else argv | |
| if arguments and arguments[0] == 'native-campaign': | |
| from borderland_eval.native_campaign import main as campaign_main | |
| return campaign_main(arguments[1:]) | |
| parser = argparse.ArgumentParser(prog='borderland', description='A seven-game campaign with shared charge and bounded memory.') | |
| commands = parser.add_subparsers(dest='command', required=True) | |
| verify = commands.add_parser('verify', help='Run offline campaign checks and the bounded twin certificate.') | |
| verify.add_argument('--output') | |
| verify.add_argument('--generation-version', type=int, choices=(2,3), default=3) | |
| verify.add_argument('--notebook-bytes', type=int, default=768) | |
| verify.add_argument('--memory-profile', choices=('static','commissioning'), default='static') | |
| evaluate = commands.add_parser('evaluate', help='Run offline reference and baseline comparisons.') | |
| evaluate.add_argument('--seeds', type=int, default=8, metavar='N') | |
| evaluate.add_argument('--start-seed', type=int, default=0) | |
| evaluate.add_argument('--output') | |
| evaluate.add_argument('--generation-version', type=int, choices=(2,3), default=2) | |
| evaluate.add_argument('--archive-workload', choices=('compact','standard','extended'), default='standard') | |
| evaluate.add_argument('--notebook-bytes', type=int, default=768) | |
| evaluate.add_argument('--memory-profile', choices=('static','commissioning'), default='static') | |
| train = commands.add_parser('train', help='Train a local non-LLM policy with reward-dependent updates.') | |
| train.add_argument('--learner', choices=('sparse', 'hierarchical'), default='sparse') | |
| train.add_argument('--episodes', type=int, default=400) | |
| train.add_argument('--seed', type=int, default=0) | |
| train.add_argument('--output', default='artifacts/training') | |
| play = commands.add_parser('play', help='Play a campaign in the terminal.') | |
| play.add_argument('--seed', type=int, default=0) | |
| server = commands.add_parser('serve', help='Open the local playable facility interface.') | |
| server.add_argument('--host', default='127.0.0.1') | |
| server.add_argument('--port', type=int, default=8765) | |
| live = commands.add_parser('live', help='Run an explicitly enabled provider evaluation.') | |
| live.add_argument('--seed', type=int, default=0) | |
| live.add_argument('--provider', choices=['reflection', 'huggingface'], default=os.environ.get('BORDERLAND_PROVIDER', 'reflection')) | |
| live.add_argument('--effort', choices=['low', 'medium', 'high', 'xhigh', 'max'], default='high') | |
| live.add_argument('--output', default='artifacts/live') | |
| for command in (play,live,server): | |
| command.add_argument('--archive-workload', choices=('compact','standard','extended'), default='standard') | |
| command.add_argument('--generation-version', type=int, choices=(2,3), default=3) | |
| command.add_argument('--notebook-bytes', type=int, default=768) | |
| command.add_argument('--memory-profile', choices=('static','commissioning'), default='static') | |
| args = parser.parse_args(argv) | |
| try: | |
| if args.command in {'play','live','serve','verify','evaluate'} and args.generation_version == 2 and args.memory_profile != 'static': | |
| parser.error('Commissioning requires generation three.') | |
| if args.command in {'play','live','serve','evaluate'} and (args.notebook_bytes < 0 or | |
| (args.generation_version == 2 and args.notebook_bytes != 768)): | |
| parser.error('Custom nonnegative notebook bytes require generation three.') | |
| if args.command == 'serve': | |
| if not 0 <= args.port <= 65535: | |
| parser.error('--port must be between 0 and 65535') | |
| from borderland_web.server import serve | |
| serve(args.host, args.port, args.archive_workload, | |
| generation_version=args.generation_version, notebook_bytes=args.notebook_bytes, memory_profile=args.memory_profile) | |
| return 0 | |
| if args.command == 'play': | |
| return _play(args.seed,args.archive_workload,args.generation_version,args.notebook_bytes,args.memory_profile) | |
| if args.command == 'evaluate': | |
| if not 1 <= args.seeds <= 10000: | |
| parser.error('--seeds must be between 1 and 10000') | |
| if args.start_seed < 0: | |
| parser.error('--start-seed must be nonnegative') | |
| from borderland_eval.suite import run_suite | |
| result = run_suite(list(range(args.start_seed, args.start_seed + args.seeds)), output_dir=args.output, | |
| generation_version=args.generation_version, archive_workload=args.archive_workload, | |
| notebook_bytes=args.notebook_bytes, memory_profile=args.memory_profile) | |
| _print({**_suite_summary(result), 'output': args.output}) | |
| return 0 | |
| if args.command == 'train': | |
| if not 1 <= args.episodes <= 1000000: | |
| parser.error('--episodes must be between 1 and 1000000') | |
| if args.learner == 'hierarchical': | |
| if not 0 <= args.seed < 2**63: | |
| parser.error('--seed must be between 0 and 2**63 - 1') | |
| from borderland_eval.hierarchical_training import train_hierarchical | |
| result = train_hierarchical(args.output, episodes=args.episodes, seed=args.seed) | |
| summary = _hierarchical_training_summary(result) | |
| else: | |
| from borderland_eval.training import train_local | |
| result = train_local(args.output, episodes=args.episodes, seed=args.seed) | |
| summary = _training_summary(result) | |
| _print({**summary, 'output': args.output}) | |
| return 0 | |
| if args.command == 'verify': | |
| if args.generation_version == 3: | |
| from borderland_eval.research import smoke | |
| receipts = [smoke(seed, 3, args.notebook_bytes, args.memory_profile) for seed in (100000,100001,100002,100003)] | |
| result = {'passed': all(row['passed'] for row in receipts), 'generation_version':3, | |
| 'scope':'Four scripted public-reference development campaigns and exact private replay. This is not Beam evaluation or an environment-quality verdict.', | |
| 'campaigns':receipts} | |
| _output(result, args.output, 'verification.json') | |
| _print(result) | |
| return 0 if result['passed'] else 1 | |
| from borderland_eval.suite import run_suite | |
| from borderland_verify.certificates import certify_twins | |
| suite = run_suite([0, 1, 2, 3], output_dir=args.output) | |
| certificate = certify_twins(17) | |
| passed = certificate.get('pass', False) and suite.get('core_pass', False) | |
| result = {'passed': bool(passed), 'suite': suite, 'certificate': certificate} | |
| _output(result, args.output, 'verification.json') | |
| _print({'passed': bool(passed), 'suite': _suite_summary(suite), | |
| 'final_valve_certificate': {'pass': certificate.get('pass', False), 'scope': certificate.get('scope'), | |
| 'bound': certificate.get('fixed_memoryless_bound'), 'failures': certificate.get('failures', [])}, | |
| 'output': args.output}) | |
| return 0 if passed else 1 | |
| if os.environ.get('BORDERLAND_LIVE_ENABLED', '').lower() not in {'1', 'true', 'yes'}: | |
| print('Live evaluation is disabled. Set BORDERLAND_LIVE_ENABLED=1 only after confirming authorized provider access.', file=sys.stderr) | |
| return 2 | |
| from borderland_eval.provider import run_live | |
| config = {'generation_version':args.generation_version,'notebook_bytes':args.notebook_bytes,'archive_workload':args.archive_workload,'memory_profile':args.memory_profile,'live_enabled': True, 'free_access_verified': os.environ.get('BORDERLAND_FREE_ACCESS_VERIFIED', '').lower() in {'1', 'true', 'yes'}, 'provider': args.provider, 'effort': args.effort, | |
| 'model': os.environ.get('BORDERLAND_MODEL', ''), 'api_base': os.environ.get('BORDERLAND_API_BASE', ''), | |
| 'api_key': os.environ.get('BORDERLAND_API_KEY') or os.environ.get('HF_TOKEN' if args.provider == 'huggingface' else 'REFLECTION_API_KEY', ''), | |
| 'max_attempts': 128, 'cash_cap_usd': 0.0} | |
| result = run_live(args.seed, config, args.output) | |
| _print({key: value for key, value in result.items() if key not in {'attempts', 'assistant_claims'}} | {'output': args.output}) | |
| return 0 | |
| except (OSError, ValueError, ImportError) as exc: | |
| print(f'{args.command} failed: {type(exc).__name__}. Check the local setup and command arguments.', file=sys.stderr) | |
| return 1 | |
| if __name__ == '__main__': | |
| raise SystemExit(main()) | |