1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 16:53:09 +00:00

Use NNUE complexity in search, retune related parameters

This builds on ideas of xoto10 and mstembera to use more output from NNUE in the search algorithm.

passed STC:
https://tests.stockfishchess.org/tests/view/62ae454fe7ee5525ef88a957
LLR: 2.95 (-2.94,2.94) <0.00,2.50>
Total: 89208 W: 24127 L: 23753 D: 41328
Ptnml(0-2): 400, 9886, 23642, 10292, 384

passed LTC:
https://tests.stockfishchess.org/tests/view/62acc6ddd89eb6cf1e0750a1
LLR: 2.93 (-2.94,2.94) <0.50,3.00>
Total: 56352 W: 15430 L: 15115 D: 25807
Ptnml(0-2): 44, 5501, 16782, 5794, 55

closes https://github.com/official-stockfish/Stockfish/pull/4088

bench 5332964
This commit is contained in:
Dubslow 2022-06-10 08:11:02 -05:00 committed by Joost VandeVondele
parent 5304b561ab
commit 442c40b43d
5 changed files with 38 additions and 25 deletions

View file

@ -1080,35 +1080,39 @@ make_v:
/// 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 Position& pos) {
Value Eval::evaluate(const Position& pos, int* complexity) {
Value v;
Color stm = pos.side_to_move();
Value psq = pos.psq_eg_stm();
// Deciding between classical and NNUE eval (~10 Elo): for high PSQ imbalance we use classical,
// but we switch to NNUE during long shuffling or with high material on the board.
bool useClassical = (pos.this_thread()->depth > 9 || pos.count<ALL_PIECES>() > 7) &&
abs(eg_value(pos.psq_score())) * 5 > (856 + pos.non_pawn_material() / 64) * (10 + pos.rule50_count());
bool useClassical = (pos.this_thread()->depth > 9 || pos.count<ALL_PIECES>() > 7)
&& abs(psq) * 5 > (856 + pos.non_pawn_material() / 64) * (10 + pos.rule50_count());
// Deciding between classical and NNUE eval (~10 Elo): for high PSQ imbalance we use classical,
// but we switch to NNUE during long shuffling or with high material on the board.
if (!useNNUE || useClassical)
{
v = Evaluation<NO_TRACE>(pos).value(); // classical
v = Evaluation<NO_TRACE>(pos).value();
useClassical = abs(v) >= 297;
}
// If result of a classical evaluation is much lower than threshold fall back to NNUE
if (useNNUE && !useClassical)
{
int complexity;
int scale = 1048 + 109 * pos.non_pawn_material() / 5120;
Color stm = pos.side_to_move();
int nnueComplexity;
int scale = 1092 + 106 * pos.non_pawn_material() / 5120;
Value optimism = pos.this_thread()->optimism[stm];
Value psq = (stm == WHITE ? 1 : -1) * eg_value(pos.psq_score());
Value nnue = NNUE::evaluate(pos, true, &complexity); // NNUE
complexity = (137 * complexity + 137 * abs(nnue - psq)) / 256;
optimism = optimism * (255 + complexity) / 256;
v = (nnue * scale + optimism * (scale - 848)) / 1024;
Value nnue = NNUE::evaluate(pos, true, &nnueComplexity);
// Blend nnue complexity with (semi)classical complexity
nnueComplexity = (104 * nnueComplexity + 131 * abs(nnue - psq)) / 256;
if (complexity) // Return hybrid NNUE complexity to caller
*complexity = nnueComplexity;
optimism = optimism * (269 + nnueComplexity) / 256;
v = (nnue * scale + optimism * (scale - 754)) / 1024;
if (pos.is_chess960())
v += fix_FRC(pos);
@ -1120,6 +1124,10 @@ Value Eval::evaluate(const Position& pos) {
// Guarantee evaluation does not hit the tablebase range
v = std::clamp(v, VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1);
// When not using NNUE, return classical complexity to caller
if (complexity && (!useNNUE || useClassical))
*complexity = abs(v - psq);
return v;
}

View file

@ -31,7 +31,7 @@ class Position;
namespace Eval {
std::string trace(Position& pos);
Value evaluate(const Position& pos);
Value evaluate(const Position& pos, int* complexity = nullptr);
extern bool useNNUE;
extern std::string currentEvalFileName;

View file

@ -143,7 +143,7 @@ namespace Stockfish::Eval::NNUE {
// overaligning stack variables with alignas() doesn't work correctly.
constexpr uint64_t alignment = CacheLineSize;
int delta = 10 - pos.non_pawn_material() / 1515;
int delta = 24 - pos.non_pawn_material() / 9560;
#if defined(ALIGNAS_ON_STACK_VARIABLES_BROKEN)
TransformedFeatureType transformedFeaturesUnaligned[
@ -166,7 +166,7 @@ namespace Stockfish::Eval::NNUE {
// Give more value to positional evaluation when adjusted flag is set
if (adjusted)
return static_cast<Value>(((128 - delta) * psqt + (128 + delta) * positional) / (128 * OutputScale));
return static_cast<Value>(((1024 - delta) * psqt + (1024 + delta) * positional) / (1024 * OutputScale));
else
return static_cast<Value>((psqt + positional) / OutputScale);
}

View file

@ -161,6 +161,7 @@ public:
bool has_repeated() const;
int rule50_count() const;
Score psq_score() const;
Value psq_eg_stm() const;
Value non_pawn_material(Color c) const;
Value non_pawn_material() const;
@ -342,6 +343,10 @@ inline Score Position::psq_score() const {
return psq;
}
inline Value Position::psq_eg_stm() const {
return (sideToMove == WHITE ? 1 : -1) * eg_value(psq);
}
inline Value Position::non_pawn_material(Color c) const {
return st->nonPawnMaterial[c];
}

View file

@ -307,7 +307,7 @@ void Thread::search() {
multiPV = std::min(multiPV, rootMoves.size());
complexityAverage.set(202, 1);
complexityAverage.set(174, 1);
trend = SCORE_ZERO;
optimism[ us] = Value(39);
@ -472,7 +472,7 @@ void Thread::search() {
double reduction = (1.56 + mainThread->previousTimeReduction) / (2.20 * timeReduction);
double bestMoveInstability = 1 + 1.7 * totBestMoveChanges / Threads.size();
int complexity = mainThread->complexityAverage.value();
double complexPosition = std::clamp(1.0 + (complexity - 326) / 1618.1, 0.5, 1.5);
double complexPosition = std::clamp(1.0 + (complexity - 277) / 1819, 0.5, 1.5);
double totalTime = Time.optimum() * fallingEval * reduction * bestMoveInstability * complexPosition;
@ -736,7 +736,9 @@ namespace {
// Never assume anything about values stored in TT
ss->staticEval = eval = tte->eval();
if (eval == VALUE_NONE)
ss->staticEval = eval = evaluate(pos);
ss->staticEval = eval = evaluate(pos, &complexity);
else // Fall back to (semi)classical complexity for TT hits, the NNUE complexity is lost
complexity = abs(ss->staticEval - pos.psq_eg_stm());
// Randomize draw evaluation
if (eval == VALUE_DRAW)
@ -749,13 +751,15 @@ namespace {
}
else
{
ss->staticEval = eval = evaluate(pos);
ss->staticEval = eval = evaluate(pos, &complexity);
// Save static evaluation into transposition table
if (!excludedMove)
tte->save(posKey, VALUE_NONE, ss->ttPv, BOUND_NONE, DEPTH_NONE, MOVE_NONE, eval);
}
thisThread->complexityAverage.update(complexity);
// Use static evaluation difference to improve quiet move ordering (~3 Elo)
if (is_ok((ss-1)->currentMove) && !(ss-1)->inCheck && !priorCapture)
{
@ -770,11 +774,7 @@ namespace {
improvement = (ss-2)->staticEval != VALUE_NONE ? ss->staticEval - (ss-2)->staticEval
: (ss-4)->staticEval != VALUE_NONE ? ss->staticEval - (ss-4)->staticEval
: 175;
improving = improvement > 0;
complexity = abs(ss->staticEval - (us == WHITE ? eg_value(pos.psq_score()) : -eg_value(pos.psq_score())));
thisThread->complexityAverage.update(complexity);
// Step 7. Razoring.
// If eval is really low check with qsearch if it can exceed alpha, if it can't,
@ -803,7 +803,7 @@ namespace {
&& (ss-1)->statScore < 14695
&& eval >= beta
&& eval >= ss->staticEval
&& ss->staticEval >= beta - 15 * depth - improvement / 15 + 198 + complexity / 28
&& ss->staticEval >= beta - 15 * depth - improvement / 15 + 201 + complexity / 24
&& !excludedMove
&& pos.non_pawn_material(us)
&& (ss->ply >= thisThread->nmpMinPly || us != thisThread->nmpColor))
@ -811,7 +811,7 @@ namespace {
assert(eval - beta >= 0);
// Null move dynamic reduction based on depth, eval and complexity of position
Depth R = std::min(int(eval - beta) / 147, 5) + depth / 3 + 4 - (complexity > 753);
Depth R = std::min(int(eval - beta) / 147, 5) + depth / 3 + 4 - (complexity > 650);
ss->currentMove = MOVE_NULL;
ss->continuationHistory = &thisThread->continuationHistory[0][0][NO_PIECE][0];