|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from benchsuite.common import repo_root, write_json |
| 8 | +from benchsuite.orchestrator import BenchOrchestrator |
| 9 | +from benchsuite.registry import get_adapter |
| 10 | + |
| 11 | +try: |
| 12 | + from tqdm.auto import tqdm |
| 13 | +except Exception: # pragma: no cover |
| 14 | + tqdm = None |
| 15 | + |
| 16 | + |
| 17 | +_ORCHESTRATOR = BenchOrchestrator() |
| 18 | + |
| 19 | + |
| 20 | +def _single_job_progress(desc: str): |
| 21 | + if tqdm is None: |
| 22 | + return None |
| 23 | + return tqdm(total=1, desc=desc, unit="job") |
| 24 | + |
| 25 | + |
| 26 | +def _run_gate(args: argparse.Namespace) -> int: |
| 27 | + adapter = get_adapter(args.model) |
| 28 | + report = adapter.compare_tokens(args.baseline, args.rust) |
| 29 | + out = args.output if args.output else args.rust.parent / "compare.json" |
| 30 | + write_json(out, report) |
| 31 | + print(out) |
| 32 | + return 0 if report["match"] else 1 |
| 33 | + |
| 34 | + |
| 35 | +def _run_bench_python(args: argparse.Namespace) -> int: |
| 36 | + adapter = get_adapter(args.model) |
| 37 | + pbar = _single_job_progress("bench-python") |
| 38 | + try: |
| 39 | + payload = adapter.run_python_bench( |
| 40 | + model_dir=args.model_dir, |
| 41 | + image=args.image, |
| 42 | + prompt=args.prompt, |
| 43 | + max_new_tokens=args.max_new_tokens, |
| 44 | + py_device=args.device, |
| 45 | + py_dtype=args.dtype, |
| 46 | + output=args.output, |
| 47 | + repo_root=repo_root(), |
| 48 | + ) |
| 49 | + if pbar is not None: |
| 50 | + pbar.update(1) |
| 51 | + finally: |
| 52 | + if pbar is not None: |
| 53 | + pbar.close() |
| 54 | + print(args.output) |
| 55 | + if args.print_json: |
| 56 | + import json |
| 57 | + |
| 58 | + print(json.dumps(payload, ensure_ascii=False)) |
| 59 | + return 0 |
| 60 | + |
| 61 | + |
| 62 | +def _run_bench_rust(args: argparse.Namespace) -> int: |
| 63 | + adapter = get_adapter(args.model) |
| 64 | + pbar = _single_job_progress("bench-rust") |
| 65 | + try: |
| 66 | + payload = adapter.run_rust_bench( |
| 67 | + cli=args.cli, |
| 68 | + image=args.image, |
| 69 | + prompt=args.prompt, |
| 70 | + max_new_tokens=args.max_new_tokens, |
| 71 | + rs_device=args.device, |
| 72 | + rs_dtype=args.dtype, |
| 73 | + output=args.output, |
| 74 | + repo_root=repo_root(), |
| 75 | + ) |
| 76 | + if pbar is not None: |
| 77 | + pbar.update(1) |
| 78 | + finally: |
| 79 | + if pbar is not None: |
| 80 | + pbar.close() |
| 81 | + print(args.output) |
| 82 | + if args.print_json: |
| 83 | + import json |
| 84 | + |
| 85 | + print(json.dumps(payload, ensure_ascii=False)) |
| 86 | + return 0 |
| 87 | + |
| 88 | + |
| 89 | +def _run_perf(args: argparse.Namespace) -> int: |
| 90 | + return _ORCHESTRATOR.run_perf(args) |
| 91 | + |
| 92 | + |
| 93 | +def _run_matrix_gate(args: argparse.Namespace) -> int: |
| 94 | + return _ORCHESTRATOR.run_matrix_gate(args) |
| 95 | + |
| 96 | + |
| 97 | +def build_parser() -> argparse.ArgumentParser: |
| 98 | + parser = argparse.ArgumentParser( |
| 99 | + prog="python -m benchsuite.cli", |
| 100 | + description="Unified benchmark + gate CLI with model adapters", |
| 101 | + ) |
| 102 | + sub = parser.add_subparsers(dest="command", required=True) |
| 103 | + |
| 104 | + p = sub.add_parser("gate", help="strict token gate: baseline vs rust output") |
| 105 | + p.add_argument("--model", default="glm-ocr") |
| 106 | + p.add_argument("--baseline", required=True, type=Path) |
| 107 | + p.add_argument("--rust", required=True, type=Path) |
| 108 | + p.add_argument("--output", type=Path) |
| 109 | + p.set_defaults(func=_run_gate) |
| 110 | + |
| 111 | + p = sub.add_parser("bench-python", help="run python benchmark for one model case") |
| 112 | + p.add_argument("--model", default="glm-ocr") |
| 113 | + p.add_argument("--model-dir", required=True, type=Path) |
| 114 | + p.add_argument("--image", required=True, type=Path) |
| 115 | + p.add_argument("--prompt", required=True) |
| 116 | + p.add_argument("--device", required=True, choices=["cpu", "mps"]) |
| 117 | + p.add_argument("--dtype", required=True, choices=["f32", "f16"]) |
| 118 | + p.add_argument("--max-new-tokens", type=int, default=64) |
| 119 | + p.add_argument("--output", required=True, type=Path) |
| 120 | + p.add_argument("--print-json", action="store_true") |
| 121 | + p.set_defaults(func=_run_bench_python) |
| 122 | + |
| 123 | + p = sub.add_parser("bench-rust", help="run rust benchmark for one model case") |
| 124 | + p.add_argument("--model", default="glm-ocr") |
| 125 | + p.add_argument("--cli", default=Path("target/release/deepseek-ocr-cli"), type=Path) |
| 126 | + p.add_argument("--image", required=True, type=Path) |
| 127 | + p.add_argument("--prompt", required=True) |
| 128 | + p.add_argument("--device", required=True, choices=["cpu", "metal"]) |
| 129 | + p.add_argument("--dtype", required=True, choices=["f32", "f16"]) |
| 130 | + p.add_argument("--max-new-tokens", type=int, required=True) |
| 131 | + p.add_argument("--output", required=True, type=Path) |
| 132 | + p.add_argument("--print-json", action="store_true") |
| 133 | + p.set_defaults(func=_run_bench_rust) |
| 134 | + |
| 135 | + p = sub.add_parser("perf", help="one-command run: py+rust compare with history") |
| 136 | + p.add_argument("--run", help="run id used under baselines/*/runs/<run>") |
| 137 | + p.add_argument("--tag", default="latest") |
| 138 | + p.add_argument("--include-models", nargs="*", default=[]) |
| 139 | + p.add_argument("--include-devices", nargs="*", default=[]) |
| 140 | + p.add_argument("--include-precision", nargs="*", default=[]) |
| 141 | + p.add_argument("--cli", default=Path("target/release/deepseek-ocr-cli"), type=Path) |
| 142 | + p.add_argument("--model-dir", type=Path) |
| 143 | + p.add_argument("--case-name") |
| 144 | + p.add_argument("--baseline-json", type=Path) |
| 145 | + p.add_argument("--matrix-source", type=Path) |
| 146 | + p.add_argument("--image", type=Path) |
| 147 | + p.add_argument("--prompt") |
| 148 | + p.add_argument("--max-new-tokens", type=int) |
| 149 | + p.add_argument("--cases", nargs="*") |
| 150 | + p.add_argument("--limit", type=int) |
| 151 | + p.add_argument("--output-root", type=Path) |
| 152 | + p.set_defaults(func=_run_perf) |
| 153 | + |
| 154 | + p = sub.add_parser("matrix-gate", help="one-command strict matrix gate run") |
| 155 | + p.add_argument("--run", help="run id used under baselines/*/runs/<run>") |
| 156 | + p.add_argument("--tag", default="latest") |
| 157 | + p.add_argument("--include-models", nargs="*", default=[]) |
| 158 | + p.add_argument("--include-devices", nargs="*", default=[]) |
| 159 | + p.add_argument("--include-precision", nargs="*", default=[]) |
| 160 | + p.add_argument("--cli", default=Path("target/release/deepseek-ocr-cli"), type=Path) |
| 161 | + p.add_argument("--model-dir", type=Path) |
| 162 | + p.add_argument("--source-matrix", type=Path) |
| 163 | + p.add_argument("--output-root", type=Path) |
| 164 | + p.add_argument("--case-name", default="adhoc") |
| 165 | + p.add_argument("--image", type=Path) |
| 166 | + p.add_argument("--prompt") |
| 167 | + p.add_argument("--max-new-tokens", type=int) |
| 168 | + p.add_argument("--cases", nargs="*") |
| 169 | + p.add_argument("--limit", type=int) |
| 170 | + p.set_defaults(func=_run_matrix_gate) |
| 171 | + |
| 172 | + return parser |
| 173 | + |
| 174 | + |
| 175 | +def main() -> int: |
| 176 | + parser = build_parser() |
| 177 | + args = parser.parse_args() |
| 178 | + return int(args.func(args)) |
| 179 | + |
| 180 | + |
| 181 | +if __name__ == "__main__": |
| 182 | + raise SystemExit(main()) |
0 commit comments