Skip to content
Open
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
23 changes: 9 additions & 14 deletions src/evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <iostream>
#include <memory>
#include <sstream>
#include <tuple>

#include "nnue/network.h"
#include "nnue/nnue_misc.h"
Expand All @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/nnue/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Network<Arch, Transformer>::evaluate(const Position& pos
const auto psqt =
featureTransformer.transform(pos, accumulatorStack, cache, transformedFeatures, bucket);
const auto positional = network[bucket].propagate(transformedFeatures);
return {static_cast<Value>(psqt / OutputScale), static_cast<Value>(positional / OutputScale)};
return static_cast<Value>((125 * psqt + 131 * positional) / (128 * OutputScale));
}


Expand Down
3 changes: 1 addition & 2 deletions src/nnue/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <optional>
#include <string>
#include <string_view>
#include <tuple>

#include "../misc.h"
#include "../types.h"
Expand All @@ -48,7 +47,7 @@ enum class EmbeddedNNUEType {
SMALL,
};

using NetworkOutput = std::tuple<Value, Value>;
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
Expand Down
13 changes: 5 additions & 8 deletions src/nnue/nnue_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <iostream>
#include <sstream>
#include <string_view>
#include <tuple>

#include "../position.h"
#include "../types.h"
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
Loading