#include #include #include "nnue.h" #include "accumulator.h" #include "board.h" #include "types.h" #include "search.h" namespace NNUE { void Network::Load(const std::string& path) { std::ifstream file(path, std::ios::binary); if (!file.is_open()){ std::cerr << "Failed to open file " + path << std::endl; } // HL weights for (int 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++) { file.read(reinterpret_cast(&accumulator_biases[i]), sizeof(int16_t)); } // Output weights for (int i = 0; i < output_weights.size(); i++) { file.read(reinterpret_cast(&output_weights[i]), sizeof(int16_t)); } // Output bias file.read(reinterpret_cast(&output_bias), sizeof(int16_t)); } #if defined(__x86_64__) || defined(__amd64__) || (defined(_WIN64) && (defined(_M_X64) || defined(_M_AMD64))) #include #if defined(__AVX512F__) #pragma message("Using AVX512 NNUE inference") using nativeVector = __m512i; #define set1_epi16 _mm512_set1_epi16 #define load_epi16 _mm512_load_si512 #define min_epi16 _mm512_min_epi16 #define max_epi16 _mm512_max_epi16 #define madd_epi16 _mm512_madd_epi16 #define mullo_epi16 _mm512_mullo_epi16 #define add_epi32 _mm512_add_epi32 #define reduce_epi32 _mm512_reduce_add_epi32 #elif defined(__AVX2__) #pragma message("Using AVX2 NNUE inference") using nativeVector = __m256i; #define set1_epi16 _mm256_set1_epi16 #define load_epi16 _mm256_load_si256 #define min_epi16 _mm256_min_epi16 #define max_epi16 _mm256_max_epi16 #define madd_epi16 _mm256_madd_epi16 #define mullo_epi16 _mm256_mullo_epi16 #define add_epi32 _mm256_add_epi32 #define reduce_epi32 \ [](nativeVector vec) { \ __m128i xmm1 = _mm256_extracti128_si256(vec, 1); \ __m128i xmm0 = _mm256_castsi256_si128(vec); \ xmm0 = _mm_add_epi32(xmm0, xmm1); \ xmm1 = _mm_shuffle_epi32(xmm0, 238); \ xmm0 = _mm_add_epi32(xmm0, xmm1); \ xmm1 = _mm_shuffle_epi32(xmm0, 85); \ xmm0 = _mm_add_epi32(xmm0, xmm1); \ return _mm_cvtsi128_si32(xmm0); \ } #else #pragma message("Using SSE NNUE inference") // Assumes SSE support here using nativeVector = __m128i; #define set1_epi16 _mm_set1_epi16 #define load_epi16 _mm_load_si128 #define min_epi16 _mm_min_epi16 #define max_epi16 _mm_max_epi16 #define madd_epi16 _mm_madd_epi16 #define mullo_epi16 _mm_mullo_epi16 #define add_epi32 _mm_add_epi32 #define reduce_epi32 \ [](nativeVector vec) { \ __m128i xmm1 = _mm_shuffle_epi32(vec, 238); \ vec = _mm_add_epi32(vec, xmm1); \ xmm1 = _mm_shuffle_epi32(vec, 85); \ vec = _mm_add_epi32(vec, xmm1); \ return _mm_cvtsi128_si32(vec); \ } #endif static int32_t VectorizedSCReLU(const Board& board, const Network& net) { 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"); const ACC::Accumulator& stmAcc = board.sideToMove == White ? board.accPair.white : board.accPair.black; const ACC::Accumulator& nstmAcc = !board.sideToMove == White ? board.accPair.white : board.accPair.black; const nativeVector VEC_QA = set1_epi16(QA); const nativeVector VEC_ZERO = set1_epi16(0); nativeVector accumulator{}; 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])); // Clamp values const nativeVector stmClamped = min_epi16(VEC_QA, max_epi16(stmAccumValues, VEC_ZERO)); 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])); // SCReLU activation const nativeVector stmActivated = madd_epi16(stmClamped, mullo_epi16(stmClamped, stmWeights)); const nativeVector nstmActivated = madd_epi16(nstmClamped, mullo_epi16(nstmClamped, nstmWeights)); accumulator = add_epi32(accumulator, stmActivated); accumulator = add_epi32(accumulator, nstmActivated); } return reduce_epi32(accumulator); } #endif int Forward(const Board& board, const Network& net) { bool stm = board.sideToMove; 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 /= QA; eval += net.output_bias; return (eval * SCALE) / (QA * QB); } int16_t Network::Evaluate(const Board& board) { return std::clamp(Forward(board, *this), (-SEARCH::MATE_SCORE + SEARCH::MAX_PLY), (SEARCH::MATE_SCORE - SEARCH::MAX_PLY)); } }