Repository navigation
Self-play tuning #350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Self-play tuning #350
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6bdb96d
Self-play tuning
alejandromarcu a7fd64d
nits
alejandromarcu 68c8a2f
show lines if it didn't find the durations, useful also for errors
alejandromarcu 5ef6f5c
switched to --overides, include training file
alejandromarcu 2ce878e
missed a files
alejandromarcu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import argparse | ||
| import re | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import time | ||
| from pathlib import Path | ||
|
|
||
| SELFPLAY_RE = re.compile(r"selfplay finished in ([\d.]+)") | ||
|
|
||
|
|
||
| def parse_selfplay_line(line): | ||
| """Parse a self-play round completion line. | ||
|
|
||
| Returns (elapsed_seconds, num_games) or None if the line doesn't match. | ||
| """ | ||
| m = SELFPLAY_RE.search(line) | ||
| if not m: | ||
| return None | ||
|
|
||
| elapsed = float(m.group(1)) | ||
|
|
||
| return elapsed | ||
|
|
||
|
|
||
| def run_single_benchmark(config_file, num_workers, parallel_games, duration, runs_dir, extra_overrides): | ||
| run_id = f"bench-w{num_workers}-g{parallel_games}-{int(time.time())}" | ||
|
|
||
| overrides = [ | ||
| f"run_id={run_id}", | ||
| f"self_play.num_workers={num_workers}", | ||
| f"self_play.parallel_games={parallel_games}", | ||
| f"training.finish_after={duration}", | ||
| "benchmarks=[]", | ||
| "wandb=None", | ||
| ] + extra_overrides | ||
|
|
||
| cmd = [sys.executable, "train_v2.py", config_file, "-o"] + overrides | ||
| if runs_dir: | ||
| cmd.insert(3, "--runs-dir") | ||
| cmd.insert(4, runs_dir) | ||
|
|
||
| src_dir = Path(__file__).parent | ||
|
|
||
| wall_start = time.time() | ||
| proc = subprocess.Popen( | ||
| cmd, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.STDOUT, | ||
| text=True, | ||
| cwd=str(src_dir), | ||
| ) | ||
| durations = [] | ||
| for line in proc.stdout: | ||
| line = line.rstrip() | ||
| parsed = parse_selfplay_line(line) | ||
| if parsed: | ||
| print(f" {line}") | ||
| durations.append(parsed) | ||
|
|
||
| proc.wait() | ||
| wall_elapsed = time.time() - wall_start | ||
|
|
||
| # Cleanup run directory | ||
| effective_runs_dir = runs_dir or str(src_dir.parent) | ||
| run_dir = Path(effective_runs_dir) / "runs" / run_id | ||
| if run_dir.exists(): | ||
| shutil.rmtree(run_dir) | ||
|
|
||
| return compute_metrics(num_workers, parallel_games, durations, wall_elapsed) | ||
|
|
||
|
|
||
| def compute_metrics(num_workers, parallel_games, durations, wall_elapsed): | ||
| if not durations: | ||
| return { | ||
| "num_workers": num_workers, | ||
| "parallel_games": parallel_games, | ||
| "total_rounds": 0, | ||
| "total_games": 0, | ||
| "avg_round_time": float("nan"), | ||
| "avg_throughput": 0.0, | ||
| } | ||
|
|
||
| total_rounds = len(durations) | ||
| total_worker_time = sum(durations) | ||
| total_games = parallel_games * total_rounds | ||
| avg_throughput = num_workers * total_games / total_worker_time | ||
|
|
||
| return { | ||
| "num_workers": num_workers, | ||
| "parallel_games": parallel_games, | ||
| "total_rounds": total_rounds, | ||
| "total_games": total_games, | ||
| "avg_round_time": total_worker_time / total_rounds, | ||
| "avg_throughput": avg_throughput, | ||
| } | ||
|
|
||
|
|
||
| def print_results_table(results): | ||
| print(f"\n{'=' * 80}") | ||
| print("BENCHMARK RESULTS") | ||
| print(f"{'=' * 80}") | ||
|
|
||
| header = f"{'nw':>10} {'pg':>10} {'rounds':>10} {'tot games':>10} {'avg round t':>14} {'games/s':>14}" | ||
| print(header) | ||
| print("-" * len(header)) | ||
|
|
||
| for r in sorted(results, key=lambda x: x["avg_throughput"], reverse=True): | ||
| print( | ||
| f"{r['num_workers']:>10} " | ||
| f"{r['parallel_games']:>10} " | ||
| f"{r['total_rounds']:>10} " | ||
| f"{r['total_games']:>10} " | ||
| f"{r['avg_round_time']:>14.2f} " | ||
| f"{r['avg_throughput']:>14.3f} " | ||
| ) | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description="Benchmark self-play throughput across configurations") | ||
| parser.add_argument("config_file", type=str, help="Base config YAML file") | ||
| parser.add_argument("--workers", type=str, help="Comma-separated num_workers values") | ||
| parser.add_argument("--games", type=str, help="Comma-separated parallel_games values") | ||
| parser.add_argument("--duration", type=str, default="2 minutes", help="Duration per combo (default: '2 minutes')") | ||
| parser.add_argument("--runs-dir", type=str, default=None, help="Directory for runs") | ||
| parser.add_argument("--extra-overrides", nargs="*", default=[], help="Additional config overrides for train_v2.py") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't this the same as |
||
| args = parser.parse_args() | ||
|
|
||
| workers_list = [int(x) for x in args.workers.split(",")] | ||
| games_list = [int(x) for x in args.games.split(",")] | ||
| combos = [(w, g) for w in workers_list for g in games_list] | ||
|
|
||
| print(f"Benchmarking {len(combos)} configurations, {args.duration} each") | ||
| print(f" Workers: {workers_list}") | ||
| print(f" Parallel games: {games_list}") | ||
|
|
||
| results = [] | ||
| for i, (num_workers, parallel_games) in enumerate(combos): | ||
| print(f"\n{'=' * 60}") | ||
| print(f"[{i + 1}/{len(combos)}] num_workers={num_workers}, parallel_games={parallel_games}") | ||
| print(f"{'=' * 60}") | ||
|
|
||
| result = run_single_benchmark( | ||
| config_file=args.config_file, | ||
| num_workers=num_workers, | ||
| parallel_games=parallel_games, | ||
| duration=args.duration, | ||
| runs_dir=args.runs_dir, | ||
| extra_overrides=args.extra_overrides, | ||
| ) | ||
| results.append(result) | ||
|
|
||
| print_results_table(results) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this to load_config_and_setup_run for clarity