diff --git a/README.md b/README.md index 6dc7b08..2243450 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ You can interact with the engine via these commands: - `stop` — Stops the current search - `bench` — Runs a speed benchmark on a set of positions +- `nnue [full]` — Shows a detailed breakdown of the neural network evaluation for the current position (`full` adds histograms, per-step math and top 8 neurons) The engine can be used in any GUI with **UCI support**. diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 8f26ea9..5100b34 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -1,13 +1,131 @@ #include #include #include +#include +#include +#include #include #include "benchmark.h" #include "search.h" #include "stopwatch.h" +#include "wdl.h" using namespace SEARCH; +static std::string BenchScoreString(int score, bool &isMate) { + std::stringstream ss; + isMate = std::abs(score) + MAX_DEPTH >= MATE_SCORE; + if (isMate) { + int mateIn = (MATE_SCORE - (std::abs(score) - 1)) / 2; + mateIn = score < 0 ? mateIn * -1 : mateIn; + if (mateIn < 0) { + ss << "-M" << std::abs(mateIn); + } else { + ss << "+M" << mateIn; + } + } else { + ss << std::showpos << std::fixed << std::setprecision(2) + << (score / 100.0) << std::noshowpos; + } + return ss.str(); +} + +static std::string BenchCountString(U64 count, const char* suffix) { + std::stringstream ss; + if (count >= 1000000) { + ss << std::fixed << std::setprecision(1) << (count / 1000000.0) << "M" << suffix; + } else if (count >= 1000) { + ss << std::fixed << std::setprecision(1) << (count / 1000.0) << "k" << suffix; + } else { + ss << count << suffix; + } + return ss.str(); +} + +static void PrintBenchHeader() { + std::cout << termcolor::bold << termcolor::color<244>; + std::cout << std::setw(4) << std::right << "#"; + std::cout << std::setw(8) << std::left << " Depth"; + std::cout << std::setw(9) << std::right << "Score"; + std::cout << std::setw(10) << std::right << "Nodes"; + std::cout << std::setw(9) << std::right << "Time"; + std::cout << std::setw(9) << std::right << "NPS"; + std::cout << " Best"; + std::cout << termcolor::reset << std::endl; + + std::cout << termcolor::color<238>; + for (int i = 0; i < 64; i++) + std::cout << '-'; + std::cout << termcolor::reset << std::endl; +} + +static void PrintBenchLine(int index, Board& board, SearchResults& results, SearchContext* ctx, U64 posNodes, int elapsedMs) { + if (index % 2 == 0) { + std::cout << termcolor::color<247>; + } else { + std::cout << termcolor::color<251>; + } + + std::cout << std::setw(4) << std::right << index; + + std::stringstream depthStr; + depthStr << BENCH_DEPTH << '/' << ctx->seldepth; + std::cout << termcolor::bold << termcolor::bright_white; + std::cout << std::setw(8) << std::left << (" " + depthStr.str()); + std::cout << termcolor::reset; + if (index % 2 == 0) { + std::cout << termcolor::color<247>; + } else { + std::cout << termcolor::color<251>; + } + + const bool isMateScore = std::abs(results.score) + MAX_DEPTH >= MATE_SCORE; + const int normalizedScore = isMateScore ? results.score : scaleEval(results.score, board); + bool isMate = false; + std::string scoreStr = BenchScoreString(normalizedScore, isMate); + if (isMate) { + int mateIn = (MATE_SCORE - (std::abs(normalizedScore) - 1)) / 2; + mateIn = normalizedScore < 0 ? mateIn * -1 : mateIn; + if (mateIn > 0) { + std::cout << termcolor::bold << termcolor::bright_green; + } else { + std::cout << termcolor::bold << termcolor::bright_red; + } + } else { + if (normalizedScore > 0) { + std::cout << termcolor::bold << termcolor::green; + } else if (normalizedScore < 0) { + std::cout << termcolor::bold << termcolor::red; + } + } + std::cout << std::setw(9) << std::right << scoreStr; + std::cout << termcolor::reset; + if (index % 2 == 0) { + std::cout << termcolor::color<247>; + } else { + std::cout << termcolor::color<251>; + } + + std::cout << std::setw(10) << std::right << BenchCountString(posNodes, ""); + + std::stringstream timeStr; + if (elapsedMs >= 1000) { + timeStr << std::fixed << std::setprecision(1) << (elapsedMs / 1000.0) << "s"; + } else { + timeStr << elapsedMs << "ms"; + } + std::cout << std::setw(9) << std::right << timeStr.str(); + + double elapsedSec = elapsedMs / 1000.0; + U64 nps = elapsedSec > 0 ? U64(posNodes / elapsedSec) : posNodes; + std::cout << std::setw(9) << std::right << BenchCountString(nps, "/s"); + + std::cout << " "; + std::cout << termcolor::bold << termcolor::bright_cyan; + std::cout << results.bestMove.GetMoveString(); + std::cout << termcolor::reset << std::endl; +} + constexpr std::array fenPositions = { "rnbq1k1r/ppp1bppp/4pn2/8/2B5/2NP1N2/PPP2PPP/R1BQR1K1 b - - 2 8", "rnbq1k1r/pp2bppp/4pn2/2p5/2B2B2/2NP1N2/PPP2PPP/R2QR1K1 b - - 1 9", @@ -68,11 +186,35 @@ void RunBenchmark() { auto ctx = std::make_unique(); ctx->TT = &BenchTT; - Stopwatch sw; - for (int i = 0; i < fenPositions.size(); i++) { - board.SetByFen(fenPositions[i]); - SearchPosition(board, SearchParams(), ctx.get()); - } + PrintBenchHeader(); - std::cout << ctx->nodes << " nodes " << int(ctx->nodes/sw.GetElapsedSec()) << " nps" << std::endl; + Stopwatch sw; + for (size_t i = 0; i < fenPositions.size(); i++) { + board.SetByFen(fenPositions[i]); + U64 nodesBefore = ctx->nodes; + Stopwatch posSw; + SearchResults results = SearchPosition(board, SearchParams(), ctx.get()); + int elapsedMs = posSw.GetElapsedMS(); + PrintBenchLine(int(i + 1), board, results, ctx.get(), ctx->nodes - nodesBefore, elapsedMs); + } + double totalSec = sw.GetElapsedSec(); + + std::cout << termcolor::color<238>; + for (int i = 0; i < 64; i++) + std::cout << '-'; + std::cout << termcolor::reset << std::endl; + + std::cout << termcolor::bold << termcolor::bright_white; + std::cout << "Positions: " << fenPositions.size(); + std::cout << " Nodes: " << BenchCountString(ctx->nodes, ""); + std::cout << " Time: "; + if (totalSec >= 1.0) { + std::cout << std::fixed << std::setprecision(1) << totalSec << "s"; + } else { + std::cout << int(totalSec * 1000) << "ms"; + } + std::cout << " Avg: " << BenchCountString(totalSec > 0 ? U64(ctx->nodes / totalSec) : ctx->nodes, "/s"); + std::cout << termcolor::reset << std::endl; + + std::cout << ctx->nodes << " nodes " << int(ctx->nodes/totalSec) << " nps" << std::endl; } diff --git a/source/board.cpp b/source/board.cpp index 693d883..2a3ec7f 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -5,11 +5,19 @@ #include "nnue.h" #include "tt.h" #include +#include +#include +#include +#include +#include #include #include #include #include "types.h" #include "utils.h" +#include "wdl.h" +#include "search.h" +#include "termcolor.hpp" void Board::Reset() { castlingRights = 0; @@ -251,8 +259,511 @@ void Board::PrintBoard() { std::cout << " Fen: " << GetFen() << std::endl; } -void Board::PrintNNUE() { - std::cout << "Final eval: " << NNUE::net.Evaluate(*this) << std::endl; +void Board::PrintNNUE(bool full) { + ACC::AccumulatorPair acc; + RefreshAccumulator(acc); + + const int pieceCount = occupied.PopCount(); + const size_t divisor = 32 / NNUE::OUTPUT_BUCKETS; + const size_t outBucket = (pieceCount - 2) / divisor; + + const int wKingSq = (pieces[King] & colors[White]).getLS1BIndex(); + const int bKingSq = (pieces[King] & colors[Black]).getLS1BIndex(); + const ACC::BucketPair buckets = GetBuckets(); + + const bool stmIsWhite = (sideToMove == White); + const ACC::Accumulator& stmAcc = stmIsWhite ? acc.white : acc.black; + const ACC::Accumulator& nstmAcc = stmIsWhite ? acc.black : acc.white; + + auto bar = [](int value, int maxValue, int width) { + std::string s; + int filled = maxValue > 0 ? (value * width + maxValue / 2) / maxValue : 0; + for (int i = 0; i < width; i++) + s += (i < filled) ? '#' : '.'; + return s; + }; + + auto section = [](const char* name) { + std::cout << termcolor::bold << termcolor::color<244>; + std::cout << "-- " << name << " --"; + std::cout << termcolor::reset << std::endl; + }; + + auto pieceLetter = [&](int sq) { + int pt = mailbox[sq]; + if (pt == nullPieceType) + return ' '; + bool white = colors[White].IsSet(sq); + return PIECE_LETTERS[pt * 2 + (white ? White : Black)]; + }; + + auto boardRow = [&](int rank) { + std::ostringstream row; + row << termcolor::color<244> << rank + 1 << termcolor::reset << " "; + for (int file = 0; file < 8; file++) { + int sq = rank * 8 + file; + int pt = mailbox[sq]; + bool isKingSq = (sq == wKingSq) || (sq == bKingSq); + if (pt == nullPieceType) { + row << termcolor::color<240> << ". " << termcolor::reset; + } else { + bool white = colors[White].IsSet(sq); + if (isKingSq) + row << termcolor::bold << termcolor::reverse; + if (white) + row << termcolor::bright_white; + else + row << termcolor::yellow; + row << pieceLetter(sq) << ' '; + row << termcolor::reset; + } + } + return row.str(); + }; + + auto bucketMapRow = [&](bool forWhite, int rank) { + std::ostringstream row; + int kingSq = forWhite ? wKingSq : bKingSq; + for (int file = 0; file < 8; file++) { + int sq = rank * 8 + file; + int b = NNUE::kingBuckets[forWhite ? White : Black][sq]; + if (sq == kingSq) + row << termcolor::bold << termcolor::reverse << termcolor::bright_cyan; + else + row << termcolor::color<244>; + row << b << ' '; + row << termcolor::reset; + } + return row.str(); + }; + + auto printBoardAndMaps = [&]() { + std::cout << " a b c d e f g h White regions Black regions" << std::endl; + for (int rank = 7; rank >= 0; rank--) { + std::cout << boardRow(rank) << " " << bucketMapRow(true, rank); + std::cout << " " << bucketMapRow(false, rank) << std::endl; + } + std::cout << " a b c d e f g h" << termcolor::color<244> << " Kings highlighted" << termcolor::reset << std::endl; + }; + + auto printGauge = [&](double pawns, const std::string& valueStr, bool positive, bool isMate) { + const int width = 41; + const int center = width / 2; + double cl = std::clamp(pawns, -5.0, 5.0); + int mark = center + int(std::round(cl / 5.0 * center)); + mark = std::clamp(mark, 0, width - 1); + std::cout << " "; + for (int i = 0; i < width; i++) { + if (i == mark) { + std::cout << termcolor::bold << termcolor::bright_white << 'O'; + } else if (i == center) { + std::cout << termcolor::color<244> << '+'; + } else if (i < center) { + std::cout << termcolor::red << '-'; + } else { + std::cout << termcolor::green << '-'; + } + std::cout << termcolor::reset; + } + std::cout << " "; + if (isMate) { + if (positive) + std::cout << termcolor::bold << termcolor::bright_green; + else + std::cout << termcolor::bold << termcolor::bright_red; + } else if (positive) { + std::cout << termcolor::bold << termcolor::green; + } else { + std::cout << termcolor::bold << termcolor::red; + } + std::cout << valueStr << termcolor::reset << std::endl; + std::cout << " " << termcolor::color<244> << "-5" << std::string(center - 2, ' ') << "0" + << std::string(center - 2, ' ') << "+5" << termcolor::reset << std::endl; + }; + + auto printWdlBar = [&](const WDLTriplet& t) { + const int width = 32; + int w = (t.wins * width + 500) / 1000; + int l = (t.losses * width + 500) / 1000; + int d = width - w - l; + std::cout << " "; + std::cout << termcolor::bright_green; + for (int i = 0; i < w; i++) std::cout << '#'; + std::cout << termcolor::color<244>; + for (int i = 0; i < d; i++) std::cout << '#'; + std::cout << termcolor::bright_red; + for (int i = 0; i < l; i++) std::cout << '#'; + std::cout << termcolor::reset << std::endl; + }; + + struct AccStats { + int16_t mn = 0; + int16_t mx = 0; + double mean = 0; + double absMean = 0; + int zeros = 0; + int saturated = 0; + }; + + auto accStats = [](const ACC::Accumulator& a) { + AccStats s; + s.mn = a[0]; + s.mx = a[0]; + int64_t sum = 0; + int64_t absSum = 0; + for (size_t i = 0; i < NNUE::HL_SIZE; i++) { + s.mn = std::min(s.mn, a[i]); + s.mx = std::max(s.mx, a[i]); + sum += a[i]; + absSum += std::abs(a[i]); + int32_t c = std::clamp(a[i], 0, NNUE::QA); + if (c == 0) + s.zeros++; + if (c >= NNUE::QA) + s.saturated++; + } + s.mean = double(sum) / double(NNUE::HL_SIZE); + s.absMean = double(absSum) / double(NNUE::HL_SIZE); + return s; + }; + + auto perspectivePart = [&](const ACC::Accumulator& a, size_t base, std::vector>* top) { + int64_t part = 0; + for (size_t i = 0; i < NNUE::HL_SIZE; i++) { + int32_t c = std::clamp(a[i], 0, NNUE::QA); + int16_t w = NNUE::net.output_weights[outBucket][base + i]; + int16_t prod = int16_t(c * w); + int64_t contrib = int64_t(c) * prod; + part += contrib; + if (top && (c > 0)) + top->emplace_back(std::llabs(contrib), int(i), int16_t(c), w); + } + return part; + }; + + std::vector> stmTop; + std::vector> nstmTop; + stmTop.reserve(NNUE::HL_SIZE); + nstmTop.reserve(NNUE::HL_SIZE); + const int64_t stmPart = perspectivePart(stmAcc, 0, &stmTop); + const int64_t nstmPart = perspectivePart(nstmAcc, NNUE::HL_SIZE, &nstmTop); + const int64_t preBias = (stmPart + nstmPart) / NNUE::QA; + const int16_t bias = NNUE::net.output_bias[outBucket]; + const int64_t biased = preBias + bias; + const int64_t rawEval = (biased * NNUE::SCALE) / (NNUE::QA * NNUE::QB); + + const int nN = pieces[Knight].PopCount(); + const int nB = pieces[Bishop].PopCount(); + const int nR = pieces[Rook].PopCount(); + const int nQ = pieces[Queen].PopCount(); + const int materialScale = 2048 + 90 * nN + 90 * nB + 180 * nR + 360 * nQ; + const int16_t finalEval = std::clamp(rawEval * materialScale / 4096, + (-SEARCH::MATE_SCORE + SEARCH::MAX_DEPTH), (SEARCH::MATE_SCORE - SEARCH::MAX_DEPTH)); + + const bool isMateScore = std::abs(finalEval) + SEARCH::MAX_DEPTH >= SEARCH::MATE_SCORE; + const int normalized = isMateScore ? finalEval : scaleEval(finalEval, *this); + const WDLTriplet wdl = getWDL(finalEval, *this); + + std::cout << termcolor::bold << termcolor::bright_white; + std::cout << "NNUE evaluation detail"; + std::cout << termcolor::reset << std::endl; + std::cout << termcolor::color<244> << "FEN: " << termcolor::reset << GetFen() << std::endl; + std::cout << termcolor::color<244> << "Side to move: " << termcolor::reset; + if (!sideToMove) { + std::cout << termcolor::bold << termcolor::bright_white << "White"; + } else { + std::cout << termcolor::bold << termcolor::color<208> << "Black"; + } + std::cout << termcolor::reset << " " << termcolor::color<244> << "Pieces: " << termcolor::reset << pieceCount << std::endl; + if (full) + std::cout << std::endl; + + section("Position"); + printBoardAndMaps(); + if (full) + std::cout << std::endl; + + section("Architecture"); + std::cout << " HL " << termcolor::bold << termcolor::bright_white << NNUE::HL_SIZE << termcolor::reset; + std::cout << " Input " << termcolor::bold << termcolor::bright_white << NNUE::INPUT_SIZE << termcolor::reset; + std::cout << " In-buckets " << termcolor::bold << termcolor::bright_white << NNUE::INPUT_BUCKETS << termcolor::reset; + std::cout << " Out-buckets " << termcolor::bold << termcolor::bright_white << NNUE::OUTPUT_BUCKETS << termcolor::reset << std::endl; + std::cout << " QA " << termcolor::bold << termcolor::bright_white << NNUE::QA << termcolor::reset; + std::cout << " QB " << termcolor::bold << termcolor::bright_white << NNUE::QB << termcolor::reset; + std::cout << " Scale " << termcolor::bold << termcolor::bright_white << NNUE::SCALE << termcolor::reset << std::endl; + if (full) + std::cout << std::endl; + + section("Buckets"); + std::cout << " Out "; + for (size_t b = 0; b < NNUE::OUTPUT_BUCKETS; b++) { + if (b == outBucket) + std::cout << termcolor::bold << termcolor::bright_cyan << '[' << b << ']' << termcolor::reset; + else + std::cout << termcolor::color<244> << ' ' << b << ' ' << termcolor::reset; + } + std::cout << termcolor::color<244> << " ((" << pieceCount << " - 2) / " << divisor << ")" << termcolor::reset << std::endl; + std::cout << " Kings " << termcolor::bold << termcolor::bright_white << squareCoords[wKingSq] << "->" << buckets.white << termcolor::reset; + std::cout << (mirroredWhite ? "(m)" : "") << " "; + std::cout << termcolor::bold << termcolor::bright_white << squareCoords[bKingSq] << "->" << buckets.black << termcolor::reset; + std::cout << (mirroredBlack ? "(m)" : "") << " Feats "; + std::cout << termcolor::bold << termcolor::bright_white << pieceCount << termcolor::reset << std::endl; + if (full) { + std::cout << " Material "; + std::cout << "N " << termcolor::bold << termcolor::bright_white << nN << termcolor::reset; + std::cout << " B " << termcolor::bold << termcolor::bright_white << nB << termcolor::reset; + std::cout << " R " << termcolor::bold << termcolor::bright_white << nR << termcolor::reset; + std::cout << " Q " << termcolor::bold << termcolor::bright_white << nQ << termcolor::reset; + std::cout << " scale = 2048+90N+90B+180R+360Q = "; + std::cout << termcolor::bold << termcolor::bright_white << materialScale << termcolor::reset; + std::cout << termcolor::color<244> << " (/4096)" << termcolor::reset << std::endl; + } else { + std::cout << " Material scale "; + std::cout << termcolor::bold << termcolor::bright_white << materialScale << termcolor::reset; + std::cout << termcolor::color<244> << " (/4096)" << termcolor::reset << std::endl; + } + if (full) + std::cout << std::endl; + + section("Accumulators"); + for (int p = 0; p < 2; p++) { + const bool isWhite = (p == 0); + const ACC::Accumulator& a = isWhite ? acc.white : acc.black; + const AccStats s = accStats(a); + const int active = int(NNUE::HL_SIZE) - s.zeros; + std::cout << " " << (isWhite ? "White" : "Black"); + if ((stmIsWhite && isWhite) || (!stmIsWhite && !isWhite)) + std::cout << termcolor::bold << termcolor::bright_cyan << " (STM)" << termcolor::reset; + else + std::cout << termcolor::color<244> << " (NSTM)" << termcolor::reset; + std::cout << " min " << termcolor::bold << termcolor::bright_white << s.mn << termcolor::reset; + std::cout << " max " << termcolor::bold << termcolor::bright_white << s.mx << termcolor::reset; + std::cout << " mean "; + std::cout << termcolor::bright_white << std::fixed << std::setprecision(1) << s.mean << termcolor::reset; + if (full) { + std::cout << " |mean| "; + std::cout << termcolor::bright_white << std::fixed << std::setprecision(1) << s.absMean << termcolor::reset; + std::cout << std::endl; + std::cout << " active " << termcolor::bold << termcolor::green << active << termcolor::reset; + std::cout << termcolor::color<244> << " / " << NNUE::HL_SIZE << termcolor::reset; + std::cout << " zero " << termcolor::color<244> << s.zeros << termcolor::reset; + std::cout << " saturated(QA) "; + if (s.saturated > 0) + std::cout << termcolor::bold << termcolor::yellow << s.saturated << termcolor::reset; + else + std::cout << termcolor::color<244> << "0" << termcolor::reset; + std::cout << std::endl; + } else { + std::cout << " act " << termcolor::bold << termcolor::green << active << termcolor::reset; + std::cout << termcolor::color<244> << "/" << NNUE::HL_SIZE << " sat "; + if (s.saturated > 0) + std::cout << termcolor::reset << termcolor::bold << termcolor::yellow << s.saturated << termcolor::reset; + else + std::cout << "0" << termcolor::reset; + std::cout << std::endl; + } + + if (full) { + int bins[8] = {}; + for (size_t i = 0; i < NNUE::HL_SIZE; i++) { + int32_t c = std::clamp(a[i], 0, NNUE::QA); + int bin = std::min(7, (c * 8) / (NNUE::QA + 1)); + bins[bin]++; + } + int binMax = *std::max_element(bins, bins + 8); + std::cout << " act "; + std::cout << termcolor::green << bar(bins[0], binMax, 8) << termcolor::reset; + std::cout << ' ' << termcolor::color<244> << "0" << termcolor::reset; + std::cout << ' ' << termcolor::green << bar(bins[7], binMax, 8) << termcolor::reset; + std::cout << ' ' << termcolor::color<244> << "QA" << termcolor::reset; + std::cout << termcolor::color<244> << " peak bin " << (std::max_element(bins, bins + 8) - bins); + std::cout << " (" << binMax << ")" << termcolor::reset << std::endl; + } + } + if (full) + std::cout << std::endl; + + section("Output layer"); + if (full) { + std::cout << " bias[" << outBucket << "] = "; + std::cout << termcolor::bold << termcolor::bright_white << bias << termcolor::reset << std::endl; + std::cout << " STM" << termcolor::color<244> << (stmIsWhite ? " (White)" : " (Black)") << termcolor::reset; + std::cout << " part = " << termcolor::bold << termcolor::bright_white << stmPart << termcolor::reset << std::endl; + std::cout << " NSTM" << termcolor::color<244> << (stmIsWhite ? " (Black)" : " (White)") << termcolor::reset; + std::cout << " part = " << termcolor::bold << termcolor::bright_white << nstmPart << termcolor::reset << std::endl; + std::cout << " (STM+NSTM) / QA + bias = "; + std::cout << termcolor::bright_white << "(" << stmPart; + if (nstmPart < 0) + std::cout << " - " << -nstmPart; + else + std::cout << " + " << nstmPart; + std::cout << ") / " << NNUE::QA << " + " << bias; + std::cout << " = " << biased << termcolor::reset << std::endl; + std::cout << " x Scale / (QA x QB) = " << termcolor::bright_white << biased << " x " << NNUE::SCALE; + std::cout << " / (" << NNUE::QA << " x " << NNUE::QB << ") = " << rawEval << termcolor::reset << std::endl; + std::cout << " x material / 4096 = " << termcolor::bright_white << rawEval << " x " << materialScale << " / 4096"; + std::cout << " = " << (rawEval * materialScale / 4096) << termcolor::reset << std::endl; + } else { + std::cout << " parts STM " << termcolor::bold << termcolor::bright_white << stmPart << termcolor::reset; + std::cout << " NSTM " << termcolor::bold << termcolor::bright_white << nstmPart << termcolor::reset; + std::cout << " bias " << termcolor::bold << termcolor::bright_white << bias << termcolor::reset; + std::cout << " -> raw " << termcolor::bold << termcolor::bright_white << rawEval << termcolor::reset; + std::cout << " x" << materialScale << "/4096" << std::endl; + } + if (full) + std::cout << std::endl; + + auto humanShort = [](int64_t v) { + std::ostringstream os; + int64_t a = v < 0 ? -v : v; + if (a >= 1000000) + os << (v < 0 ? "-" : "") << std::fixed << std::setprecision(1) << (a / 1000000.0) << 'M'; + else if (a >= 1000) + os << (v < 0 ? "-" : "") << std::fixed << std::setprecision(1) << (a / 1000.0) << 'k'; + else + os << v; + return os.str(); + }; + + auto denseEntry = [&](int idx, int16_t act, int16_t w, int64_t contrib, int64_t maxAbs) { + std::ostringstream os; + int barLen = int((std::llabs(contrib) * 10 + maxAbs / 2) / maxAbs); + os << ' ' << std::setw(4) << idx << ' ' << std::setw(4) << act << ' ' << std::setw(6) << w << ' '; + if (contrib >= 0) + os << termcolor::green; + else + os << termcolor::red; + os << std::setw(7) << humanShort(contrib); + for (int k = 0; k < barLen; k++) os << '#'; + for (int k = barLen; k < 10; k++) os << ' '; + os << termcolor::reset; + return os.str(); + }; + + auto printTopPair = [&](const char* leftLabel, std::vector>& left, + const char* rightLabel, std::vector>& right, size_t count) { + auto byAbs = [](const auto& x, const auto& y) { return std::get<0>(x) > std::get<0>(y); }; + size_t nl = std::min(count, left.size()); + size_t nr = std::min(count, right.size()); + size_t n = std::max(nl, nr); + if (nl > 0) std::partial_sort(left.begin(), left.begin() + nl, left.end(), byAbs); + if (nr > 0) std::partial_sort(right.begin(), right.begin() + nr, right.end(), byAbs); + int64_t maxAbs = 1; + if (nl > 0) maxAbs = std::max(maxAbs, std::get<0>(left[0])); + if (nr > 0) maxAbs = std::max(maxAbs, std::get<0>(right[0])); + std::cout << " " << termcolor::bold << termcolor::bright_white << leftLabel << termcolor::reset; + std::cout << std::string(36 - std::string(leftLabel).size(), ' ') << " "; + std::cout << termcolor::bold << termcolor::bright_white << rightLabel << termcolor::reset << std::endl; + for (size_t i = 0; i < n; i++) { + std::cout << " "; + if (i < nl) { + int idx; int16_t act; int16_t w; int64_t ab; + std::tie(ab, idx, act, w) = left[i]; + std::cout << denseEntry(idx, act, w, int64_t(act) * int16_t(act * w), maxAbs); + } else { + std::cout << std::string(36, ' '); + } + std::cout << " "; + if (i < nr) { + int idx; int16_t act; int16_t w; int64_t ab; + std::tie(ab, idx, act, w) = right[i]; + std::cout << denseEntry(idx, act, w, int64_t(act) * int16_t(act * w), maxAbs); + } + std::cout << std::endl; + } + }; + + auto printTop = [&](const char* label, std::vector>& v, size_t count) { + std::cout << " Top " << label << " neurons" << termcolor::color<244> << " (idx act w contrib)" << termcolor::reset << std::endl; + size_t n = std::min(count, v.size()); + std::partial_sort(v.begin(), v.begin() + n, v.end(), + [](const auto& x, const auto& y) { return std::get<0>(x) > std::get<0>(y); }); + int64_t maxAbs = n > 0 ? std::get<0>(v[0]) : 1; + for (size_t i = 0; i < n; i++) { + int64_t ab; + int idx; + int16_t act; + int16_t w; + std::tie(ab, idx, act, w) = v[i]; + int64_t contrib = int64_t(act) * int16_t(act * w); + int barLen = int((ab * 14 + maxAbs / 2) / maxAbs); + std::cout << " "; + std::cout << termcolor::color<244> << "#" << termcolor::reset; + std::cout << termcolor::bold << termcolor::bright_white << std::setw(5) << idx << termcolor::reset; + std::cout << " act " << termcolor::bright_white << std::setw(4) << act << termcolor::reset; + std::cout << " w " << std::setw(7) << w; + std::cout << " c "; + if (contrib >= 0) + std::cout << termcolor::green; + else + std::cout << termcolor::red; + std::cout << std::setw(11) << contrib; + for (int k = 0; k < barLen; k++) std::cout << '#'; + std::cout << termcolor::reset << std::endl; + } + }; + + section("Top neurons"); + if (full) { + printTop(stmIsWhite ? "White/STM" : "Black/STM", stmTop, 8); + printTop(stmIsWhite ? "Black/NSTM" : "White/NSTM", nstmTop, 8); + } else { + printTopPair(stmIsWhite ? "STM White" : "STM Black", stmTop, + stmIsWhite ? "NSTM Black" : "NSTM White", nstmTop, 3); + std::cout << termcolor::color<244> << " (idx act w contrib, nnue full for top 8)" << termcolor::reset << std::endl; + } + if (full) + std::cout << std::endl; + + section("Eval"); + { + std::stringstream ss; + if (isMateScore) { + int mateIn = (SEARCH::MATE_SCORE - (std::abs(finalEval) - 1)) / 2; + mateIn = finalEval < 0 ? mateIn * -1 : mateIn; + ss << ((mateIn < 0) ? "-M" : "+M") << std::abs(mateIn); + printGauge(mateIn > 0 ? 5.0 : -5.0, ss.str(), mateIn > 0, true); + } else { + ss << std::showpos << std::fixed << std::setprecision(2) << (normalized / 100.0) << std::noshowpos; + printGauge(normalized / 100.0, ss.str(), normalized > 0, false); + } + } + std::cout << " Raw "; + std::cout << termcolor::bold << termcolor::bright_white << rawEval << termcolor::reset; + std::cout << " Normalized "; + if (isMateScore) { + int mateIn = (SEARCH::MATE_SCORE - (std::abs(finalEval) - 1)) / 2; + mateIn = finalEval < 0 ? mateIn * -1 : mateIn; + if (mateIn > 0) + std::cout << termcolor::bold << termcolor::bright_green; + else + std::cout << termcolor::bold << termcolor::bright_red; + std::cout << ((mateIn < 0) ? "-M" : "+M") << std::abs(mateIn); + std::cout << termcolor::reset; + } else { + if (normalized > 0) + std::cout << termcolor::bold << termcolor::green; + else if (normalized < 0) + std::cout << termcolor::bold << termcolor::red; + else + std::cout << termcolor::bright_white; + std::stringstream ss; + ss << std::showpos << std::fixed << std::setprecision(2) << (normalized / 100.0) << std::noshowpos; + std::cout << ss.str(); + std::cout << termcolor::reset; + } + std::cout << " WDL " << termcolor::color<244>; + std::cout << wdl.wins << "W " << wdl.draws << "D " << wdl.losses << "L"; + std::cout << termcolor::reset << std::endl; + printWdlBar(wdl); + std::cout << " Final eval: "; + if (finalEval > 0) + std::cout << termcolor::bold << termcolor::green; + else if (finalEval < 0) + std::cout << termcolor::bold << termcolor::red; + else + std::cout << termcolor::bold << termcolor::bright_white; + std::cout << finalEval << termcolor::reset << std::endl; } void Board::AddMove(Move move) { diff --git a/source/board.h b/source/board.h index d5770e5..9b6ed3c 100644 --- a/source/board.h +++ b/source/board.h @@ -58,7 +58,7 @@ public: std::string GetFen(); void PrintBoard(); - void PrintNNUE(); + void PrintNNUE(bool full = false); void AddMove(Move move); void ResetMoves(); diff --git a/source/perft.cpp b/source/perft.cpp index 41f9446..8662173 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -1,46 +1,265 @@ #include "perft.h" #include "stopwatch.h" #include "movegen.h" -#include - -static U64 HelperPerft(Board &board, int depth) { - MOVEGEN::GenerateMoves(board, true); - - if (depth == 0) return 1ULL; +#include "termcolor.hpp" +#include +#include +#include +struct PerftCounts { U64 nodes = 0; + U64 captures = 0; + U64 ep = 0; + U64 castles = 0; + U64 promotions = 0; + U64 checks = 0; + U64 discovery = 0; + U64 doubles = 0; + U64 mates = 0; - for (int i = 0; i < board.currentMoveIndex; i++) { - if (!board.IsLegal(board.moveList[i])) continue; - - Board copy = board; - copy.MakeMove(board.moveList[i]); - - nodes += HelperPerft(copy, depth - 1); + PerftCounts& operator+=(const PerftCounts& other) { + nodes += other.nodes; + captures += other.captures; + ep += other.ep; + castles += other.castles; + promotions += other.promotions; + checks += other.checks; + discovery += other.discovery; + doubles += other.doubles; + mates += other.mates; + return *this; } - return nodes; +}; + +static bool IsDirectCheck(Board& pos, Move move) { + int mover = !pos.sideToMove; + int kingSq = (pos.pieces[King] & pos.colors[pos.sideToMove]).getLS1BIndex(); + if (kingSq < 0 || kingSq >= 64) + return false; + int to = move.MoveTo(); + int flags = move.GetFlags(); + if (flags == kingCastle || flags == queenCastle) { + int rookFrom = mover ? (flags == kingCastle ? h8 : a8) : (flags == kingCastle ? h1 : a1); + int rookTo = flags == kingCastle ? rookFrom - 2 : rookFrom + 3; + Bitboard rookAtt = MOVEGEN::getRookAttack(rookTo, pos.occupied); + return rookAtt.IsSet(kingSq); + } + int endPiece = pos.GetPieceType(to); + if (endPiece == nullPieceType || endPiece == King) + return false; + Bitboard att = MOVEGEN::getPieceAttacks(to, endPiece, mover, pos.occupied); + return att.IsSet(kingSq); +} + +static void CountLeaf(Board& pos, Move move, PerftCounts& out) { + out.nodes++; + int flags = move.GetFlags(); + if (move.IsCapture()) + out.captures++; + if (flags == epCapture) + out.ep++; + if (flags == kingCastle || flags == queenCastle) + out.castles++; + if (move.IsPromo()) + out.promotions++; + if (pos.InCheck()) { + out.checks++; + if (pos.checkers.PopCount() == 2) + out.doubles++; + if (!IsDirectCheck(pos, move)) + out.discovery++; + MOVEGEN::GenerateMoves(pos, true); + bool hasLegal = false; + for (int i = 0; i < pos.currentMoveIndex; i++) { + Move m = pos.moveList[i]; + if (pos.IsLegal(m)) { + hasLegal = true; + break; + } + } + if (!hasLegal) + out.mates++; + } +} + +static void PerftRec(Board& board, int depth, PerftCounts& out) { + MOVEGEN::GenerateMoves(board, true); + for (int i = 0; i < board.currentMoveIndex; i++) { + Move move = board.moveList[i]; + if (!board.IsLegal(move)) + continue; + Board copy = board; + copy.MakeMove(move); + if (depth == 1) + CountLeaf(copy, move, out); + else + PerftRec(copy, depth - 1, out); + } +} + +static void PrintPerftHeader() { + std::cout << termcolor::bold << termcolor::color<244>; + std::cout << std::setw(6) << std::left << "Move"; + std::cout << std::setw(11) << std::right << "Nodes"; + std::cout << std::setw(8) << std::right << "Capt"; + std::cout << std::setw(7) << std::right << "E.P."; + std::cout << std::setw(7) << std::right << "Cast"; + std::cout << std::setw(7) << std::right << "Promo"; + std::cout << std::setw(8) << std::right << "Check"; + std::cout << std::setw(7) << std::right << "Disc"; + std::cout << std::setw(5) << std::right << "Dbl"; + std::cout << std::setw(7) << std::right << "Mate"; + std::cout << termcolor::reset << std::endl; + + std::cout << termcolor::color<238>; + for (int i = 0; i < 73; i++) + std::cout << '-'; + std::cout << termcolor::reset << std::endl; +} + +static void PrintPerftBase(int index) { + if (index % 2 == 0) + std::cout << termcolor::color<247>; + else + std::cout << termcolor::color<251>; +} + +static void PrintPerftLine(int index, const std::string& moveStr, const PerftCounts& c) { + PrintPerftBase(index); + + std::cout << termcolor::bold << termcolor::bright_cyan; + std::cout << std::setw(6) << std::left << (moveStr + ":"); + std::cout << termcolor::reset; + PrintPerftBase(index); + + std::cout << termcolor::bold << termcolor::bright_white; + std::cout << std::setw(11) << std::right << c.nodes; + std::cout << termcolor::reset; + PrintPerftBase(index); + + if (c.captures > 0) + std::cout << termcolor::bold << termcolor::yellow; + else + std::cout << termcolor::color<244>; + std::cout << std::setw(8) << std::right << c.captures; + std::cout << termcolor::reset; + PrintPerftBase(index); + + if (c.ep > 0) + std::cout << termcolor::bold << termcolor::bright_yellow; + else + std::cout << termcolor::color<244>; + std::cout << std::setw(7) << std::right << c.ep; + std::cout << termcolor::reset; + PrintPerftBase(index); + + if (c.castles > 0) + std::cout << termcolor::bold << termcolor::cyan; + else + std::cout << termcolor::color<244>; + std::cout << std::setw(7) << std::right << c.castles; + std::cout << termcolor::reset; + PrintPerftBase(index); + + if (c.promotions > 0) + std::cout << termcolor::bold << termcolor::magenta; + else + std::cout << termcolor::color<244>; + std::cout << std::setw(7) << std::right << c.promotions; + std::cout << termcolor::reset; + PrintPerftBase(index); + + if (c.checks > 0) + std::cout << termcolor::bold << termcolor::green; + else + std::cout << termcolor::color<244>; + std::cout << std::setw(8) << std::right << c.checks; + std::cout << termcolor::reset; + PrintPerftBase(index); + + if (c.discovery > 0) + std::cout << termcolor::bold << termcolor::bright_magenta; + else + std::cout << termcolor::color<244>; + std::cout << std::setw(7) << std::right << c.discovery; + std::cout << termcolor::reset; + PrintPerftBase(index); + + if (c.doubles > 0) + std::cout << termcolor::bold << termcolor::bright_red; + else + std::cout << termcolor::color<244>; + std::cout << std::setw(5) << std::right << c.doubles; + std::cout << termcolor::reset; + PrintPerftBase(index); + + if (c.mates > 0) + std::cout << termcolor::bold << termcolor::red; + else + std::cout << termcolor::color<244>; + std::cout << std::setw(7) << std::right << c.mates; + std::cout << termcolor::reset << std::endl; } void Perft(Board &board, int depth) { - //board.SetByFen(StartingFen); - MOVEGEN::GenerateMoves(board, true); - - U64 totalNodes = 0; - Stopwatch sw; - for (int i = 0; i < board.currentMoveIndex; i++) { - if (!board.IsLegal(board.moveList[i])) continue; - Board copy = board; - copy.MakeMove(board.moveList[i]); - U64 nodeCount = HelperPerft(copy, depth - 1); - totalNodes += nodeCount; - board.moveList[i].PrintMove(); - std::cout << ": " << nodeCount << std::endl; + if (depth <= 0) { + std::cout << std::endl << "Depth: " << depth << std::endl; + std::cout << "Total nodes: 1" << std::endl; + std::cout << "Time took: " << sw.GetElapsedSec() << "s ("; + std::cout << int(1 / sw.GetElapsedSec()) << " nodes/sec)" << std::endl; + return; } + MOVEGEN::GenerateMoves(board, true); + + PerftCounts total; + int moveCount = 0; + + PrintPerftHeader(); + + for (int i = 0; i < board.currentMoveIndex; i++) { + Move move = board.moveList[i]; + if (!board.IsLegal(move)) + continue; + Board copy = board; + copy.MakeMove(move); + PerftCounts local; + if (depth == 1) + CountLeaf(copy, move, local); + else + PerftRec(copy, depth - 1, local); + total += local; + PrintPerftLine(moveCount, move.GetMoveString(), local); + moveCount++; + } + + double elapsed = sw.GetElapsedSec(); + + std::cout << termcolor::color<238>; + for (int i = 0; i < 73; i++) + std::cout << '-'; + std::cout << termcolor::reset << std::endl; + + PrintPerftLine(moveCount, "Total", total); + + std::cout << std::endl; + std::cout << termcolor::bold << termcolor::bright_white; + std::cout << "Depth: " << depth; + std::cout << " Moves: " << moveCount; + std::cout << " Nodes: " << total.nodes; + U64 nps = elapsed > 0 ? U64(double(total.nodes) / elapsed) : total.nodes; + std::cout << " Time: "; + if (elapsed >= 1.0) + std::cout << elapsed << "s"; + else + std::cout << int(elapsed * 1000) << "ms"; + std::cout << " Avg: " << nps << "/s"; + std::cout << termcolor::reset << std::endl; + std::cout << std::endl << "Depth: " << depth << std::endl; - std::cout << "Total nodes: " << totalNodes << std::endl; - std::cout << "Time took: " << sw.GetElapsedSec() << "s ("; - std::cout << int(totalNodes / sw.GetElapsedSec()) << " nodes/sec)" << std::endl; + std::cout << "Total nodes: " << total.nodes << std::endl; + std::cout << "Time took: " << elapsed << "s ("; + std::cout << int(double(total.nodes) / elapsed) << " nodes/sec)" << std::endl; } diff --git a/source/search.cpp b/source/search.cpp index 4163913..03b627e 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -976,14 +976,26 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* c } ctx->pvLine.Clear(); + if constexpr (mode == normal || mode == nodesMode) { + if (ctx->doPrint && !UCIEnabled) + PrintSearchHeader(); + } + SearchResults results = ID(board, params, ctx); if constexpr (mode != normal && mode != nodesMode) return results; if (ctx->doPrint) { - std::cout << "bestmove "; - results.bestMove.PrintMove(); - std::cout << std::endl; + if (UCIEnabled) { + std::cout << "bestmove "; + results.bestMove.PrintMove(); + std::cout << std::endl; + } else { + std::cout << termcolor::color<244> << "bestmove " << termcolor::reset; + std::cout << termcolor::bold << termcolor::bright_green; + results.bestMove.PrintMove(); + std::cout << termcolor::reset << std::endl; + } } return results; @@ -1006,6 +1018,27 @@ int MoveEstimatedValue(Board& board, Move& move) { return value; } +void PrintSearchHeader() { + if (UCIEnabled) + return; + + std::cout << termcolor::bold << termcolor::color<244>; + std::cout << std::setw(7) << std::left << "Depth"; + std::cout << std::setw(9) << std::right << "Time"; + std::cout << std::setw(9) << std::right << "Score"; + std::cout << std::setw(14) << std::right << "W-D-L"; + std::cout << std::setw(7) << std::right << "TT"; + std::cout << std::setw(9) << std::right << "Nodes"; + std::cout << std::setw(9) << std::right << "NPS"; + std::cout << " PV"; + std::cout << termcolor::reset << std::endl; + + std::cout << termcolor::color<238>; + for (int i = 0; i < 72; i++) + std::cout << '-'; + std::cout << termcolor::reset << std::endl; +} + void PrintSearchInfo(Board& board, SearchContext* ctx, SearchResults& results, int depth, int elapsed) { WDLTriplet wdl = getWDL(results.score, board); const bool isMateScore = std::abs(results.score) + MAX_DEPTH >= MATE_SCORE; @@ -1045,7 +1078,14 @@ void PrintSearchInfo(Board& board, SearchContext* ctx, SearchResults& results, i std::stringstream depthStr; depthStr << depth << '/' << ctx->seldepth; - std::cout << std::setw(6) << std::left << depthStr.str(); + std::cout << termcolor::bold << termcolor::bright_white; + std::cout << std::setw(7) << std::left << depthStr.str(); + std::cout << termcolor::reset; + if (depth % 2 == 0) { + std::cout << termcolor::color<247>; + } else { + std::cout << termcolor::color<251>; + } std::stringstream timeStr; if (elapsed >= 1000) { @@ -1053,7 +1093,7 @@ void PrintSearchInfo(Board& board, SearchContext* ctx, SearchResults& results, i } else { timeStr << elapsed << "ms"; } - std::cout << std::setw(10) << std::right << timeStr.str(); + std::cout << std::setw(9) << std::right << timeStr.str(); std::stringstream scoreStr; bool isMate = false; @@ -1075,15 +1115,15 @@ void PrintSearchInfo(Board& board, SearchContext* ctx, SearchResults& results, i if (isMate) { if (mateIn > 0) { - std::cout << termcolor::bright_green; + std::cout << termcolor::bold << termcolor::bright_green; } else { - std::cout << termcolor::bright_red; + std::cout << termcolor::bold << termcolor::bright_red; } } else { if (results.score > 0) { - std::cout << termcolor::green; + std::cout << termcolor::bold << termcolor::green; } else if (results.score < 0) { - std::cout << termcolor::red; + std::cout << termcolor::bold << termcolor::red; } } @@ -1100,11 +1140,31 @@ void PrintSearchInfo(Board& board, SearchContext* ctx, SearchResults& results, i wdlStr << displayWDL.wins << "W " << displayWDL.draws << "D " << displayWDL.losses << "L"; + std::cout << termcolor::color<244>; std::cout << std::setw(14) << std::right << wdlStr.str(); + if (depth % 2 == 0) { + std::cout << termcolor::color<247>; + } else { + std::cout << termcolor::color<251>; + } - std::stringstream hashfullStr; - hashfullStr << "TT: " << (ctx->TT->GetUsedPercentage() + 5) / 10 << '%'; - std::cout << std::setw(10) << std::right << hashfullStr.str(); + int hashUsed = ctx->TT->GetUsedPercentage(); + std::stringstream hashStr; + hashStr << (hashUsed + 5) / 10 << '%'; + if (hashUsed >= 950) { + std::cout << termcolor::bright_red; + } else if (hashUsed >= 700) { + std::cout << termcolor::yellow; + } else { + std::cout << termcolor::green; + } + std::cout << std::setw(7) << std::right << hashStr.str(); + std::cout << termcolor::reset; + if (depth % 2 == 0) { + std::cout << termcolor::color<247>; + } else { + std::cout << termcolor::color<251>; + } std::stringstream nodesStr; if (ctx->nodes >= 1000000) { @@ -1114,7 +1174,7 @@ void PrintSearchInfo(Board& board, SearchContext* ctx, SearchResults& results, i } else { nodesStr << ctx->nodes; } - std::cout << std::setw(11) << std::right << nodesStr.str(); + std::cout << std::setw(9) << std::right << nodesStr.str(); int nps = int(ctx->nodes/ctx->sw.GetElapsedSec()); std::stringstream npsStr; @@ -1125,7 +1185,7 @@ void PrintSearchInfo(Board& board, SearchContext* ctx, SearchResults& results, i } else { npsStr << nps << "/s"; } - std::cout << std::setw(12) << std::right << npsStr.str(); + std::cout << std::setw(9) << std::right << npsStr.str(); std::cout << " "; ctx->pvLine.Print(0, depth % 2 == 0); diff --git a/source/search.h b/source/search.h index 4fceee4..e9f6e40 100644 --- a/source/search.h +++ b/source/search.h @@ -170,7 +170,7 @@ public: void Print(int n, bool isEven = false) { for (int i = 0; i < length[n]; i++) { if (i == 0 && !UCIEnabled) { - std::cout << termcolor::bright_cyan; + std::cout << termcolor::bold << termcolor::bright_cyan; table[n][i].PrintMove(); std::cout << termcolor::reset; @@ -182,7 +182,8 @@ public: } else { table[n][i].PrintMove(); } - std::cout << ' '; + if (i + 1 < length[n]) + std::cout << ' '; } } @@ -272,5 +273,7 @@ bool IsDraw(Board &board, SearchContext* ctx); void PrintSearchInfo(Board& board, SearchContext* ctx, SearchResults& results, int depth, int elapsed); +void PrintSearchHeader(); + int MoveEstimatedValue(Board& board, Move& move); } diff --git a/source/uci.cpp b/source/uci.cpp index b346f00..0082599 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -443,7 +443,7 @@ void UCILoop(Board &board) { } if (input.find("nnue") != std::string::npos) { - board.PrintNNUE(); + board.PrintNNUE(input.find("full") != std::string::npos); continue; }