UCI engine: python -m chess_nnue.uci
Browse filesFiles changed (4)
196 unmodified lines197198199200201202196 unmodified lines```- Browser board: `python -m chess_nnue.ui` (http://127.0.0.1:7863): drag or click to move, promotion picker, eval bar, take back, flip.- Lichess bot: `chess_nnue/lichess_bot.py` is a [lichess-bot](https://github.com/lichess-bot-devs/lichess-bot) "homemade" engine.## Run the benchmarks196 unmodified lines197198199200201202203196 unmodified lines```- Browser board: `python -m chess_nnue.ui` (http://127.0.0.1:7863): drag or click to move, promotion picker, eval bar, take back, flip.- UCI engine (any chess GUI or harness, e.g. Cute Chess, Arena, python-chess): `python -m chess_nnue.uci` from `reasoning_harness/`.- Lichess bot: `chess_nnue/lichess_bot.py` is a [lichess-bot](https://github.com/lichess-bot-devs/lichess-bot) "homemade" engine.## Run the benchmarks
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778"""depth 5 as a UCI engine, so any chess GUI or harness can run it.
python -m chess_nnue.uci [--weights PATH] (run from reasoning_harness/)
Supports: uci, isready, ucinewgame, position (startpos/fen + moves), go(movetime / wtime btime winc binc / depth), quit.shortcut: search runs synchronously, so "stop" is ignored until the move is found(the search always ends at its deadline); add a search thread if a GUI needs ponder/infinite."""import argparseimport sysfrom pathlib import Path
import chess
from .engine import Engine, MATEfrom .net import NNUE
DEFAULT = Path(__file__).parent.parent / "artifacts" / "chess_nnue" / "chess.nnue"
def budget_ms(board, args): """Same clock rule as the benchmarks: a 20th of the clock plus most of the increment.""" opt = dict(zip(args[::2], args[1::2])) if "movetime" in opt: return max(10, int(opt["movetime"]) - 20) side = "w" if board.turn == chess.WHITE else "b" if side + "time" in opt: clock, inc = int(opt[side + "time"]), int(opt.get(side + "inc", 0)) return max(10, min(clock / 20 + inc * 0.75, clock / 2 - 50)) return 1000
def main(argv=None, inp=sys.stdin, out=sys.stdout): ap = argparse.ArgumentParser() ap.add_argument("--weights", default=str(DEFAULT)) a = ap.parse_args(argv) nnue = NNUE(a.weights) engine, board = Engine(nnue), chess.Board()
def say(line): out.write(line + "\n") out.flush()
for line in inp: parts = line.split() if not parts: continue cmd, args = parts[0], parts[1:] if cmd == "uci": say("id name depth 5") say("id author Kedhareswer") say("uciok") elif cmd == "isready": say("readyok") elif cmd == "ucinewgame": engine = Engine(nnue) elif cmd == "position": if args[0] == "startpos": board, rest = chess.Board(), args[1:] else: end = args.index("moves") if "moves" in args else len(args) board, rest = chess.Board(" ".join(args[1:end])), args[end:] for u in rest[1:]: board.push_uci(u) elif cmd == "go": opt = dict(zip(args[::2], args[1::2])) move, info = engine.search(board, budget_ms(board, args), int(opt.get("depth", 64))) s = info["score"] score = f"mate {(MATE - abs(s) + 1) // 2 * (1 if s > 0 else -1)}" if abs(s) >= MATE - 100 else f"cp {s}" say(f"info depth {info['depth']} nodes {info['nodes']} time {int(info['ms'])} score {score}") say(f"bestmove {move.uci()}") elif cmd == "quit": break
if __name__ == "__main__": main()61 unmodified lines62636465666738 unmodified lines10610710810911011161 unmodified lines.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; }h1 { font-size: 18px; margin: 0; letter-spacing: -.01em; }.sub { color: var(--muted); font-size: 13px; margin: 2px 0 0; }.player { display: flex; align-items: center; gap: 10px; padding: 6px 0; }.player + .player { border-top: 1px solid var(--line); }.swatch { width: 14px; height: 14px; border-radius: 3px; border: 1px solid var(--line); flex: none; }38 unmodified lines<div class="card c-about"><h1>depth 5</h1><p class="sub">A 4.8 MB neural network plus alpha-beta search, running on CPU. Drag a piece or click it, then click a square.</p></div><div class="card c-players" aria-live="polite"><div class="player" id="p-engine"><span class="swatch"></span><span class="name">depth 5</span><span class="meta" id="eng-meta"></span><span class="dots" aria-label="thinking"><i></i><i></i><i></i></span></div>61 unmodified lines6263646566676838 unmodified lines10710810911011111211361 unmodified lines.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; }h1 { font-size: 18px; margin: 0; letter-spacing: -.01em; }.sub { color: var(--muted); font-size: 13px; margin: 2px 0 0; }.sub a { color: var(--accent); }.player { display: flex; align-items: center; gap: 10px; padding: 6px 0; }.player + .player { border-top: 1px solid var(--line); }.swatch { width: 14px; height: 14px; border-radius: 3px; border: 1px solid var(--line); flex: none; }38 unmodified lines<div class="card c-about"><h1>depth 5</h1><p class="sub">A 4.8 MB neural network plus alpha-beta search, running on CPU. Drag a piece or click it, then click a square.</p><p class="sub"><a href="https://llm-chess-agentic.vercel.app" target="_blank" rel="noopener">Play it against LLMs in LLM Chess Arena</a> · <a href="https://huggingface.co/Kedhareswer/depth-5" target="_blank" rel="noopener">model and benchmarks</a></p></div><div class="card c-players" aria-live="polite"><div class="player" id="p-engine"><span class="swatch"></span><span class="name">depth 5</span><span class="meta" id="eng-meta"></span><span class="dots" aria-label="thinking"><i></i><i></i><i></i></span></div>
114 unmodified lines115116117118119120114 unmodified linesb = chess.Board("r1bqkb1r/ppp2p2/2n2n1p/4p1p1/P5P1/5P1N/1PPBP2P/RN1QKB1R w KQkq - 2 8")self.assertNotEqual(Engine(PST_Eval(), "plain").search(b, 1500)[0], chess.Move.from_uci("h3g5"))def test_finds_mate_in_one(self):b = chess.Board("6k1/5ppp/8/8/8/8/8/R5K1 w - - 0 1")for eng in (Engine(self.nn), Engine(PST_Eval(), "plain")):114 unmodified lines115116117118119120121122123124125126127128129130131132133134135136137114 unmodified linesb = chess.Board("r1bqkb1r/ppp2p2/2n2n1p/4p1p1/P5P1/5P1N/1PPBP2P/RN1QKB1R w KQkq - 2 8")self.assertNotEqual(Engine(PST_Eval(), "plain").search(b, 1500)[0], chess.Move.from_uci("h3g5"))def test_uci_engine_plays_legal_moves(self):import sysimport chess.engineroot = Path(__file__).parent.parentuci = chess.engine.SimpleEngine.popen_uci([sys.executable, "-m", "chess_nnue.uci", "--weights", str(self.path)], cwd=root)try:self.assertEqual(uci.id["name"], "depth 5")board = chess.Board()for _ in range(6):move = uci.play(board, chess.engine.Limit(time=0.2)).moveself.assertIn(move, board.legal_moves)board.push(move)mate = chess.Board("6k1/5ppp/8/8/8/8/8/R5K1 w - - 0 1")self.assertEqual(uci.play(mate, chess.engine.Limit(white_clock=5, black_clock=5)).move, chess.Move.from_uci("a1a8"))finally:uci.quit()def test_finds_mate_in_one(self):b = chess.Board("6k1/5ppp/8/8/8/8/8/R5K1 w - - 0 1")for eng in (Engine(self.nn), Engine(PST_Eval(), "plain")):
README.md+1
chess_nnue/uci.pyAdded+78
chess_nnue/ui.html+2
tests/test_chess_nnue.py+17