summaryrefslogtreecommitdiff
path: root/src/eval_tune
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2026-09-18 09:43:17 -0700
committerScott Gasch <[email protected]>2026-09-18 09:43:17 -0700
commitf698c02de213d0ed8968fb7cca5f70df51fa0763 (patch)
tree080d6443fbe62d71de092f9acde61da387dff408 /src/eval_tune
parent4a7ec9ac91939957fb3d3e5564fc24a385b422ff (diff)
Redesign lazy/super-lazy eval margins with position-specific percentile
tables, and fix four real search/recognizer bugs found while validating it Main theme: replace the flat, heavily-padded LAZY_EVAL_BASE_MARGIN / SUPER_LAZY_MARGIN_BY_ARMY constants with per-material-bucket percentile tables (king safety by attacker army, residual by own army, separate tables for the lazy vs super-lazy baselines) derived from a 30k-position calibration pool, targeting a measured ~1-1.5% exceeded-margin rate instead of an unmeasured "add lots of headroom" floor. QSearch's FUTILITY_BASE_MARGIN_LAZY/SUPERLAZY were re-derived to match the new, now-meaningful (p50-based) iPositional this produces. Self-play confirms the working tree beats both HEAD, a more liberal (~5% miss target) variant, and a more conservative (near-zero miss) variant of the same margin mechanism. Along the way, an initial self-play regression (this margin work bundled with several changes in one pass, one of which broke things) led to a day of ZFS-snapshot bisection and then targeted DEBUG-build fuzzing against a stratified 200k-position corpus (tests/cross_sectional_200k.ep_, new), which is what actually found and fixed four distinct, real, pre-existing engine bugs -- none of them related to the margin redesign itself: - searchsup.c (CommonSearchInit): three early-return paths (timer abort, draw, too-deep bailout) left a node's own PV[] row stale instead of clearing it, letting an ancestor's UpdatePV splice a leftover move from a completely unrelated node into the displayed PV -- root cause of a WalkPV/MakeMove assert ("moving a piece to capture, but nothing was on the target square"). - dynamic.c (RecordEnprisePieceAtPly): the two-slot en-prise dedup compared (square, piece) instead of square alone, so a same-square re-record with a different piece (legitimate -- a fail-high capture's victim is restored by UnmakeMove and can differ across calls at the same ply) shifted a duplicate square into both slots, tripping ValueOfMaterialInTroubleDespiteMove's two-distinct-squares assert. - recogn.c (_RecognizeKBKP): the wrong-colored-bishop corner-fortress draw shortcut fired even when the weak side has its own pawn(s), which can hand the strong side real zugzwang resources the fortress logic doesn't account for -- produced a false hard draw bound where EGTB shows a near-forced loss. - eval.c (_EvalPasserRacesAgainstLoneKings): a rook pawn's race was credited as winning even when the strong side's own king sits exactly on its own pawn's queening square (distance 0 specifically -- adjacent is fine, the square is empty) -- a classic stalemate resource the race heuristic can't see. Only reachable through _RecognizeKPK's pawn-hash-cache-hit path, which is why testrecogn.c's TestRecognExhaustiveKPK reported "0 wrong" the whole time; extended it to cover both color assignments and the full board (no mirror-shortcut) and documented the remaining cache-hit coverage gap for a future session to close. Also: match_play.py gains a local_ebf metric (geometric-mean of depth-normalized node-growth ratios between consecutive completed iterations) alongside the existing cumulative nodes**(1/depth) ebf, which turned out to be misleading when comparing engines with different lazy-exit aggressiveness -- and probe.c/hash.c pick up two small, independently-found correctness fixes (TB root-probe promotion piece missing its color bit; ASSERT(g_pHashTable) crashing on the legitimate "--hash none" config) noticed along the way. Co-Authored-By: Claude Sonnet 5 <[email protected]>
Diffstat (limited to 'src/eval_tune')
-rw-r--r--src/eval_tune/build_curated_texel_pool.py90
-rw-r--r--src/eval_tune/compare_king_safety_crafty.py112
-rw-r--r--src/eval_tune/fit_king_safety_logistic_material.py147
-rw-r--r--src/eval_tune/fit_king_safety_ridge.py202
-rw-r--r--src/eval_tune/lazy_eval_margins/README.md194
-rw-r--r--src/eval_tune/lazy_eval_margins/aggregate_marginsafety_shards.py92
-rw-r--r--src/eval_tune/lazy_eval_margins/hist_percentile.py86
-rw-r--r--src/eval_tune/lazy_eval_margins/residual_raw_histograms_guess2_30k.txt26
-rwxr-xr-xsrc/eval_tune/match_play.py102
-rw-r--r--src/eval_tune/sample_eval_calibration_positions.py182
-rwxr-xr-xsrc/eval_tune/test_vs_head.sh4
11 files changed, 1231 insertions, 6 deletions
diff --git a/src/eval_tune/build_curated_texel_pool.py b/src/eval_tune/build_curated_texel_pool.py
new file mode 100644
index 0000000..86f61b8
--- /dev/null
+++ b/src/eval_tune/build_curated_texel_pool.py
@@ -0,0 +1,90 @@
+#!/usr/bin/env python3
+"""Turn a match_controls.py-matched king-safety CSV (mate/control pairs,
+fen retained -- see extract_king_features.py's 2026-09-15 fen-retention
+fix) into a Texel-ready (fen, result) pool: runs the same qsearch-
+agreement quiet filter used for the generic TWIC pool (this data was
+NEVER filtered for quietness at mining time by design -- see mine_king_
+mates.py's is_quiet_position, added 2026-09-15 -- so this step matters
+here at least as much as it does for TWIC, arguably more, since "N plies
+before a real mate" specifically targets the moment before a real attack
+lands, exactly where a quiet-by-SEE-but-not-by-search position is most
+likely).
+
+Result-label framing (a real modeling choice, not a literal game
+result): label==1 rows are "this color's king got mated in this game
+from here" -- treated as a loss (0.0) for that color. label==0 rows are
+the matched sound/control side -- treated as a win (1.0) for that color.
+Output is always from WHITE's POV (matching eval_white_pov_batch's own
+convention), so a row is flipped if mated_color_or_sampled_color is
+black. This is an approximation (these aren't actual final game
+results, just "survived this specific king-safety episode or not") --
+flagged explicitly rather than silently treated as equivalent to a real
+Texel outcome label.
+"""
+import argparse
+import csv
+import sys
+
+from tune_eval_dna import Engine, filter_by_qsearch_agreement
+
+
+def main():
+ ap = argparse.ArgumentParser()
+ ap.add_argument("matched_csv")
+ ap.add_argument("out_tsv")
+ ap.add_argument("--engine", default="../typhoon")
+ ap.add_argument("--qsearch-max-diff", type=int, default=30)
+ ap.add_argument("--dna-scratch-dir", default="/tmp/typhoon_curated_pool")
+ args = ap.parse_args()
+
+ with open(args.matched_csv, newline="") as f:
+ rows = list(csv.DictReader(f))
+ print(f"Loaded {len(rows)} matched rows from {args.matched_csv}",
+ file=sys.stderr)
+
+ fen_to_row = {}
+ for r in rows:
+ fen_to_row[r["fen"]] = r
+ if len(fen_to_row) != len(rows):
+ print(f"WARNING: {len(rows) - len(fen_to_row)} duplicate FENs "
+ f"collapsed -- matched.csv shouldn't have dupes normally, "
+ f"investigate if this is large.", file=sys.stderr)
+
+ # (fen, result) pairs for the quiet-filter -- result value here is a
+ # placeholder (unused by filter_by_qsearch_agreement, which only
+ # reads/returns fens), real result labels are attached after.
+ placeholder_positions = [(fen, 0.0) for fen in fen_to_row]
+
+ with Engine(args.engine, args.dna_scratch_dir) as engine:
+ kept = filter_by_qsearch_agreement(
+ engine, placeholder_positions, max_diff_cp=args.qsearch_max_diff
+ )
+
+ n_flipped = 0
+ out_rows = []
+ for fen, _ in kept:
+ r = fen_to_row[fen]
+ label = int(r["label"])
+ color = r["mated_color_or_sampled_color"]
+ # label==1 (mate) -> loss (0.0) for that color; label==0
+ # (control/sound) -> win (1.0) for that color.
+ result_for_color = 0.0 if label == 1 else 1.0
+ if color == "black":
+ result_white_pov = 1.0 - result_for_color
+ n_flipped += 1
+ else:
+ result_white_pov = result_for_color
+ out_rows.append((fen, result_white_pov))
+
+ with open(args.out_tsv, "w") as f:
+ for fen, result in out_rows:
+ f.write(f"{fen}\t{result}\n")
+
+ print(f"Quiet-filter kept {len(kept)}/{len(fen_to_row)} "
+ f"({len(kept)/len(fen_to_row):.1%})", file=sys.stderr)
+ print(f"Wrote {len(out_rows)} (fen, result) rows to {args.out_tsv} "
+ f"({n_flipped} POV-flipped from black)", file=sys.stderr)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/src/eval_tune/compare_king_safety_crafty.py b/src/eval_tune/compare_king_safety_crafty.py
new file mode 100644
index 0000000..a018fc7
--- /dev/null
+++ b/src/eval_tune/compare_king_safety_crafty.py
@@ -0,0 +1,112 @@
+#!/usr/bin/env python3
+"""Empirical magnitude sanity check: for the same ~500 curated near-mate
+"quiet" positions used to derive the king-safety ring-defect features
+(see mine_king_mates.py / matched_combined_full.csv), compare typhoon's
+own king-safety subscore (iSafety, dumped via KING_DEFECT_DUMP's new
+'realsafety' field -- see chess.h/eval.c/command.c 2026-09-15 additions)
+against Crafty's "kings" row from its `score` command breakdown, for the
+side whose king safety is actually being assessed in each row.
+
+Crafty is spawned fresh (one process per FEN, via `setboard <fen>;
+score; quit`) rather than batched in one long-lived process -- testing
+showed a second setboard+score call inside one Crafty session silently
+reused the FIRST position's cached score, not a real re-evaluation, so
+batching would have silently produced 500 copies of the same number.
+"""
+import csv
+import re
+import subprocess
+import sys
+
+CRAFTY_BIN = "/home/scott/crafty/crafty"
+CRAFTY_CWD = "/home/scott/crafty"
+TYPHOON_KINGDUMP = "/usr/home/scott/typhoon/src/typhoon_kingdump"
+IN_CSV = "/tmp/typhoon/near_mate_500.csv"
+OUT_CSV = "/tmp/typhoon/king_safety_crafty_compare.csv"
+
+KINGS_ROW_RE = re.compile(
+ r"kings\.+\s+([-\d.]+)\s+\|\s+([-\d.]+)\s+([-\d.]+)\s+([-\d.]+)\s+\|\s+"
+ r"([-\d.]+)\s+([-\d.]+)\s+([-\d.]+)\s+\|"
+)
+
+
+def crafty_kings_comp(fen, color):
+ """Returns Crafty's 'kings' row 'comp' column (in pawns) for `color`."""
+ script = f"setboard {fen}\nscore\nquit\n"
+ p = subprocess.run([CRAFTY_BIN], input=script, cwd=CRAFTY_CWD,
+ capture_output=True, text=True, timeout=30)
+ m = KINGS_ROW_RE.search(p.stdout)
+ if not m:
+ return None
+ white_comp, black_comp = float(m.group(1)), float(m.group(5))
+ return white_comp if color == "white" else black_comp
+
+
+def load_rows():
+ with open(IN_CSV, newline="") as f:
+ return list(csv.DictReader(f))
+
+
+def typhoon_realsafety_batch(rows):
+ """Batches all FENs through one typhoon_kingdump `script` call (this
+ engine's setboard/eval IS known to work correctly across repeated
+ calls in one session -- see extract_king_features.py's run_shard)."""
+ script_path = "/tmp/typhoon/compare_batch.script"
+ out_path = "/tmp/typhoon/compare_batch.out"
+ with open(script_path, "w") as f:
+ f.write("force\nnopost\n")
+ for r in rows:
+ f.write(f"setboard {r['fen']}\n")
+ f.write("evalkingdump\n")
+ cmd = [TYPHOON_KINGDUMP, "--batch", "--command", f"script {script_path}"]
+ with open(out_path, "w") as logf:
+ subprocess.run(cmd, stdout=logf, stderr=subprocess.STDOUT, timeout=300)
+ dumps = []
+ with open(out_path) as f:
+ for line in f:
+ if line.startswith("KINGDEFECTDUMP"):
+ color = re.search(r"color=(\S+)", line).group(1)
+ realsafety = int(re.search(r"realsafety=(-?\d+)", line).group(1))
+ dumps.append((color, realsafety))
+ if len(dumps) != 2 * len(rows):
+ raise RuntimeError(f"expected {2*len(rows)} dump lines, got {len(dumps)}")
+ out = []
+ for i, r in enumerate(rows):
+ black_color, black_rs = dumps[2 * i]
+ white_color, white_rs = dumps[2 * i + 1]
+ assert black_color == "black" and white_color == "white"
+ out.append(black_rs if r["mated_color_or_sampled_color"] == "black" else white_rs)
+ return out
+
+
+def main():
+ rows = load_rows()
+ print(f"Loaded {len(rows)} rows", file=sys.stderr)
+
+ typhoon_vals = typhoon_realsafety_batch(rows)
+ print(f"typhoon: got {len(typhoon_vals)} realsafety values", file=sys.stderr)
+
+ results = []
+ for i, r in enumerate(rows):
+ crafty_comp = crafty_kings_comp(r["fen"], r["mated_color_or_sampled_color"])
+ if crafty_comp is None:
+ continue
+ results.append({
+ "fen": r["fen"],
+ "color": r["mated_color_or_sampled_color"],
+ "typhoon_realsafety_cp": typhoon_vals[i],
+ "crafty_kings_comp_cp": round(crafty_comp * 100, 1),
+ })
+ if (i + 1) % 50 == 0:
+ print(f" ...{i+1}/{len(rows)} crafty positions done", file=sys.stderr)
+
+ with open(OUT_CSV, "w", newline="") as f:
+ w = csv.DictWriter(f, fieldnames=["fen", "color", "typhoon_realsafety_cp",
+ "crafty_kings_comp_cp"])
+ w.writeheader()
+ w.writerows(results)
+ print(f"Wrote {len(results)} rows to {OUT_CSV}", file=sys.stderr)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/src/eval_tune/fit_king_safety_logistic_material.py b/src/eval_tune/fit_king_safety_logistic_material.py
new file mode 100644
index 0000000..6f364f3
--- /dev/null
+++ b/src/eval_tune/fit_king_safety_logistic_material.py
@@ -0,0 +1,147 @@
+#!/usr/bin/env python3
+"""Logistic-regression calibration of king-safety ring-defect terms
+against material balance as an external, non-circular yardstick.
+
+Why material balance and not the engine's own iSafety: iSafety is
+mechanically a weighted sum of (a subset of) these same ring-defect
+features under the CURRENT baseline weights -- using it as a covariate
+regresses the model against itself, collinear by construction, not just
+"maybe wrong". Material balance is computed independently, straight from
+the FEN via python-chess with standard piece values -- it has nothing to
+do with king-ring geometry and is anchored to a genuinely external,
+centuries-old convention (pawn=100, knight/bishop=~320, rook=500,
+queen=900), the same scale eval.c's own PIECE_VALUE table already uses
+elsewhere in the engine, unrelated to the king-safety subsystem.
+
+Logistic (not linear) regression on the same curated mate/control label,
+material_balance included as one covariate: its fitted coefficient tells
+you how many logit-units one pawn (100cp) is worth in this exact
+dataset. Divide every king-safety feature's coefficient by that, times
+100, and you get that feature's danger contribution in real centipawns,
+calibrated against an external ruler -- no Texel, no single hand-picked
+anchor constant.
+"""
+import csv
+import sys
+import chess
+import numpy as np
+from sklearn.linear_model import LogisticRegression
+
+sys.path.insert(0, "/usr/home/scott/typhoon/src/eval_tune")
+from fit_king_safety_ridge import tier_slots
+
+IN_CSV = sys.argv[1] if len(sys.argv) > 1 else "/tmp/typhoon/matched_combined_full.csv"
+
+PIECE_VALUES = {
+ chess.PAWN: 1.0,
+ chess.KNIGHT: 3.2,
+ chess.BISHOP: 3.3,
+ chess.ROOK: 5.0,
+ chess.QUEEN: 9.0,
+ chess.KING: 0.0,
+}
+
+NONPAWN_PIECES = ("minor", "rook", "queen")
+
+
+def material_balance_pawns(fen, color):
+ """Material balance in pawns, from `color`'s point of view."""
+ board = chess.Board(fen)
+ total = {chess.WHITE: 0.0, chess.BLACK: 0.0}
+ for piece_type, value in PIECE_VALUES.items():
+ for c in (chess.WHITE, chess.BLACK):
+ total[c] += value * len(board.pieces(piece_type, c))
+ my_color = chess.WHITE if color == "white" else chess.BLACK
+ other = chess.BLACK if my_color == chess.WHITE else chess.WHITE
+ return total[my_color] - total[other]
+
+
+def load_rows(path):
+ with open(path, newline="") as f:
+ return list(csv.DictReader(f))
+
+
+def build_features(rows_in):
+ feat_rows = []
+ y = []
+ col_names = None
+ skipped = 0
+ for r in rows_in:
+ color = r["mated_color_or_sampled_color"]
+
+ def offboard(s):
+ return int(r.get(f"sq{s}_offboard", 0) or 0) != 0
+
+ feat = {}
+ for tier in ("front", "back", "side"):
+ slots = tier_slots(color, tier)
+ for p in NONPAWN_PIECES:
+ feat[f"{tier}_atk_{p}"] = sum(
+ int(r[f"sq{s}_atk_{p}"]) for s in slots if not offboard(s)
+ )
+ feat[f"{tier}_undef"] = sum(
+ int(r[f"sq{s}_undef"]) for s in slots if not offboard(s)
+ )
+ feat["ring_atk_pawn"] = 0
+ feat["ring_xray"] = 0
+ for tier in ("front", "back", "side"):
+ slots = tier_slots(color, tier)
+ feat["ring_atk_pawn"] += sum(
+ int(r[f"sq{s}_atk_pawn"]) for s in slots if not offboard(s)
+ )
+ feat["ring_xray"] += sum(
+ int(r[f"sq{s}_xray"]) for s in slots if not offboard(s)
+ )
+ feat["front_flight"] = sum(
+ int(r[f"sq{s}_flight"]) for s in tier_slots(color, "front")
+ if not offboard(s)
+ )
+ feat["back_flight"] = sum(
+ int(r[f"sq{s}_flight"]) for s in tier_slots(color, "back")
+ if not offboard(s)
+ )
+ try:
+ feat["material_balance"] = material_balance_pawns(r["fen"], color)
+ except Exception:
+ skipped += 1
+ continue
+ if col_names is None:
+ col_names = list(feat.keys())
+ feat_rows.append([feat[c] for c in col_names])
+ y.append(int(r["label"]))
+ if skipped:
+ print(f"skipped {skipped} rows with unparseable FENs", file=sys.stderr)
+ return np.array(feat_rows, dtype=float), np.array(y, dtype=int), col_names
+
+
+def main():
+ rows_in = load_rows(IN_CSV)
+ print(f"Loaded {len(rows_in)} rows from {IN_CSV}")
+ X, y, col_names = build_features(rows_in)
+ print(f"Built {X.shape[1]} features (incl. material_balance): {col_names}")
+
+ C = float(sys.argv[2]) if len(sys.argv) > 2 else 0.05
+ m = LogisticRegression(penalty="l1", solver="liblinear", C=C, max_iter=5000)
+ m.fit(X, y)
+ coef = m.coef_[0]
+
+ mat_idx = col_names.index("material_balance")
+ beta_material = coef[mat_idx]
+ print(f"\nC={C} intercept={m.intercept_[0]:.4f}")
+ print(f"beta_material (logit per pawn of material) = {beta_material:.5f}")
+ if abs(beta_material) < 1e-6:
+ print("material_balance coefficient ~0 -- can't calibrate, "
+ "try a larger C (less regularization).")
+ return
+
+ print(f"\n{'feature':20s} {'beta (logit/unit)':>18s} {'cp (calibrated)':>16s}")
+ for name, b in zip(col_names, coef):
+ if name == "material_balance":
+ continue
+ cp = 100.0 * b / beta_material
+ flag = "" if abs(b) > 1e-9 else " (zeroed by L1)"
+ print(f"{name:20s} {b:18.5f} {cp:16.2f}{flag}")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/src/eval_tune/fit_king_safety_ridge.py b/src/eval_tune/fit_king_safety_ridge.py
new file mode 100644
index 0000000..072c972
--- /dev/null
+++ b/src/eval_tune/fit_king_safety_ridge.py
@@ -0,0 +1,202 @@
+#!/usr/bin/env python3
+"""Sanity-check alternative to Texel for the king-safety terms: fit Ridge
+and ElasticNet regression directly against the mate/control label (1=mated
+from this position, 0=survived-to-a-real-draw control), using the same
+matched_combined_small.csv the curated-Texel run consumed (see
+build_curated_texel_pool.py / eval_tune's 2026-09-15 king-safety session).
+
+Unlike Texel, this fits a real regularized linear model in one shot (no
+coordinate descent over a live engine), so it can't reproduce Texel's
+sign-flip pathology from correlated coordinate updates stepping on each
+other -- if Ridge/ElasticNet still flip signs on physically-obvious terms
+(e.g. an undefended ring square should never be a *bonus*), that is strong
+evidence the flips are a real property of the feature collinearity, not an
+artifact of Texel's optimizer.
+
+Features are built to mirror the actual _EvalKing ring-tier terms as
+closely as the KING_DEFECT_DUMP per-square dump allows: slots 0-2 and 5-7
+swap front/back meaning by color (see data.c's InitializeKingAttackTables
+iFrontDeltas/iBackDeltas -- white front = deltas -17/-16/-15, black front =
++15/+16/+17), slots 3-4 are always the side squares. Each row's dump was
+already collapsed to the mated/sampled color's own perspective (see
+extract_king_features.py), so slot->tier mapping only depends on that row's
+color column, not board color.
+"""
+import csv
+import sys
+import numpy as np
+from sklearn.linear_model import Ridge, ElasticNet
+from sklearn.model_selection import train_test_split
+from sklearn.preprocessing import StandardScaler
+
+IN_CSV = sys.argv[1] if len(sys.argv) > 1 else "/tmp/typhoon/matched_combined_small.csv"
+
+PIECE_TYPES = ("pawn", "minor", "rook", "queen")
+WHITE_FRONT_SLOTS = (0, 1, 2)
+WHITE_BACK_SLOTS = (5, 6, 7)
+SIDE_SLOTS = (3, 4)
+
+
+def tier_slots(color, tier):
+ if tier == "side":
+ return SIDE_SLOTS
+ is_front = (tier == "front")
+ use_white_front_slots = is_front == (color == "white")
+ return WHITE_FRONT_SLOTS if use_white_front_slots else WHITE_BACK_SLOTS
+
+
+def load_rows(path):
+ with open(path, newline="") as f:
+ return list(csv.DictReader(f))
+
+
+def build_features(rows_in):
+ feat_rows = []
+ y = []
+ col_names = None
+ for r in rows_in:
+ color = r["mated_color_or_sampled_color"]
+
+ def offboard(s):
+ return int(r.get(f"sq{s}_offboard", 0) or 0) != 0
+
+ feat = {}
+ for tier in ("front", "back", "side"):
+ slots = tier_slots(color, tier)
+ for p in PIECE_TYPES:
+ feat[f"{tier}_atk_{p}"] = sum(
+ int(r[f"sq{s}_atk_{p}"]) for s in slots if not offboard(s)
+ )
+ feat[f"{tier}_undef"] = sum(
+ int(r[f"sq{s}_undef"]) for s in slots if not offboard(s)
+ )
+ feat[f"{tier}_xray"] = sum(
+ int(r[f"sq{s}_xray"]) for s in slots if not offboard(s)
+ )
+ feat[f"{tier}_flight"] = sum(
+ int(r[f"sq{s}_flight"]) for s in slots if not offboard(s)
+ )
+ feat["queenproximity"] = int(r["queenproximity"])
+ feat["filestorm"] = int(r["filestorm"])
+ feat["filestormopenfile"] = int(r["filestormopenfile"])
+ feat["filestormpawn"] = int(r["filestormpawn"])
+ feat["castled"] = int(r["castled"])
+ feat["minorsathome"] = int(r["minorsathome"])
+ feat["misscastleshort"] = int(r["misscastleshort"])
+ feat["misscastlelong"] = int(r["misscastlelong"])
+ if col_names is None:
+ col_names = list(feat.keys())
+ feat_rows.append([feat[c] for c in col_names])
+ y.append(float(r["label"]))
+ return np.array(feat_rows, dtype=float), np.array(y, dtype=float), col_names
+
+
+def report(name, model, X_cols, coefs, intercept):
+ print(f"\n=== {name} ===")
+ print(f"intercept: {intercept:.4f}")
+ order = np.argsort(-np.abs(coefs))
+ for i in order:
+ if abs(coefs[i]) < 1e-6:
+ continue
+ print(f" {X_cols[i]:28s} {coefs[i]:+.4f}")
+
+
+SANITY_KEYS = ("front_undef", "back_undef", "side_undef",
+ "front_xray", "back_xray", "side_xray",
+ "queenproximity", "filestorm")
+
+
+def sign_flips(coefs, col_names):
+ n = 0
+ for key in SANITY_KEYS:
+ if key in col_names:
+ if coefs[col_names.index(key)] < -1e-9:
+ n += 1
+ return n
+
+
+def sweep(X_train, y_train, X_test, y_test, col_names):
+ print("\n=== Ridge alpha sweep ===")
+ print(f"{'alpha':>10} {'train MSE':>10} {'test MSE':>10} {'sign flips':>11}")
+ for alpha in (0.1, 1, 3, 10, 30, 100, 300, 1000):
+ m = Ridge(alpha=alpha).fit(X_train, y_train)
+ tr = np.mean((m.predict(X_train) - y_train) ** 2)
+ te = np.mean((m.predict(X_test) - y_test) ** 2)
+ flips = sign_flips(m.coef_, col_names)
+ print(f"{alpha:>10} {tr:>10.4f} {te:>10.4f} {flips:>11}")
+
+ print("\n=== ElasticNet alpha/l1_ratio sweep ===")
+ print(f"{'alpha':>8} {'l1_ratio':>9} {'train MSE':>10} {'test MSE':>10} "
+ f"{'nnz':>4} {'sign flips':>11}")
+ for alpha in (0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2):
+ for l1_ratio in (0.1, 0.3, 0.5, 0.7, 0.9, 1.0):
+ m = ElasticNet(alpha=alpha, l1_ratio=l1_ratio, max_iter=20000)
+ m.fit(X_train, y_train)
+ tr = np.mean((m.predict(X_train) - y_train) ** 2)
+ te = np.mean((m.predict(X_test) - y_test) ** 2)
+ nnz = int(np.sum(np.abs(m.coef_) > 1e-9))
+ flips = sign_flips(m.coef_, col_names)
+ print(f"{alpha:>8} {l1_ratio:>9} {tr:>10.4f} {te:>10.4f} "
+ f"{nnz:>4} {flips:>11}")
+
+
+def main():
+ rows_in = load_rows(IN_CSV)
+ print(f"Loaded {len(rows_in)} rows from {IN_CSV}")
+ X, y, col_names = build_features(rows_in)
+ print(f"Built {X.shape[1]} features")
+
+ X_train, X_test, y_train, y_test = train_test_split(
+ X, y, test_size=0.15, random_state=42
+ )
+ scaler = StandardScaler().fit(X_train)
+ X_train_s = scaler.transform(X_train)
+ X_test_s = scaler.transform(X_test)
+
+ if "--sweep" in sys.argv:
+ sweep(X_train_s, y_train, X_test_s, y_test, col_names)
+ return
+
+ ridge = Ridge(alpha=10.0)
+ ridge.fit(X_train_s, y_train)
+ ridge_train_err = np.mean((ridge.predict(X_train_s) - y_train) ** 2)
+ ridge_test_err = np.mean((ridge.predict(X_test_s) - y_test) ** 2)
+ print(f"\nRidge(alpha=10): train MSE={ridge_train_err:.4f} test MSE={ridge_test_err:.4f}")
+ report("Ridge coefficients (standardized X)", ridge, col_names, ridge.coef_, ridge.intercept_)
+
+ enet_alpha = float(sys.argv[sys.argv.index("--enet-alpha") + 1]) if "--enet-alpha" in sys.argv else 0.02
+ enet_l1_ratio = float(sys.argv[sys.argv.index("--enet-l1-ratio") + 1]) if "--enet-l1-ratio" in sys.argv else 0.5
+ enet = ElasticNet(alpha=enet_alpha, l1_ratio=enet_l1_ratio, max_iter=20000)
+ enet.fit(X_train_s, y_train)
+ enet_train_err = np.mean((enet.predict(X_train_s) - y_train) ** 2)
+ enet_test_err = np.mean((enet.predict(X_test_s) - y_test) ** 2)
+ print(f"\nElasticNet(alpha={enet_alpha}, l1_ratio={enet_l1_ratio}): train MSE={enet_train_err:.4f} test MSE={enet_test_err:.4f}")
+ report("ElasticNet coefficients (standardized X)", enet, col_names, enet.coef_, enet.intercept_)
+
+ # Sign sanity check: undefended-attack terms and any "flight" term for
+ # the front tier should be non-negative-danger (i.e. push toward
+ # label==1/mate, positive coefficient here since label=1 is mate) --
+ # a *negative* coefficient on front_undef/back_undef/side_undef would
+ # mean "more undefended attacked squares near the king make mate LESS
+ # likely", which is not physically sane.
+ print("\n=== Sign sanity check (positive = more mate-associated, as expected) ===")
+ for name, coefs in (("Ridge", ridge.coef_), ("ElasticNet", enet.coef_)):
+ cols = col_names
+ bad = []
+ for key in ("front_undef", "back_undef", "side_undef",
+ "front_xray", "back_xray", "side_xray",
+ "queenproximity", "filestorm"):
+ if key in cols:
+ c = coefs[cols.index(key)]
+ flag = "" if c >= 0 else " <-- SIGN FLIP (should be >= 0)"
+ print(f" [{name}] {key:16s} {c:+.4f}{flag}")
+ if c < 0:
+ bad.append(key)
+ if bad:
+ print(f" [{name}] {len(bad)} suspicious sign(s): {bad}")
+ else:
+ print(f" [{name}] all checked terms have the expected sign")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/src/eval_tune/lazy_eval_margins/README.md b/src/eval_tune/lazy_eval_margins/README.md
new file mode 100644
index 0000000..239bb26
--- /dev/null
+++ b/src/eval_tune/lazy_eval_margins/README.md
@@ -0,0 +1,194 @@
+# Lazy/super-lazy eval margin redesign (2026-09-17)
+
+## What changed and why
+
+Original goal: speed up `Eval()` (eval.c) by letting more nodes exit
+through the cheap "super lazy" (material-only, runs before `_EvalPawns`)
+and "lazy" (material + pawns + passers + bad-trade + bishop-pairs) tiers
+instead of paying for full eval, motivated by comparing against Crafty's
+much higher NPS.
+
+The old margin construction (still visible via `git show HEAD:src/eval.c`
+at commit `4a7ec9a`) was a patchwork: a flat `LAZY_EVAL_BASE_MARGIN` (75)
+seed, widened by a `CountKingSafetyDefects`-indexed king term (`_EvalKing`'s
+own slow bitboard scan, called *again* just to estimate a margin) plus a
+flat residual constant, then `MAX()`'d against a separately-measured
+flat floor per material bucket. Super-lazy was a single flat
+`SUPER_LAZY_MARGIN_BY_ARMY[8]` lookup, no per-position information at all.
+Both had real problems: `CountKingSafetyDefects` is slow despite being
+bitboard-based (defeats the point of being in the *cheap* path), and the
+`MAX()`-against-a-flat-floor pattern silently overrode the
+position-specific estimate in most cases (found and fixed twice this
+session, once per tier).
+
+## The new construction
+
+For **both** tiers, at every node, cheap **O(1) table lookups only** (no
+`CountKingSafetyDefects`, no attack-table generation):
+
+```
+margin_a = MAX0( (king_p_hi[my_bucket] - king_p_lo[opp_bucket])
+ + (resid_p_hi[opp_bucket] - resid_p_lo[my_bucket]) )
+margin_b = MAX0( (king_p_hi[opp_bucket] - king_p_lo[my_bucket])
+ + (resid_p_hi[my_bucket] - resid_p_lo[opp_bucket]) )
+```
+
+- `margin_a` gates the fail-high check (`score - margin_a > beta`):
+ soundness requires assuming the most pessimistic-for-me,
+ optimistic-for-opponent outcome for each term.
+- `margin_b` gates the fail-low check (`score + margin_b < alpha`):
+ the mirror image.
+- **King term**: bucketed by the **attacker's** own army scaler
+ (`MINU(7, armyScaler/4)`, 8 buckets, 0-31 scaler range) -- danger to a
+ king is a function of the attacker's material, not the king-owner's.
+ Shared between both tiers (`iKingP2ByAttackerArmyOnly` /
+ `iKingP98ByAttackerArmyOnly` in eval.c).
+- **Residual term** (mobility/trapped/passers-not-yet-counted/etc.):
+ bucketed by **each side's own** army scaler. Two separate tables
+ because the two tiers have different baselines to estimate *from*:
+ - `iResidualP2/98ByOwnArmy` -- regular lazy's baseline already
+ includes pawns/passers/bad-trade/bishop-pairs, so this residual
+ only needs to cover what's left (mobility, trapped pieces, etc).
+ - `iSuperLazyResidualP2/98ByOwnArmy` -- super-lazy's baseline is pure
+ material, so this residual is much wider (includes everything
+ regular lazy's own residual excludes).
+
+`_hi`/`_lo` are currently p98/p2 as shipped, but genuinely **per-bucket
+tunable** -- see "How to keep tuning" below. The king table has very
+little spread above p90 in every bucket (already checked against the
+full histogram) and was deliberately left alone throughout tuning;
+all of today's percentile-adjustment work only ever touched the two
+residual tables.
+
+`iPositional[side]` (the value fed back to QSearch's futility pruning,
+`search.c`'s `FUTILITY_BASE_MARGIN_BY_SOURCE[tier] + iPositional[side]`)
+is now a **separate**, deliberately different statistic: a **p50**
+(median, not tail-bound) typical-value estimate of that side's own
+missing terms, matching what full-eval's own `iPositional` actually is
+(a real point estimate, not a safety margin). See `iKingP50By...` /
+`iResidualP50By...` / `iSuperLazyResidualP50By...` in eval.c. Don't
+confuse these with the p98/p2 margin tables -- different statistic,
+different purpose (bias correction vs. soundness bound).
+
+## Where the histogram data lives
+
+`eval.c`'s `_PersistRawHistogramsToFile()` (called automatically every
+time a `DUMP_RESIDUAL_SAMPLES` build runs `calibrate residual`) writes
+**every raw bin** of all three histograms (residual, sl_residual, king)
+to `/var/tmp/typhoon/residual_raw_histograms.txt` -- not just the
+handful of percentile columns that happen to be printed to the console.
+This directory (`src/eval_tune/lazy_eval_margins/`) has copies from the
+2026-09-17 "guess #2" 30k-position calibration run
+(`residual_raw_histograms_guess2_30k.txt`) plus the reader script
+(`hist_percentile.py`) -- **copy the fresh file here after any future
+30k run you want to keep**, since `/var/tmp` is scratch and could be
+cleared.
+
+To read *any* percentile out of a saved histogram, without re-running
+anything:
+
+```sh
+python3 hist_percentile.py residual_raw_histograms_guess2_30k.txt # default spread
+python3 hist_percentile.py residual_raw_histograms_guess2_30k.txt 0.93 0.965 # exact percentiles you want
+```
+
+## How to keep tuning
+
+1. **Build**: `gmake clean && gmake -j5 GENETIC=1 PERF_COUNTERS=1 MP=1 SIXTYFOUR=1 DUMP_RESIDUAL_SAMPLES=1 CALIBRATE_MARGIN_SAFETY=1`
+2. **Run the 30k stratified pool** (both commands in one invocation --
+ `calibrate residual` persists the histogram file, `calibrate
+ marginsafety` prints the real exceeded rate per **material** bucket
+ for both tiers):
+ ```sh
+ ./typhoon --cpus 1 --hash 256m --logfile /var/tmp/typhoon/run.log \
+ --batch --command "force; book name /nonexistent.book.bin; sd 6; \
+ script ../tests/eval_calibration_sample_strat30k.ep_; \
+ calibrate residual; calibrate marginsafety" \
+ > /var/tmp/typhoon/run.stdout 2>&1
+ ```
+ Takes roughly 20-25 minutes. Run it in the background
+ (`run_in_background`/`&`), don't block on it.
+3. **Read the exceeded rate per material bucket** from the
+ `calibrate marginsafety` output (`exceeded / n`, printed separately
+ for "Regular lazy" and "Super lazy", by **material** bucket --
+ combined army scaler / 8, a *different* bucketing than the
+ percentile tables' per-side scaler/4 buckets -- see the caveat
+ below).
+4. **Copy the fresh `/var/tmp/typhoon/residual_raw_histograms.txt`**
+ into this directory before it can get lost.
+5. **Pick new percentiles per per-side bucket** using
+ `hist_percentile.py` against the fresh file, and edit
+ `iResidualP2/98ByOwnArmy` / `iSuperLazyResidualP2/98ByOwnArmy` in
+ eval.c directly (keep each bucket's low/high pair **symmetric** --
+ e.g. p95/p5 together, never p98 mixed with p10 in the same bucket).
+6. **Rebuild and re-run step 2** to verify the real effect.
+
+### The two gotchas that cost real time this session
+
+- **The 500-position "quick check" sample is unreliable for this
+ specific measurement.** It's fine for cheap direction-of-travel
+ checks on most things, but exceeded-rate is a rare-event percentage,
+ and 500 positions (not even 500 independent draws -- a single game's
+ search tree can dominate a material bucket's node count) can be off
+ by 2-5x from the true 30k-pool rate. One bucket (super-lazy material
+ bucket 1) got tightened based on a 500-position reading of 0.035%,
+ then the real 30k number came back at 1.13% -- a dangerous-direction
+ miss. **Always confirm with the full 30k pool before trusting a
+ number**, especially anything under ~1%.
+- **Changing one bucket's margin changes measured behavior in OTHER
+ buckets too**, even ones whose own table entries never moved. This
+ isn't a bug -- `Eval()`'s exit decisions are real search behavior, so
+ widening/narrowing any margin reshapes the actual search tree
+ (different alpha/beta bounds propagate differently), which changes
+ which exact nodes land in *every* material bucket, not just the one
+ you touched. Material bucket 3's exceeded rate moved from 0.21% to
+ 0.94% to 0.65% to 1.44% across different runs despite its own table
+ entries never changing once. Don't chase a bucket that "moved" if you
+ didn't touch its inputs -- check what else changed first.
+- **Material buckets and per-side buckets are different bucketing
+ schemes.** `calibrate marginsafety` reports by **material bucket**
+ (`_MaterialBucket`: combined army scaler for both sides, /8). The
+ percentile tables you actually edit are indexed by **per-side**
+ bucket (`armyScaler/4`, one bucket per side, looked up twice -- once
+ as "my bucket", once as "opponent's bucket"). A single material
+ bucket (e.g. combined scaler 24-31) is populated by many different
+ per-side bucket combinations (3+4, 2+5, 1+6, 0+7...), so there's no
+ clean 1:1 way to "fix material bucket 3" by editing "per-side bucket
+ 3" alone -- expect diffuse, imprecise control, not a dial.
+
+## On the "insane" 2% idea
+
+Structurally straightforward: pick a looser matched percentile pair
+(e.g. p90/p10, or go outside the currently-recorded columns using
+`hist_percentile.py` against a fresh histogram) for whichever buckets
+you want at ~2%, following the same edit-rebuild-reverify loop above.
+Two things worth checking once you have it:
+
+1. **NPS gain isn't guaranteed to scale linearly with the exceeded-rate
+ relaxation** -- this session found the NPS win from the whole
+ redesign was much smaller than expected (see the `git log`/session
+ history around `EvalLookForDrawishSituations` getting added to both
+ cheap tiers, and `fBareKingEitherSide`'s two unconditional
+ `TotalNonPawnCount` calls on every node -- both are new fixed costs
+ that eat into any margin-driven exit-rate gain). Worth re-checking
+ the `Eval exit breakdown` / new `calibrate exitrate` numbers
+ (below) alongside NPS, not just NPS alone.
+2. **`FUTILITY_BASE_MARGIN_LAZY`/`SUPERLAZY`** (search.c/chess.h) were
+ calibrated against *this* `iPositional` distribution
+ (`CALIBRATE_QSEARCH_FUTILITY`, see chess.h's derivation comment near
+ those two `#define`s) -- if the eval-exit margins move a lot, especially
+ in a direction that changes how often/how confidently each tier
+ exits, it's worth re-running that calibration too rather than
+ assuming the existing 350/175 values still hold.
+
+## New tooling added this session (always available, no build flag)
+
+- **`calibrate exitrate`** -- prints eval exit rate (% super lazy / %
+ lazy / % full) **by material bucket**, not just the flat aggregate
+ "Eval exit breakdown" line every search already prints. Complements
+ the exceeded-rate data: a bucket with low exit rate AND low exceeded
+ rate has real untapped speed headroom; a bucket already near its
+ target exceeded rate with a healthy exit rate is well-calibrated.
+ Implemented in eval.c (`g_u64SuperLazyEvalsByBucket` /
+ `g_u64LazyEvalsByBucket` / `g_u64FullEvalsByBucket`,
+ `DumpEvalExitRateByBucket`) and wired into command.c.
diff --git a/src/eval_tune/lazy_eval_margins/aggregate_marginsafety_shards.py b/src/eval_tune/lazy_eval_margins/aggregate_marginsafety_shards.py
new file mode 100644
index 0000000..69e5c8e
--- /dev/null
+++ b/src/eval_tune/lazy_eval_margins/aggregate_marginsafety_shards.py
@@ -0,0 +1,92 @@
+#!/usr/bin/env python3
+"""Combine N shards' persisted raw swing histograms (eval.c's
+PersistMarginSafetyHistogramsToFile, "calibrate marginsafety <path>")
+into one true aggregate: sum count/exceeded/sum/sumSq/histogram bins
+bucket-by-bucket across all shard files, then recompute mean/stdev/
+percentiles from the combined totals. Percentiles/means from
+individual shards can't be validly averaged -- this reads raw counts
+only and derives every statistic fresh from the combined totals.
+
+Usage:
+ python3 aggregate_marginsafety_shards.py <shard_file> [<shard_file> ...]
+"""
+import sys
+from collections import defaultdict
+
+BIN_WIDTH = 10
+
+
+def load(path):
+ """Yield (tier, bucket, count, exceeded, sum_, sumsq, max_, bins)."""
+ with open(path) as f:
+ for line in f:
+ if line.startswith('#') or not line.strip():
+ continue
+ parts = line.split()
+ tier, bucket = parts[0], int(parts[1])
+ count, exceeded, sum_, sumsq, max_ = map(int, parts[2:7])
+ bins = list(map(int, parts[7:]))
+ yield tier, bucket, count, exceeded, sum_, sumsq, max_, bins
+
+
+def percentile(bins, total, p):
+ target = p * total
+ running = 0
+ for i, c in enumerate(bins):
+ running += c
+ if running >= target:
+ return i * BIN_WIDTH + BIN_WIDTH // 2
+ return (len(bins) - 1) * BIN_WIDTH
+
+
+def main():
+ if len(sys.argv) < 2:
+ print(__doc__)
+ sys.exit(1)
+
+ agg = defaultdict(lambda: {"count": 0, "exceeded": 0, "sum": 0,
+ "sumsq": 0, "max": 0})
+ agg_bins = {}
+
+ for path in sys.argv[1:]:
+ for tier, bucket, count, exceeded, sum_, sumsq, max_, bins in load(path):
+ key = (tier, bucket)
+ a = agg[key]
+ a["count"] += count
+ a["exceeded"] += exceeded
+ a["sum"] += sum_
+ a["sumsq"] += sumsq
+ a["max"] = max(a["max"], max_)
+ if key not in agg_bins:
+ agg_bins[key] = bins[:]
+ else:
+ for i, c in enumerate(bins):
+ agg_bins[key][i] += c
+
+ for tier in ("lazy", "superlazy"):
+ print(f"--- {tier} ---")
+ for bucket in range(8):
+ key = (tier, bucket)
+ if key not in agg:
+ continue
+ a = agg[key]
+ n = a["count"]
+ if n == 0:
+ print(f" bucket {bucket}: no exits taken")
+ continue
+ mean = a["sum"] / n
+ variance = max(0.0, a["sumsq"] / n - mean * mean)
+ stdev = variance ** 0.5
+ pct = 100.0 * a["exceeded"] / n
+ bins = agg_bins[key]
+ p10 = percentile(bins, n, 0.10)
+ p50 = percentile(bins, n, 0.50)
+ p90 = percentile(bins, n, 0.90)
+ p99 = percentile(bins, n, 0.99)
+ print(f" bucket {bucket}: n={n} exceeded={a['exceeded']} "
+ f"({pct:.3f}%) max={a['max']} avg={mean:.1f} "
+ f"stdev={stdev:.1f} p10={p10} p50={p50} p90={p90} p99={p99}")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/src/eval_tune/lazy_eval_margins/hist_percentile.py b/src/eval_tune/lazy_eval_margins/hist_percentile.py
new file mode 100644
index 0000000..bd90444
--- /dev/null
+++ b/src/eval_tune/lazy_eval_margins/hist_percentile.py
@@ -0,0 +1,86 @@
+#!/usr/bin/env python3
+"""
+Read any percentile out of the raw lazy/super-lazy-eval histograms
+persisted by eval.c's _PersistRawHistogramsToFile (called automatically
+every time a DUMP_RESIDUAL_SAMPLES build runs `calibrate residual`).
+
+Usage:
+ python3 hist_percentile.py <histograms_file> [percentiles...]
+
+ python3 hist_percentile.py residual_raw_histograms_guess2_30k.txt
+ python3 hist_percentile.py residual_raw_histograms_guess2_30k.txt 0.93 0.97 0.985
+
+With no percentile arguments, prints a default useful spread for every
+bucket of all three histograms (residual, sl_residual, king). With
+percentile arguments (0.0-1.0), prints just those, letting you probe
+any exact percentile the fixed comment-block charts in eval.c didn't
+happen to include -- no re-running the 30k pool required, as long as
+this file (or a fresher one) still exists.
+
+File format (one line per histogram/bucket):
+ <name> <bucket 0-7> <min> <max> <bin0-count> <bin1-count> ... <binN-1-count>
+Bin i covers [RESID_HIST_MIN + i*RESID_HIST_BIN_WIDTH,
+ RESID_HIST_MIN + (i+1)*RESID_HIST_BIN_WIDTH).
+These constants are echoed in the file's own header comment and also
+hardcoded below (must match eval.c's #defines -- they haven't changed
+since this was built, but check if eval.c's RESID_HIST_* macros ever
+do).
+"""
+import sys
+
+BIN_WIDTH = 20
+MIN_VAL = -900
+DEFAULT_PERCENTILES = [0.01, 0.02, 0.05, 0.10, 0.15, 0.50,
+ 0.85, 0.90, 0.95, 0.98, 0.99]
+
+
+def load(path):
+ hists = {}
+ with open(path) as f:
+ for line in f:
+ if line.startswith('#') or not line.strip():
+ continue
+ parts = line.split()
+ name, bucket = parts[0], int(parts[1])
+ mn, mx = int(parts[2]), int(parts[3])
+ bins = list(map(int, parts[4:]))
+ hists[(name, bucket)] = (mn, mx, bins)
+ return hists
+
+
+def percentile(bins, total, p):
+ target = p * total
+ running = 0
+ for i, c in enumerate(bins):
+ running += c
+ if running >= target:
+ return MIN_VAL + i * BIN_WIDTH + BIN_WIDTH // 2
+ return MIN_VAL + len(bins) * BIN_WIDTH
+
+
+def main():
+ if len(sys.argv) < 2:
+ print(__doc__)
+ sys.exit(1)
+ path = sys.argv[1]
+ percentiles = [float(x) for x in sys.argv[2:]] or DEFAULT_PERCENTILES
+
+ hists = load(path)
+ names = sorted(set(name for name, _ in hists.keys()))
+ for name in names:
+ print(f"--- {name} ---")
+ for b in range(8):
+ if (name, b) not in hists:
+ continue
+ mn, mx, bins = hists[(name, b)]
+ total = sum(bins)
+ if total == 0:
+ print(f" bucket {b}: no samples")
+ continue
+ vals = " ".join(f"p{p*100:g}={percentile(bins, total, p)}"
+ for p in percentiles)
+ print(f" bucket {b}: n={total} min={mn} {vals} max={mx}")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/src/eval_tune/lazy_eval_margins/residual_raw_histograms_guess2_30k.txt b/src/eval_tune/lazy_eval_margins/residual_raw_histograms_guess2_30k.txt
new file mode 100644
index 0000000..2b986fd
--- /dev/null
+++ b/src/eval_tune/lazy_eval_margins/residual_raw_histograms_guess2_30k.txt
@@ -0,0 +1,26 @@
+# RESID_HIST_BINS=90 RESID_HIST_BIN_WIDTH=20 RESID_HIST_MIN=-900
+# format: <histogram> <bucket> <min> <max> <bin0> <bin1> ... <binN-1>
+residual 0 -144 767 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 15 164 1363 26504 174479 870508 9266911 49119730 10195507 9392713 9036132 4277890 3487761 2369088 1537274 999863 733265 510947 325044 214567 159847 115810 75069 48198 53969 37634 17605 11800 13104 10928 6699 4315 1992 702 497 596 343 115 133 43 5 6 7 3 0 5 0 0 0 0 0 0
+residual 1 -156 817 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 44 510 2519 27653 88357 885128 6171719 17661445 21810645 17631977 17341084 11832862 7552148 6791430 4867074 3212050 2748617 1753842 1210171 1050324 619975 429316 296650 188513 141398 94497 77357 66118 48128 33631 28220 22011 16774 6674 4299 2332 2356 728 258 129 118 114 65 105 178 90 12 6 0 0 0 0
+residual 2 -183 767 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 39 298 1221 7993 62272 318787 1469893 6092481 18509685 37928483 55472424 40013831 25427921 16592110 10713766 7338910 5431623 3411498 2200485 1762575 1326601 917085 652040 541064 448084 316076 207264 141942 100658 72110 46937 32947 29716 21672 11431 8430 4380 2749 3811 4795 2881 1284 371 81 46 21 14 4 0 0 0 0 0 0
+residual 3 -210 708 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 11 102 778 7487 39369 185878 803334 2696551 7792351 17127020 31285590 43472163 35858955 18973011 11014510 7015793 4092753 2517064 1766213 1251219 892795 649846 465365 307463 220300 150647 108576 70034 54041 44466 24664 16116 8143 3820 2821 2969 2367 898 456 277 314 294 49 4 0 2 0 0 0 0 0 0 0 0 0
+residual 4 -225 735 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 26 355 2543 11604 72812 290938 1156430 4063826 9580273 17459580 27571067 37693079 41349983 32425962 19576659 10348483 5596569 3020560 1769703 1054860 663900 408572 251828 160650 111173 70030 48591 34664 28081 18251 10057 6322 4553 3129 2430 2085 1623 1246 648 227 136 107 37 7 11 3 1 3 0 0 0 0 0 0 0 0
+residual 5 -225 728 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 41 259 1047 6877 35991 215344 954842 2475083 5520028 11787046 23343313 36563338 41059148 31171892 17495367 8653512 4388935 2295381 1170946 613945 327609 182686 94836 58512 33370 22357 14125 7054 4429 2678 1824 1213 693 540 262 225 210 92 45 40 20 14 0 2 3 6 0 1 0 0 0 0 0 0 0 0
+residual 6 -224 760 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 25 193 1633 11244 59390 297180 1126668 3332277 7944759 14947141 23012354 27606104 25076655 16782590 8821595 4172757 2073401 1032648 497320 231717 113521 48847 25571 13957 9194 5013 3337 2048 763 658 376 369 399 182 156 158 100 54 30 56 35 30 11 5 0 2 0 0 0 1 0 0 0 0 0 0
+residual 7 -256 796 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 77 596 5373 33876 182971 1124882 5159235 14160661 23154137 28288490 27723692 24273561 18434696 11921382 6003346 2701510 1202658 564453 271480 120240 46627 25543 12741 6227 4692 3975 3596 2684 2263 2613 2155 1532 1027 1047 1954 2956 4217 2730 2126 967 332 208 116 73 24 31 16 8 4 8 1 2 0 0 0 0 0
+sl_residual 0 -363 2383 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 93 484 640 153 944 6091 20107 51368 43682 61452 73815 450532 1070740 1185017 1377715 1882895 6976318 18555405 16351680 6913999 5535807 6075056 5768506 5203691 4766197 3607623 2891038 2201502 1963000 1754691 1432133 1186087 971353 821438 589362 472728 426170 328019 262892 212348 175548 135083 114599 93897 72212 54737 41617 37979 29664 24002 15597 12435 12510 8346 6653 5274 4019 4598 3301 2277 2236 2950 746847
+sl_residual 1 -350 2456 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 5 12 209 154 212 1274 12139 17551 47595 38725 102721 275278 539485 924729 1517142 3059707 6915457 10963773 11809407 12944977 11134953 8917513 6063037 6607549 6690745 6033403 4709770 3464565 3120172 3067544 2518393 2152774 1875512 1409464 1171932 1008477 819500 684053 565223 450984 357881 297513 245308 193249 151350 115055 97969 79972 58946 51025 47163 37878 31393 29576 22418 16450 11622 7403 6610 4681 3334 1216751
+sl_residual 2 -341 2226 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 20 208 396 602 1065 5506 13385 39278 61658 95967 234519 537941 1151958 2189188 4202147 8406507 16076015 23816608 26868181 22293821 17876248 15042703 14417036 14403728 13616478 11421449 8195783 5804731 4559679 4050663 3497138 3025192 2548203 2088973 1766790 1506917 1243835 1005108 812946 678053 562124 499853 407287 313262 244722 202921 178275 143852 116515 86324 76908 71566 46813 32523 27196 25513 20425 15831 11815 9744 8976 991720
+sl_residual 3 -311 2183 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 21 61 154 542 1617 6306 18634 57796 137250 340158 813627 1900843 3798631 7183830 12657185 18955192 21399463 18622591 15039544 13069881 12002181 11690756 11082693 9079555 6607407 4844608 3632648 2812063 2286639 1952744 1628981 1325755 1088511 902859 716353 593121 507472 394467 286453 226564 184610 158212 129615 86332 75782 62737 49833 35072 29724 26078 19926 9245 7706 5500 4922 3131 3186 2083 1118 366910
+sl_residual 4 -332 1946 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 5 21 80 211 622 3216 7794 32108 85027 225564 575581 1288954 3162189 6503748 11524536 18055696 23379479 24854534 22254347 18468757 15612518 13595561 12248658 10461509 8232521 5973240 4332984 3091915 2436182 1930237 1434378 1046871 880613 710435 546475 437304 349561 246922 199709 151603 109361 79442 59223 45107 34391 31970 22142 15522 9573 9427 8249 7055 5312 2786 2100 2159 1781 1156 769 677 83810
+sl_residual 5 -255 1959 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 57 329 4118 5157 14547 53072 161291 460439 1366767 3711631 8379895 15114309 21009055 22617644 20719402 17623487 15520726 14438771 12605962 9666895 7026907 4848987 3389235 2392977 1724454 1361809 1038860 801316 631963 463280 347886 252687 203077 147415 112983 79834 51836 36878 26805 24492 15956 9257 7965 7546 4340 2921 2696 1937 1309 1570 558 619 572 317 501 410 9468
+sl_residual 6 -322 1921 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 10 71 75 533 2537 11871 45926 131486 411320 1022733 2455401 5155972 9050806 13534299 16064085 16289462 15246951 13506364 11485469 9336040 7270302 5223679 3423875 2232509 1548254 1085937 748196 537179 397751 283388 198894 143374 115014 84269 62144 41887 31983 20610 16322 8934 6864 4099 2740 1912 1078 897 984 861 495 349 232 220 168 174 1281 152 167 187 3754
+sl_residual 7 -290 2039 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 31 121 586 1390 5878 24253 78844 278500 840574 2421193 6465722 13171899 20355153 24186909 23376822 20790625 16475246 12347211 8814924 5940613 3741540 2250945 1341224 835775 536221 375668 245355 159741 114766 70869 52534 35541 27512 20952 14552 8525 6352 4366 3373 1674 1291 964 1139 1439 1256 1046 786 2036 1771 1237 1465 545 193 198 343 553 394 248 18929
+king 0 -68 181 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 69 13211 207523 934645 86648386 13071969 1298059 789094 81956 47100 5587 1482 69 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
+king 1 0 161 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 108064710 13984126 1444075 1033373 138118 49190 4019 2051 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
+king 2 -89 162 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1722 114112 2065803 15570929 85638651 125730930 7266574 838747 361815 45770 15105 279 350 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
+king 3 -176 137 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1661 19108 103023 459746 1201670 4330223 13600383 37139535 114485792 17300124 199194 64499 16526 4483 884 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
+king 4 -307 129 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 407 4859 24035 132051 369749 1106700 1918977 3315303 3740907 4954043 7287145 13988322 25447798 39536019 49741182 59441529 3861220 2098 608 489 235 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
+king 5 -354 45 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5827 43950 125055 504347 1037805 1783337 2511730 2350938 1689828 2132156 5231247 11219252 16784636 15952530 32037510 44817856 36448686 12878906 949585 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
+king 6 -401 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 5868 21230 53145 247683 406957 618270 890299 1239517 639988 477285 1587414 3082228 6139626 10016755 5146310 20237958 24080439 40947637 12534859 7979863 899195 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
+king 7 -394 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2363 7209 17894 78444 122463 206455 294717 391409 200453 202923 629244 1181690 2913486 5469013 3799599 16806699 19832913 50537919 22693260 38337669 1733993 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
diff --git a/src/eval_tune/match_play.py b/src/eval_tune/match_play.py
index e2e089a..d8a8e7b 100755
--- a/src/eval_tune/match_play.py
+++ b/src/eval_tune/match_play.py
@@ -31,8 +31,8 @@ Usage:
Prints:
CANDIDATE_SCORE=<0..1> GAMES=<n> WINS=<a> DRAWS=<b> LOSSES=<c> ELO=<+/-x.x>
LOWER95=<x> [SPRT=... LLR=... BOUNDS=... ELO0=... ELO1=...]
- CANDIDATE_STATS: nps=... ebf=... first_move_beta=...% nodes=... plies=...
- HEAD_STATS: nps=... ebf=... first_move_beta=...% nodes=... plies=...
+ CANDIDATE_STATS: nps=... ebf=... local_ebf=... first_move_beta=...% nodes=... plies=...
+ HEAD_STATS: nps=... ebf=... local_ebf=... first_move_beta=...% nodes=... plies=...
for the orchestrator to parse. Speed/tree-shape stats (EngineStats) are
pulled from the same per-move PostMoveSearchReport block every "go"
already prints, not a separate benchmark -- see EngineStats' docstring.
@@ -69,6 +69,21 @@ _SEARCHED_RE = re.compile(
_DEPTH_RE = re.compile(r"^tellothers d(\d+),")
_FIRST_MOVE_BETA_RE = re.compile(
r"^First move beta cutoff rate was\s+([\d.]+) percent\.")
+# 2026-09-18: per-iteration line UtilPrintPV (util.c) prints once per
+# completed depth during a move's search (both under-xboard "%2u %6d
+# %5u %-12llu %s" and human "%2u[+-]? %5s %-9s %-11llu %s" formats --
+# see util.c's UtilPrintPV; engines here run under-xboard since Engine
+# sends "xboard" at startup, per _STATS_TRAILING_LINES' comment above,
+# but this regex tolerates either since it only cares about the depth
+# and node-count fields, not the score/time/PV formatting in between).
+# u64TotalNodeCount is a running total for the WHOLE search (not reset
+# per iteration), so consecutive lines' node counts are monotonic
+# non-decreasing including across an aspiration-window fail-low/high
+# re-search at the SAME depth (root.c re-enters Search() with a wider
+# window at the same g_uIterateDepth, printing another line before
+# depth actually advances) -- see the EBF-from-consecutive-iterations
+# derivation in EngineStats.add_ply's caller for how that's handled.
+_ITER_RE = re.compile(r"^\s*(\d+)[+-]?\s+\S+\s+\S+\s+(\d+)\s")
# Reading PostMoveSearchReport's trailing block used to stop either at a
# bounded line count or at gamelist.c's "_white(move N)_"/"_black(move N)_"
# prompt -- both unreliable: the block's line count depends on whether the
@@ -283,6 +298,21 @@ def play_one_game(white_engine_path, black_engine_path, scratch_dir, fen,
# way around -- keep reading a bounded number of further
# lines to pick it up before moving on to the next ply.
ply_nodes = ply_time = ply_depth = ply_fmb = None
+ # 2026-09-18: local (per-iteration) branching factor, an
+ # alternative to the cumulative nodes**(1/depth) EBF below
+ # that isn't sensitive to which integer depth a move's
+ # search happens to finish on -- see _ITER_RE's comment and
+ # EngineStats' docstring for the full rationale. Track the
+ # most recently seen (depth, nodes) pair; each time depth
+ # actually advances (not just a same-depth aspiration
+ # re-search line), record nodes_now/nodes_prev as one
+ # branching-factor sample for this ply, geometric-mean them
+ # together at the end (arithmetic mean of ratios would let
+ # one huge outlier iteration dominate; geometric mean is
+ # the correct way to average ratios/growth-factors).
+ last_iter_depth = last_iter_nodes = None
+ iter_log_ratio_sum = 0.0
+ iter_ratio_count = 0
for _ in range(_STATS_TRAILING_LINES):
line = _readline_or_none(mover.stdout, _STATS_IDLE_TIMEOUT_SEC)
if line is None:
@@ -309,8 +339,27 @@ def play_one_game(white_engine_path, black_engine_path, scratch_dir, fen,
if fm:
ply_fmb = float(fm.group(1))
continue
+ im = _ITER_RE.match(line)
+ if im:
+ iter_depth = int(im.group(1))
+ iter_nodes = int(im.group(2))
+ if (last_iter_depth is not None and
+ last_iter_nodes is not None and
+ iter_depth != last_iter_depth and
+ iter_nodes > last_iter_nodes and
+ iter_depth > last_iter_depth):
+ ratio = iter_nodes / last_iter_nodes
+ depth_delta = iter_depth - last_iter_depth
+ iter_log_ratio_sum += math.log(ratio) / depth_delta
+ iter_ratio_count += 1
+ last_iter_depth = iter_depth
+ last_iter_nodes = iter_nodes
+ continue
if ply_nodes is not None and ply_time is not None:
- mover_stats.add_ply(ply_nodes, ply_time, ply_depth)
+ ply_local_ebf = (math.exp(iter_log_ratio_sum / iter_ratio_count)
+ if iter_ratio_count else None)
+ mover_stats.add_ply(ply_nodes, ply_time, ply_depth,
+ ply_local_ebf)
if ply_fmb is not None:
if white_to_move:
white_last_fmb = ply_fmb
@@ -376,7 +425,33 @@ class EngineStats:
(nodes**(1/depth) per move, see head_reference notes on why this can
diverge from first-move-beta-cutoff-rate when pruning/aspiration-
window re-searches inflate node counts independent of true tree
- shape), then averaged across all moves an engine played. First-move
+ shape), then averaged across all moves an engine played.
+
+ 2026-09-18: that cumulative nodes**(1/depth) EBF turned out to be
+ misleading when comparing two engines with different lazy-eval-exit
+ aggressiveness -- one engine measured LOWER nps and HIGHER avg_depth
+ than the other while ALSO reporting HIGHER ebf, which is not
+ reconcilable if ebf is really "nodes needed per unit depth" (lower
+ nps + higher depth implies fewer total nodes were needed to get
+ there, which should mean lower ebf, not higher). Root cause: this
+ formula uses the SEARCH-WIDE cumulative node count raised to
+ 1/(the single integer depth reported at the end), so it's extremely
+ sensitive to exactly which whole ply a move's search happens to
+ finish on within its time budget -- a difference in time-management
+ timing that pushes a move to complete one more (or one fewer) full
+ iteration can swing this number a lot with no real change in tree
+ efficiency, and this effect doesn't reliably average out because
+ node-count magnitude varies hugely across game phases/positions.
+ `local_ebf` (see add_ply) fixes this by computing depth-normalized
+ branching ratios from CONSECUTIVE completed-iteration node counts
+ within a single move's search (nodes_now/nodes_prev, depth-delta-
+ normalized, geometric-meaned across a move's iterations, then
+ arithmetic-meaned across moves same as avg_ebf) -- a true local
+ branching-factor measurement, insensitive to which absolute depth
+ the search happened to stop at. Kept the old ebf/avg_ebf fields
+ alongside rather than replacing them, since the cumulative version
+ is still what script.c's own suite runs report and there's value in
+ being able to cross-check the two against each other. First-move
beta cutoff rate is read as a running total inside a single engine
process's lifetime (root.c never resets those counters mid-process),
and since play_one_game spawns a fresh process per game, the value
@@ -391,12 +466,14 @@ class EngineStats:
self.plies = 0
self.ebf_sum = 0.0
self.ebf_count = 0
+ self.local_ebf_sum = 0.0
+ self.local_ebf_count = 0
self._fmb_weighted_sum = 0.0
self._fmb_weight = 0
self.depth_sum = 0
self.depth_count = 0
- def add_ply(self, nodes, elapsed, depth):
+ def add_ply(self, nodes, elapsed, depth, local_ebf=None):
self.total_nodes += nodes
self.total_time += elapsed
self.plies += 1
@@ -406,6 +483,9 @@ class EngineStats:
if depth and depth > 0 and nodes > 0:
self.ebf_sum += nodes ** (1.0 / depth)
self.ebf_count += 1
+ if local_ebf is not None:
+ self.local_ebf_sum += local_ebf
+ self.local_ebf_count += 1
def add_game_first_move_beta(self, pct, plies_in_game):
if pct is not None and plies_in_game > 0:
@@ -418,6 +498,8 @@ class EngineStats:
self.plies += other.plies
self.ebf_sum += other.ebf_sum
self.ebf_count += other.ebf_count
+ self.local_ebf_sum += other.local_ebf_sum
+ self.local_ebf_count += other.local_ebf_count
self._fmb_weighted_sum += other._fmb_weighted_sum
self._fmb_weight += other._fmb_weight
self.depth_sum += other.depth_sum
@@ -432,6 +514,11 @@ class EngineStats:
return self.ebf_sum / self.ebf_count if self.ebf_count else 0.0
@property
+ def avg_local_ebf(self):
+ return (self.local_ebf_sum / self.local_ebf_count
+ if self.local_ebf_count else 0.0)
+
+ @property
def avg_first_move_beta(self):
return (self._fmb_weighted_sum / self._fmb_weight
if self._fmb_weight else 0.0)
@@ -442,6 +529,7 @@ class EngineStats:
def summary(self):
return (f"nps={self.nps:.0f} ebf={self.avg_ebf:.3f} "
+ f"local_ebf={self.avg_local_ebf:.3f} "
f"depth={self.avg_depth:.2f} "
f"first_move_beta={self.avg_first_move_beta:.1f}% "
f"nodes={self.total_nodes} plies={self.plies}")
@@ -718,7 +806,9 @@ def main():
f"head={head_stats.nps:.0f})",
file=sys.stderr)
print(f" avg ebf: candidate={candidate_stats.avg_ebf:.3f} "
- f"head={head_stats.avg_ebf:.3f}",
+ f"head={head_stats.avg_ebf:.3f} "
+ f"(local: candidate={candidate_stats.avg_local_ebf:.3f} "
+ f"head={head_stats.avg_local_ebf:.3f})",
file=sys.stderr)
if sprt is not None and sprt_decision is None:
diff --git a/src/eval_tune/sample_eval_calibration_positions.py b/src/eval_tune/sample_eval_calibration_positions.py
new file mode 100644
index 0000000..6bf42d2
--- /dev/null
+++ b/src/eval_tune/sample_eval_calibration_positions.py
@@ -0,0 +1,182 @@
+#!/usr/bin/env python3
+"""Sample random (fen) positions from one or more PGN pools, at random
+plies anywhere in the game (not filtered for quiet/tactical), for the
+margin-safety recalibration described in board_representation/EVAL.md
+section 9. Unlike tune_eval_dna.py's quiet-position pools, this
+deliberately does NOT filter on SEE/hanging-material/qsearch-agreement --
+Eval() runs at every node of the real search tree, quiet or not, so the
+calibration sample should look like "all chess positions", not just the
+quiet subset used for Texel tuning.
+
+Uses the same random-byte-offset-seek trick as match_play.py's
+sample_openings() to avoid a full linear scan of multi-GB pools.
+
+Output: one FEN per source game (one random ply from openings-excluded
+range), written as alternating "setboard <fen>" / "go" lines matching
+tests/twic_sample.ep_'s format, suitable for a `script <file>` run
+against a CALIBRATE_MARGIN_SAFETY build.
+"""
+import argparse
+import hashlib
+import io
+import random
+import sys
+from pathlib import Path
+
+import chess
+import chess.pgn
+
+
+def iter_random_game_texts(pgn_path, rng, max_attempts):
+ """Yield raw PGN text blocks for randomly-located games in pgn_path,
+ same seek-scan-extract approach as match_play.py's sample_openings."""
+ st = Path(pgn_path).stat()
+ file_size = st.st_size
+ attempts = 0
+ with open(pgn_path, "rb") as fb:
+ while attempts < max_attempts:
+ attempts += 1
+ offset = rng.randint(0, max(file_size - 1, 0))
+ fb.seek(offset)
+ fb.readline()
+ start = fb.tell()
+ line = fb.readline()
+ while line and not line.startswith(b"[Event "):
+ start = fb.tell()
+ line = fb.readline()
+ if not line:
+ continue
+ fb.seek(start)
+ fb.readline()
+ end = fb.tell()
+ line = fb.readline()
+ while line and not line.startswith(b"[Event "):
+ end = fb.tell()
+ line = fb.readline()
+ fb.seek(start)
+ raw = fb.read(end - start).decode("utf-8", errors="replace")
+ yield raw
+
+
+PIECE_VALUES = {chess.KNIGHT: 3, chess.BISHOP: 3, chess.ROOK: 5, chess.QUEEN: 9}
+
+
+def material_bucket(board):
+ """Same bucketing as eval.c's _MaterialBucket: combined non-pawn
+ army value (pawn units) / 8, capped at 7 (0 = bare kings, 7 = full
+ army)."""
+ total = 0
+ for pt, v in PIECE_VALUES.items():
+ total += v * len(board.pieces(pt, chess.WHITE))
+ total += v * len(board.pieces(pt, chess.BLACK))
+ return min(7, total // 8)
+
+
+def sample_positions(pgn_paths, n, min_ply, seed, cache_dir, stratify=False):
+ key_parts = []
+ for p in pgn_paths:
+ st = Path(p).stat()
+ key_parts.append(f"{p}|{st.st_mtime}|{st.st_size}")
+ key = hashlib.sha256(
+ f"{'|'.join(key_parts)}|{n}|{min_ply}|{seed}|{stratify}".encode()
+ ).hexdigest()[:16]
+ cache_path = Path(cache_dir) / f"eval_calib_{key}.fens"
+ if cache_path.exists():
+ fens = cache_path.read_text().splitlines()
+ print(f" loaded {len(fens)} positions from cache {cache_path}",
+ file=sys.stderr)
+ return fens
+
+ rng = random.Random(seed)
+ fens = []
+ # Stratified mode needs many more discarded candidates (most draws
+ # land in the naturally-common middlegame buckets, not the rare
+ # near-endgame ones we're specifically trying to fill), so give it
+ # a much larger attempt budget than the unstratified path.
+ max_attempts = (n * 200 + 2000) if stratify else (n * 30 + 500)
+ attempts = 0
+ # Round-robin across pools so a large pool (twic_filtered) doesn't
+ # drown out a smaller one (any single lichess_elite month) purely by
+ # byte-offset odds; weight by requested count via cycling.
+ pool_iters = [iter_random_game_texts(p, rng, max_attempts) for p in pgn_paths]
+ idx = 0
+
+ # Unstratified: just take the first n candidates. Stratified: fill
+ # 8 equal-size buckets (by combined material) up to quota each,
+ # discarding candidates for buckets that are already full -- this
+ # is what actually fixes cross-sectional coverage (uniform-random-
+ # ply sampling is heavily skewed toward the middlegame/opening
+ # buckets simply because games spend more plies there than grinding
+ # out endgames; more positions alone doesn't fix that skew, only
+ # explicit per-bucket quotas do).
+ num_buckets = 8
+ quota = -(-n // num_buckets) if stratify else 0 # ceil division
+ bucket_counts = [0] * num_buckets
+
+ while len(fens) < n and attempts < max_attempts:
+ raw = next(pool_iters[idx % len(pool_iters)], None)
+ idx += 1
+ attempts += 1
+ if raw is None:
+ continue
+ game = chess.pgn.read_game(io.StringIO(raw))
+ if game is None:
+ continue
+ moves = list(game.mainline_moves())
+ if len(moves) < min_ply + 1:
+ continue
+ # Pick one random ply strictly after min_ply and strictly before
+ # the last move (avoid checkmate/stalemate terminal boards, which
+ # `go` can't search meaningfully).
+ target_ply = rng.randint(min_ply, len(moves) - 1)
+ board = game.board()
+ for i, mv in enumerate(moves):
+ if i == target_ply:
+ break
+ board.push(mv)
+ if board.is_game_over():
+ continue
+ if stratify:
+ b = material_bucket(board)
+ if bucket_counts[b] >= quota:
+ continue
+ bucket_counts[b] += 1
+ fens.append(board.fen())
+ if len(fens) % 2000 == 0:
+ print(f" sampled {len(fens)}/{n}...", file=sys.stderr)
+
+ if stratify:
+ print(f" final bucket counts: {bucket_counts}", file=sys.stderr)
+
+ Path(cache_dir).mkdir(parents=True, exist_ok=True)
+ cache_path.write_text("\n".join(fens) + "\n")
+ print(f" wrote {len(fens)} positions to cache {cache_path}", file=sys.stderr)
+ return fens
+
+
+def main():
+ ap = argparse.ArgumentParser()
+ ap.add_argument("pgn_paths", nargs="+", help="one or more PGN pool files")
+ ap.add_argument("--n", type=int, default=20000)
+ ap.add_argument("--min-ply", type=int, default=4,
+ help="skip book-ish very early plies")
+ ap.add_argument("--seed", type=int, default=0)
+ ap.add_argument("--cache-dir", default="eval_tune/position_cache")
+ ap.add_argument("--out", required=True, help="output .ep_ file")
+ ap.add_argument("--stratify", action="store_true",
+ help="fill 8 equal-size combined-material buckets "
+ "up to quota each, instead of taking positions "
+ "in natural (middlegame-skewed) draw order")
+ args = ap.parse_args()
+
+ fens = sample_positions(args.pgn_paths, args.n, args.min_ply, args.seed,
+ args.cache_dir, stratify=args.stratify)
+ with open(args.out, "w") as f:
+ for fen in fens:
+ f.write(f"setboard {fen}\n")
+ f.write("go\n")
+ print(f"Wrote {len(fens)} positions ({2 * len(fens)} lines) to {args.out}")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/src/eval_tune/test_vs_head.sh b/src/eval_tune/test_vs_head.sh
index 29c050f..e937389 100755
--- a/src/eval_tune/test_vs_head.sh
+++ b/src/eval_tune/test_vs_head.sh
@@ -36,6 +36,10 @@ LABEL="${3:-match}"
if [ -n "$2" ]; then
CANDIDATE="$2"
else
+ cd ..
+ gmake clean
+ gmake release MP=1 PERF_COUNTERS=1
+ cd eval_tune
CANDIDATE=/tmp/typhoon/typhoon_match_play_candidate
cp -f ../typhoon ${CANDIDATE}
fi