Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
Bench: 2525875
  • Loading branch information
SzilBalazs committed Jul 19, 2023
1 parent ead3982 commit 20ec168
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/core/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

#include "constants.h"

#include <bit>
#include <cassert>
#include <immintrin.h>
#include <bit>

#ifdef _MSC_VER
#include <intrin.h>
Expand Down
2 changes: 1 addition & 1 deletion src/core/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <algorithm>
#include <sstream>
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
3 changes: 1 addition & 2 deletions src/network/adam.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Gradient> &gradients, Network &network) {
Expand Down Expand Up @@ -67,4 +66,4 @@ namespace nn {
}
};

}
} // namespace nn
12 changes: 6 additions & 6 deletions src/network/data_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ namespace nn {

class DataParser {
public:

DataParser(const std::string &path) {
file.open(path, std::ios::in);
}
Expand All @@ -58,7 +57,6 @@ namespace nn {
}

private:

std::ifstream file;

TrainingEntry parse_entry(const std::string &entry) {
Expand All @@ -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 {
Expand All @@ -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;
}
Expand All @@ -111,4 +111,4 @@ namespace nn {
return res;
}
};
}
} // namespace nn
9 changes: 4 additions & 5 deletions src/network/layers/accumulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

#include "../activations/none.h"

#include <array>
#include <cstring>
#include <fstream>
#include <type_traits>
#include <vector>
#include <cstring>
#include <array>

namespace nn::layers {

Expand All @@ -32,7 +33,6 @@ namespace nn::layers {
static_assert(std::is_invocable_r_v<T, decltype(ACTIVATION::forward), T>, "Invalid ACTIVATION::forward");

public:

void load_from_file(std::ifstream &file) {
file.read(reinterpret_cast<char *>(biases.data()), sizeof(biases));
file.read(reinterpret_cast<char *>(weights.data()), sizeof(weights));
Expand Down Expand Up @@ -73,7 +73,6 @@ namespace nn::layers {
}

private:

std::array<T, OUT> accumulator;
std::array<T, OUT> biases;
std::array<T, IN * OUT> weights;
Expand All @@ -82,4 +81,4 @@ namespace nn::layers {
std::copy(biases.begin(), biases.end(), accumulator.begin());
}
};
}
} // namespace nn::layers
6 changes: 2 additions & 4 deletions src/network/layers/dense_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
#pragma once

#include <array>
#include <cstring>
#include <functional>
#include <random>
#include <thread>
#include <vector>
#include <random>
#include <cstring>

#include "../activations/none.h"

Expand Down Expand Up @@ -51,7 +51,6 @@ namespace nn::layers {
template<unsigned int IN, unsigned int OUT, typename T, typename T2, typename ACTIVATION = activations::none<T>>
class DenseLayer {
private:

static_assert(std::is_invocable_r_v<T2, decltype(ACTIVATION::forward), T2>, "Invalid ACTIVATION::forward");
static_assert(std::is_invocable_r_v<T2, decltype(ACTIVATION::backward), T2>, "Invalid ACTIVATION::backward");

Expand All @@ -62,7 +61,6 @@ namespace nn::layers {
}

public:

std::array<T, OUT> biases;
std::array<T, IN * OUT> weights;

Expand Down
3 changes: 1 addition & 2 deletions src/network/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -119,6 +119,5 @@ namespace nn {

file.close();
}

};
} // namespace nn
11 changes: 7 additions & 4 deletions src/network/nnue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cassert>

namespace nn {

Expand All @@ -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));
Expand Down Expand Up @@ -110,4 +113,4 @@ namespace nn {
layers::DenseLayer<L1_SIZE, 1, int16_t, int32_t, activations::none<int16_t>> l1;
};

}
} // namespace nn
12 changes: 6 additions & 6 deletions src/network/train.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#pragma once

#include "../utils/logger.h"
#include "data_parser.h"
#include "adam.h"
#include "data_parser.h"

#include <filesystem>
#include <optional>
Expand All @@ -32,7 +32,6 @@ namespace nn {

class Trainer {
public:

Trainer(const std::string &training_data, const std::string &validation_data, const std::optional<std::string> &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) {

Expand Down Expand Up @@ -71,7 +70,8 @@ namespace nn {

while (!is_new_epoch) {
iter++;
epoch_iter++; checkpoint_iter++;
epoch_iter++;
checkpoint_iter++;

delete[] entries;
entries = entries_next;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -232,4 +232,4 @@ namespace nn {
logger.print("Found", entry_count, "positions");
}
};
}
} // namespace nn
6 changes: 3 additions & 3 deletions src/selfplay/selfplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
#include "engine.h"

#include <ctime>
#include <random>
#include <filesystem>
#include <iomanip>
#include <random>
#include <sstream>

namespace selfplay {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -165,7 +165,7 @@ namespace selfplay {

std::random_device rd;
std::mt19937 g(rd());
std::uniform_int_distribution<int> dist(0, dropout-1);
std::uniform_int_distribution<int> dist(0, dropout - 1);

while (std::getline(book, tmp)) {
if (dist(g) == 0)
Expand Down
12 changes: 6 additions & 6 deletions src/uci/uci.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()));
Expand All @@ -102,20 +102,20 @@ namespace uci {
std::optional<int> dropout = find_element<int>(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<std::string> input_data = find_element<std::string>(tokens, "input");
std::optional<std::string> output_data1 = find_element<std::string>(tokens, "output1");
std::optional<std::string> output_data2 = find_element<std::string>(tokens, "output2");
std::optional<int> rate = find_element<int>(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<std::string> input = find_element<std::string>(tokens, "input");
std::optional<std::string> output = find_element<std::string>(tokens, "output");
nn::Network network_file(input.value_or("input.bin"));
network_file.quantize<int16_t, nn::NNUE::QSCALE>(output.value_or("output.bin"));
});
commands.emplace_back("train", [&](context tokens){
commands.emplace_back("train", [&](context tokens) {
std::optional<std::string> network_path = find_element<std::string>(tokens, "network");
std::optional<std::string> training_data = find_element<std::string>(tokens, "training_data");
std::optional<std::string> validation_data = find_element<std::string>(tokens, "validation_data");
Expand Down
6 changes: 4 additions & 2 deletions src/utils/split.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 20ec168

Please sign in to comment.