Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ A rewrite of the engine core and the training pipeline. See

### Changed

- Champions: a network also plays the champion when none has for `champion_every` generations
(default 8), not only when the rating ladder puts it above the champion. The ladder's error grows
along the chain of generations and missed a network 250 Elo stronger than the champion
- Engine split into packages: `internal/{game,sgf,nn,mcts,selfplay,arena,gtp}`; `cmd/gofer` is a
subcommand CLI (`gtp`, `play`, `selfplay`, `arena`, `bench`) replacing the v2 flag set
- Board with incremental chains and exact liberties: ~0.7 µs per move including legal-move generation;
Expand Down
7 changes: 7 additions & 0 deletions docs/training.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ Each generation:
(`champion_sprt`, default `0,50`) accepts H1. The first network rated at
least `champion_min_elo` becomes the first champion.

A network rated below the champion also plays it once no network has for
`champion_every` generations (default 8). The ratings cannot be trusted to
call for that match: every generation is rated through the chain of
40-game matches behind it, so over twenty generations the error is some
hundreds of Elo. In one run the last network beat the champion 97–23
directly after twenty generations in which none had been rated above it.

The SPRT counts opening pairs (each opening played with both colors) that
one side wins twice, in pair order, and runs Wald's test on them. Under the
usual logistic Elo model the chance a decisive pair is the challenger's is
Expand Down
8 changes: 7 additions & 1 deletion training/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class LoopConfig:
# champions: on anchor generations, a network rated above the champion plays
# it directly and takes over only if the match's SPRT accepts H1
champion_games: int = 200 # game limit (SPRT usually stops sooner); 0 disables champions
# ...and so does a network rated below it, once no network has played the
# champion for this many generations (0: never). The ladder cannot see slow
# progress: its error grows along the chain of generations
champion_every: int = 8
champion_visits: int = 200
champion_sprt: str = "0,50" # elo0,elo1: false promotion <= 5.3% when not stronger
champion_min_elo: float = 1500.0 # early generations (gen 0 = 0) never become champion
Expand Down Expand Up @@ -229,10 +233,12 @@ def champion(self, gen: int) -> None:
mine = ratings[name(new)]["elo"]
champ = self.state.get("champion")
theirs = ratings.get(name(champ["gen"]), {}).get("elo", champ["elo"]) if champ else None
if mine >= c.champion_min_elo and (champ is None or mine > theirs):
due = champ is not None and c.champion_every > 0 and new - champ.get("challenged", champ["gen"]) >= c.champion_every
if mine >= c.champion_min_elo and (champ is None or mine > theirs or due):
if champ is None:
self.promote(new, ratings, None)
else:
champ["challenged"] = new
r = self.match(f"nn:{self.model_path(new)}", f"nn:{self.model_path(champ['gen'])}", c.champion_games,
c.champion_visits, f"{name(new)}-vs-champion-{name(champ['gen'])}", ["-sprt", c.champion_sprt])
self.state.setdefault("results", []).append(
Expand Down
48 changes: 48 additions & 0 deletions training/tests/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,54 @@ def test_lower_rated_generation_does_not_play_the_champion(run, monkeypatch):
assert run.state["champion"]["gen"] == 2


def test_a_champion_is_challenged_on_a_schedule_whatever_the_ratings_say(run, monkeypatch):
# Seen in a real run: the last network beat the champion 97-23 in a direct
# match, but the ladder had rated every generation after the champion below
# it for 20 generations, so none of them ever played it.
run.state["champion"] = {"gen": 2, "elo": 1200, "tag": "model-9x9-g0002"}
for g in range(5, 21):
run.model_path(g).write_bytes(f"model {g}".encode())
run.state["done"][str(g)] = {"selfplay": {"rows": 100, "seconds": 1.0}}
played = []
result = {"wins": 45, "losses": 55, "draws": 0, "elo": -35, "elo_ci_low": -90, "elo_ci_high": 20, "sprt": "H0"}

def match(a, b, games, visits, tag, extra=None):
played.append(tag)
return result

monkeypatch.setattr(run, "match", match)
below = lambda new: set_ratings(run, {0: 0, 2: 1200, new: 1100})

for new in (4, 6, 8): # fewer than champion_every (8) generations after the champion
below(new)
run.champion(new - 1)
assert played == []
below(10)
run.champion(9) # eight generations after it: plays although rated below
assert played == ["gen-0010-vs-champion-gen-0002"] and run.state["champion"]["gen"] == 2
below(12)
run.champion(11) # the champion was challenged two generations ago
assert len(played) == 1
result = {"wins": 97, "losses": 23, "draws": 0, "elo": 250, "elo_ci_low": 172, "elo_ci_high": 328, "sprt": "H1"}
below(18)
run.champion(17) # eight generations after the last challenge
assert played[-1] == "gen-0018-vs-champion-gen-0002" and run.state["champion"]["gen"] == 18
below(20)
run.champion(19) # the new champion's own count starts at its generation
assert len(played) == 2


def test_champion_every_zero_leaves_challenges_to_the_ratings(tmp_path, monkeypatch):
monkeypatch.setenv("ONNXRUNTIME_SHARED_LIBRARY_PATH", "/dev/null")
r = Run(tmp_path / "9x9", LoopConfig(champion_min_elo=1000, champion_every=0), Path("gofer"), threads=1)
monkeypatch.setattr(r, "log", lambda msg: None)
monkeypatch.setattr(r, "match", lambda *a, **k: pytest.fail("no match expected"))
r.state["champion"] = {"gen": 2, "elo": 1200, "tag": "model-9x9-g0002"}
set_ratings(r, {0: 0, 2: 1200, 40: 1100})
r.champion(39)
assert r.state["champion"]["gen"] == 2


def test_publish_newest_pending_with_stable_asset_name(run, tmp_path):
set_ratings(run, {0: 0, 2: 1200, 4: 1300})
run.promote(2, json.loads((run.root / "eval" / "ratings.json").read_text()), None)
Expand Down
Loading