Skip to content

Commit

Permalink
Tune output Layer
Browse files Browse the repository at this point in the history
  • Loading branch information
PikaCat-OuO committed Oct 11, 2024
1 parent 060bc59 commit 8a8aa74
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ class Engine {
std::string thread_allocation_information_as_string() const;
std::string thread_binding_information_as_string() const;

private:
const std::string binaryDirectory;

NumaReplicationContext numaContext;
Expand Down
76 changes: 76 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,48 @@

using namespace Stockfish;

static int weights[Eval::NNUE::LayerStacks * Eval::NNUE::NetworkArchitecture::FC_1_OUTPUTS]{
-20, 7, 58, -18, 26, -16, 61, -68, -14, 7, -18, 41, 21, 8, 10, 7, -39, -13, -11,
7, -5, 14, -13, -30, -19, 7, -26, 19, -11, -10, -7, -15, -4, 14, 17, -5, 27, -6,
-38, -33, -4, 9, -9, 10, 11, 58, 47, 10, -21, -4, -24, -66, -5, 6, -19, -11, 16,
20, -18, 23, -16, 4, -74, -31, -24, -30, 9, -15, 9, -9, 20, 7, -11, 14, -8, 9,
9, 19, -2, 18, -13, -11, -17, -13, -23, 9, 1, -10, -18, 49, -25, -2, -32, -11, -16,
-8, -8, 6, 14, 10, -11, -7, 3, 9, -12, 14, -17, 13, 12, 6, 29, 7, -12, -17,
55, -14, -32, 9, -20, -12, -7, 15, -10, 20, -30, -5, -6, 7, -34, 14, 52, -11, 23,
-15, 17, 124, -14, 14, -36, 58, 11, 24, 13, 15, -19, -18, -28, -7, -20, 14, -31, -19,
-11, 27, -11, 14, -13, -10, -9, -38, -8, 9, 7, -27, 89, -8, -15, 10, -9, 14, -7,
12, 28, 9, 23, -18, -48, -29, 31, -56, -10, 17, -15, -5, -9, 17, -15, 61, -14, -24,
-11, -27, -7, 11, 8, -9, 14, -13, 11, 89, -19, 8, -15, 16, 16, -7, 8, 6, -12,
-35, -12, -14, -21, 7, 11, -18, -15, 18, -8, 23, -9, -17, 15, -10, -7, 6, 13, -7,
24, -7, -13, 5, -13, 13, -8, 33, 11, 7, 8, 13, -14, -14, -6, -26, 24, 6, -8,
-11, -8, 7, -19, 18, -10, -19, -15, -17, -15, 15, 9, -21, 16, -8, 14, 27, -16, 12,
-11, 13, 13, 30, 28, -22, -12, -13, -15, -43, -21, 16, -26, -10, -21, 10, -13, 2, -44,
-14, 20, -17, -8, -5, 3, -36, 3, -14, -18, 0, -14, 11, -10, 17, 12, 14, 12, 1,
-17, -9, 17, -28, -9, 15, 9, -16, 16, 23, -11, 35, -17, -12, -12, -18, -22, -10, 9,
-11, 11, -14, -21, 21, -11, 7, -12, 27, 12, -25, 8, 19, -21, -19, -10, -27, -2, 10,
0, -13, -11, 19, -11, 42, -13, 1, -10, 2, -7, -16, 15, -17, 8, -11, 12, 11, -12,
10, -10, 29, 23, 8, 19, 5, -10, -9, -8, -19, -37, 7, -16, -12, -17, 6, -9, 6,
-9, 6, -9, -3, -21, 23, 16, -21, 13, -9, 14, 12, -8, 9, -21, 7, 12, -17, 10,
13, -15, -28, 8, -19, -23, 6, 13, -12, 9, 27, -8, 11, -28, -12, -11, -31, -9, 33,
8, -29, 18, -21, -48, 16, -14, 25, -3, 27, 19, 20, 21, -9, -11, -41, -11, -9, -13,
35, -15, -16, -9, 9, -13, -25, 47, -11, -31, -8, -44, 35, 6, -11, 11, -3, -15, 7,
-46, 10, -26, 10, 10, 14, 14, 40, -17, 3, -26, -33, -10, 11, 26, -11, 7, 17, -37,
19, -8, -31, 11, -12, -8, 13, 13, -10, 70, -25, -37, 9, -17, 24, -28, 71, 13, -29,
20, -15, -16, -27, -15, -20, 48, 15, 0, -12, -15, 36, -20, 53, -21, -6, -11, -10};
static int biases[Eval::NNUE::LayerStacks]{-947, -285, 279, -601, 850, 284, -188, -119,
826, 655, 244, -263, 1496, 142, 91, 3392};

auto myfunc127 = [](int m) {
return m == 0 ? std::pair<int, int>(-1, 1)
: m > 0 ? std::pair<int, int>(m / 2, std::min(3 * m / 2, 127))
: std::pair<int, int>(std::max(-128, 3 * m / 2), m / 2);
};

std::function<void()> update_hook;
static void update() { update_hook(); }
TUNE(SetRange(myfunc127), weights, update);
TUNE(biases, update);

int main(int argc, char* argv[]) {

std::cout << engine_info() << std::endl;
Expand All @@ -36,6 +78,40 @@ int main(int argc, char* argv[]) {

UCIEngine uci(argc, argv);

update_hook = [&] {
auto& evalFile = uci.engine.network->evalFile;
std::string evalfilePath = uci.engine.options["EvalFile"];
if (evalfilePath.empty())
evalfilePath = evalFile.defaultName;
if (evalFile.current != evalfilePath)
return;
for (uint8_t i = 0; i < Eval::NNUE::LayerStacks; ++i)
{
auto& net = uci.engine.network->network[i];
for (int j = 0; j < Eval::NNUE::NetworkArchitecture::FC_1_OUTPUTS; ++j)
net.fc_2.weights[j] =
weights[i * Eval::NNUE::NetworkArchitecture::FC_1_OUTPUTS + j];
net.fc_2.biases[0] = biases[i];
}
};

// std::cout << "{";
// for (uint8_t i = 0; i < Eval::NNUE::LayerStacks; ++i)
// {
// auto& net = uci.engine.network->network[i];
// for (uint8_t j = 0; j < Eval::NNUE::NetworkArchitecture::FC_1_OUTPUTS; ++j)
// std::cout << int(net.fc_2.weights[j]) << ",";
// }
// std::cout << "}" << std::endl;
// std::cout << "{";
// for (uint8_t i = 0; i < Eval::NNUE::LayerStacks; ++i)
// {
// auto& net = uci.engine.network->network[i];
// std::cout << int(net.fc_2.biases[0]) << ",";
// }
// std::cout << "}";
// return 0;

Tune::init(uci.engine_options());

uci.loop();
Expand Down
1 change: 0 additions & 1 deletion src/nnue/layers/affine_transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ class AffineTransform {
#endif
}

private:
using BiasType = OutputType;
using WeightType = std::int8_t;

Expand Down
1 change: 0 additions & 1 deletion src/nnue/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class Network {
void verify(std::string evalfilePath, const std::function<void(std::string_view)>&) const;
NnueEvalTrace trace_evaluate(const Position& pos, AccumulatorCaches::Cache* cache) const;

private:
void load_user_net(const std::string&, const std::string&);

void initialize();
Expand Down
1 change: 0 additions & 1 deletion src/uci.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class UCIEngine {

auto& engine_options() { return engine.get_options(); }

private:
Engine engine;
CommandLine cli;

Expand Down

0 comments on commit 8a8aa74

Please sign in to comment.