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

bench: 2552509 #540

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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: 7 additions & 0 deletions src/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void ClearBoard(Board* board) {
board->fmr = 0;
board->nullply = 0;
board->phase = 0;
board->matScore = 0;
}

void ParseFen(char* fen, Board* board) {
Expand All @@ -76,6 +77,7 @@ void ParseFen(char* fen, Board* board) {
board->squares[i] = piece;

board->phase += PHASE_VALUES[PieceType(piece)];
board->matScore += PC_VALUES[piece];

if (*fen != 'K' && *fen != 'k')
board->piecesCounts += PieceCount(piece);
Expand Down Expand Up @@ -379,6 +381,7 @@ void MakeMoveUpdate(Move move, Board* board, int update) {

board->piecesCounts -= PieceCount(captured);
board->phase -= PHASE_VALUES[PieceType(captured)];
board->matScore -= PC_VALUES[captured];
board->fmr = 0;
}

Expand Down Expand Up @@ -412,6 +415,7 @@ void MakeMoveUpdate(Move move, Board* board, int update) {
board->pawnZobrist ^= ZOBRIST_PIECES[piece][to];
board->piecesCounts += PieceCount(promoted) - PieceCount(piece);
board->phase += PHASE_VALUES[PieceType(promoted)];
board->matScore += PC_VALUES[promoted] - PC_VALUES[piece];
}

board->fmr = 0;
Expand All @@ -435,6 +439,7 @@ void MakeMoveUpdate(Move move, Board* board, int update) {

board->accumulators++;
board->accumulators->correct[WHITE] = board->accumulators->correct[BLACK] = 0;
board->accumulators->lazyCorrect[WHITE] = board->accumulators->lazyCorrect[BLACK] = 0;
}
}

Expand All @@ -459,6 +464,7 @@ void UndoMove(Move move, Board* board) {
board->squares[to] = piece;
board->piecesCounts -= PieceCount(promoted) - PieceCount(piece);
board->phase -= PHASE_VALUES[PieceType(promoted)];
board->matScore -= PC_VALUES[promoted] - PC_VALUES[piece];
}

FlipBits(board->pieces[piece], to, from);
Expand Down Expand Up @@ -492,6 +498,7 @@ void UndoMove(Move move, Board* board) {

board->piecesCounts += PieceCount(captured);
board->phase += PHASE_VALUES[PieceType(captured)];
board->matScore += PC_VALUES[captured];
}
}

Expand Down
150 changes: 79 additions & 71 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "uci.h"
#include "util.h"

const int PC_VALUES[12] = {100, -100, 318, -318, 348, -348, 554, -554, 1068, -1068, 0, 0};
const int PHASE_VALUES[6] = {0, 3, 3, 5, 10, 0};
const int MAX_PHASE = 64;

Expand All @@ -42,17 +43,22 @@ Score Evaluate(Board* board, ThreadData* thread) {
if (IsMaterialDraw(board))
return 0;

const int lazy = abs(board->matScore) >= EVAL_LAZY_MARGIN;

Accumulator* acc = board->accumulators;
for (int c = WHITE; c <= BLACK; c++) {
if (!acc->correct[c]) {
if (CanEfficientlyUpdate(acc, c))
ApplyLazyUpdates(acc, board, c);
int correct = lazy ? acc->lazyCorrect[c] : acc->correct[c];

if (!correct) {
if (CanEfficientlyUpdate(acc, c, lazy))
BackfillUpdates(acc, board, c, lazy);
else
RefreshAccumulator(acc, board, c);
RefreshAccumulator(acc, board, c, lazy);
}
}

int score = board->stm == WHITE ? Propagate(acc, WHITE) : Propagate(acc, BLACK);
int score = Propagate(acc, board->stm, lazy);
// int score = Predict(board);

// static contempt
score += thread->contempt[board->stm];
Expand All @@ -64,77 +70,79 @@ Score Evaluate(Board* board, ThreadData* thread) {
}

void EvaluateTrace(Board* board) {
// The UCI board has no guarantee of accumulator allocation
// so we have to set that up here.
board->accumulators = AlignedMalloc(sizeof(Accumulator), 64);
ResetAccumulator(board->accumulators, board, WHITE);
ResetAccumulator(board->accumulators, board, BLACK);

int base = Propagate(board->accumulators, board->stm);
base = board->stm == WHITE ? base : -base;
int scaled = (128 + board->phase) * base / 128;

printf("\nNNUE derived piece values:\n");

for (int r = 0; r < 8; r++) {
printf("+-------+-------+-------+-------+-------+-------+-------+-------+\n");
printf("|");
for (int f = 0; f < 16; f++) {
if (f == 8)
printf("\n|");

int sq = r * 8 + (f > 7 ? f - 8 : f);
int pc = board->squares[sq];

if (pc == NO_PIECE) {
printf(" |");
} else if (f < 8) {
printf(" %c |", PIECE_TO_CHAR[pc]);
} else if (PieceType(pc) == KING) {
printf(" |");
} else {
// To calculate the piece value, we pop it
// reset the accumulators and take a diff
PopBit(OccBB(BOTH), sq);
ResetAccumulator(board->accumulators, board, WHITE);
ResetAccumulator(board->accumulators, board, BLACK);
int new = Propagate(board->accumulators, board->stm);
new = board->stm == WHITE ? new : -new;
SetBit(OccBB(BOTH), sq);

int diff = base - new;
int normalized = Normalize(diff);
int v = abs(normalized);

char buffer[6];
buffer[5] = '\0';
buffer[0] = diff > 0 ? '+' : diff < 0 ? '-' : ' ';
if (v >= 1000) {
buffer[1] = '0' + v / 1000;
v %= 1000;
buffer[2] = '0' + v / 100;
v %= 100;
buffer[3] = '.';
buffer[4] = '0' + v / 10;
for (int lazy = 0; lazy <= 1; lazy++) {
// The UCI board has no guarantee of accumulator allocation
// so we have to set that up here.
board->accumulators = AlignedMalloc(sizeof(Accumulator), 64);
ResetAccumulator(board->accumulators, board, WHITE, lazy);
ResetAccumulator(board->accumulators, board, BLACK, lazy);

int base = Propagate(board->accumulators, board->stm, lazy);
base = board->stm == WHITE ? base : -base;
int scaled = (128 + board->phase) * base / 128;

printf("\nNNUE derived piece values:\n");

for (int r = 0; r < 8; r++) {
printf("+-------+-------+-------+-------+-------+-------+-------+-------+\n");
printf("|");
for (int f = 0; f < 16; f++) {
if (f == 8)
printf("\n|");

int sq = r * 8 + (f > 7 ? f - 8 : f);
int pc = board->squares[sq];

if (pc == NO_PIECE) {
printf(" |");
} else if (f < 8) {
printf(" %c |", PIECE_TO_CHAR[pc]);
} else if (PieceType(pc) == KING) {
printf(" |");
} else {
buffer[1] = '0' + v / 100;
v %= 100;
buffer[2] = '.';
buffer[3] = '0' + v / 10;
v %= 10;
buffer[4] = '0' + v;
// To calculate the piece value, we pop it
// reset the accumulators and take a diff
PopBit(OccBB(BOTH), sq);
ResetAccumulator(board->accumulators, board, WHITE, lazy);
ResetAccumulator(board->accumulators, board, BLACK, lazy);
int new = Propagate(board->accumulators, board->stm, lazy);
new = board->stm == WHITE ? new : -new;
SetBit(OccBB(BOTH), sq);

int diff = base - new;
int normalized = Normalize(diff);
int v = abs(normalized);

char buffer[6];
buffer[5] = '\0';
buffer[0] = diff > 0 ? '+' : diff < 0 ? '-' : ' ';
if (v >= 1000) {
buffer[1] = '0' + v / 1000;
v %= 1000;
buffer[2] = '0' + v / 100;
v %= 100;
buffer[3] = '.';
buffer[4] = '0' + v / 10;
} else {
buffer[1] = '0' + v / 100;
v %= 100;
buffer[2] = '.';
buffer[3] = '0' + v / 10;
v %= 10;
buffer[4] = '0' + v;
}
printf(" %s |", buffer);
}
printf(" %s |", buffer);
}
}

printf("\n");
}
printf("\n");
}

printf("+-------+-------+-------+-------+-------+-------+-------+-------+\n\n");
printf("+-------+-------+-------+-------+-------+-------+-------+-------+\n\n");

printf(" NNUE Score: %dcp (white)\n", (int) Normalize(base));
printf("Final Score: %dcp (white)\n", (int) Normalize(scaled));
printf(" NNUE Score: %dcp (white)\n", (int) Normalize(base));
printf("Final Score: %dcp (white)\n", (int) Normalize(scaled));

AlignedFree(board->accumulators);
AlignedFree(board->accumulators);
}
}
4 changes: 3 additions & 1 deletion src/eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
#include "types.h"
#include "util.h"

#define EVAL_UNKNOWN 2046
#define EVAL_UNKNOWN 2046
#define EVAL_LAZY_MARGIN 450

INLINE int ClampEval(int eval) {
return Min(EVAL_UNKNOWN - 1, Max(-EVAL_UNKNOWN + 1, eval));
}

extern const int PC_VALUES[12];
extern const int PHASE_VALUES[6];
extern const int MAX_PHASE;

Expand Down
Loading
Loading