All Projects → SamRagusa → Batch-First

SamRagusa / Batch-First

Licence: GPL-3.0 license
A JIT compiled chess engine which traverses the search tree in batches in a best-first manner, allowing for neural network batching, asynchronous GPU use, and vectorized CPU computations.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Batch-First

transonic
🚀 Make your Python code fly at transonic speeds!
Stars: ✭ 93 (+244.44%)
Mutual labels:  numpy, jit, numba
Pleco
A Rust-based re-write of the Stockfish Chess Engine
Stars: ✭ 137 (+407.41%)
Mutual labels:  chess, engine
Walleye
A chess engine written from scratch in Rust ♞
Stars: ✭ 82 (+203.7%)
Mutual labels:  chess, chess-ai
php-chess
A chess library for PHP.
Stars: ✭ 42 (+55.56%)
Mutual labels:  chess, engine
NumPyANN
Implementation of Artificial Neural Networks using NumPy
Stars: ✭ 85 (+214.81%)
Mutual labels:  numpy, ann
chessalyzer.js
A JavaScript library for batch analyzing chess games
Stars: ✭ 14 (-48.15%)
Mutual labels:  chess, batch
Sunfish
Sunfish: a Python Chess Engine in 111 lines of code
Stars: ✭ 2,254 (+8248.15%)
Mutual labels:  chess, chess-ai
Glumpy
Python+Numpy+OpenGL: fast, scalable and beautiful scientific visualization
Stars: ✭ 882 (+3166.67%)
Mutual labels:  engine, numpy
fastchess
Predicts the best chess move with 27.5% accuracy by a single matrix multiplication
Stars: ✭ 75 (+177.78%)
Mutual labels:  chess, chess-ai
libjit
Unofficial libjit mirror.
Stars: ✭ 46 (+70.37%)
Mutual labels:  jit, jit-compiler
neos
Language agnostic scripting engine with a custom bytecode JIT
Stars: ✭ 36 (+33.33%)
Mutual labels:  jit, jit-compiler
Minic
A simple chess engine to learn and play with
Stars: ✭ 65 (+140.74%)
Mutual labels:  chess, engine
ChessVisionBot
Chessbot using computer vision to play on any chess website
Stars: ✭ 32 (+18.52%)
Mutual labels:  chess, chess-ai
Bit-Genie
UCI chess engine in C++
Stars: ✭ 19 (-29.63%)
Mutual labels:  chess, chess-ai
Je
A distributed job execution engine for the execution of batch jobs, workflows, remediations and more.
Stars: ✭ 30 (+11.11%)
Mutual labels:  engine, batch
Irwin
irwin - the protector of lichess from all chess players villainous
Stars: ✭ 138 (+411.11%)
Mutual labels:  chess, numpy
Numba
NumPy aware dynamic Python compiler using LLVM
Stars: ✭ 7,090 (+26159.26%)
Mutual labels:  numpy, llvm
Numba Scipy
numba_scipy extends Numba to make it aware of SciPy
Stars: ✭ 98 (+262.96%)
Mutual labels:  numpy, llvm
FastLua
Lua trace JIT compiler using LLVM-C
Stars: ✭ 22 (-18.52%)
Mutual labels:  llvm, jit
adorad
Fast, Expressive, & High-Performance Programming Language for those who dare
Stars: ✭ 54 (+100%)
Mutual labels:  llvm, jit

Batch First

Batch First is a JIT compiled chess engine which traverses the search tree in batches in a best-first manner, allowing for neural network batching, asynchronous GPU use, and vectorized CPU computations. Utilizing NumPy's powerful ndarray for representing boards, TensorFlow for neural networks computations, and Numba to JIT compile it all, Batch First balances the rapid prototyping desired in artificial intelligence with the runtime efficiency needed for a competitive chess engine.

Engine Characteristics

The following table highlights a few key aspects of the Batch First engine.

Characteristic Explanation / Reasoning
Written in Python Python is both easily readable and extremely flexible, but it's runtime speed has historically prevented it's use in competitive chess engines. Through the use of high level packages, Batch First balances runtime speed and code readability
JIT Compiled To avoid the execution time of Python, Numba is used to JIT compile the Python code to native machine instructions using the LLVM compiler infrastructure
Batched ANN Inference By using a K-best-first search algorithm, evaluation of boards and moves can be done in batches, both minimizing the effect of latency and maximizing the throughput for GPUs
Priority Bins Best-first algorithms such as SSS* are often disregarded due to the cost of maintaining a global list of open nodes in priority order. This is addressed by instead using a pseudo-priority order, separating nodes based on their binned heuristic values
Vectorized Asynchronous CPU Operations Through a combination of NumPy and Numba, the array oriented computations are vectorized and compiled to run while the ANNs are being evaluated, and with the Python GIL released

Board Evaluation CNN

Input Features/Training Data

Boards are given to the ANN as an 8x8x15 one-hot encoding, which consists of 12 feature planes for each piece and color, 2 for each player's rooks with the ability to castle, and 1 for en passant capture squares. The label for each board is the precomputed value of the board by an established engine (currently StockFish is used).

Input Layers

To model the movement of chess pieces, a novel architecture is used where the ANNs 'first layer' is replaced with 9 convolutional layers. When concatenated, the squares considered by the input convolutions centered at any given square are the squares which could contain a piece able to threaten that square, and the square itself.

This is accomplished by a set of dilated padded convolutional layers, and can be explained in two parts.

  • An n-dilated convolutional layer with kernel size 3x3 will consider all potential rank, file, and diagonal threats n squares away from the kernel's center. Having 7 of these with dilation factors of 1-7 encompasses all potential movement of every piece but the knight.

  • To capture the movement of the knight, two convolutional layers with kernel size 2x2 and dilation factor 2x4 and 4x2 are used. Combined, the kernels of these two layers consider only the squares a knight has the potential to move to.

The following diagram shows the structure of the input layers:

1 2 3 4 5 6 7 8 9
Kernel Size 3x3 3x3 3x3 3x3 3x3 3x3 3x3 2x2 2x2
Dilation Factor 1x1 2x2 3x3 4x4 5x5 6x6 7x7 2x4 4x2

Loss

The network learns to score boards through classification methods, this is accomplished by learning an ordinal representation of the training boards. More specifically, it learns to classify pairs of boards as having the first or second board be preferable.

Extending this to batches of data, the calculated value of each board is compared against every other board in the batch (with unequal desired score).

Thus for a training batch B with desired and computed values D and C (shaped [1,n]), the network learns by minimizing the following:

equation

Where S is the sigmoid function, CrossEntropy is a function who's first and second parameters are logits and labels respectively, LowerTriangular is a function which replaces entries above the main diagonal with zeros, multiplication is done element-wise (Hadamard product), and subtraction is calculated using broadcasting (similar to NumPy's broadcasting).

If the values of D are unique, batch B will produce n(n-1)/2 pairs of boards to compare.

Dependencies

The versions listed are known to work, but are not necessarily the only versions which will work.

The tools listed below can be used, but are not needed. They provide significant speed improvements to the engine.

Miscellaneous Information

  • If you use this engine or the ideas it's built around to build or experiment with something interesting, please be vocal about it!
  • If you have any questions, ideas, or just want to say hi, feel free to get in contact with me!
  • Trained neural networks are not yet included (they still have some room to improve), but can/will be added if requested
  • Special thanks to the python-chess package which is heavily relied upon throughout the engine, and which most of the move generation code is based on

License

Batch First is licensed under the GPL 3. The full text can be found in the LICENSE.txt file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].