#!/usr/bin/env python3 """Untrusted, stdlib-only stage ticket client. Authority lives in the outer worker.""" import argparse import json import os import re import shlex import sys import time import uuid from pathlib import Path NAMES = {n.split('-', 1)[1]: n for n in ('01-fetch', '02-baseline', '03-export', '04-calibrate', '05-compile', '06-hbm', '07-board', '08-eval')} def rollout_dir(): return Path(os.environ['BENCH_ROLLOUT_DIR']) def submit(stage, config, poll_hint=60, *, recipe=None, submission=None): name = NAMES.get(stage, stage) if name not in NAMES.values() and name != 'noop': raise SystemExit('阶段不允许: ' + name) rd = rollout_dir() if recipe and submission: raise ValueError('--recipe and --submission are mutually exclusive') if submission and not re.fullmatch(r'[a-zA-Z0-9_-]+', submission): raise ValueError('submission must be a handle from this rollout') if recipe: p = Path(recipe) if p.is_absolute(): p = p.relative_to(rd / 'workspace') elif p.parts and p.parts[0] == 'workspace': p = Path(*p.parts[1:]) recipe = str(p) cfg = None if config: ws = rd / 'workspace' cfg = Path(config) if not cfg.is_absolute(): if cfg.parts[0] == 'workspace': cfg = Path(*cfg.parts[1:]) cfg = ws / cfg cfg = cfg.resolve() if not cfg.is_relative_to(ws.resolve()) or not cfg.is_file(): raise SystemExit('--config 必须是 workspace 内文件') hid = name + '-' + uuid.uuid4().hex hd = rd / 'handles' / hid hd.mkdir() (hd / 'status.json').write_text(json.dumps({'state': 'QUEUED', 'queued_at': time.time()})) tmp = hd / 'request.tmp' tmp.write_text(json.dumps({'stage': name, 'config': str(cfg) if cfg else None, 'recipe': recipe, 'submission': submission, 'submitted_at': time.time()})) tmp.rename(hd / 'request.json') print(hid) print('# Pi wait: terminal_exec(' + json.dumps({'wait_handle': hid}) + ')') return hid def dev(command, timeout=3600, cwd=None): """bench dev: run one command in the development environment (Salty container with the toolchain).""" rd = rollout_dir() ws = (rd / 'workspace').resolve() here = Path.cwd().resolve() cwd = cwd or (str(here.relative_to(ws)) if here.is_relative_to(ws) else '.') hid = 'terminal-' + uuid.uuid4().hex hd = rd / 'handles' / hid hd.mkdir() (hd / 'status.json').write_text(json.dumps({'state': 'QUEUED', 'queued_at': time.time()})) tmp = hd / 'request.tmp' tmp.write_text(json.dumps({'stage': 'terminal', 'target': 'development', 'command': command, 'timeout': timeout, 'cwd': cwd})) tmp.rename(hd / 'request.json') print(hid) print('# Pi wait: terminal_exec(' + json.dumps({'wait_handle': hid}) + ')') return hid def wait(handle: str, poll: int = 60) -> int: """不再轮询,看一眼就返回:结束了打印结果;没结束返回 2,并指向 terminal_exec wait_handle(一次等到阶段结束、不占轮数)。 10-16:efficientvit.b0 400 轮里 307 轮是 bench stage wait,每次还往上下文里加一段输出。""" hd = rollout_dir() / "handles" / handle if not hd.exists(): raise SystemExit(f"handle 不存在:{handle}") st = json.loads((hd / "status.json").read_text()) if st.get("state") != "DONE": print(f"{handle}: {st.get('state')}" f"{'(阶段计算已结束,等待数据库登记;无需重跑)' if st.get('registration') == 'PENDING' else ''}" f"。不要反复查:调用 terminal_exec({json.dumps({'wait_handle': handle})}),它一次等到这个阶段结束,不占轮数。") return 2 _print_status(st) return 0 if st["result"] in ("OK", "SKIPPED") else 1 def status(handle: str) -> int: hd = rollout_dir() / "handles" / handle st = json.loads((hd / "status.json").read_text()) _print_status(st) return 0 def _print_status(st: dict) -> None: if st.get("state") != "DONE": print(json.dumps(st)); return print(f"result: {st['result']} exit_code: {st['exit_code']} error_class: {st['error_class']} {st['duration_s']}s") print(f"stdout: {st['stdout']}\nstderr: {st['stderr']}\nmeta: {st['meta']}") for name in ('stdout_tail', 'stderr_tail'): if st.get(name): print(f"--- {name} ---\n{st[name]}") if st.get("measurements"): print("measurements: " + json.dumps(st["measurements"], ensure_ascii=False)) if st["result"] != "OK": try: tail = Path(st["stderr"]).read_text(errors="replace").splitlines()[-30:] print("--- stderr tail ---"); print("\n".join(tail)) except OSError: pass def main(): if sys.argv[1:2] == ['dev']: p = argparse.ArgumentParser(prog='bench dev', description='Run a command in the development environment') p.add_argument('--timeout', type=int, default=3600, help='seconds, at most 43200') p.add_argument('--cwd', help='workspace-relative directory; default: the current one') p.add_argument('--board', action='store_true', help='run on the task board instead (S100P tasks): this directory, ' 'with files that only exist in the development workspace such as a compiled HBM, at most 30 min') p.add_argument('command', nargs=argparse.REMAINDER) a = p.parse_args(sys.argv[2:]) words = a.command[1:] if a.command[:1] == ['--'] else a.command if not words: p.error('command required') command = words[0] if len(words) == 1 else shlex.join(words) dev(('#bench:board\n' if a.board else '') + command, a.timeout, a.cwd) # harness/terminal.py BOARD_MARK return 0 p = argparse.ArgumentParser() p.add_argument('command', choices=['stage']) p.add_argument('op', choices=['run', 'wait', 'status']) p.add_argument('stage') p.add_argument('--config') p.add_argument('--recipe') p.add_argument('--submission') p.add_argument('--poll', type=int, default=60) a = p.parse_args() if not re.fullmatch(r'[a-zA-Z0-9_-]+', a.stage): p.error('invalid handle') try: if a.op == 'run': submit(a.stage, a.config, recipe=a.recipe, submission=a.submission); return 0 if a.op == 'wait': return wait(a.stage, max(1, a.poll)) return status(a.stage) except (OSError, ValueError) as e: print(f'handle 不存在或尚未就绪: {a.stage} ({e.strerror if isinstance(e, OSError) else e})', file=sys.stderr) return 1 if __name__ == '__main__': sys.exit(main())