Skip to content

Commit

Permalink
Only evaluate the PSQT part of the small net for large evals.
Browse files Browse the repository at this point in the history
Thanks to Viren6 for suggesting to set complexity to 0.

STC https://tests.stockfishchess.org/tests/view/65d7d6709b2da0226a5a203f
LLR: 2.92 (-2.94,2.94) <0.00,2.00>
Total: 328384 W: 85316 L: 84554 D: 158514
Ptnml(0-2): 1414, 39076, 82486, 39766, 1450

LTC https://tests.stockfishchess.org/tests/view/65dce6d290f639b028a54d2e
LLR: 2.95 (-2.94,2.94) <0.50,2.50>
Total: 165162 W: 41918 L: 41330 D: 81914
Ptnml(0-2): 102, 18332, 45124, 18922, 101

closes #5083

bench: 1504003
  • Loading branch information
mstembera authored and vondele committed Mar 3, 2024
1 parent 0a3eb1d commit 7831131
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 144 deletions.
5 changes: 3 additions & 2 deletions src/evaluate.cpp
Expand Up @@ -194,11 +194,12 @@ Value Eval::evaluate(const Position& pos, int optimism) {

int simpleEval = simple_eval(pos, pos.side_to_move());
bool smallNet = std::abs(simpleEval) > 1050;
bool psqtOnly = std::abs(simpleEval) > 2500;

int nnueComplexity;

Value nnue = smallNet ? NNUE::evaluate<NNUE::Small>(pos, true, &nnueComplexity)
: NNUE::evaluate<NNUE::Big>(pos, true, &nnueComplexity);
Value nnue = smallNet ? NNUE::evaluate<NNUE::Small>(pos, true, &nnueComplexity, psqtOnly)
: NNUE::evaluate<NNUE::Big>(pos, true, &nnueComplexity, false);

// Blend optimism and eval with nnue complexity and material imbalance
optimism += optimism * (nnueComplexity + std::abs(simpleEval - nnue)) / 512;
Expand Down
47 changes: 27 additions & 20 deletions src/nnue/evaluate_nnue.cpp
Expand Up @@ -179,16 +179,16 @@ write_parameters(std::ostream& stream, NetSize netSize, const std::string& netDe

void hint_common_parent_position(const Position& pos) {

int simpleEval = simple_eval(pos, pos.side_to_move());
if (std::abs(simpleEval) > 1050)
featureTransformerSmall->hint_common_access(pos);
int simpleEvalAbs = std::abs(simple_eval(pos, pos.side_to_move()));
if (simpleEvalAbs > 1050)
featureTransformerSmall->hint_common_access(pos, simpleEvalAbs > 2500);
else
featureTransformerBig->hint_common_access(pos);
featureTransformerBig->hint_common_access(pos, false);
}

// Evaluation function. Perform differential calculation.
template<NetSize Net_Size>
Value evaluate(const Position& pos, bool adjusted, int* complexity) {
Value evaluate(const Position& pos, bool adjusted, int* complexity, bool psqtOnly) {

// We manually align the arrays on the stack because with gcc < 9.3
// overaligning stack variables with alignas() doesn't work correctly.
Expand All @@ -213,15 +213,19 @@ Value evaluate(const Position& pos, bool adjusted, int* complexity) {

ASSERT_ALIGNED(transformedFeatures, alignment);

const int bucket = (pos.count<ALL_PIECES>() - 1) / 4;
const auto psqt = Net_Size == Small
? featureTransformerSmall->transform(pos, transformedFeatures, bucket)
: featureTransformerBig->transform(pos, transformedFeatures, bucket);
const auto positional = Net_Size == Small ? networkSmall[bucket]->propagate(transformedFeatures)
: networkBig[bucket]->propagate(transformedFeatures);
const int bucket = (pos.count<ALL_PIECES>() - 1) / 4;
const auto psqt =
Net_Size == Small
? featureTransformerSmall->transform(pos, transformedFeatures, bucket, psqtOnly)
: featureTransformerBig->transform(pos, transformedFeatures, bucket, psqtOnly);

const auto positional =
!psqtOnly ? (Net_Size == Small ? networkSmall[bucket]->propagate(transformedFeatures)
: networkBig[bucket]->propagate(transformedFeatures))
: 0;

if (complexity)
*complexity = std::abs(psqt - positional) / OutputScale;
*complexity = !psqtOnly ? std::abs(psqt - positional) / OutputScale : 0;

// Give more value to positional evaluation when adjusted flag is set
if (adjusted)
Expand All @@ -231,8 +235,8 @@ Value evaluate(const Position& pos, bool adjusted, int* complexity) {
return static_cast<Value>((psqt + positional) / OutputScale);
}

template Value evaluate<Big>(const Position& pos, bool adjusted, int* complexity);
template Value evaluate<Small>(const Position& pos, bool adjusted, int* complexity);
template Value evaluate<Big>(const Position& pos, bool adjusted, int* complexity, bool psqtOnly);
template Value evaluate<Small>(const Position& pos, bool adjusted, int* complexity, bool psqtOnly);

struct NnueEvalTrace {
static_assert(LayerStacks == PSQTBuckets);
Expand Down Expand Up @@ -265,8 +269,9 @@ static NnueEvalTrace trace_evaluate(const Position& pos) {
t.correctBucket = (pos.count<ALL_PIECES>() - 1) / 4;
for (IndexType bucket = 0; bucket < LayerStacks; ++bucket)
{
const auto materialist = featureTransformerBig->transform(pos, transformedFeatures, bucket);
const auto positional = networkBig[bucket]->propagate(transformedFeatures);
const auto materialist =
featureTransformerBig->transform(pos, transformedFeatures, bucket, false);
const auto positional = networkBig[bucket]->propagate(transformedFeatures);

t.psqt[bucket] = static_cast<Value>(materialist / OutputScale);
t.positional[bucket] = static_cast<Value>(positional / OutputScale);
Expand Down Expand Up @@ -370,16 +375,18 @@ std::string trace(Position& pos) {
auto st = pos.state();

pos.remove_piece(sq);
st->accumulatorBig.computed[WHITE] = false;
st->accumulatorBig.computed[BLACK] = false;
st->accumulatorBig.computed[WHITE] = st->accumulatorBig.computed[BLACK] =
st->accumulatorBig.computedPSQT[WHITE] = st->accumulatorBig.computedPSQT[BLACK] =
false;

Value eval = evaluate<NNUE::Big>(pos);
eval = pos.side_to_move() == WHITE ? eval : -eval;
v = base - eval;

pos.put_piece(pc, sq);
st->accumulatorBig.computed[WHITE] = false;
st->accumulatorBig.computed[BLACK] = false;
st->accumulatorBig.computed[WHITE] = st->accumulatorBig.computed[BLACK] =
st->accumulatorBig.computedPSQT[WHITE] = st->accumulatorBig.computedPSQT[BLACK] =
false;
}

writeSquare(f, r, pc, v);
Expand Down
5 changes: 4 additions & 1 deletion src/nnue/evaluate_nnue.h
Expand Up @@ -76,7 +76,10 @@ using LargePagePtr = std::unique_ptr<T, LargePageDeleter<T>>;

std::string trace(Position& pos);
template<NetSize Net_Size>
Value evaluate(const Position& pos, bool adjusted = false, int* complexity = nullptr);
Value evaluate(const Position& pos,
bool adjusted = false,
int* complexity = nullptr,
bool psqtOnly = false);
void hint_common_parent_position(const Position& pos);

std::optional<std::string> load_eval(std::istream& stream, NetSize netSize);
Expand Down
1 change: 1 addition & 0 deletions src/nnue/nnue_accumulator.h
Expand Up @@ -34,6 +34,7 @@ struct alignas(CacheLineSize) Accumulator {
std::int16_t accumulation[2][Size];
std::int32_t psqtAccumulation[2][PSQTBuckets];
bool computed[2];
bool computedPSQT[2];
};

} // namespace Stockfish::Eval::NNUE
Expand Down

0 comments on commit 7831131

Please sign in to comment.