Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update NNUE arch to (768->1536x)x16->1x8 #493

Merged
merged 35 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f058a6c
read bucketed net
PGG106 Nov 16, 2024
07115dc
add buckets array
PGG106 Nov 16, 2024
4f906ef
ue stuff
PGG106 Nov 16, 2024
f0b3969
mirror the array
PGG106 Nov 16, 2024
d1f6b09
restore old flip thing
PGG106 Nov 16, 2024
7fdeb97
fix missing space
PGG106 Nov 16, 2024
5d958a6
fix another space
PGG106 Nov 16, 2024
3c2cf91
inline getBucket
PGG106 Nov 16, 2024
a9e05e3
use piece on
PGG106 Nov 16, 2024
41b2073
satisfy ocd
PGG106 Nov 16, 2024
bd4d8cf
fixxing
PGG106 Nov 16, 2024
77c6f61
fix ue 4real
PGG106 Nov 16, 2024
302f66b
fix embedded net reading
PGG106 Nov 16, 2024
1638207
add net
PGG106 Nov 17, 2024
a4d7e70
add net
PGG106 Nov 17, 2024
a71e0d1
add net
PGG106 Nov 18, 2024
9282155
remove useless sort
PGG106 Nov 19, 2024
a5fd8ef
another net
PGG106 Nov 20, 2024
a7fd881
fix me being a dumbass
PGG106 Nov 20, 2024
6ff4fe2
fix me being a dumbass
PGG106 Nov 20, 2024
6e92391
early net
PGG106 Nov 21, 2024
7dc36a5
early net
PGG106 Nov 21, 2024
1f7a379
early net
PGG106 Nov 21, 2024
bf026d9
new net
PGG106 Nov 22, 2024
9b49db2
Merge pull request #490 from PGG106/master
PGG106 Dec 11, 2024
9ded991
new net
PGG106 Dec 11, 2024
d6a99b8
16 buckets
PGG106 Dec 14, 2024
a817774
make bucket layout more readable
PGG106 Dec 14, 2024
4a271af
start tweaking makefile
PGG106 Dec 14, 2024
6382834
nettus remuvus
PGG106 Dec 14, 2024
1bc1d08
update readme
PGG106 Dec 14, 2024
4b3b771
tweak readme
PGG106 Dec 15, 2024
b853def
add net downloading to CI
PGG106 Dec 15, 2024
41793a8
rename net in CI
PGG106 Dec 15, 2024
aa6619d
update version
PGG106 Dec 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/alexandria.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ jobs:
msystem: ${{matrix.config.msys_sys}}
install: mingw-w64-${{matrix.config.msys_env}} make git expect

- name: download net
run: curl -sOL https://github.com/PGG106/Alexandria-networks/releases/download/net01/1536x16.net

- name: make
if: runner.os == 'Linux'
run: make build=debug
run: make build=debug EVALFILE=1536x16.net -j

- name: make build windows
if: runner.os == 'Windows'
run: make build=release
run: make build=release EVALFILE=1536x16.net -j

- name: Bench
run: |
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
## Building
> [!WARNING]
> Due to a bug present in older gcc versions compiling with versions < 12 isn't officially supported.
Clone the Alexandria repository.
```bash
$ git clone https://github.com/PGG106/Alexandria
$ cd alexandria
```
Download the latest neural network for Alexandria from this [repository](https://github.com/PGG106/Alexandria-networks/releases) and save it in the source root as `nn.net`.
```bash
$ make
$ ./Alexandria
```
Expand Down
3 changes: 1 addition & 2 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NETWORK_NAME = nn.net
_THIS := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
_ROOT := $(_THIS)
EVALFILE = $(NETWORK_NAME)
EVALFILE = nn.net
CXX := g++
TARGET := Alexandria
WARNINGS = -Wall -Wcast-qual -Wextra -Wshadow -Wdouble-promotion -Wformat=2 -Wnull-dereference -Wlogical-op -Wold-style-cast -Wundef -pedantic
Expand Down
Binary file removed nn.net
Binary file not shown.
14 changes: 10 additions & 4 deletions src/makemove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ void ClearPiece(const int piece, const int from, Position* pos) {
// Do this first because if we happened to have moved the king we first need to get1lsb the king bitboard before removing it
if constexpr(UPDATE){
std::array<bool, 2> flip({get_file[KingSQ(pos, WHITE)] > 3, get_file[KingSQ(pos, BLACK)] > 3});
pos->AccumulatorTop().AppendSubIndex(piece, from, flip);
auto wkSq = KingSQ(pos, WHITE);
auto bkSq = KingSQ(pos, BLACK);
pos->AccumulatorTop().AppendSubIndex(piece, from, wkSq, bkSq, flip);
}
const int color = Color[piece];
pop_bit(pos->bitboards[piece], from);
Expand Down Expand Up @@ -55,7 +57,9 @@ void AddPiece(const int piece, const int to, Position* pos) {
// Do this last because if we happened to have moved the king we first need to re-add to the piece bitboards least we get1lsb an empty bitboard
if constexpr(UPDATE){
std::array<bool, 2> flip({get_file[KingSQ(pos, WHITE)] > 3, get_file[KingSQ(pos, BLACK)] > 3});
pos->AccumulatorTop().AppendAddIndex(piece, to, flip);
auto wkSq = KingSQ(pos, WHITE);
auto bkSq = KingSQ(pos, BLACK);
pos->AccumulatorTop().AppendAddIndex(piece, to, wkSq, bkSq, flip);
}
}

Expand Down Expand Up @@ -330,9 +334,11 @@ void MakeMove(const Move move, Position* pos) {
// Figure out if we need to refresh the accumulator
if constexpr (UPDATE) {
if (PieceType[Piece(move)] == KING) {
if (shouldFlip(From(move), To(move))) {
auto kingColor = Color[Piece(move)];
const auto startBucket = kingColor == WHITE ? (getBucket(From(move) ^ 56)) : (getBucket(From(move)));
const auto endBucket = kingColor == WHITE ? (getBucket(To(move) ^ 56)) : (getBucket(To(move)));
if (shouldFlip(From(move), To(move)) || startBucket != endBucket) {
// tell the right accumulator it'll need a refresh
auto kingColor = Color[Piece(move)];
pos->accumStack[pos->accumStackHead-1].perspective[kingColor].needsRefresh = true;
}
}
Expand Down
17 changes: 10 additions & 7 deletions src/nnue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void NNUE::init(const char *file) {
const size_t fileSize = sizeof(Network);
const size_t objectsExpected = fileSize / sizeof(int16_t);

read += fread(net.FTWeights, sizeof(int16_t), NUM_INPUTS * L1_SIZE, nn);
read += fread(net.FTWeights, sizeof(int16_t), INPUT_BUCKETS * NUM_INPUTS * L1_SIZE, nn);
read += fread(net.FTBiases, sizeof(int16_t), L1_SIZE, nn);
read += fread(net.L1Weights, sizeof(int16_t), L1_SIZE * 2 * OUTPUT_BUCKETS, nn);
read += fread(net.L1Biases, sizeof(int16_t), OUTPUT_BUCKETS, nn);
Expand All @@ -55,8 +55,8 @@ void NNUE::init(const char *file) {
} else {
// if we don't find the nnue file we use the net embedded in the exe
uint64_t memoryIndex = 0;
std::memcpy(net.FTWeights, &gEVALData[memoryIndex], NUM_INPUTS * L1_SIZE * sizeof(int16_t));
memoryIndex += NUM_INPUTS * L1_SIZE * sizeof(int16_t);
std::memcpy(net.FTWeights, &gEVALData[memoryIndex], INPUT_BUCKETS * NUM_INPUTS * L1_SIZE * sizeof(int16_t));
memoryIndex += INPUT_BUCKETS * NUM_INPUTS * L1_SIZE * sizeof(int16_t);
std::memcpy(net.FTBiases, &gEVALData[memoryIndex], L1_SIZE * sizeof(int16_t));
memoryIndex += L1_SIZE * sizeof(int16_t);

Expand Down Expand Up @@ -245,28 +245,31 @@ void NNUE::Pov_Accumulator::accumulate(Position *pos) {
values[i] = net.FTBiases[i];
}

const auto kingSq = KingSQ(pos, pov);
const bool flip = get_file[KingSQ(pos, pov)] > 3;

for (int square = 0; square < 64; square++) {
const bool input = pos->pieces[square] != EMPTY;
if (!input) continue;
const auto Idx = GetIndex(pos->pieces[square], square, flip);
const auto Idx = GetIndex(pos->PieceOn(square), square, kingSq, flip);
const auto Add = &net.FTWeights[Idx * L1_SIZE];
for (int j = 0; j < L1_SIZE; j++) {
values[j] += Add[j];
}
}
}

int NNUE::Pov_Accumulator::GetIndex(const int piece, const int square, bool flip) const {
int NNUE::Pov_Accumulator::GetIndex(const int piece, const int square, const int kingSq, bool flip) const {
constexpr std::size_t COLOR_STRIDE = 64 * 6;
constexpr std::size_t PIECE_STRIDE = 64;
const int piecetype = GetPieceType(piece);
const int pieceColor = Color[piece];
auto pieceColorPov = pov == WHITE ? pieceColor : (1 ^ pieceColor);
auto pieceColorPov = pov == WHITE ? pieceColor : (pieceColor ^ 1);
// Get the final indexes of the updates, accounting for hm
auto squarePov = pov == WHITE ? (square ^ 0b111'000) : square;
if(flip) squarePov ^= 0b000'111;
std::size_t Idx = pieceColorPov * COLOR_STRIDE + piecetype * PIECE_STRIDE + squarePov;
const auto finalKingSq = pov == WHITE ? (kingSq ^ 56) : (kingSq);
const int bucket = getBucket(finalKingSq);
std::size_t Idx = bucket * NUM_INPUTS + pieceColorPov * COLOR_STRIDE + piecetype * PIECE_STRIDE + squarePov;
return Idx;
}
44 changes: 30 additions & 14 deletions src/nnue.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,31 @@
#include "simd.h"
#include "types.h"

// Net arch: (768 -> L1_SIZE)x2 -> 1xOUTPUT_BUCKETS
// Net arch: (768xINPUT_BUCKETS -> L1_SIZE)x2 -> 1xOUTPUT_BUCKETS
constexpr int NUM_INPUTS = 768;
constexpr int L1_SIZE = 2048;
constexpr int INPUT_BUCKETS = 16;
constexpr int L1_SIZE = 1536;
constexpr int OUTPUT_BUCKETS = 8;

constexpr int FT_QUANT = 362;
constexpr int L1_QUANT = 64;
constexpr int FT_QUANT = 362;
constexpr int L1_QUANT = 64;
constexpr int NET_SCALE = 400;

constexpr int buckets[64] = {
0, 1, 2, 3, 3, 2, 1, 0,
4, 5, 6, 7, 7, 6, 5, 4,
8, 9, 10, 11, 11, 10, 9, 8,
8, 9, 10, 11, 11, 10, 9, 8,
12, 12, 13, 13, 13, 13, 12, 12,
12, 12, 13, 13, 13, 13, 12, 12,
14, 14, 15, 15, 15, 15, 14, 14,
14, 14, 15, 15, 15, 15, 14, 14
};

[[nodiscard]] inline int getBucket(int kingSquare){
return buckets[kingSquare];
}

#if defined(USE_SIMD)
constexpr int CHUNK_SIZE = sizeof(vepi16) / sizeof(int16_t);
#else
Expand All @@ -25,10 +41,10 @@ constexpr int CHUNK_SIZE = 1;
using NNUEIndices = std::array<std::size_t, 2>;

struct Network {
int16_t FTWeights[NUM_INPUTS * L1_SIZE];
int16_t FTBiases [L1_SIZE];
int16_t FTWeights[INPUT_BUCKETS * NUM_INPUTS * L1_SIZE];
int16_t FTBiases[L1_SIZE];
int16_t L1Weights[L1_SIZE * 2 * OUTPUT_BUCKETS];
int16_t L1Biases [OUTPUT_BUCKETS];
int16_t L1Biases[OUTPUT_BUCKETS];
};

extern Network net;
Expand All @@ -45,7 +61,7 @@ class NNUE {
bool needsRefresh = false;

void accumulate(Position *pos);
[[nodiscard]] int GetIndex(const int piece, const int square, const bool flip) const;
[[nodiscard]] int GetIndex(const int piece, const int square, const int kingSq , bool flip) const;
void addSub(NNUE::Pov_Accumulator &prev_acc, std::size_t add, std::size_t sub);
void addSubSub(NNUE::Pov_Accumulator &prev_acc, std::size_t add, std::size_t sub1, std::size_t sub2);
void applyUpdate(Pov_Accumulator& previousPovAccumulator);
Expand All @@ -65,18 +81,18 @@ class NNUE {

std::array<Pov_Accumulator, 2> perspective;

void AppendAddIndex(int piece, int square, std::array<bool, 2> flip) {
void AppendAddIndex(int piece, int square, const int wkSq, const int bkSq, std::array<bool, 2> flip) {
assert(this->perspective[WHITE].NNUEAdd.size() <= 1);
assert(this->perspective[BLACK].NNUEAdd.size() <= 1);
this->perspective[WHITE].NNUEAdd.emplace_back(perspective[WHITE].GetIndex(piece,square,flip[WHITE]));
this->perspective[BLACK].NNUEAdd.emplace_back(perspective[BLACK].GetIndex(piece,square,flip[BLACK]));
this->perspective[WHITE].NNUEAdd.emplace_back(perspective[WHITE].GetIndex(piece, square,wkSq, flip[WHITE]));
this->perspective[BLACK].NNUEAdd.emplace_back(perspective[BLACK].GetIndex(piece, square, bkSq, flip[BLACK]));
}

void AppendSubIndex(int piece, int square, std::array<bool, 2> flip) {
void AppendSubIndex(int piece, int square, const int wkSq, const int bkSq, std::array<bool, 2> flip) {
assert(this->perspective[WHITE].NNUESub.size() <= 1);
assert(this->perspective[BLACK].NNUESub.size() <= 1);
this->perspective[WHITE].NNUESub.emplace_back(perspective[WHITE].GetIndex(piece,square,flip[WHITE]));
this->perspective[BLACK].NNUESub.emplace_back(perspective[BLACK].GetIndex(piece,square,flip[BLACK]));
this->perspective[WHITE].NNUESub.emplace_back(perspective[WHITE].GetIndex(piece, square, wkSq, flip[WHITE]));
this->perspective[BLACK].NNUESub.emplace_back(perspective[BLACK].GetIndex(piece, square, bkSq, flip[BLACK]));
}

void ClearAddIndex() {
Expand Down
2 changes: 1 addition & 1 deletion src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// include the tune stuff here to give it global visibility
#include "tune.h"

#define NAME "Alexandria-7.1.2"
#define NAME "Alexandria-7.1.3"

inline int reductions[2][64][64];
inline int lmp_margin[64][2];
Expand Down