data-gen-bpu/bin/stageclient.py
bench 21fce5e2e0 v22:bench stage wait 只看一眼不轮询;板上自测先查大小、拒绝有原因、失败也删副本;速度参考编不出来写 unavailable
- bench stage wait(stageclient 与 stagerun 两个入口)不再睡,没结束就返回 2 并指向 terminal_exec wait_handle。
  10-16 efficientvit.b0 400 轮里 307 轮是轮询。
- bench dev --board:先用 salty storage ls 量开发环境里的目录(>512 MiB 或 >40 个子目录直接拒绝),
  取回后已到时限也明确拒绝;拒绝原因作为命令结果回给 agent;snapshot/dev-files/tar 在任何路径都删。
  10-16 四次自测三次从 1.2 GB 的目录提交,取回 40 分钟、超时、exit 2 无原因、VM 留下 2.5 GB。
- speed_ref:默认配置编不出来时写 latency_ms_est=null、status=unavailable,并补齐以前的 FAILED。
- SYSTEM.md 对应三处;agent/VERSION 22。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 15:24:22 +08:00

154 lines
6.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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())