Skip to content

Commit

Permalink
Merge pull request #45 from SzilBalazs/nnue
Browse files Browse the repository at this point in the history
2.0
  • Loading branch information
SzilBalazs authored Oct 19, 2022
2 parents 8b47c66 + 91004a2 commit 5c0c545
Show file tree
Hide file tree
Showing 23 changed files with 1,118 additions and 751 deletions.
21 changes: 14 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,55 @@ jobs:
compiler: g++-11,
arch: popcnt,
target: BlackCore-popcnt-linux,
upload_target: true,
}
- {
name: "Ubuntu g++ modern",
os: ubuntu-latest,
compiler: g++-11,
arch: modern,
target: BlackCore-modern-linux,
upload_target: true,
}
- {
name: "Ubuntu g++ avx2",
os: ubuntu-latest,
compiler: g++-11,
arch: avx2,
target: BlackCore-avx2-linux,
}
- {
name: "Ubuntu g++ bmi2",
os: ubuntu-latest,
compiler: g++-11,
arch: bmi2,
target: BlackCore-bmi2-linux,
upload_target: true,
}
- {
name: "Windows g++ popcnt",
os: windows-latest,
compiler: g++,
arch: popcnt,
target: BlackCore-popcnt-win.exe,
upload_target: true,
}
- {
name: "Windows g++ modern",
os: windows-latest,
compiler: g++,
arch: popcnt,
target: BlackCore-modern-win.exe,
upload_target: true,
}
- {
name: "Windows g++ avx2",
os: windows-latest,
compiler: g++,
arch: avx2,
target: BlackCore-avx2-win.exe,
}
- {
name: "Windows g++ bmi2",
os: windows-latest,
compiler: g++,
arch: bmi2,
target: BlackCore-bmi2-win.exe,
upload_target: true,
}

steps:
Expand All @@ -84,7 +92,6 @@ jobs:
cd src
./${{matrix.config.target}} bench
- name: Upload artifact
if: ${{matrix.config.upload_target}}
uses: actions/upload-artifact@v3
with:
name: BlackCore
Expand Down
109 changes: 47 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

## Overview

BlackCore is a UCI compatible c++ chess engine written from scratch, capable of completing high performance tasks.
Its alpha beta search uses various pruning techniques, powered by a handcrafted evaluation function and a blazing fast
BlackCore is a grandmaster level UCI compatible c++ chess engine written from scratch.
Its alpha beta search uses various pruning techniques, powered by a neural network evaluation and a blazing fast
move generator.

## Files
Expand All @@ -22,7 +22,7 @@ This project contains the following files:

* UCI support
* Perft test
* Up to ~240M nps
* Up to ~240M nps (with NNUE accumulator disabled)
* Intel i3-7100 3.9Ghz CPU
* Single-threaded
* Hashing disabled
Expand All @@ -32,95 +32,64 @@ This project contains the following files:
* Bitboard representation
* Engine
* Search
* Parameters tuned using <a href="https://github.com/dsekercioglu/weather-factory">weather
factory</a>
* Iterative deepening
* Aspiration window
* Alpha-Beta
* Negamax
* Transposition table
* Cut-offs
* Entry aging
* Bucket system
* 1 always replace
* 1 depth preferred
* Principal variation search
* Late move reduction
* R = max(2, LMR_BASE + (log(moveIndex) * log(depth) / LMR_SCALE)));
* R = max(2, LMR_BASE + (ln(moveIndex) * ln(depth) / LMR_SCALE)));
* Move count/late move pruning
* Razoring
* Dropping into qsearch at frontier nodes
* Reverse futility pruning
* With improving detection
* Null move pruning
* Reduction depends on depth searched
* Quintessence search
* Stand-pat
* Delta pruning
* Static-exchange-evaluation pruning
* Move ordering
* Hash move
* Promotions
* Under promotions
* Captures
* MVV-LVA
* SEE
* Quiet moves
* Killer heuristic
* History heuristic
* MVV-LVA and SEE
* Killer and history heuristics
* Fast repetition detection
* Time management
* Allocate time using search stability
* Supports
* Sudden death
* Increment per move
* Move-time
* Moves to go
* Handcrafted evaluation
* Tapered eval
* Mid-game and end-game
* Material balance
* Piece-square tables
* Pawn structure
* Double pawns
* Isolated pawns
* Passed pawns
* King safety
* Pawn king shield
* Bonus for castled king
* Trapped rooks
* Penalty for enemy pieces threataning king
* Knight mobility
* Restricted by enemy pawns
* Bishop mobility
* Depending on the pawn structure
* Rooks
* Mobility
* Bonuses for open and half open files
* Tempo
* Time management based on search stability
* NNUE evaluation
* Trained using <a href="https://github.com/SzilBalazs/CoreTrainer">CoreTrainer</a>
* Training data was generated using <a href="https://github.com/jhonnold/berserk">Berserk</a> data
* Support for AVX2 architecture for vectorized accumulator updates
* Net embedded using incbin (for license see /src/incbin/UNLICENSE)

## Installation

### Building from source
### Building from source (recommended)

After downloading the sources (preferably the source of the latest release) you can run the following commands, to build
After downloading the source, you can run the following commands, to build
a native binary.
BlackCore uses c++20 standard, so older versions of compilers might not work.
This option gives the best performance.
**Please update your compiler before building!**

With any questions or problems feel free to create a github issue.

```
cd src
make clean build CXX=g++-11 ARCH=native
make clean build CXX=g++ ARCH=native
```

ARCH = popcnt/modern/bmi2/native
ARCH = popcnt/modern/avx2/bmi2/native

CXX = the compiler of your choice (I recommend using g++, as it gives the best performance)

### Downloading prebuilt binary

You can download the latest release <a href="https://github.com/SzilBalazs/BlackCore/releases/latest">here</a> both for
Windows and Linux.
To select the right binary, choose the bmi2 build if you have a fairly new CPU, otherwise you can use the modern build
or in case of an older
processor use the popcnt build. Only 64 bits CPUs with popcnt are supported at the moment.
To select the right binary use the first instruction set that your CPU supports (doesn't crash), in the order of BMI2 ->
AVX2 -> modern -> popcnt

## Usage

Expand All @@ -136,26 +105,42 @@ or <a href="http://www.playwitharena.de/">Arena</a>) for the best user experienc
this
higher if you notice that the engine often runs out of time.

## Big thanks to
## Special thanks to

### <a href="https://www.chessprogramming.org/Main_Page">Chess Programming Wiki</a>

The Chess Programming Wiki is the greatest
resource for everybody who wants to be informed about the basics and the state-of-the-art technologies of chess
programming.

### <a href="https://github.com/AndyGrant/OpenBench">OpenBench</a> by Andrew Grant
### <a href="https://github.com/AndyGrant/OpenBench">OpenBench</a> by <a href="https://github.com/AndyGrant">Andrew Grant</a>

OpenBench is an usefull SPRT testing framework, which contributed
to the development of BlackCore substantially.
OpenBench is a SPRT testing framework, used for the testing of different techniques in BlackCore

### <a href="https://github.com/official-stockfish/Stockfish">StockFish</a> by The StockFish team
### <a href="https://github.com/dsekercioglu/weather-factory">Weather factory</a> by <a href="https://github.com/dsekercioglu">Pali</a>

Thanks to the StockFish team for making such a wonderful and an easy-to-read codebase, that inspired me to get into
chess programming in the first place.
Weather factory was used to train various parameters of BlackCore using
the <a href="https://www.chessprogramming.org/SPSA">
SPSA method</a>.

### <a href="https://github.com/jhonnold/berserk">Berserk</a> by <a href="https://github.com/jhonnold">Jay</a>

Berserk is a strong chess engine that generated the training data with the contribution of Sohail which was used in the
latest neural network and
crucial for the progress made in BlackCore.

### <a href="https://github.com/TheBlackPlague">Shaheryar Sohail</a>

Sohail (developer of <a href="https://github.com/TheBlackPlague/StockNemo">StockNemo</a>) helped me in countless
problems
regarding NNUE and I really can't thank him enough

### <a href="https://github.com/Disservin/Smallbrain">Smallbrain</a> by <a href="https://github.com/Disservin">Disservin</a>

Smallbrain is a great chess engine which helped me understand many important concepts, and thanks to Disservin for
giving me many great ideas how can I further improve my engine.

### <a href="https://github.com/official-stockfish/Stockfish">StockFish</a> by The StockFish team

Thanks to the StockFish team for making such a wonderful and an easy-to-read codebase, that inspired me to get into
chess programming in the first place.
15 changes: 10 additions & 5 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ CXX = g++
TARGET_FLAGS = -static -static-libgcc -static-libstdc++
ARCH=native
NAME = BlackCore
VERSION_MAJOR = 1
VERSION_MINOR = 1
VERSION_MAJOR = 2
VERSION_MINOR = 0
OBJECT_DIR = objects
SOURCES := $(wildcard *.cpp)
OBJECTS := $(patsubst %.cpp, $(OBJECT_DIR)/%.o, $(SOURCES))
Expand All @@ -27,12 +27,17 @@ ifeq ($(ARCH), native)
endif

ifeq ($(ARCH), bmi2)
ARCH_FLAGS = -march=x86-64 -mpopcnt -msse -msse2 -msse3 -mssse3 -msse4.1 -mbmi2
DEFINE_FLAGS = -DBMI2
ARCH_FLAGS = -march=x86-64 -mpopcnt -msse -msse2 -mssse3 -msse4.1 -mavx2 -mbmi2
DEFINE_FLAGS = -DAVX2 -DBMI2
endif

ifeq ($(ARCH), avx2)
ARCH_FLAGS = -march=x86-64 -mpopcnt -msse -msse2 -mssse3 -msse4.1 -mavx2
DEFINE_FLAGS = -DAVX2
endif

ifeq ($(ARCH), modern)
ARCH_FLAGS = -march=x86-64 -mpopcnt -msse -msse2 -msse3 -mssse3 -msse4.1
ARCH_FLAGS = -march=x86-64 -mpopcnt -msse -msse2 -mssse3 -msse4.1
endif

ifeq ($(ARCH), popcnt)
Expand Down
2 changes: 2 additions & 0 deletions src/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const TestPosition testPositions[posCount] = {
};

void testPerft() {
initSearch();
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
U64 totalNodes = 0;
bool ok = true;
Expand All @@ -78,6 +79,7 @@ void testPerft() {
}

void testSearch() {
initSearch();
ttResize(searchTestHashSize);

startSearch(0, 0, 0, 0);
Expand Down
6 changes: 1 addition & 5 deletions src/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
#include <nmmintrin.h>
#endif

#if defined(NATIVE) && defined(__BMI2__)
#define BMI2
#endif

struct Bitboard {

U64 bb = 0;
Expand Down Expand Up @@ -65,7 +61,7 @@ struct Bitboard {
_BitScanForward64(&a, bb);
return Square(a);
#else
#error "Unable to determine least significant bit please use a compatible compiler!"
#error "Unsupported compiler!"
#endif

}
Expand Down
9 changes: 9 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@

// #define TUNE

#if defined(NATIVE) && defined(__BMI2__)
#define BMI2
#endif

#if defined(NATIVE) && defined(__AVX2__)
#define AVX2
#endif


typedef uint64_t U64;
typedef int32_t Score;
typedef int32_t Depth;
Expand Down
Binary file added src/corenet.bin
Binary file not shown.
Loading

0 comments on commit 5c0c545

Please sign in to comment.