|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +import torch |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from engine.mcts import MCTS |
| 14 | + from game.board import AtaxxBoard |
| 15 | + |
| 16 | + |
| 17 | +def _ensure_src_on_path() -> None: |
| 18 | + root = Path(__file__).resolve().parents[1] |
| 19 | + src = root / "src" |
| 20 | + if str(src) not in sys.path: |
| 21 | + sys.path.insert(0, str(src)) |
| 22 | + |
| 23 | + |
| 24 | +def _parse_args() -> argparse.Namespace: |
| 25 | + parser = argparse.ArgumentParser( |
| 26 | + description="Run a short automated duel between two Ataxx checkpoints.", |
| 27 | + ) |
| 28 | + parser.add_argument("--checkpoint-a", required=True, help="Path to checkpoint A (.pt/.ckpt).") |
| 29 | + parser.add_argument("--checkpoint-b", required=True, help="Path to checkpoint B (.pt/.ckpt).") |
| 30 | + parser.add_argument("--games", type=int, default=8, help="Number of games to play.") |
| 31 | + parser.add_argument("--device", default="auto", choices=["auto", "cpu", "cuda"]) |
| 32 | + parser.add_argument("--mcts-sims", "--sims", type=int, default=96) |
| 33 | + parser.add_argument("--c-puct", type=float, default=1.5) |
| 34 | + parser.add_argument("--seed", type=int, default=42) |
| 35 | + parser.add_argument("--json", action="store_true", help="Print machine-readable JSON summary.") |
| 36 | + return parser.parse_args() |
| 37 | + |
| 38 | + |
| 39 | +def _resolve_device(device: str) -> str: |
| 40 | + if device == "auto": |
| 41 | + return "cuda" if torch.cuda.is_available() else "cpu" |
| 42 | + if device == "cuda" and not torch.cuda.is_available(): |
| 43 | + print("CUDA requested but not available; falling back to CPU.") |
| 44 | + return "cpu" |
| 45 | + return device |
| 46 | + |
| 47 | + |
| 48 | +def _pick_model_action_idx(board: AtaxxBoard, mcts: MCTS) -> int: |
| 49 | + probs = mcts.run(board=board, add_dirichlet_noise=False, temperature=0.0) |
| 50 | + return int(np.argmax(probs)) |
| 51 | + |
| 52 | + |
| 53 | +def main() -> None: |
| 54 | + args = _parse_args() |
| 55 | + _ensure_src_on_path() |
| 56 | + |
| 57 | + from engine.mcts import MCTS |
| 58 | + from game.actions import ACTION_SPACE |
| 59 | + from game.board import AtaxxBoard |
| 60 | + from inference.checkpoint_duel_runtime import ( |
| 61 | + build_match_schedule, |
| 62 | + load_system_from_checkpoint, |
| 63 | + summarize_match_results, |
| 64 | + ) |
| 65 | + |
| 66 | + checkpoint_a = Path(args.checkpoint_a) |
| 67 | + checkpoint_b = Path(args.checkpoint_b) |
| 68 | + if not checkpoint_a.exists(): |
| 69 | + raise FileNotFoundError(f"Checkpoint A not found: {checkpoint_a}") |
| 70 | + if not checkpoint_b.exists(): |
| 71 | + raise FileNotFoundError(f"Checkpoint B not found: {checkpoint_b}") |
| 72 | + |
| 73 | + device = _resolve_device(args.device) |
| 74 | + system_a = load_system_from_checkpoint(checkpoint_a, device=device) |
| 75 | + system_b = load_system_from_checkpoint(checkpoint_b, device=device) |
| 76 | + mcts_a = MCTS(model=system_a.model, c_puct=args.c_puct, n_simulations=args.mcts_sims, device=device) |
| 77 | + mcts_b = MCTS(model=system_b.model, c_puct=args.c_puct, n_simulations=args.mcts_sims, device=device) |
| 78 | + |
| 79 | + schedule = build_match_schedule(games=max(1, int(args.games))) |
| 80 | + rng = np.random.default_rng(seed=int(args.seed)) |
| 81 | + results: list[dict[str, int]] = [] |
| 82 | + |
| 83 | + for idx, (checkpoint_a_player, checkpoint_b_player) in enumerate(schedule, start=1): |
| 84 | + board = AtaxxBoard() |
| 85 | + turn_seed = int(rng.integers(0, 2**31 - 1)) |
| 86 | + torch.manual_seed(turn_seed) |
| 87 | + np.random.seed(turn_seed) |
| 88 | + turns = 0 |
| 89 | + while not board.is_game_over(): |
| 90 | + turns += 1 |
| 91 | + if board.current_player == checkpoint_a_player: |
| 92 | + action_idx = _pick_model_action_idx(board, mcts_a) |
| 93 | + elif board.current_player == checkpoint_b_player: |
| 94 | + action_idx = _pick_model_action_idx(board, mcts_b) |
| 95 | + else: |
| 96 | + raise RuntimeError("Unexpected player assignment while comparing checkpoints.") |
| 97 | + board.step(ACTION_SPACE.decode(action_idx)) |
| 98 | + |
| 99 | + winner = board.get_result() |
| 100 | + results.append( |
| 101 | + { |
| 102 | + "winner": int(winner), |
| 103 | + "turns": turns, |
| 104 | + "checkpoint_a_player": checkpoint_a_player, |
| 105 | + }, |
| 106 | + ) |
| 107 | + color_a = "p1" if checkpoint_a_player == 1 else "p2" |
| 108 | + print( |
| 109 | + f"[{idx}/{len(schedule)}] " |
| 110 | + f"checkpoint_a={color_a} winner={winner} turns={turns}", |
| 111 | + ) |
| 112 | + |
| 113 | + summary = summarize_match_results(results=results) |
| 114 | + output: dict[str, float | int | str] = { |
| 115 | + **summary, |
| 116 | + "checkpoint_a": str(checkpoint_a), |
| 117 | + "checkpoint_b": str(checkpoint_b), |
| 118 | + "device": device, |
| 119 | + "mcts_sims": int(args.mcts_sims), |
| 120 | + } |
| 121 | + |
| 122 | + if args.json: |
| 123 | + print(json.dumps(output, indent=2)) |
| 124 | + return |
| 125 | + |
| 126 | + print("") |
| 127 | + print("Summary") |
| 128 | + print(f" checkpoint_a: {checkpoint_a}") |
| 129 | + print(f" checkpoint_b: {checkpoint_b}") |
| 130 | + print(f" games: {summary['games']}") |
| 131 | + print(f" checkpoint_a_wins: {summary['checkpoint_a_wins']}") |
| 132 | + print(f" checkpoint_b_wins: {summary['checkpoint_b_wins']}") |
| 133 | + print(f" draws: {summary['draws']}") |
| 134 | + print(f" checkpoint_a_score: {float(summary['checkpoint_a_score']):.3f}") |
| 135 | + print(f" avg_turns: {float(summary['avg_turns']):.1f}") |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + main() |
0 commit comments