diff --git a/nnue.bin b/nnue.bin index b9f16d4..b079e86 100644 Binary files a/nnue.bin and b/nnue.bin differ diff --git a/source/board.cpp b/source/board.cpp index c876384..06fda23 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -218,26 +218,6 @@ void Board::PrintBoard() { } void Board::PrintNNUE() { - std::cout << "First 16 HL biases: "; - - for (int i = 0; i < 16; i++) { - std::cout << NNUE::net.accumulator_biases[i] << ' '; - } - - std::cout << std::endl; - - std::cout << "Output neuron bias: " << NNUE::net.output_bias << std::endl; - - std::cout << "First 16 accumulator values (pre-activation)[white] "; - - for (int i = 0; i < 16; i++) { - if (accPair.white[i] != 0) { - std::cout << accPair.white[i] << ' '; - } - } - - std::cout << std::endl; - std::cout << "Final eval: " << NNUE::net.Evaluate(*this) << std::endl; } diff --git a/source/nnue.cpp b/source/nnue.cpp index 4b0b5f9..2d62538 100644 --- a/source/nnue.cpp +++ b/source/nnue.cpp @@ -27,11 +27,15 @@ void Network::Load(const std::string& path) { // Output weights for (size_t i = 0; i < output_weights.size(); i++) { - file.read(reinterpret_cast(&output_weights[i]), sizeof(int16_t)); + for (size_t j = 0; j < output_weights[i].size(); j++) { + file.read(reinterpret_cast(&output_weights[i][j]), sizeof(int16_t)); + } } - // Output bias - file.read(reinterpret_cast(&output_bias), sizeof(int16_t)); + // Output biases + for (size_t i = 0; i < output_bias.size(); i++) { + file.read(reinterpret_cast(&output_bias[i]), sizeof(int16_t)); + } } #if defined(__x86_64__) || defined(__amd64__) || (defined(_WIN64) && (defined(_M_X64) || defined(_M_AMD64))) @@ -89,7 +93,7 @@ using nativeVector = __m128i; } #endif -static int32_t VectorizedSCReLU(const Board& board, const Network& net) { +static int32_t VectorizedSCReLU(const Board& board, const Network& net, size_t outputBucket) { const size_t VECTOR_SIZE = sizeof(nativeVector) / sizeof(int16_t); static_assert(HL_SIZE % VECTOR_SIZE == 0, "HL size must be divisible by the native register size of your CPU for vectorization to work"); @@ -111,8 +115,8 @@ static int32_t VectorizedSCReLU(const Board& board, const Network& net) { const nativeVector nstmClamped = min_epi16(VEC_QA, max_epi16(nstmAccumValues, VEC_ZERO)); // Load weights - const nativeVector stmWeights = load_epi16(reinterpret_cast(&net.output_weights[i])); - const nativeVector nstmWeights = load_epi16(reinterpret_cast(&net.output_weights[i + HL_SIZE])); + const nativeVector stmWeights = load_epi16(reinterpret_cast(&net.output_weights[outputBucket][i])); + const nativeVector nstmWeights = load_epi16(reinterpret_cast(&net.output_weights[outputBucket][i + HL_SIZE])); // SCReLU activation const nativeVector stmActivated = madd_epi16(stmClamped, mullo_epi16(stmClamped, stmWeights)); @@ -129,16 +133,19 @@ static int32_t VectorizedSCReLU(const Board& board, const Network& net) { int Forward(const Board& board, const Network& net) { bool stm = board.sideToMove; + const size_t divisor = 32 / OUTPUT_BUCKETS; + const size_t outputBucket = (board.occupied.PopCount() - 2) / divisor; + const ACC::Accumulator& stmACC = stm ? board.accPair.white : board.accPair.black; const ACC::Accumulator& nstmACC = !stm ? board.accPair.white : board.accPair.black; int64_t eval = 0; - eval = VectorizedSCReLU(board, net); + eval = VectorizedSCReLU(board, net, outputBucket); eval /= QA; - eval += net.output_bias; + eval += net.output_bias[outputBucket]; return (eval * SCALE) / (QA * QB); } diff --git a/source/nnue.h b/source/nnue.h index a91f31f..ff5dcc2 100644 --- a/source/nnue.h +++ b/source/nnue.h @@ -17,6 +17,7 @@ namespace NNUE { constexpr size_t INPUT_SIZE = 768; constexpr size_t HL_SIZE = 256; +constexpr size_t OUTPUT_BUCKETS = 8; constexpr int16_t SCALE = 400; constexpr int16_t QA = 255; @@ -25,8 +26,8 @@ constexpr int16_t QB = 64; struct Network { alignas(ALIGNMENT) std::array accumulator_weights; alignas(ALIGNMENT) std::array accumulator_biases; - alignas(ALIGNMENT) std::array output_weights; - int16_t output_bias; + alignas(ALIGNMENT) std::array, OUTPUT_BUCKETS> output_weights; + std::array output_bias; void Load(const std::string& path);