Skip to content

Commit

Permalink
Reapply "Simplify NNUE code (PGG106#514)"
Browse files Browse the repository at this point in the history
This reverts commit cfa18db.
  • Loading branch information
cj5716 committed Feb 22, 2025
1 parent cfa18db commit feacfea
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 372 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,5 @@ perf.data.old
/src/data1.txt
/Alexandria.vcxproj
/Alexandria.vcxproj.filters

.idea
6 changes: 1 addition & 5 deletions src/eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@
}

[[nodiscard]] inline int EvalPositionRaw(Position* pos) {
// Update accumulators to ensure we are up to date on the current board state
nnue.update(&pos->AccumulatorTop(), pos);
const int pieceCount = pos->PieceCount();
const int outputBucket = std::min((63 - pieceCount) * (32 - pieceCount) / 225, 7);
return nnue.output(pos->AccumulatorTop(), pos->side, outputBucket);
return nnue.output(pos);
}

// position evaluation
Expand Down
2 changes: 2 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ void InitNewGame(ThreadData* td) {
SearchData* sd = &td->sd;
SearchInfo* info = &td->info;

pos->resetFinnyTable();

CleanHistories(sd);

// Clean the PV Table
Expand Down
127 changes: 38 additions & 89 deletions src/makemove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,9 @@ void inline HashKey(ZobristKey& originalKey , ZobristKey key) {
originalKey ^= key;
}

template void ClearPiece<false>(const int piece, const int to, Position* pos);
template void ClearPiece<true>(const int piece, const int to, Position* pos);

// Remove a piece from a square, the UPDATE params determines whether we want to update the NNUE weights or not
template <bool UPDATE = true>
// Remove a piece from a square
void ClearPiece(const int piece, const int from, Position* pos) {
assert(piece != EMPTY);
// 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});
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);
pop_bit(pos->occupancies[color], from);
Expand All @@ -36,11 +25,6 @@ void ClearPiece(const int piece, const int from, Position* pos) {
HashKey(pos->blackNonPawnKey, PieceKeys[piece][from]);
}

template void AddPiece<false>(const int piece, const int to, Position* pos);
template void AddPiece<true>(const int piece, const int to, Position* pos);

// Add a piece to a square, the UPDATE params determines whether we want to update the NNUE weights or not
template <bool UPDATE = true>
void AddPiece(const int piece, const int to, Position* pos) {
assert(piece != EMPTY);
const int color = Color[piece];
Expand All @@ -54,20 +38,11 @@ void AddPiece(const int piece, const int to, Position* pos) {
HashKey(pos->whiteNonPawnKey, PieceKeys[piece][to]);
else
HashKey(pos->blackNonPawnKey, PieceKeys[piece][to]);
// 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});
auto wkSq = KingSQ(pos, WHITE);
auto bkSq = KingSQ(pos, BLACK);
pos->AccumulatorTop().AppendAddIndex(piece, to, wkSq, bkSq, flip);
}
}

// Move a piece from the [to] square to the [from] square, the UPDATE params determines whether we want to update the NNUE weights or not
template <bool UPDATE = true>
void MovePiece(const int piece, const int from, const int to, Position* pos) {
ClearPiece<UPDATE>(piece, from, pos);
AddPiece<UPDATE>(piece, to, pos);
ClearPiece(piece, from, pos);
AddPiece(piece, to, pos);
}

void UpdateCastlingPerms(Position* pos, int source_square, int target_square) {
Expand All @@ -87,49 +62,46 @@ inline void resetEpSquare(Position* pos) {
}
}

template <bool UPDATE>
void MakeCastle(const Move move, Position* pos) {
// parse move
const int sourceSquare = From(move);
const int targetSquare = To(move);
const int piece = Piece(move);
// Remove the piece fom the square it moved from
ClearPiece<UPDATE>(piece, sourceSquare, pos);
ClearPiece(piece, sourceSquare, pos);
// Set the piece to the destination square, if it was a promotion we directly set the promoted piece
AddPiece<UPDATE>(piece, targetSquare, pos);

AddPiece(piece, targetSquare, pos);
resetEpSquare(pos);

// move the rook
switch (targetSquare) {
// white castles king side
case (g1):
// move H rook
MovePiece<UPDATE>(WR, h1, f1, pos);
MovePiece(WR, h1, f1, pos);
break;

// white castles queen side
case (c1):
// move A rook
MovePiece<UPDATE>(WR, a1, d1, pos);
MovePiece(WR, a1, d1, pos);
break;

// black castles king side
case (g8):
// move H rook
MovePiece<UPDATE>(BR, h8, f8, pos);
MovePiece(BR, h8, f8, pos);
break;

// black castles queen side
case (c8):
// move A rook
MovePiece<UPDATE>(BR, a8, d8, pos);
MovePiece(BR, a8, d8, pos);
break;
}
UpdateCastlingPerms(pos, sourceSquare, targetSquare);
}

template <bool UPDATE>
void MakeEp(const Move move, Position* pos) {
pos->state.fiftyMove = 0;

Expand All @@ -142,20 +114,19 @@ void MakeEp(const Move move, Position* pos) {
const int pieceCap = GetPiece(PAWN, pos->side ^ 1);
pos->history[pos->historyStackHead].capture = pieceCap;
const int capturedPieceLocation = targetSquare + SOUTH;
ClearPiece<UPDATE>(pieceCap, capturedPieceLocation, pos);
ClearPiece(pieceCap, capturedPieceLocation, pos);

// Remove the piece fom the square it moved from
ClearPiece<UPDATE>(piece, sourceSquare, pos);
ClearPiece(piece, sourceSquare, pos);
// Set the piece to the destination square
AddPiece<UPDATE>(piece, targetSquare, pos);
AddPiece(piece, targetSquare, pos);

// Reset EP square
assert(pos->getEpSquare() != no_sq);
HashKey(pos->posKey, enpassant_keys[pos->getEpSquare()]);
pos->state.enPas = no_sq;
}

template <bool UPDATE>
void MakePromo(const Move move, Position* pos) {
pos->state.fiftyMove = 0;

Expand All @@ -166,16 +137,15 @@ void MakePromo(const Move move, Position* pos) {
const int promotedPiece = GetPiece(getPromotedPiecetype(move), pos->side);

// Remove the piece fom the square it moved from
ClearPiece<UPDATE>(piece, sourceSquare, pos);
ClearPiece(piece, sourceSquare, pos);
// Set the piece to the destination square, if it was a promotion we directly set the promoted piece
AddPiece<UPDATE>(promotedPiece , targetSquare, pos);
AddPiece(promotedPiece , targetSquare, pos);

resetEpSquare(pos);

UpdateCastlingPerms(pos, sourceSquare, targetSquare);
}

template <bool UPDATE>
void MakePromocapture(const Move move, Position* pos) {
pos->state.fiftyMove = 0;

Expand All @@ -188,21 +158,20 @@ void MakePromocapture(const Move move, Position* pos) {
const int pieceCap = pos->PieceOn(targetSquare);
assert(pieceCap != EMPTY);
assert(GetPieceType(pieceCap) != KING);
ClearPiece<UPDATE>(pieceCap, targetSquare, pos);
ClearPiece(pieceCap, targetSquare, pos);

pos->history[pos->historyStackHead].capture = pieceCap;

// Remove the piece fom the square it moved from
ClearPiece<UPDATE>(piece, sourceSquare, pos);
ClearPiece(piece, sourceSquare, pos);
// Set the piece to the destination square, if it was a promotion we directly set the promoted piece
AddPiece<UPDATE>(promotedPiece , targetSquare, pos);
AddPiece(promotedPiece , targetSquare, pos);

resetEpSquare(pos);

UpdateCastlingPerms(pos, sourceSquare, targetSquare);
}

template <bool UPDATE>
void MakeQuiet(const Move move, Position* pos) {
// parse move
const int sourceSquare = From(move);
Expand All @@ -213,14 +182,13 @@ void MakeQuiet(const Move move, Position* pos) {
if (GetPieceType(piece) == PAWN)
pos->state.fiftyMove = 0;

MovePiece<UPDATE>(piece,sourceSquare,targetSquare,pos);
MovePiece(piece,sourceSquare,targetSquare,pos);

resetEpSquare(pos);

UpdateCastlingPerms(pos, sourceSquare, targetSquare);
}

template <bool UPDATE>
void MakeCapture(const Move move, Position* pos) {
// parse move
const int sourceSquare = From(move);
Expand All @@ -232,17 +200,16 @@ void MakeCapture(const Move move, Position* pos) {
const int pieceCap = pos->PieceOn(targetSquare);
assert(pieceCap != EMPTY);
assert(GetPieceType(pieceCap) != KING);
ClearPiece<UPDATE>(pieceCap, targetSquare, pos);
ClearPiece(pieceCap, targetSquare, pos);
pos->history[pos->historyStackHead].capture = pieceCap;

MovePiece<UPDATE>(piece, sourceSquare, targetSquare, pos);
MovePiece(piece, sourceSquare, targetSquare, pos);

resetEpSquare(pos);

UpdateCastlingPerms(pos, sourceSquare, targetSquare);
}

template <bool UPDATE>
void MakeDP(const Move move, Position* pos)
{ pos->state.fiftyMove = 0;

Expand All @@ -251,7 +218,7 @@ void MakeDP(const Move move, Position* pos)
const int targetSquare = To(move);
const int piece = Piece(move);

MovePiece<UPDATE>(piece,sourceSquare,targetSquare, pos);
MovePiece(piece,sourceSquare,targetSquare, pos);

resetEpSquare(pos);

Expand Down Expand Up @@ -281,10 +248,11 @@ bool shouldFlip(int from, int to) {
// make move on chess board
template <bool UPDATE>
void MakeMove(const Move move, Position* pos) {
if constexpr (UPDATE){

if constexpr (UPDATE) {
saveBoardState(pos);
pos->accumStackHead++;
}

// Store position key in the array of searched position
pos->played_positions.emplace_back(pos->posKey);

Expand All @@ -300,25 +268,25 @@ void MakeMove(const Move move, Position* pos) {
pos->hisPly++;

if(castling){
MakeCastle<UPDATE>(move,pos);
MakeCastle(move,pos);
}
else if(doublePush){
MakeDP<UPDATE>(move,pos);
MakeDP(move,pos);
}
else if(enpass){
MakeEp<UPDATE>(move,pos);
MakeEp(move,pos);
}
else if(promotion && capture){
MakePromocapture<UPDATE>(move,pos);
MakePromocapture(move,pos);
}
else if(promotion){
MakePromo<UPDATE>(move,pos);
MakePromo(move,pos);
}
else if(!capture){
MakeQuiet<UPDATE>(move,pos);
MakeQuiet(move,pos);
}
else {
MakeCapture<UPDATE>(move, pos);
MakeCapture(move, pos);
}

if constexpr (UPDATE)
Expand All @@ -331,18 +299,6 @@ void MakeMove(const Move move, Position* pos) {
// Update pinmasks and checkers
UpdatePinsAndCheckers(pos, pos->side);

// Figure out if we need to refresh the accumulator
if constexpr (UPDATE) {
if (PieceType[Piece(move)] == KING) {
auto kingColor = Color[Piece(move)];
const auto startBucket = getBucket(From(move), kingColor);
const auto endBucket = getBucket(To(move), kingColor);
if (shouldFlip(From(move), To(move)) || startBucket != endBucket) {
// tell the right accumulator it'll need a refresh
pos->accumStack[pos->accumStackHead-1].perspective[kingColor].needsRefresh = true;
}
}
}
// Make sure a freshly generated zobrist key matches the one we are incrementally updating
assert(pos->posKey == GeneratePosKey(pos));
assert(pos->pawnKey == GeneratePawnKey(pos));
Expand All @@ -355,13 +311,6 @@ void UnmakeMove(const Move move, Position* pos) {

restorePreviousBoardState(pos);

pos->AccumulatorTop().ClearAddIndex();
pos->AccumulatorTop().ClearSubIndex();
pos->AccumulatorTop().perspective[WHITE].needsRefresh = false;
pos->AccumulatorTop().perspective[BLACK].needsRefresh = false;

pos->accumStackHead--;

// parse move
const int sourceSquare = From(move);
const int targetSquare = To(move);
Expand All @@ -374,17 +323,17 @@ void UnmakeMove(const Move move, Position* pos) {
const int piece = promotion ? GetPiece(getPromotedPiecetype(move), pos->side ^ 1) : Piece(move);

// clear the piece on the target square
ClearPiece<false>(piece, targetSquare, pos);
ClearPiece(piece, targetSquare, pos);
// no matter what add back the piece that was originally moved, ignoring any promotion
AddPiece<false>(Piece(move), sourceSquare, pos);
AddPiece(Piece(move), sourceSquare, pos);

// handle captures
if (capture) {
const int SOUTH = pos->side == WHITE ? -8 : 8;
// Retrieve the captured piece we have to restore
const int piececap = pos->getCapturedPiece();
const int capturedPieceLocation = enpass ? targetSquare + SOUTH : targetSquare;
AddPiece<false>(piececap, capturedPieceLocation, pos);
AddPiece(piececap, capturedPieceLocation, pos);
}

// handle castling moves
Expand All @@ -394,25 +343,25 @@ void UnmakeMove(const Move move, Position* pos) {
// white castles king side
case (g1):
// move H rook
MovePiece<false>(WR, f1, h1, pos);
MovePiece(WR, f1, h1, pos);
break;

// white castles queen side
case (c1):
// move A rook
MovePiece<false>(WR, d1, a1, pos);
MovePiece(WR, d1, a1, pos);
break;

// black castles king side
case (g8):
// move H rook
MovePiece<false>(BR, f8, h8, pos);
MovePiece(BR, f8, h8, pos);
break;

// black castles queen side
case (c8):
// move A rook
MovePiece<false>(BR, d8, a8, pos);
MovePiece(BR, d8, a8, pos);
break;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/makemove.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

struct Position;

template <bool UPDATE>
void ClearPiece(const int piece, const int from, Position* pos);

template <bool UPDATE>
void AddPiece(const int piece, const int to, Position* pos);

void UpdateCastlingPerms(Position* pos, int source_square, int target_square);
Expand Down
Loading

0 comments on commit feacfea

Please sign in to comment.