Thursday, May 08, 2025

chess engine: numbfish: py + numpy NNUE "AI"

 dimdano/numbfish: ♟︎ A strong python chess bot running on 1-thread @GitHub

Numbfish is a simple but strong pythonic chess engine. Numbfish is based on Sunfish but with several additional features, the most important of which is an Efficiently Updatable Neural Network (NNUE) implemented and optimized first time using numpy

NNUE is efficiently implemented using incremental updates of the input layer outputs in make and unmake moves just like Stockfish does in C++. The additional positional information entailed from NNUE makes this engine probably the strongest python engine running on 1-thread CPU.

Numbfish keeps a very simple but optimized python interface, taking up just 140 lines of code! (see compressed.pyYet it plays at 2300 ratings at Lichess. 





NNUE (Efficiently Updatable Neural Network) is a technique used in computer chess, particularly in Stockfish, to improve the evaluation of chess positions. It involves using a neural network to replace the traditional handcrafted evaluation function, which assigns a value to a chess position. NNUE achieves this by training a neural network on a massive dataset of positions and their evaluations, enabling it to learn and generalize the patterns of a strong chess engine.

Efficiently updatable neural network - Wikipedia


Now even Sunfish.py "evolved" to have NNUE, and that increased number of lines of code to 131.
Sunfish is a simple, but strong chess engine, written in Python. With its simple UCI interface, and removing comments and whitespace, it takes up just 131 lines of code! (build/clean.sh sunfish.py | wc -l). Yet it plays at ratings above 2000 at Lichess.

In contrast to the large NNUE in say, Stockfish, this network is only 1207 bytes! That makes sure sunfish NNUE can still be packed into less than 4KB. Using NNUE, sunfish will play better positionally, but worse tactically, since the implementation is still not fast enough.

to run current version of sunfish.py on Windows computers (i.e. with default PowerShell console) provided commands from GitHub will not work. Those are for unix based systems (Mac, Linux).
This works

python .\tools\fancy.py -cmd "python .\sunfish.py"

The reason is that sunfish.py, like many other chess playing programs is now using "UCI" interface, no embedded UI, and requires a separate UI app, in this case "fancy.py".


here is one all-in-one chess app in python, but much bigger and more complex then above two. 

No comments: