Code Monkey home page Code Monkey logo

caissa's Introduction

Caissa Chess Engine

LinuxBuildStatus

ArtImage

(image generated with DALL·E 2)

Overview

Strong, UCI command-line chess engine, written from scratch in C++ in development since early 2021. Optimized for regular chess, FRC (Fischer Random Chess) and DFRC (Double Fischer Random Chess).

Playing strength

Caissa is listed on many chess engines ranking lists:

History / Originality

The engine has been written from the ground up. In early versions it used a simple PeSTO evaluation, which was replaced by the Stockfish NNUE for a short time. Since version 0.7, Caissa uses it's own efficiently updated neural network, trained with Caissa self-play games using a custom trainer. In a way, the first own Caissa network is based on Stockfish's network, but it was much weaker because of the small data set used back then (a few million positions). Currently (as of version 1.18) over 12 billion newly generated positions are used. Also, the old self-play games are successively purged, so that the newer networks are trained only on the most recent games generated by the most recent engine, and so on.

The runtime neural network evaluation code is located in PackedNeuralNetwork.cpp and was inspired by nnue.md document. The neural network trainer is written completely from scratch and is located in NetworkTrainer.cpp, NeuralNetwork.cpp and other NeuralNetwork* files. The trainer is purely CPU-based and is heavily optimized to take advantage of many threads and AVX instructions as well as it exploits the sparse nature of the nets.

The games are generated with the utility SelfPlay.cpp, which generates games with a fixed number of nodes/depth and saves them in a custom binary game format to save space. The opening books used are either Stefan's Pohl UHO books or DFRC openings with few random moves played at the beginning.

Supported UCI options

  • Hash (int) Sets the size of the transposition table in megabytes.
  • MultiPV (int) Sets the number of PV lines to search and print.
  • MoveOverhead (int) Sets move overhead in milliseconds. Should be increased if the engine loses time.
  • Threads (int) Sets the number of threads used for searching.
  • Ponder (bool) Enables pondering.
  • EvalFile (string) Neural network evaluation file.
  • EvalRandomization (int) Allows introducing non-determinism and weakens the engine.
  • StaticContempt (int) Static contempt value used throughout whole game.
  • DynamicContempt (int) Dynamic contempt value used in the opening/middlegame stage.
  • SyzygyPath (string) Semicolon-separated list of paths to Syzygy endgame tablebases.
  • SyzygyProbeLimit (int) Maximum number of pieces on the board where Syzygy tablebases can be used.
  • UCI_AnalyseMode (bool) Enables analysis mode: search full PV lines and disable any depth constraints.
  • UCI_Chess960 (bool) Enables chess 960 mode: castling moves are printed as "king captures rook".
  • UCI_ShowWDL (bool) Print win/draw/loss probabilities along with classic centipawn evaluation.
  • UseSAN (bool) Enables short algebraic notation output (FIDE standard) instead of default long algebraic notation.
  • ColorConsoleOutput (bool) Enables colored console output for better readability.

Provided EXE versions

  • AVX-512 - Fastest, requires a x64 CPU with AVX-512 instruction set support. May not be supported on consumer-grade CPUs.
  • BMI2 - Fast, requires a x64 CPU with AVX2 and BMI2 instruction set support. Supported by majority of modern CPUs.
  • AVX2 - Fast, requires a x64 CPU with AVX2 instruction set support. May be faster than BMI2 on some older CPUs (e.g. Intel Haswell processors).
  • POPCNT - Slower, requires a x64 CPU with SSE4 and POPCNT instruction set support. For older CPUs.
  • Legacy - Slowest, requires any x64 CPU. For very old x64 CPUs.

Features

General

  • UCI protocol
  • Neural network evaluation
  • Syzygy and Gaviota endgame tablebases support
  • Chess960 (Fischer Random) support

Search Algorithm

  • Negamax with alpha-beta pruning
  • Iterative Deepening with Aspiration Windows
  • Principal Variation Search (PVS)
  • Quiescence Search
  • Transposition Table
  • Multi-PV search
  • Multithreaded search via shared transposition table

Evaluation

  • Neural network evaluation
    • (5x768→1024)x2→1 architecture
    • effectively updated first layer
    • manually vectorized code supporting SSE2, AVX2, AVX-512 and ARM NEON instructions
    • clipped-ReLU activation function
    • 8 variants of last layer weights selected based on piece count
    • input features: absolute piece coordinates with horizontal symmetry, 11 king buckets
  • Special endgame evaluation routines

Neural net trainer

  • Custom CPU-based trainer using Adam algorithm
  • Heavily optimized using AVX instructions, multithreading, and exploiting sparsity of the first layer input
  • Network trained on data generated purely from self-play games

Misc

  • Large Pages Support for Transposition Table
  • Magic Bitboards
  • Handling non-standard chess positions (e.g. 64 pieces on the board, etc.)
  • Outstanding performance at ultra-short games (sub-second for whole game).

Modules

The projects comprises following modules:

  • backend (library) - engine's core
  • frontend (executable) - UCI wrapper for the backend
  • utils (executable) - various utilities, such as unit tests, neural network trainer, self-play data generator, etc.

Compilation

Linux - makefile

To compile for Linux just call make in src directory:

cd src
make -j

NOTE: This will compile the default AVX2/BMI2 version.

Linux - CMake

To compile for Linux using CMake:

mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Final ..
make -j

NOTE: Currently, the project compiles with AVX2/BMI2 support by default.

There are three configurations supported:

  • Final - final version, without asserts, etc.
  • Release - development version with asserts enabled and with optimizations enabled for better performance
  • Debug - development version with asserts enabled and optimizations disabled

Windows - Visual Studio

To compile for Windows, use GenerateVisualStudioSolution.bat to generate Visual Studio solution. The only tested Visual Studio version is 2022. Using CMake directly in Visual Studio was not tested.

NOTE: After compilation make sure you copy appropriate neural net file from data/neuralNets directory to location where executable file is generated (build/bin on Linux or build\bin\x64\<Configuration> on Windows).

caissa's People

Contributors

selimwaly avatar skiminki avatar witek902 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

caissa's Issues

Caissa 1.11 disconnections/Crashes/Timeouts

OS: Archlinux

Caissa works, tested in Nibbler GUI and everything was fine yet it seems something is wrong as I try to make it play games against other engines in Cutechess gui nor BanksiaGUI. I am curious about if it is also the same for Windows.

Android NDK

When I compile with Android NDK (which uses the Clang compiler) by using the flag "-DUSE_ARM_NEON", I get the following error message:

backend/PackedNeuralNetwork.cpp:544:33: fatal error: no matching function for call to 'vaddq_s32'
val += vaddvq_s32(vaddq_s32(vaddq_s32(sumA, sumB, vaddq_s32(sumC, sumD));

I assume the line must look like this:

val += vaddvq_s32(vaddq_s32(vaddq_s32(sumA, sumB), vaddq_s32(sumC, sumD)));

Finally, an additional "#include <cmath>" is required in Evaluate.hpp.

With these changes, Caissa compiles smoothly with Android NDK.

connection stalls in CuteChess

i let Caissa v1.3 play several games in CuteChess .. all went well but it lost the last game on time : "connection stalls" .. see screenshot. At that moment Caissa had a lost position anyway, maybe that was the cause, but it shouldn't happen. The CuteChess log did not show anything. I don't know what can be done to fix this, i just want to mention this incident.

Caissa_v1-3_connection_stalls

how to get PNN file ?

hi, i managed to compile your newest Caissa on Linux .. the UCI info says v1.6.11 but also : Failed to load default neural network eval-7.pnn .. indeed this file is missing, where to get it ?

unable to compile v1.7 on Linux

again i'm unable to compile your (v1.7) source files on Linux .. i tried to get the source files by git clone and also the supplied ZIP, both fail :

[ i'm on Xubuntu 22.04 ]

$ make
[  1%] Building CXX object src/backend/CMakeFiles/backend.dir/Bitboard.cpp.o
[  3%] Building CXX object src/backend/CMakeFiles/backend.dir/Common.cpp.o
[  5%] Building CXX object src/backend/CMakeFiles/backend.dir/Endgame.cpp.o
In file included from /home/roelof/Compiled/Caissa-1.7/src/backend/Endgame.cpp:7:
/home/roelof/Compiled/Caissa-1.7/src/backend/MoveGen.hpp:13:8: error: duplicate ‘inline’
   13 | INLINE inline void GeneratePromotionsMoveList(const Square from, const Square to, MoveList& outMoveList)
      |        ^~~~~~
      |        ------
In file included from /home/roelof/Compiled/Caissa-1.7/src/backend/Endgame.cpp:7:
/home/roelof/Compiled/Caissa-1.7/src/backend/MoveGen.hpp: In lambda function:
/home/roelof/Compiled/Caissa-1.7/src/backend/MoveGen.hpp:59:101: error: expected primary-expression before ‘)’ token
   59 |                 Square(targetIndex).Shift_Unsafe<pawnRevDirection>().Shift_Unsafe<pawnRevDirection>(),
      |                                                                                                     ^
In file included from /home/roelof/Compiled/Caissa-1.7/src/backend/Endgame.cpp:8:
/home/roelof/Compiled/Caissa-1.7/src/backend/Math.hpp: In function ‘uint64_t MulHi64(uint64_t, uint64_t)’:
/home/roelof/Compiled/Caissa-1.7/src/backend/Math.hpp:35:23: warning: ISO C++ does not support ‘__int128’ for ‘type name’ [-Wpedantic]
   35 |     return ((unsigned __int128)a * (unsigned __int128)b) >> 64;
      |                       ^~~~~~~~
/home/roelof/Compiled/Caissa-1.7/src/backend/Math.hpp:35:46: warning: ISO C++ does not support ‘__int128’ for ‘type name’ [-Wpedantic]
   35 |     return ((unsigned __int128)a * (unsigned __int128)b) >> 64;
      |                                              ^~~~~~~~
In file included from /home/roelof/Compiled/Caissa-1.7/src/backend/Endgame.cpp:7:
/home/roelof/Compiled/Caissa-1.7/src/backend/MoveGen.hpp: In instantiation of ‘void GeneratePawnMoveList(const Position&, MoveList&) [with MoveGenerationMode mode = MoveGenerationMode::Quiets; Color sideToMove = Color::White]’:
/home/roelof/Compiled/Caissa-1.7/src/backend/MoveGen.hpp:227:43:   required from ‘void GenerateMoveList(const Position&, MoveList&) [with MoveGenerationMode mode = MoveGenerationMode::Quiets; Color sideToMove = Color::White]’
/home/roelof/Compiled/Caissa-1.7/src/backend/MoveGen.hpp:273:45:   required from ‘void GenerateMoveList(const Position&, MoveList&) [with MoveGenerationMode mode = MoveGenerationMode::Quiets]’
/home/roelof/Compiled/Caissa-1.7/src/backend/MoveGen.hpp:284:49:   required from here
/home/roelof/Compiled/Caissa-1.7/src/backend/MoveGen.hpp:59:82: error: no match for ‘operator<’ (operand types are ‘<unresolved overloaded function type>’ and ‘const Direction’)
   59 |                 Square(targetIndex).Shift_Unsafe<pawnRevDirection>().Shift_Unsafe<pawnRevDirection>(),
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/home/roelof/Compiled/Caissa-1.7/src/backend/MoveGen.hpp: In instantiation of ‘void GeneratePawnMoveList(const Position&, MoveList&) [with MoveGenerationMode mode = MoveGenerationMode::Quiets; Color sideToMove = Color::Black]’:
/home/roelof/Compiled/Caissa-1.7/src/backend/MoveGen.hpp:227:43:   required from ‘void GenerateMoveList(const Position&, MoveList&) [with MoveGenerationMode mode = MoveGenerationMode::Quiets; Color sideToMove = Color::Black]’
/home/roelof/Compiled/Caissa-1.7/src/backend/MoveGen.hpp:277:45:   required from ‘void GenerateMoveList(const Position&, MoveList&) [with MoveGenerationMode mode = MoveGenerationMode::Quiets]’
/home/roelof/Compiled/Caissa-1.7/src/backend/MoveGen.hpp:284:49:   required from here
/home/roelof/Compiled/Caissa-1.7/src/backend/MoveGen.hpp:59:82: error: no match for ‘operator<’ (operand types are ‘<unresolved overloaded function type>’ and ‘const Direction’)
At global scope:
cc1plus: note: unrecognized command-line option ‘-Wno-four-char-constants’ may have been intended to silence earlier diagnostics
cc1plus: note: unrecognized command-line option ‘-Wno-nested-anon-types’ may have been intended to silence earlier diagnostics
cc1plus: note: unrecognized command-line option ‘-Wno-gnu-anonymous-struct’ may have been intended to silence earlier diagnostics
make[2]: *** [src/backend/CMakeFiles/backend.dir/build.make:104: src/backend/CMakeFiles/backend.dir/Endgame.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:149: src/backend/CMakeFiles/backend.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

more endgame knowledge

hi, your v1.9 release page states "Add more endgame knowledge, should evaluate some fortress positions better" .. well, a known issue here is mate with 2 kNights, which will never happen, but many engines do not fear to exchange pieces and pawns to reach such position, while they think it is winning due to the material balance .. however, without a pawn, White team KkPP will never win, eg. see https://en.wikipedia.org/wiki/Two_knights_endgame .. Btw. also read about the exception : the opponent might have (only) one pawn, then a mate is possible ..

is Caissa aware of this matter ?

about the Contempt options

hi,

i just managed to compile your v1.12 on Xubuntu 22.04, AVX2 version .. i see two "contempt" options 'Static' and 'Dynamic' .. what are those ?

as i understand 'contempt' is something like 'optimism' .. i can imagine how playing style & bestmove can be different when setting such values .. maybe you even have test positions to show the effect of these options ?

Reflect a bug in Caissa1.14.1

3r3k/7p/2Q5/8/2B2PK1/6P1/4P3/5q2 b - - 98 99
Please forgive me for my poor English. Caissa1.14.1 is very powerful! But there is a bug, the above situation went through Rd8-d4 in the engine league, resulting in one step being killed by the king. I hope to correct it.

compile on Linux ?

is the source code also for Linux ? I guess not .. after doing cmake -DCMAKE_BUILD_TYPE=Debug i get a makefile .. then i do make but i get these errors :

~/Compiled/Caissa$ make
[  2%] Building CXX object src/backend/CMakeFiles/backend.dir/Bitboard.cpp.o
[  4%] Building CXX object src/backend/CMakeFiles/backend.dir/Common.cpp.o
[  6%] Building CXX object src/backend/CMakeFiles/backend.dir/Endgame.cpp.o
[  8%] Building CXX object src/backend/CMakeFiles/backend.dir/Evaluate.cpp.o
In file included from /home/roelof/Compiled/Caissa/src/backend/Evaluate.cpp:8:
/home/roelof/Compiled/Caissa/src/backend/Search.hpp:247:44: error: ‘unique_ptr’ in namespace ‘std’ does not name a template type
  247 |         using NNEvaluatorContextPtr = std::unique_ptr<NNEvaluatorContext>;
      |                                            ^~~~~~~~~~
/home/roelof/Compiled/Caissa/src/backend/Search.hpp:8:1: note: ‘std::unique_ptr’ is defined in header ‘<memory>’; did you forget to ‘#include <memory>’?
    7 | #include "Memory.hpp"
  +++ |+#include <memory>
    8 | #include "NeuralNetworkEvaluator.hpp"
/home/roelof/Compiled/Caissa/src/backend/Search.hpp:248:9: error: ‘NNEvaluatorContextPtr’ does not name a type; did you mean ‘NNEvaluatorContext’?
  248 |         NNEvaluatorContextPtr nnContextStack[MaxSearchDepth];
      |         ^~~~~~~~~~~~~~~~~~~~~
      |         NNEvaluatorContext
cc1plus: note: unrecognized command-line option ‘-Wno-four-char-constants’ may have been intended to silence earlier diagnostics
cc1plus: note: unrecognized command-line option ‘-Wno-nested-anon-types’ may have been intended to silence earlier diagnostics
cc1plus: note: unrecognized command-line option ‘-Wno-gnu-anonymous-struct’ may have been intended to silence earlier diagnostics
make[2]: *** [src/backend/CMakeFiles/backend.dir/build.make:118: src/backend/CMakeFiles/backend.dir/Evaluate.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:149: src/backend/CMakeFiles/backend.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

Compiles

Hi,

may you release a non-AVX2/BMI version too, please? I can't run it, I'm limited to AVS and SSE.

Thanx,
Alex

Android compitability

Any chance that the source code will become compatible with Android again (arm64-v8a and armeabi-v7a)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.