using NNUE eval | bench 10737067

This commit is contained in:
rektdie committed 2025-04-08 22:42:31 +02:00
1 parent d72a0c341e
commit 2ea6bd1c64
8 files changed
+109 -44

No files matched your search

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+64 -7
View File
@@ -4,7 +4,7 @@
namespace ACC {
int CalculateIndex(bool perspective, int square, int pieceType, bool side) {
int CalculateIndex(bool perspective, bool side, int pieceType, int square) {
if (perspective == Black)
{
side = 1 - side; // Flip side
@@ -14,15 +14,72 @@ int CalculateIndex(bool perspective, int square, int pieceType, bool side) {
return side * 64 * 6 + pieceType * 64 + square;
}
void Accumulator::Add(const NNUE::Network* net, int index) {
for (int i = 0; i < NNUE::HL_SIZE; i ++) {
values[i] += net->accumulator_weights[index * NNUE::INPUT_SIZE + i];
// All friendly, for quiets
void AccumulatorPair::addSub(bool stm, int add, int addPT, int sub, int subPT) {
int addW = CalculateIndex(White, stm, addPT, add);
int addB = CalculateIndex(Black, stm, addPT, add);
int subW = CalculateIndex(White, stm, subPT, sub);
int subB = CalculateIndex(Black, stm, subPT, sub);
for (int i = 0; i < NNUE::HL_SIZE; i++) {
white.values[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i];
black.values[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i];
white.values[i] -= NNUE::net.accumulator_weights[subW * NNUE::HL_SIZE + i];
black.values[i] -= NNUE::net.accumulator_weights[subB * NNUE::HL_SIZE + i];
}
}
void Accumulator::Sub(const NNUE::Network* net, int index) {
for (int i = 0; i < NNUE::HL_SIZE; i ++) {
values[i] -= net->accumulator_weights[index * NNUE::INPUT_SIZE + i];
// Captures
void AccumulatorPair::addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2) {
int addW = CalculateIndex(White, stm, addPT, add);
int addB = CalculateIndex(Black, stm, addPT, add);
int subW1 = CalculateIndex(White, stm, subPT1, sub1);
int subB1 = CalculateIndex(Black, stm, subPT1, sub1);
int subW2 = CalculateIndex(White, ~stm, subPT2, sub2);
int subB2 = CalculateIndex(Black, ~stm, subPT2, sub2);
for (int i = 0; i < NNUE::HL_SIZE; i++) {
white.values[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i];
black.values[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i];
white.values[i] -= NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i];
black.values[i] -= NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i];
white.values[i] -= NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i];
black.values[i] -= NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i];
}
}
// Castling
void AccumulatorPair::addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2) {
int addW1 = CalculateIndex(White, stm, addPT1, add1);
int addB1 = CalculateIndex(Black, stm, addPT1, add1);
int addW2 = CalculateIndex(White, stm, addPT2, add2);
int addB2 = CalculateIndex(Black, stm, addPT2, add2);
int subW1 = CalculateIndex(White, stm, subPT1, sub1);
int subB1 = CalculateIndex(Black, stm, subPT1, sub1);
int subW2 = CalculateIndex(White, stm, subPT2, sub2);
int subB2 = CalculateIndex(Black, stm, subPT2, sub2);
for (int i = 0; i < NNUE::HL_SIZE; i++) {
white.values[i] += NNUE::net.accumulator_weights[addW1 * NNUE::HL_SIZE + i];
black.values[i] += NNUE::net.accumulator_weights[addB1 * NNUE::HL_SIZE + i];
white.values[i] += NNUE::net.accumulator_weights[addW2 * NNUE::HL_SIZE + i];
black.values[i] += NNUE::net.accumulator_weights[addB2 * NNUE::HL_SIZE + i];
white.values[i] -= NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i];
black.values[i] -= NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i];
white.values[i] -= NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i];
black.values[i] -= NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i];
}
}
+6 -4
View File
@@ -5,16 +5,18 @@ namespace ACC {
struct Accumulator {
std::array<int16_t, NNUE::HL_SIZE> values;
void Add(const NNUE::Network* net, int index);
void Sub(const NNUE::Network* net, int index);
};
struct AccumulatorPair {
Accumulator white;
Accumulator black;
// yoinked from Quinniboi - Prelude
void addSub(bool stm, int add, int addPT, int sub, int subPT);
void addSubSub(bool stm, int add, int addPT, 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);
};
int CalculateIndex(bool perspective, int square, int pieceType, bool side);
int CalculateIndex(bool perspective, bool side, int pieceType, int square);
}
+32 -30
View File
@@ -278,9 +278,33 @@ bool Board::MakeMove(Move move) {
int targetPiece = GetPieceType(move.MoveTo());
int direction = attackerColor ? south : north;
int endPiece = attackerPiece;
// Removing attacker piece from old position
RemovePiece(attackerPiece, move.MoveFrom(), attackerColor);
if (move.IsPromo()) {
if (move.IsCapture()) {
endPiece = move.GetFlags() - 9;
Promote(move.MoveTo(), endPiece, sideToMove, true);
} else {
endPiece = move.GetFlags() - 5;
Promote(move.MoveTo(), endPiece, sideToMove, false);
}
}
if (move.IsCapture()) {
if (move.GetFlags() != epCapture) {
accPair.addSubSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, move.MoveTo(), targetPiece);
} else {
accPair.addSubSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, enPassantTarget, Pawn);
}
} else {
if (move.GetFlags() != kingCastle && move.GetFlags() != queenCastle) {
accPair.addSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece);
}
}
switch (move.GetFlags())
{
case quiet:
@@ -312,6 +336,8 @@ bool Board::MakeMove(Move move) {
SetPiece(Rook, rookSquare - 2, attackerColor);
SetPiece(attackerPiece, move.MoveTo(), attackerColor);
accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare - 2, Rook, move.MoveFrom(), King, rookSquare, Rook);
break;
}
case queenCastle:
@@ -325,32 +351,9 @@ bool Board::MakeMove(Move move) {
SetPiece(Rook, rookSquare + 3, attackerColor);
SetPiece(attackerPiece, move.MoveTo(), attackerColor);
accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare + 3, Rook, move.MoveFrom(), King, rookSquare, Rook);
break;
}
case knightPromotion:
Promote(move.MoveTo(), Knight, attackerColor, false);
break;
case bishopPromotion:
Promote(move.MoveTo(), Bishop, attackerColor, false);
break;
case rookPromotion:
Promote(move.MoveTo(), Rook, attackerColor, false);
break;
case queenPromotion:
Promote(move.MoveTo(), Queen, attackerColor, false);
break;
case knightPromoCapture:
Promote(move.MoveTo(), Knight, attackerColor, true);
break;
case bishopPromoCapture:
Promote(move.MoveTo(), Bishop, attackerColor, true);
break;
case rookPromoCapture:
Promote(move.MoveTo(), Rook, attackerColor, true);
break;
case queenPromoCapture:
Promote(move.MoveTo(), Queen, attackerColor, true);
break;
default:
break;
}
@@ -370,7 +373,6 @@ bool Board::MakeMove(Move move) {
}
enPassantTarget = newEpTarget;
MOVEGEN::GenThreatMaps(*this);
@@ -387,7 +389,7 @@ bool Board::MakeMove(Move move) {
} else {
halfMoves++;
}
positionIndex++;
positionHistory[positionIndex] = hashKey;
@@ -416,8 +418,8 @@ void Board::ResetAccPair() {
while (whitePieces) {
int square = whitePieces.getLS1BIndex();
int wInput = ACC::CalculateIndex(White, square, GetPieceType(square), White);
int bInput = ACC::CalculateIndex(Black, square, GetPieceType(square), White);
int wInput = ACC::CalculateIndex(White, White, GetPieceType(square), square);
int bInput = ACC::CalculateIndex(Black, White, GetPieceType(square), square);
for (int i = 0; i < NNUE::HL_SIZE; i++) {
accPair.white.values[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i];
@@ -430,8 +432,8 @@ void Board::ResetAccPair() {
while (blackPieces) {
int square = blackPieces.getLS1BIndex();
int wInput = ACC::CalculateIndex(White, square, GetPieceType(square), Black);
int bInput = ACC::CalculateIndex(Black, square, GetPieceType(square), Black);
int wInput = ACC::CalculateIndex(White, Black, GetPieceType(square), square);
int bInput = ACC::CalculateIndex(Black, Black, GetPieceType(square), square);
for (int i = 0; i < NNUE::HL_SIZE; i++) {
accPair.white.values[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i];
+1 -1
View File
@@ -13,7 +13,7 @@ int main(int argc, char* argv[]) {
UTILS::InitZobrist();
SEARCH::InitLMRTable();
NNUE::net.Load("beans.bin");
NNUE::net.Load("nnue.bin");
Board board;
+4
View File
@@ -60,6 +60,10 @@ public:
|| GetFlags() == queenCastle || GetFlags() == doublePawnPush);
}
bool IsPromo() {
return (GetFlags() >= knightPromotion);
}
void PrintMove() {
std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()];
+2 -2
View File
@@ -156,7 +156,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) {
}
}
int bestScore = HCE::Evaluate(board);
int bestScore = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board));
TTEntry *entry = TT.GetRawEntry(board.hashKey);
if (entry->hashKey == board.hashKey) {
@@ -239,7 +239,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
if (depth <= 0) return Quiescence<mode>(board, alpha, beta, ply);
const int staticEval = HCE::Evaluate(board);
const int staticEval = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board));
if (!board.InCheck()) {
if (ply) {