Skip to content

Commit

Permalink
Merge pull request #183 from Joachim26/0425_Main_SFNNv9.6.3
Browse files Browse the repository at this point in the history
0425_Main_SFNNv9.6.3
  • Loading branch information
Joachim26 authored May 1, 2024
2 parents 0f7224e + 9552176 commit 5fbdf12
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 295 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Bryan Cross (crossbr)
candirufish
Chess13234
Chris Cain (ceebo)
Ciekce
clefrks
Clemens L. (rn5f107s2)
Cody Ho (aesrentai)
Expand Down
12 changes: 10 additions & 2 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Engine::Engine(std::string path) :
NN::NetworkMedium({EvalFileDefaultNameMedium, "None", ""}, NN::EmbeddedNNUEType::MEDIUM),
NN::NetworkSmall({EvalFileDefaultNameSmall, "None", ""}, NN::EmbeddedNNUEType::SMALL))) {
pos.set(StartFEN, false, &states->back());
capSq = SQ_NONE;
}

std::uint64_t Engine::perft(const std::string& fen, Depth depth, bool isChess960) {
Expand All @@ -62,9 +63,10 @@ std::uint64_t Engine::perft(const std::string& fen, Depth depth, bool isChess960
return Benchmark::perft(fen, depth, isChess960);
}

void Engine::go(const Search::LimitsType& limits) {
void Engine::go(Search::LimitsType& limits) {
assert(limits.perft == 0);
verify_networks();
limits.capSq = capSq;

threads.start_thinking(options, pos, states, limits);
}
Expand Down Expand Up @@ -103,6 +105,7 @@ void Engine::set_position(const std::string& fen, const std::vector<std::string>
states = StateListPtr(new std::deque<StateInfo>(1));
pos.set(fen, options["UCI_Chess960"], &states->back());

capSq = SQ_NONE;
for (const auto& move : moves)
{
auto m = UCIEngine::to_move(pos, move);
Expand All @@ -112,6 +115,11 @@ void Engine::set_position(const std::string& fen, const std::vector<std::string>

states->emplace_back();
pos.do_move(m, states->back());

capSq = SQ_NONE;
DirtyPiece& dp = states->back().dirtyPiece;
if (dp.dirty_num > 1 && dp.to[1] == SQ_NONE)
capSq = m.to_sq();
}
}

Expand Down Expand Up @@ -180,4 +188,4 @@ std::string Engine::visualize() const {
return ss.str();
}

}
}
11 changes: 7 additions & 4 deletions src/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@
#define ENGINE_H_INCLUDED

#include <cstddef>
#include <cstdint>
#include <functional>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <cstdint>

#include "nnue/network.h"
#include "position.h"
#include "search.h"
#include "syzygy/tbprobe.h" // for Stockfish::Depth
#include "thread.h"
#include "tt.h"
#include "ucioption.h"
#include "syzygy/tbprobe.h" // for Stockfish::Depth

namespace Stockfish {

enum Square : int;

class Engine {
public:
using InfoShort = Search::InfoShort;
Expand All @@ -50,7 +52,7 @@ class Engine {
std::uint64_t perft(const std::string& fen, Depth depth, bool isChess960);

// non blocking call to start searching
void go(const Search::LimitsType&);
void go(Search::LimitsType&);
// non blocking call to stop searching
void stop();

Expand Down Expand Up @@ -93,6 +95,7 @@ class Engine {

Position pos;
StateListPtr states;
Square capSq;

OptionsMap options;
ThreadPool threads;
Expand All @@ -105,4 +108,4 @@ class Engine {
} // namespace Stockfish


#endif // #ifndef ENGINE_H_INCLUDED
#endif // #ifndef ENGINE_H_INCLUDED
28 changes: 16 additions & 12 deletions src/evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,30 @@ Value Eval::evaluate(const Eval::NNUE::Networks& networks,
bool psqtOnly = std::abs(simpleEval) > PsqtOnlyThreshold;
int nnueComplexity;
int v;


//Value nnue = smallNet
// ? networks.small.evaluate(pos, &caches.small, true, &nnueComplexity, psqtOnly)
// : networks.big.evaluate(pos, &caches.big, true, &nnueComplexity, false);

Value nnue;

if (smallNet)
nnue = networks.small.evaluate(pos, nullptr, true, &nnueComplexity, psqtOnly);
nnue = networks.small.evaluate(pos, &caches.small, true, &nnueComplexity, psqtOnly);
else {
if (Eval::mediumNetOn)
nnue = networks.medium.evaluate(pos, nullptr, true, &nnueComplexity, false); //funktioniert Cache? Nö &caches.medium
else
nnue = networks.big.evaluate(pos, &caches.big, true, &nnueComplexity, false);
}
const auto adjustEval = [&](int optDiv, int nnueDiv, int npmDiv, int pawnCountConstant,
int pawnCountMul, int npmConstant, int evalDiv,
int shufflingConstant, int shufflingDiv) {
}
const auto adjustEval = [&](int optDiv, int nnueDiv, int pawnCountConstant, int pawnCountMul,
int npmConstant, int evalDiv, int shufflingConstant,
int shufflingDiv) {

// Blend optimism and eval with nnue complexity and material imbalance
optimism += optimism * (nnueComplexity + std::abs(simpleEval - nnue)) / optDiv;
nnue -= nnue * (nnueComplexity * 5 / 3) / nnueDiv;

int npm = pos.non_pawn_material() / npmDiv;
int npm = pos.non_pawn_material() / 64;
v = (nnue * (npm + pawnCountConstant + pawnCountMul * pos.count<PAWN>())
+ optimism * (npmConstant + npm))
/ evalDiv;
Expand All @@ -97,11 +101,11 @@ Value Eval::evaluate(const Eval::NNUE::Networks& networks,
};

if (!smallNet)
adjustEval(524, 32395, 66, 942, 11, 139, 1058, 178, 204);
adjustEval(524, 32395, 942, 11, 139, 1058, 178, 204);
else if (psqtOnly)
adjustEval(517, 32857, 65, 908, 7, 155, 1006, 224, 238);
adjustEval(517, 32857, 908, 7, 155, 1006, 224, 238);
else
adjustEval(515, 32793, 63, 944, 9, 140, 1067, 206, 206);
adjustEval(515, 32793, 944, 9, 140, 1067, 206, 206);

// SFnps Begin //
if((NNUE::RandomEval) || (NNUE::WaitMs))
Expand Down Expand Up @@ -133,11 +137,11 @@ Value Eval::evaluate(const Eval::NNUE::Networks& networks,
// Trace scores are from white's point of view
std::string Eval::trace(Position& pos, const Eval::NNUE::Networks& networks) {

auto caches = std::make_unique<Eval::NNUE::AccumulatorCaches>();

if (pos.checkers())
return "Final evaluation: none (in check)";

auto caches = std::make_unique<Eval::NNUE::AccumulatorCaches>(networks);

std::stringstream ss;
ss << std::showpoint << std::noshowpos << std::fixed << std::setprecision(2);
ss << '\n' << NNUE::trace(pos, networks, *caches) << '\n';
Expand Down
10 changes: 7 additions & 3 deletions src/nnue/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,19 @@ void Network<Arch, Transformer>::verify(std::string evalfilePath) const {
exit(EXIT_FAILURE);
}

sync_cout << "info string NNUE evaluation using " << evalfilePath << sync_endl;
size_t size = sizeof(*featureTransformer) + sizeof(*network) * LayerStacks;
sync_cout << "info string NNUE evaluation using " << evalfilePath << " ("
<< size / (1024 * 1024) << "MiB, (" << featureTransformer->InputDimensions << ", "
<< network[0]->TransformedFeatureDimensions << ", " << network[0]->FC_0_OUTPUTS
<< ", " << network[0]->FC_1_OUTPUTS << ", 1))" << sync_endl;
}


template<typename Arch, typename Transformer>
void Network<Arch, Transformer>::hint_common_access(const Position& pos,
AccumulatorCaches::Cache<FTDimensions>* cache,
bool psqtOnl) const {
featureTransformer->hint_common_access(pos, cache, psqtOnl);
bool psqtOnly) const {
featureTransformer->hint_common_access(pos, cache, psqtOnly);
}

template<typename Arch, typename Transformer>
Expand Down
2 changes: 1 addition & 1 deletion src/nnue/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Network {

void hint_common_access(const Position& pos,
AccumulatorCaches::Cache<FTDimensions>* cache,
bool psqtOnl) const;
bool psqtOnly) const;

void verify(std::string evalfilePath) const;
NnueEvalTrace trace_evaluate(const Position& pos,
Expand Down
38 changes: 22 additions & 16 deletions src/nnue/nnue_accumulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,54 +50,60 @@ struct alignas(CacheLineSize) Accumulator {
// is commonly referred to as "Finny Tables".
struct AccumulatorCaches {

template<typename Networks>
AccumulatorCaches(const Networks& networks) {
clear(networks);
}

template<IndexType Size>
struct alignas(CacheLineSize) Cache {

struct alignas(CacheLineSize) Entry {
BiasType accumulation[COLOR_NB][Size];
PSQTWeightType psqtAccumulation[COLOR_NB][PSQTBuckets];
Bitboard byColorBB[COLOR_NB][COLOR_NB];
Bitboard byTypeBB[COLOR_NB][PIECE_TYPE_NB];
BiasType accumulation[Size];
PSQTWeightType psqtAccumulation[PSQTBuckets];
Bitboard byColorBB[COLOR_NB];
Bitboard byTypeBB[PIECE_TYPE_NB];
bool psqtOnly;

// To initialize a refresh entry, we set all its bitboards empty,
// so we put the biases in the accumulation, without any weights on top
void clear(const BiasType* biases) {

std::memset(byColorBB, 0, sizeof(byColorBB));
std::memset(byTypeBB, 0, sizeof(byTypeBB));

std::memcpy(accumulation[WHITE], biases, Size * sizeof(BiasType));
std::memcpy(accumulation[BLACK], biases, Size * sizeof(BiasType));

std::memset(psqtAccumulation, 0, sizeof(psqtAccumulation));
std::memcpy(accumulation, biases, sizeof(accumulation));
std::memset((uint8_t*) this + offsetof(Entry, psqtAccumulation), 0,
sizeof(Entry) - offsetof(Entry, psqtAccumulation));
}
};

template<typename Network>
void clear(const Network& network) {
for (auto& entry : entries)
entry.clear(network.featureTransformer->biases);
for (auto& entries1D : entries)
for (auto& entry : entries1D)
entry.clear(network.featureTransformer->biases);
}

void clear(const BiasType* biases) {
for (auto& entry : entries)
entry.clear(biases);
}

Entry& operator[](Square sq) { return entries[sq]; }
std::array<Entry, COLOR_NB>& operator[](Square sq) { return entries[sq]; }

std::array<Entry, SQUARE_NB> entries;
std::array<std::array<Entry, COLOR_NB>, SQUARE_NB> entries;
};

template<typename Networks>
void clear(const Networks& networks) {
big.clear(networks.big);
small.clear(networks.small);
}

// When adding a new cache for a network, i.e. the smallnet
// the appropriate condition must be added to FeatureTransformer::update_accumulator_refresh.
//Cache<TransformedFeatureDimensionsMedium> medium;
Cache<TransformedFeatureDimensionsBig> big;
Cache<TransformedFeatureDimensionsBig> big;
Cache<TransformedFeatureDimensionsMedium> medium;
Cache<TransformedFeatureDimensionsSmall> small;
};

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

0 comments on commit 5fbdf12

Please sign in to comment.