Skip to content

Commit

Permalink
Test psqt only
Browse files Browse the repository at this point in the history
  • Loading branch information
PikaCat-OuO committed Mar 3, 2024
1 parent db78ce1 commit 5204c98
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 131 deletions.
3 changes: 2 additions & 1 deletion src/evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ Value Eval::evaluate(const Position& pos, int optimism) {
Color stm = pos.side_to_move();
int shuffling = pos.rule60_count();
int simpleEval = simple_eval(pos, stm);
bool psqtOnly = std::abs(simpleEval) > 2500;

int nnueComplexity;
Value nnue = NNUE::evaluate(pos, true, &nnueComplexity);
Value nnue = NNUE::evaluate(pos, true, &nnueComplexity, psqtOnly);

// Blend optimism and eval with nnue complexity and material imbalance
optimism += optimism * (nnueComplexity + std::abs(simpleEval - nnue)) / 781;
Expand Down
26 changes: 14 additions & 12 deletions src/nnue/evaluate_nnue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ static bool write_parameters(std::ostream& stream, const std::string& netDescrip
}

void hint_common_parent_position(const Position& pos) {
featureTransformer->hint_common_access(pos);
int simpleEvalAbs = std::abs(simple_eval(pos, pos.side_to_move()));
featureTransformer->hint_common_access(pos, simpleEvalAbs > 2500);
}

// Evaluation function. Perform differential calculation.
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 @@ -172,12 +173,12 @@ 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 = featureTransformer->transform(pos, transformedFeatures, bucket);
const auto positional = network[bucket]->propagate(transformedFeatures);
const int bucket = (pos.count<ALL_PIECES>() - 1) / 4;
const auto psqt = featureTransformer->transform(pos, transformedFeatures, bucket, psqtOnly);
const auto positional = !psqtOnly ? network[bucket]->propagate(transformedFeatures) : 0;

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

// Adjust psqt and positional ratio in evaluation when adjusted flag is set
if (adjusted)
Expand Down Expand Up @@ -216,8 +217,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 = featureTransformer->transform(pos, transformedFeatures, bucket);
const auto positional = network[bucket]->propagate(transformedFeatures);
const auto materialist =
featureTransformer->transform(pos, transformedFeatures, bucket, false);
const auto positional = network[bucket]->propagate(transformedFeatures);

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

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

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

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

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

std::string trace(Position& pos);
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);
Expand Down
1 change: 1 addition & 0 deletions src/nnue/nnue_accumulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct alignas(CacheLineSize) Accumulator {
std::int16_t accumulation[2][TransformedFeatureDimensions];
std::int32_t psqtAccumulation[2][PSQTBuckets];
bool computed[2];
bool computedPSQT[2];
};

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

0 comments on commit 5204c98

Please sign in to comment.