From 20ec168ba25e797adfeb5131eccd45690cb5755b Mon Sep 17 00:00:00 2001 From: SzilBalazs Date: Wed, 19 Jul 2023 14:55:04 +0200 Subject: [PATCH] Cleanup Bench: 2525875 --- src/core/bitboard.h | 2 +- src/core/board.h | 2 +- src/main.cpp | 2 +- src/network/adam.h | 3 +-- src/network/data_parser.h | 12 ++++++------ src/network/layers/accumulator.h | 9 ++++----- src/network/layers/dense_layer.h | 6 ++---- src/network/network.h | 3 +-- src/network/nnue.h | 11 +++++++---- src/network/train.h | 12 ++++++------ src/selfplay/selfplay.h | 6 +++--- src/uci/uci.h | 12 ++++++------ src/utils/split.h | 6 ++++-- 13 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/core/bitboard.h b/src/core/bitboard.h index 12001ab..e7e2e82 100644 --- a/src/core/bitboard.h +++ b/src/core/bitboard.h @@ -19,9 +19,9 @@ #include "constants.h" +#include #include #include -#include #ifdef _MSC_VER #include diff --git a/src/core/board.h b/src/core/board.h index c9db8a1..d07aea9 100644 --- a/src/core/board.h +++ b/src/core/board.h @@ -17,12 +17,12 @@ #pragma once +#include "../network/nnue.h" #include "../utils/utilities.h" #include "attacks.h" #include "bitboard.h" #include "board_state.h" #include "move.h" -#include "../network/nnue.h" #include #include diff --git a/src/main.cpp b/src/main.cpp index 36887f5..3808495 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -61,7 +61,7 @@ int main(int argc, char *argv[]) { test::run(); } else if (mode == "bench") { run_bench(); - } else { + } else { uci::UCI protocol; protocol.start(); } diff --git a/src/network/adam.h b/src/network/adam.h index 7e72a16..f78aca0 100644 --- a/src/network/adam.h +++ b/src/network/adam.h @@ -23,7 +23,6 @@ namespace nn { class Adam { public: - Adam(float learning_rate) : LR(learning_rate), m_gradient(), v_gradient() {} void update(const std::vector &gradients, Network &network) { @@ -67,4 +66,4 @@ namespace nn { } }; -} \ No newline at end of file +} // namespace nn \ No newline at end of file diff --git a/src/network/data_parser.h b/src/network/data_parser.h index 86ecd53..b1f9e6b 100644 --- a/src/network/data_parser.h +++ b/src/network/data_parser.h @@ -38,7 +38,6 @@ namespace nn { class DataParser { public: - DataParser(const std::string &path) { file.open(path, std::ios::in); } @@ -58,7 +57,6 @@ namespace nn { } private: - std::ifstream file; TrainingEntry parse_entry(const std::string &entry) { @@ -85,7 +83,7 @@ namespace nn { for (; idx < fen.size() && fen[idx] != ' '; idx++) { char c = fen[idx]; if ('1' <= c && c <= '8') { - sq += c-'0'; + sq += c - '0'; } else if (c == '/') { sq -= 16; } else { @@ -98,8 +96,10 @@ namespace nn { Color stm = COLOR_EMPTY; idx++; - if (fen[idx] == 'w') stm = WHITE; - else if (fen[idx] == 'b') stm = BLACK; + if (fen[idx] == 'w') + stm = WHITE; + else if (fen[idx] == 'b') + stm = BLACK; else { throw 1; } @@ -111,4 +111,4 @@ namespace nn { return res; } }; -} \ No newline at end of file +} // namespace nn \ No newline at end of file diff --git a/src/network/layers/accumulator.h b/src/network/layers/accumulator.h index a646034..558e28f 100644 --- a/src/network/layers/accumulator.h +++ b/src/network/layers/accumulator.h @@ -19,10 +19,11 @@ #include "../activations/none.h" +#include +#include +#include #include #include -#include -#include namespace nn::layers { @@ -32,7 +33,6 @@ namespace nn::layers { static_assert(std::is_invocable_r_v, "Invalid ACTIVATION::forward"); public: - void load_from_file(std::ifstream &file) { file.read(reinterpret_cast(biases.data()), sizeof(biases)); file.read(reinterpret_cast(weights.data()), sizeof(weights)); @@ -73,7 +73,6 @@ namespace nn::layers { } private: - std::array accumulator; std::array biases; std::array weights; @@ -82,4 +81,4 @@ namespace nn::layers { std::copy(biases.begin(), biases.end(), accumulator.begin()); } }; -} \ No newline at end of file +} // namespace nn::layers \ No newline at end of file diff --git a/src/network/layers/dense_layer.h b/src/network/layers/dense_layer.h index a0f2a81..f248099 100644 --- a/src/network/layers/dense_layer.h +++ b/src/network/layers/dense_layer.h @@ -18,11 +18,11 @@ #pragma once #include +#include #include +#include #include #include -#include -#include #include "../activations/none.h" @@ -51,7 +51,6 @@ namespace nn::layers { template> class DenseLayer { private: - static_assert(std::is_invocable_r_v, "Invalid ACTIVATION::forward"); static_assert(std::is_invocable_r_v, "Invalid ACTIVATION::backward"); @@ -62,7 +61,6 @@ namespace nn::layers { } public: - std::array biases; std::array weights; diff --git a/src/network/network.h b/src/network/network.h index e57cbb0..e2ee24f 100644 --- a/src/network/network.h +++ b/src/network/network.h @@ -17,8 +17,8 @@ #pragma once -#include "activations/relu.h" #include "activations/crelu.h" +#include "activations/relu.h" #include "activations/sigmoid.h" #include "layers/dense_layer.h" @@ -119,6 +119,5 @@ namespace nn { file.close(); } - }; } // namespace nn \ No newline at end of file diff --git a/src/network/nnue.h b/src/network/nnue.h index 2f876d6..810c2b9 100644 --- a/src/network/nnue.h +++ b/src/network/nnue.h @@ -17,10 +17,14 @@ #pragma once +#include "../external/incbin/incbin.h" +#include "../core/constants.h" +#include "../utils/logger.h" #include "activations/crelu.h" #include "layers/accumulator.h" #include "layers/dense_layer.h" -#include "../external/incbin/incbin.h" + +#include namespace nn { @@ -29,11 +33,10 @@ namespace nn { class NNUE { public: - static constexpr int QSCALE = 64; NNUE() { - const unsigned char* data = gDefaultNetworkData; + const unsigned char *data = gDefaultNetworkData; int magic; std::memcpy(&magic, data, sizeof(int)); @@ -110,4 +113,4 @@ namespace nn { layers::DenseLayer> l1; }; -} +} // namespace nn diff --git a/src/network/train.h b/src/network/train.h index b8b2165..85a798c 100644 --- a/src/network/train.h +++ b/src/network/train.h @@ -18,8 +18,8 @@ #pragma once #include "../utils/logger.h" -#include "data_parser.h" #include "adam.h" +#include "data_parser.h" #include #include @@ -32,7 +32,6 @@ namespace nn { class Trainer { public: - Trainer(const std::string &training_data, const std::string &validation_data, const std::optional &network_path, float learning_rate, int epochs, int batch_size, int thread_count) : adam(learning_rate), training_parser(training_data), validation_parser(validation_data), batch_size(batch_size), thread_count(thread_count) { @@ -71,7 +70,8 @@ namespace nn { while (!is_new_epoch) { iter++; - epoch_iter++; checkpoint_iter++; + epoch_iter++; + checkpoint_iter++; delete[] entries; entries = entries_next; @@ -203,8 +203,8 @@ namespace nn { network.forward(entry.features, l0_output, l1_output, entry.phase); float prediction = l1_output[0]; - float error = (1.0f - EVAL_INFLUENCE) * (prediction - entry.wdl) * (prediction - entry.wdl) - + EVAL_INFLUENCE * (prediction - entry.eval) * (prediction - entry.eval); + float error = (1.0f - EVAL_INFLUENCE) * (prediction - entry.wdl) * (prediction - entry.wdl) + + EVAL_INFLUENCE * (prediction - entry.eval) * (prediction - entry.eval); errors[id] += error; accuracy[id] += ((entry.wdl - 0.5f) * (prediction - 0.5f) > 0.0f) || std::abs(entry.wdl - prediction) < 0.05f; @@ -232,4 +232,4 @@ namespace nn { logger.print("Found", entry_count, "positions"); } }; -} \ No newline at end of file +} // namespace nn \ No newline at end of file diff --git a/src/selfplay/selfplay.h b/src/selfplay/selfplay.h index 6571353..969b55b 100644 --- a/src/selfplay/selfplay.h +++ b/src/selfplay/selfplay.h @@ -20,9 +20,9 @@ #include "engine.h" #include -#include #include #include +#include #include namespace selfplay { @@ -126,7 +126,7 @@ namespace selfplay { logger.print("Combining files..."); std::ofstream file(output_file, std::ios::app | std::ios::out); - for (const auto& entry : std::filesystem::directory_iterator(path)) { + for (const auto &entry : std::filesystem::directory_iterator(path)) { std::ifstream in(entry.path()); std::string tmp; while (std::getline(in, tmp)) { @@ -165,7 +165,7 @@ namespace selfplay { std::random_device rd; std::mt19937 g(rd()); - std::uniform_int_distribution dist(0, dropout-1); + std::uniform_int_distribution dist(0, dropout - 1); while (std::getline(book, tmp)) { if (dist(g) == 0) diff --git a/src/uci/uci.h b/src/uci/uci.h index 9aae89e..cccb9b4 100644 --- a/src/uci/uci.h +++ b/src/uci/uci.h @@ -18,12 +18,12 @@ #pragma once #include "../core/board.h" +#include "../network/train.h" #include "../search/search_manager.h" #include "../selfplay/selfplay.h" -#include "../network/train.h" #include "../tests/perft.h" -#include "../utils/split.h" #include "../utils/logger.h" +#include "../utils/split.h" #include "../utils/utilities.h" #include "command.h" #include "option.h" @@ -87,7 +87,7 @@ namespace uci { commands.emplace_back("display", [&](context tokens) { board.display(); }); - commands.emplace_back("eval", [&](context tokens){ + commands.emplace_back("eval", [&](context tokens) { nn::NNUE network{}; network.refresh(board.to_features()); logger.print("Eval:", network.evaluate(board.get_stm())); @@ -102,20 +102,20 @@ namespace uci { std::optional dropout = find_element(tokens, "dropout"); selfplay::start_generation(limits, book.value_or("book.epd"), output.value_or("data.plain"), thread_count.value_or(1), dropout.value_or(1)); }); - commands.emplace_back("split", [&](context tokens){ + commands.emplace_back("split", [&](context tokens) { std::optional input_data = find_element(tokens, "input"); std::optional output_data1 = find_element(tokens, "output1"); std::optional output_data2 = find_element(tokens, "output2"); std::optional rate = find_element(tokens, "rate"); split_data(input_data.value_or("data.plain"), output_data1.value_or("train.plain"), output_data2.value_or("validation.plain"), rate.value_or(10)); }); - commands.emplace_back("quantize", [&](context tokens){ + commands.emplace_back("quantize", [&](context tokens) { std::optional input = find_element(tokens, "input"); std::optional output = find_element(tokens, "output"); nn::Network network_file(input.value_or("input.bin")); network_file.quantize(output.value_or("output.bin")); }); - commands.emplace_back("train", [&](context tokens){ + commands.emplace_back("train", [&](context tokens) { std::optional network_path = find_element(tokens, "network"); std::optional training_data = find_element(tokens, "training_data"); std::optional validation_data = find_element(tokens, "validation_data"); diff --git a/src/utils/split.h b/src/utils/split.h index 98e6055..6a975f4 100644 --- a/src/utils/split.h +++ b/src/utils/split.h @@ -30,8 +30,10 @@ inline void split_data(const std::string &input, const std::string &output1, con std::string line; while (std::getline(in, line)) { - if (dist(g) == 0) out2 << line << "\n"; - else out1 << line << "\n"; + if (dist(g) == 0) + out2 << line << "\n"; + else + out1 << line << "\n"; } in.close();