diff --git a/source/accumulator.cpp b/source/accumulator.cpp index ffb7379..0a8a36e 100644 --- a/source/accumulator.cpp +++ b/source/accumulator.cpp @@ -23,11 +23,11 @@ void AccumulatorPair::addSub(bool stm, int add, int addPT, int sub, int subPT) { int subB = CalculateIndex(Black, stm, subPT, sub); for (int i = 0; i < NNUE::HL_SIZE; i++) { - white.values[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i]; - black.values[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i]; + white[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i]; + black[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i]; - white.values[i] -= NNUE::net.accumulator_weights[subW * NNUE::HL_SIZE + i]; - black.values[i] -= NNUE::net.accumulator_weights[subB * NNUE::HL_SIZE + i]; + white[i] -= NNUE::net.accumulator_weights[subW * NNUE::HL_SIZE + i]; + black[i] -= NNUE::net.accumulator_weights[subB * NNUE::HL_SIZE + i]; } } @@ -43,14 +43,14 @@ void AccumulatorPair::addSubSub(bool stm, int add, int addPT, int sub1, int subP int subB2 = CalculateIndex(Black, !stm, subPT2, sub2); for (int i = 0; i < NNUE::HL_SIZE; i++) { - white.values[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i]; - black.values[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i]; + white[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i]; + black[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i]; - white.values[i] -= NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i]; - black.values[i] -= NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i]; + white[i] -= NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i]; + black[i] -= NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i]; - white.values[i] -= NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i]; - black.values[i] -= NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i]; + white[i] -= NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i]; + black[i] -= NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i]; } } @@ -69,17 +69,17 @@ void AccumulatorPair::addAddSubSub(bool stm, int add1, int addPT1, int add2, int int subB2 = CalculateIndex(Black, stm, subPT2, sub2); for (int i = 0; i < NNUE::HL_SIZE; i++) { - white.values[i] += NNUE::net.accumulator_weights[addW1 * NNUE::HL_SIZE + i]; - black.values[i] += NNUE::net.accumulator_weights[addB1 * NNUE::HL_SIZE + i]; + white[i] += NNUE::net.accumulator_weights[addW1 * NNUE::HL_SIZE + i]; + black[i] += NNUE::net.accumulator_weights[addB1 * NNUE::HL_SIZE + i]; - white.values[i] += NNUE::net.accumulator_weights[addW2 * NNUE::HL_SIZE + i]; - black.values[i] += NNUE::net.accumulator_weights[addB2 * NNUE::HL_SIZE + i]; + white[i] += NNUE::net.accumulator_weights[addW2 * NNUE::HL_SIZE + i]; + black[i] += NNUE::net.accumulator_weights[addB2 * NNUE::HL_SIZE + i]; - white.values[i] -= NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i]; - black.values[i] -= NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i]; + white[i] -= NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i]; + black[i] -= NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i]; - white.values[i] -= NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i]; - black.values[i] -= NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i]; + white[i] -= NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i]; + black[i] -= NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i]; } } diff --git a/source/accumulator.h b/source/accumulator.h index a3b10d5..6d20563 100644 --- a/source/accumulator.h +++ b/source/accumulator.h @@ -3,9 +3,7 @@ namespace ACC { -struct Accumulator { - alignas(32) std::array values; -}; +using Accumulator = std::array; struct AccumulatorPair { alignas(32) Accumulator white; diff --git a/source/board.cpp b/source/board.cpp index 2358139..59be5b7 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -231,8 +231,8 @@ void Board::PrintNNUE() { std::cout << "First 16 accumulator values (pre-activation)[white] "; for (int i = 0; i < 16; i++) { - if (accPair.white.values[i] != 0) { - std::cout << accPair.white.values[i] << ' '; + if (accPair.white[i] != 0) { + std::cout << accPair.white[i] << ' '; } } @@ -491,8 +491,8 @@ void Board::ResetAccPair() { Bitboard whitePieces = colors[White]; Bitboard blackPieces = colors[Black]; - accPair.white.values = NNUE::net.accumulator_biases; - accPair.black.values = NNUE::net.accumulator_biases; + accPair.white = NNUE::net.accumulator_biases; + accPair.black = NNUE::net.accumulator_biases; while (whitePieces) { int square = whitePieces.getLS1BIndex(); @@ -501,8 +501,8 @@ void Board::ResetAccPair() { int bInput = ACC::CalculateIndex(Black, White, GetPieceType(square), square); for (int i = 0; i < NNUE::HL_SIZE; i++) { - accPair.white.values[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i]; - accPair.black.values[i] += NNUE::net.accumulator_weights[bInput * NNUE::HL_SIZE + i]; + accPair.white[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i]; + accPair.black[i] += NNUE::net.accumulator_weights[bInput * NNUE::HL_SIZE + i]; } whitePieces.PopBit(square); @@ -515,10 +515,10 @@ void Board::ResetAccPair() { int bInput = ACC::CalculateIndex(Black, Black, GetPieceType(square), square); for (int i = 0; i < NNUE::HL_SIZE; i++) { - accPair.white.values[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i]; - accPair.black.values[i] += NNUE::net.accumulator_weights[bInput * NNUE::HL_SIZE + i]; + accPair.white[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i]; + accPair.black[i] += NNUE::net.accumulator_weights[bInput * NNUE::HL_SIZE + i]; } blackPieces.PopBit(square); } -} \ No newline at end of file +} diff --git a/source/nnue.cpp b/source/nnue.cpp index 7fb5ed6..4b0b5f9 100644 --- a/source/nnue.cpp +++ b/source/nnue.cpp @@ -16,17 +16,17 @@ void Network::Load(const std::string& path) { } // HL weights - for (int i = 0; i < accumulator_weights.size(); i++) { + for (size_t i = 0; i < accumulator_weights.size(); i++) { file.read(reinterpret_cast(&accumulator_weights[i]), sizeof(int16_t)); } // HL biases - for (int i = 0; i < accumulator_biases.size(); i++) { + for (size_t i = 0; i < accumulator_biases.size(); i++) { file.read(reinterpret_cast(&accumulator_biases[i]), sizeof(int16_t)); } // Output weights - for (int i = 0; i < output_weights.size(); i++) { + for (size_t i = 0; i < output_weights.size(); i++) { file.read(reinterpret_cast(&output_weights[i]), sizeof(int16_t)); } @@ -103,8 +103,8 @@ static int32_t VectorizedSCReLU(const Board& board, const Network& net) { for (int i = 0; i < HL_SIZE; i += VECTOR_SIZE) { // Load accumulators - const nativeVector stmAccumValues = load_epi16(reinterpret_cast(&stmAcc.values[i])); - const nativeVector nstmAccumValues = load_epi16(reinterpret_cast(&nstmAcc.values[i])); + const nativeVector stmAccumValues = load_epi16(reinterpret_cast(&stmAcc[i])); + const nativeVector nstmAccumValues = load_epi16(reinterpret_cast(&nstmAcc[i])); // Clamp values const nativeVector stmClamped = min_epi16(VEC_QA, max_epi16(stmAccumValues, VEC_ZERO)); diff --git a/source/nnue.h b/source/nnue.h index ccf59e1..3e977af 100644 --- a/source/nnue.h +++ b/source/nnue.h @@ -7,19 +7,25 @@ class Board; +#ifdef __AVX512F__ +constexpr usize ALIGNMENT = 64; +#else +constexpr size_t ALIGNMENT = 32; +#endif + namespace NNUE { -constexpr int INPUT_SIZE = 768; -constexpr int HL_SIZE = 128; +constexpr size_t INPUT_SIZE = 768; +constexpr size_t HL_SIZE = 128; -constexpr int SCALE = 400; -constexpr int QA = 255; -constexpr int QB = 64; +constexpr int16_t SCALE = 400; +constexpr int16_t QA = 255; +constexpr int16_t QB = 64; struct Network { - alignas(32) std::array accumulator_weights; - alignas(32) std::array accumulator_biases; - alignas(32) std::array output_weights; + alignas(ALIGNMENT) std::array accumulator_weights; + alignas(ALIGNMENT) std::array accumulator_biases; + alignas(ALIGNMENT) std::array output_weights; int16_t output_bias; void Load(const std::string& path);