diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 745bd3e4d56..3c2a5da9fcb 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include "nnue/network.h" #include "nnue/nnue_misc.h" @@ -46,8 +45,6 @@ int Eval::simple_eval(const Position& pos) { - pos.non_pawn_material(~c); } -bool Eval::use_smallnet(const Position& pos) { return std::abs(simple_eval(pos)) > 962; } - // Evaluate is the evaluator for the outer world. It returns a static evaluation // of the position from the point of view of the side to move. Value Eval::evaluate(const Eval::NNUE::Networks& networks, @@ -58,22 +55,21 @@ Value Eval::evaluate(const Eval::NNUE::Networks& networks, assert(!pos.checkers()); - bool smallNet = use_smallnet(pos); - auto [psqt, positional] = smallNet ? networks.small.evaluate(pos, accumulators, caches.small) - : networks.big.evaluate(pos, accumulators, caches.big); + int simpleEval = simple_eval(pos); + bool smallNet = std::abs(simpleEval) > 962; - Value nnue = (125 * psqt + 131 * positional) / 128; + Value nnue = smallNet ? networks.small.evaluate(pos, accumulators, caches.small) + : networks.big.evaluate(pos, accumulators, caches.big); // Re-evaluate the position when higher eval accuracy is worth the time spent if (smallNet && (std::abs(nnue) < 277)) { - std::tie(psqt, positional) = networks.big.evaluate(pos, accumulators, caches.big); - nnue = (125 * psqt + 131 * positional) / 128; - smallNet = false; + nnue = networks.big.evaluate(pos, accumulators, caches.big); + smallNet = false; } // Blend optimism and eval with nnue complexity - int nnueComplexity = std::abs(psqt - positional); + int nnueComplexity = std::abs(2 * simpleEval - nnue) - (smallNet ? 630 : 80); optimism += optimism * nnueComplexity / 476; nnue -= nnue * nnueComplexity / 18236; @@ -107,9 +103,8 @@ std::string Eval::trace(Position& pos, const Eval::NNUE::Networks& networks) { ss << std::showpoint << std::showpos << std::fixed << std::setprecision(2) << std::setw(15); - auto [psqt, positional] = networks.big.evaluate(pos, *accumulators, caches->big); - Value v = psqt + positional; - v = pos.side_to_move() == WHITE ? v : -v; + Value v = networks.big.evaluate(pos, *accumulators, caches->big); + v = pos.side_to_move() == WHITE ? v : -v; ss << "NNUE evaluation " << 0.01 * UCIEngine::to_cp(v, pos) << " (white side)\n"; v = evaluate(networks, pos, *accumulators, *caches, VALUE_ZERO); diff --git a/src/nnue/network.cpp b/src/nnue/network.cpp index d1f2b14c321..2354d446ad3 100644 --- a/src/nnue/network.cpp +++ b/src/nnue/network.cpp @@ -185,7 +185,7 @@ Network::evaluate(const Position& pos const auto psqt = featureTransformer.transform(pos, accumulatorStack, cache, transformedFeatures, bucket); const auto positional = network[bucket].propagate(transformedFeatures); - return {static_cast(psqt / OutputScale), static_cast(positional / OutputScale)}; + return static_cast((125 * psqt + 131 * positional) / (128 * OutputScale)); } diff --git a/src/nnue/network.h b/src/nnue/network.h index cb433718d43..3435f199a34 100644 --- a/src/nnue/network.h +++ b/src/nnue/network.h @@ -27,7 +27,6 @@ #include #include #include -#include #include "../misc.h" #include "../types.h" @@ -48,7 +47,7 @@ enum class EmbeddedNNUEType { SMALL, }; -using NetworkOutput = std::tuple; +using NetworkOutput = Value; // The network must be a trivial type, i.e. the memory must be in-line. // This is required to allow sharing the network via shared memory, as diff --git a/src/nnue/nnue_misc.cpp b/src/nnue/nnue_misc.cpp index 66a6764a33d..94cfd758891 100644 --- a/src/nnue/nnue_misc.cpp +++ b/src/nnue/nnue_misc.cpp @@ -28,7 +28,6 @@ #include #include #include -#include #include "../position.h" #include "../types.h" @@ -124,9 +123,8 @@ trace(Position& pos, const Eval::NNUE::Networks& networks, Eval::NNUE::Accumulat // We estimate the value of each piece by doing a differential evaluation from // the current base eval, simulating the removal of the piece from its square. - auto [psqt, positional] = networks.big.evaluate(pos, *accumulators, caches.big); - Value base = psqt + positional; - base = pos.side_to_move() == WHITE ? base : -base; + Value base = networks.big.evaluate(pos, *accumulators, caches.big); + base = pos.side_to_move() == WHITE ? base : -base; for (File f = FILE_A; f <= FILE_H; ++f) for (Rank r = RANK_1; r <= RANK_8; ++r) @@ -140,10 +138,9 @@ trace(Position& pos, const Eval::NNUE::Networks& networks, Eval::NNUE::Accumulat pos.remove_piece(sq); accumulators->reset(); - std::tie(psqt, positional) = networks.big.evaluate(pos, *accumulators, caches.big); - Value eval = psqt + positional; - eval = pos.side_to_move() == WHITE ? eval : -eval; - v = base - eval; + Value eval = networks.big.evaluate(pos, *accumulators, caches.big); + eval = pos.side_to_move() == WHITE ? eval : -eval; + v = base - eval; pos.put_piece(pc, sq); }