4 king buckets #175

Merged
rektdie merged 3 commits from 4-king-buckets into main 2026-03-31 18:47:48 +02:00
7 changed files with 85 additions and 35 deletions

No files matched your search

BIN
View File
Binary file not shown.
+21 -21
View File
@@ -18,7 +18,7 @@ int CalculateIndex(bool perspective, bool side, int pieceType, int square, bool
} }
// All friendly, for quiets // All friendly, for quiets
void AccumulatorPair::addSub(bool stm, int add, int addPT, int sub, int subPT) { void AccumulatorPair::addSub(bool stm, int add, int addPT, int sub, int subPT, BucketPair bp) {
int addW = CalculateIndex(White, stm, addPT, add, mirroredWhite); int addW = CalculateIndex(White, stm, addPT, add, mirroredWhite);
int addB = CalculateIndex(Black, stm, addPT, add, mirroredBlack); int addB = CalculateIndex(Black, stm, addPT, add, mirroredBlack);
@@ -26,16 +26,16 @@ void AccumulatorPair::addSub(bool stm, int add, int addPT, int sub, int subPT) {
int subB = CalculateIndex(Black, stm, subPT, sub, mirroredBlack); int subB = CalculateIndex(Black, stm, subPT, sub, mirroredBlack);
for (int i = 0; i < NNUE::HL_SIZE; i++) { for (int i = 0; i < NNUE::HL_SIZE; i++) {
white[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i] white[i] += NNUE::net.accumulator_weights[bp.white][addW * NNUE::HL_SIZE + i]
- NNUE::net.accumulator_weights[subW * NNUE::HL_SIZE + i]; - NNUE::net.accumulator_weights[bp.white][subW * NNUE::HL_SIZE + i];
black[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i] black[i] += NNUE::net.accumulator_weights[bp.black][addB * NNUE::HL_SIZE + i]
- NNUE::net.accumulator_weights[subB * NNUE::HL_SIZE + i]; - NNUE::net.accumulator_weights[bp.black][subB * NNUE::HL_SIZE + i];
} }
} }
// Captures // Captures
void AccumulatorPair::addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2) { void AccumulatorPair::addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2, BucketPair bp) {
int addW = CalculateIndex(White, stm, addPT, add, mirroredWhite); int addW = CalculateIndex(White, stm, addPT, add, mirroredWhite);
int addB = CalculateIndex(Black, stm, addPT, add, mirroredBlack); int addB = CalculateIndex(Black, stm, addPT, add, mirroredBlack);
@@ -46,18 +46,18 @@ void AccumulatorPair::addSubSub(bool stm, int add, int addPT, int sub1, int subP
int subB2 = CalculateIndex(Black, !stm, subPT2, sub2, mirroredBlack); int subB2 = CalculateIndex(Black, !stm, subPT2, sub2, mirroredBlack);
for (int i = 0; i < NNUE::HL_SIZE; i++) { for (int i = 0; i < NNUE::HL_SIZE; i++) {
white[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i] white[i] += NNUE::net.accumulator_weights[bp.white][addW * NNUE::HL_SIZE + i]
- NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i] - NNUE::net.accumulator_weights[bp.white][subW1 * NNUE::HL_SIZE + i]
- NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i]; - NNUE::net.accumulator_weights[bp.white][subW2 * NNUE::HL_SIZE + i];
black[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i] black[i] += NNUE::net.accumulator_weights[bp.black][addB * NNUE::HL_SIZE + i]
- NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i] - NNUE::net.accumulator_weights[bp.black][subB1 * NNUE::HL_SIZE + i]
- NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i]; - NNUE::net.accumulator_weights[bp.black][subB2 * NNUE::HL_SIZE + i];
} }
} }
// Castling // Castling
void AccumulatorPair::addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2) { void AccumulatorPair::addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2, BucketPair bp) {
int addW1 = CalculateIndex(White, stm, addPT1, add1, mirroredWhite); int addW1 = CalculateIndex(White, stm, addPT1, add1, mirroredWhite);
int addB1 = CalculateIndex(Black, stm, addPT1, add1, mirroredBlack); int addB1 = CalculateIndex(Black, stm, addPT1, add1, mirroredBlack);
@@ -71,15 +71,15 @@ void AccumulatorPair::addAddSubSub(bool stm, int add1, int addPT1, int add2, int
int subB2 = CalculateIndex(Black, stm, subPT2, sub2, mirroredBlack); int subB2 = CalculateIndex(Black, stm, subPT2, sub2, mirroredBlack);
for (int i = 0; i < NNUE::HL_SIZE; i++) { for (int i = 0; i < NNUE::HL_SIZE; i++) {
white[i] += NNUE::net.accumulator_weights[addW1 * NNUE::HL_SIZE + i] white[i] += NNUE::net.accumulator_weights[bp.white][addW1 * NNUE::HL_SIZE + i]
+ NNUE::net.accumulator_weights[addW2 * NNUE::HL_SIZE + i] + NNUE::net.accumulator_weights[bp.white][addW2 * NNUE::HL_SIZE + i]
- NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i] - NNUE::net.accumulator_weights[bp.white][subW1 * NNUE::HL_SIZE + i]
- NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i]; - NNUE::net.accumulator_weights[bp.white][subW2 * NNUE::HL_SIZE + i];
black[i] += NNUE::net.accumulator_weights[addB1 * NNUE::HL_SIZE + i] black[i] += NNUE::net.accumulator_weights[bp.black][addB1 * NNUE::HL_SIZE + i]
+ NNUE::net.accumulator_weights[addB2 * NNUE::HL_SIZE + i] + NNUE::net.accumulator_weights[bp.black][addB2 * NNUE::HL_SIZE + i]
- NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i] - NNUE::net.accumulator_weights[bp.black][subB1 * NNUE::HL_SIZE + i]
- NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i]; - NNUE::net.accumulator_weights[bp.black][subB2 * NNUE::HL_SIZE + i];
} }
} }
+8 -3
View File
@@ -3,6 +3,11 @@
namespace ACC { namespace ACC {
struct BucketPair {
int white;
int black;
};
using Accumulator = std::array<int16_t, NNUE::HL_SIZE>; using Accumulator = std::array<int16_t, NNUE::HL_SIZE>;
struct AccumulatorPair { struct AccumulatorPair {
@@ -13,9 +18,9 @@ struct AccumulatorPair {
bool mirroredBlack = false; bool mirroredBlack = false;
// yoinked from Quinniboi - Prelude // yoinked from Quinniboi - Prelude
void addSub(bool stm, int add, int addPT, int sub, int subPT); void addSub(bool stm, int add, int addPT, int sub, int subPT, BucketPair bp);
void addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2); void addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2, BucketPair bp);
void addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2); void addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2, BucketPair bp);
}; };
int CalculateIndex(bool perspective, bool side, int pieceType, int square, bool mirrored); int CalculateIndex(bool perspective, bool side, int pieceType, int square, bool mirrored);
+26 -9
View File
@@ -1,10 +1,13 @@
#include "board.h" #include "board.h"
#include "accumulator.h"
#include "movegen.h" #include "movegen.h"
#include "nnue.h"
#include "tt.h" #include "tt.h"
#include <iostream> #include <iostream>
#include <ranges> #include <ranges>
#include <string_view> #include <string_view>
#include <cassert> #include <cassert>
#include "types.h"
#include "utils.h" #include "utils.h"
void Board::Reset() { void Board::Reset() {
@@ -395,6 +398,7 @@ void Board::MakeMove(Move move) {
int endPiece = attackerPiece; int endPiece = attackerPiece;
bool fullRecalc = false; bool fullRecalc = false;
const auto bucketPair = GetBuckets();
if (attackerPiece == King) { if (attackerPiece == King) {
const int toFile = move.MoveTo() % 8; const int toFile = move.MoveTo() % 8;
@@ -410,6 +414,10 @@ void Board::MakeMove(Move move) {
fullRecalc = true; fullRecalc = true;
} }
} }
if (NNUE::kingBuckets[attackerColor][move.MoveFrom()] != NNUE::kingBuckets[attackerColor][move.MoveTo()]) {
fullRecalc = true;
}
} }
// Removing attacker piece from old position // Removing attacker piece from old position
@@ -428,13 +436,13 @@ void Board::MakeMove(Move move) {
if (!fullRecalc) { if (!fullRecalc) {
if (move.IsCapture()) { if (move.IsCapture()) {
if (move.GetFlags() != epCapture) { if (move.GetFlags() != epCapture) {
accPair.addSubSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, move.MoveTo(), targetPiece); accPair.addSubSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, move.MoveTo(), targetPiece, bucketPair);
} else { } else {
accPair.addSubSub(sideToMove, enPassantTarget, endPiece, move.MoveFrom(), attackerPiece, move.MoveTo() - direction, Pawn); accPair.addSubSub(sideToMove, enPassantTarget, endPiece, move.MoveFrom(), attackerPiece, move.MoveTo() - direction, Pawn, bucketPair);
} }
} else { } else {
if (move.GetFlags() != kingCastle && move.GetFlags() != queenCastle) { if (move.GetFlags() != kingCastle && move.GetFlags() != queenCastle) {
accPair.addSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece); accPair.addSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, bucketPair);
} }
} }
} }
@@ -472,7 +480,7 @@ void Board::MakeMove(Move move) {
SetPiece(attackerPiece, move.MoveTo(), attackerColor); SetPiece(attackerPiece, move.MoveTo(), attackerColor);
if (!fullRecalc) { if (!fullRecalc) {
accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare - 2, Rook, move.MoveFrom(), King, rookSquare, Rook); accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare - 2, Rook, move.MoveFrom(), King, rookSquare, Rook, bucketPair);
} }
break; break;
@@ -490,7 +498,7 @@ void Board::MakeMove(Move move) {
SetPiece(attackerPiece, move.MoveTo(), attackerColor); SetPiece(attackerPiece, move.MoveTo(), attackerColor);
if (!fullRecalc) { if (!fullRecalc) {
accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare + 3, Rook, move.MoveFrom(), King, rookSquare, Rook); accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare + 3, Rook, move.MoveFrom(), King, rookSquare, Rook, bucketPair);
} }
break; break;
@@ -553,6 +561,13 @@ bool Board::InPossibleZug() {
return !toCheck; return !toCheck;
} }
ACC::BucketPair Board::GetBuckets() {
int wKingSq = (pieces[King] & colors[White]).getLS1BIndex();
int bKingSq = (pieces[King] & colors[Black]).getLS1BIndex();
return {NNUE::kingBuckets[White][wKingSq],NNUE::kingBuckets[Black][bKingSq]};
}
void Board::ResetAccPair() { void Board::ResetAccPair() {
Bitboard whitePieces = colors[White]; Bitboard whitePieces = colors[White];
Bitboard blackPieces = colors[Black]; Bitboard blackPieces = colors[Black];
@@ -560,6 +575,8 @@ void Board::ResetAccPair() {
accPair.white = NNUE::net.accumulator_biases; accPair.white = NNUE::net.accumulator_biases;
accPair.black = NNUE::net.accumulator_biases; accPair.black = NNUE::net.accumulator_biases;
const auto bucketPair = GetBuckets();
while (whitePieces) { while (whitePieces) {
int square = whitePieces.getLS1BIndex(); int square = whitePieces.getLS1BIndex();
@@ -567,8 +584,8 @@ void Board::ResetAccPair() {
int bInput = ACC::CalculateIndex(Black, White, GetPieceType(square), square, accPair.mirroredBlack); int bInput = ACC::CalculateIndex(Black, White, GetPieceType(square), square, accPair.mirroredBlack);
for (int i = 0; i < NNUE::HL_SIZE; i++) { for (int i = 0; i < NNUE::HL_SIZE; i++) {
accPair.white[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i]; accPair.white[i] += NNUE::net.accumulator_weights[bucketPair.white][wInput * NNUE::HL_SIZE + i];
accPair.black[i] += NNUE::net.accumulator_weights[bInput * NNUE::HL_SIZE + i]; accPair.black[i] += NNUE::net.accumulator_weights[bucketPair.black][bInput * NNUE::HL_SIZE + i];
} }
whitePieces.PopBit(square); whitePieces.PopBit(square);
@@ -581,8 +598,8 @@ void Board::ResetAccPair() {
int bInput = ACC::CalculateIndex(Black, Black, GetPieceType(square), square, accPair.mirroredBlack); int bInput = ACC::CalculateIndex(Black, Black, GetPieceType(square), square, accPair.mirroredBlack);
for (int i = 0; i < NNUE::HL_SIZE; i++) { for (int i = 0; i < NNUE::HL_SIZE; i++) {
accPair.white[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i]; accPair.white[i] += NNUE::net.accumulator_weights[bucketPair.white][wInput * NNUE::HL_SIZE + i];
accPair.black[i] += NNUE::net.accumulator_weights[bInput * NNUE::HL_SIZE + i]; accPair.black[i] += NNUE::net.accumulator_weights[bucketPair.black][bInput * NNUE::HL_SIZE + i];
} }
blackPieces.PopBit(square); blackPieces.PopBit(square);
+1
View File
@@ -62,6 +62,7 @@ public:
void ResetMoves(); void ResetMoves();
void ListMoves(); void ListMoves();
ACC::BucketPair GetBuckets();
void ResetAccPair(); void ResetAccPair();
int GetPieceType(int square); int GetPieceType(int square);
+3 -1
View File
@@ -17,7 +17,9 @@ void Network::Load(const std::string& path) {
// HL weights // HL weights
for (size_t i = 0; i < accumulator_weights.size(); i++) { for (size_t i = 0; i < accumulator_weights.size(); i++) {
file.read(reinterpret_cast<char*>(&accumulator_weights[i]), sizeof(int16_t)); for (size_t j = 0; j < accumulator_weights[i].size(); j++) {
file.read(reinterpret_cast<char*>(&accumulator_weights[i][j]), sizeof(int16_t));
}
} }
// HL biases // HL biases
+26 -1
View File
@@ -15,14 +15,39 @@ namespace NNUE {
constexpr size_t INPUT_SIZE = 768; constexpr size_t INPUT_SIZE = 768;
constexpr size_t HL_SIZE = 1536; constexpr size_t HL_SIZE = 1536;
constexpr size_t INPUT_BUCKETS = 4;
constexpr size_t OUTPUT_BUCKETS = 8; constexpr size_t OUTPUT_BUCKETS = 8;
constexpr int16_t SCALE = 400; constexpr int16_t SCALE = 400;
constexpr int16_t QA = 255; constexpr int16_t QA = 255;
constexpr int16_t QB = 64; constexpr int16_t QB = 64;
constexpr int kingBuckets[2][64] = {
{
0, 0, 1, 1, 1, 1, 0, 0,
2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3
},
{
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
2, 2, 2, 2, 2, 2, 2, 2,
0, 0, 1, 1, 1, 1, 0, 0
}
};
struct Network { struct Network {
alignas(ALIGNMENT) std::array<int16_t, HL_SIZE * INPUT_SIZE> accumulator_weights; alignas(ALIGNMENT) std::array<std::array<int16_t, HL_SIZE * INPUT_SIZE>, INPUT_BUCKETS> accumulator_weights;
alignas(ALIGNMENT) std::array<int16_t, HL_SIZE> accumulator_biases; alignas(ALIGNMENT) std::array<int16_t, HL_SIZE> accumulator_biases;
alignas(ALIGNMENT) std::array<std::array<int16_t, 2 * HL_SIZE>, OUTPUT_BUCKETS> output_weights; alignas(ALIGNMENT) std::array<std::array<int16_t, 2 * HL_SIZE>, OUTPUT_BUCKETS> output_weights;
std::array<int16_t, OUTPUT_BUCKETS> output_bias; std::array<int16_t, OUTPUT_BUCKETS> output_bias;