File size: 3,752 Bytes
7722921 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | """blunder_test.py <net> [sims] [tag]: does a net REPEAT the blunders of our earlier nets? (box side)
Positions = dag6_test.json (mkroots.py): positions of held-out games (fold != 0) where one of our nets lost >= 0.07 win probability
with its move (Stockfish judge). The net searches each position (gmcts3, `sims` simulations, fresh tree, the settings of play:
TensorRT fp16 for our nets / Lc0 nets unless GM3_TRT=0), then Stockfish (150k nodes, 1 thread, NPROC processes) judges the move
it chose: loss = win probability of the position before - after, from the mover's side. Prints and uploads (HF results/blunder/):
repeat rate (loss >= 0.07), serious rate (>= 0.15), mean loss, and the list of chosen moves.
net = the ckpt_<net>.pt name (dist4, dist6, t3, lc0, ...); GM3_TRT=0 runs our net in torch bf16 instead of TensorRT fp16.
Run it for dist4 (baseline), dist6 and t3 (reference); fp16 vs bf16: the same net with GM3_TRT=1 and GM3_TRT=0."""
import json, math, os, sys, time
from multiprocessing import Pool
import chess
import chess.engine
os.environ.setdefault("LC0_TRT", "1")
os.environ.setdefault("GM3_TRT", "1")
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
net = sys.argv[1]
sims = int(sys.argv[2]) if len(sys.argv) > 2 else 20000
tag = sys.argv[3] if len(sys.argv) > 3 else ""
NODES, NPROC = 150000, int(os.environ.get("NPROC", 14))
ENG = None
def wpf(cp):
return 1 / (1 + math.exp(-0.00368208 * max(-1500, min(1500, cp))))
def init():
global ENG
ENG = chess.engine.SimpleEngine.popen_uci("/marimo/chess/sf")
ENG.configure({"Threads": 1, "Hash": 64})
def wp_mover(b, mover):
if b.is_game_over():
return {"1-0": 1.0, "0-1": 0.0}.get(b.result(), 0.5) if mover == chess.WHITE else {"1-0": 0.0, "0-1": 1.0}.get(b.result(), 0.5)
w = wpf(ENG.analyse(b, chess.engine.Limit(nodes=NODES))["score"].white().score(mate_score=100000))
return w if mover == chess.WHITE else 1 - w
def judge(a):
fen, uci = a
b = chess.Board(fen)
before = wp_mover(b, b.turn)
mover = b.turn
b.push_uci(uci)
return before, wp_mover(b, mover)
if __name__ == "__main__":
import gmcts3
pos = json.load(open("/marimo/chess/dag6/dag6_test.json"))
g = gmcts3.GM3(f"/marimo/chess/ckpt_{net}.pt", B=256, K=128, cap=200_000)
t0, chosen = time.time(), []
for p in pos:
b = chess.Board(p["fen"])
g.tree, g.n, g.ne = None, 1, 0
m, _ = g.think(b, 1e9, max_sims=sims)
chosen.append((p["fen"], m.uci()))
print(f"{net}: searched {len(pos)} positions in {time.time() - t0:.0f} s (TRT {os.environ['GM3_TRT']})", flush=True)
with Pool(NPROC, initializer=init) as pool:
res = pool.map(judge, chosen, chunksize=1)
loss = [max(0.0, b - a) for b, a in res]
n = len(loss)
out = {"net": net, "sims": sims, "trt": os.environ["GM3_TRT"], "n": n, "repeat": sum(x >= 0.07 for x in loss) / n,
"serious": sum(x >= 0.15 for x in loss) / n, "mean_loss": sum(loss) / n,
"moves": [[p["g"], p["ply"], p["loss"], c[1], round(x, 3)] for p, c, x in zip(pos, chosen, loss)]}
print(f"BLUNDER_TEST {net} sims {sims} trt {os.environ['GM3_TRT']}: n={n} repeated (>=0.07) {out['repeat']:.2f}, serious (>=0.15) "
f"{out['serious']:.2f}, mean loss {100 * out['mean_loss']:.1f} pts (originals: mean {100 * sum(p['loss'] for p in pos) / n:.1f})")
fn = f"/tmp/blunder_{net}{tag}_trt{os.environ['GM3_TRT']}.json"
json.dump(out, open(fn, "w"))
from huggingface_hub import HfApi
HfApi().upload_file(path_or_fileobj=fn, path_in_repo=f"results/blunder/{os.path.basename(fn)}", repo_id="Asilarkness/chess-nets",
commit_message=f"blunder test {net}{tag}")
print("BLUNDER_DONE")
|