created basic nnue files and structures
This commit is contained in:
1 parent
a9e31a9917
commit
82c969a2ad
7 files changed
+127
No files matched your search
@@ -0,0 +1,29 @@
|
||||
#include "accumulator.h"
|
||||
#include "types.h"
|
||||
|
||||
namespace ACC {
|
||||
|
||||
int CalculateIndex(bool perspective, int square, int pieceType, bool side) {
|
||||
if (perspective == Black)
|
||||
{
|
||||
side = 1 - side; // Flip side
|
||||
square = square ^ 0b111000; // Mirror square
|
||||
}
|
||||
|
||||
return side * 64 * 6 + pieceType * 64 + square;
|
||||
}
|
||||
|
||||
void Accumulator::Add(const NNUE::Network* net, int index) {
|
||||
for (int i = 0; i < NNUE::HL_SIZE; i ++) {
|
||||
values[i] += net->accumulator_weights[index][i];
|
||||
}
|
||||
}
|
||||
|
||||
void Accumulator::Sub(const NNUE::Network* net, int index) {
|
||||
for (int i = 0; i < NNUE::HL_SIZE; i ++) {
|
||||
values[i] -= net->accumulator_weights[index][i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include "nnue.h"
|
||||
|
||||
namespace ACC {
|
||||
|
||||
struct Accumulator {
|
||||
int16_t values[NNUE::HL_SIZE];
|
||||
|
||||
void Add(const NNUE::Network* net, int index);
|
||||
void Sub(const NNUE::Network* net, int index);
|
||||
};
|
||||
|
||||
struct AccumulatorPair {
|
||||
Accumulator white;
|
||||
Accumulator black;
|
||||
};
|
||||
|
||||
int CalculateIndex(bool perspective, int square, int pieceType, bool side);
|
||||
|
||||
}
|
||||
@@ -16,6 +16,8 @@ void Board::Reset() {
|
||||
sideToMove = White;
|
||||
occupied = 0ULL;
|
||||
|
||||
accPair = ACC::AccumulatorPair();
|
||||
|
||||
pieces = std::array<Bitboard, 6>();
|
||||
colors = std::array<Bitboard, 2>();
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "bitboard.h"
|
||||
#include "types.h"
|
||||
#include "move.h"
|
||||
#include "accumulator.h"
|
||||
|
||||
constexpr int nullPieceType = 100;
|
||||
constexpr int noEPTarget = -1;
|
||||
@@ -23,6 +24,8 @@ public:
|
||||
|
||||
Bitboard occupied;
|
||||
|
||||
ACC::AccumulatorPair accPair;
|
||||
|
||||
std::array<Bitboard, 6> pieceThreats;
|
||||
std::array<Bitboard, 2> colorThreats;
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <immintrin.h>
|
||||
#include "nnue.h"
|
||||
#include "accumulator.h"
|
||||
#include "board.h"
|
||||
#include "types.h"
|
||||
|
||||
namespace NNUE {
|
||||
|
||||
static int32_t Forward(const Board& board,
|
||||
const Network* net) {
|
||||
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;
|
||||
|
||||
int32_t eval = 0;
|
||||
|
||||
const __m256i vec_zero = _mm256_setzero_si256();
|
||||
const __m256i vec_qa = _mm256_set1_epi16(QA);
|
||||
__m256i sum = vec_zero;
|
||||
|
||||
for (int i = 0; i < HL_SIZE / 16; i++) {
|
||||
const __m256i us = _mm256_load_si256(reinterpret_cast<const __m256i*>(&stmAcc.values[16 * i])); // Load from accumulator
|
||||
const __m256i them = _mm256_load_si256(reinterpret_cast<const __m256i*>(&nstmAcc.values[16 * i]));
|
||||
|
||||
const __m256i us_weights = _mm256_load_si256(reinterpret_cast<const __m256i*>(&net->output_weights[16 * i])); // Load from net
|
||||
const __m256i them_weights = _mm256_load_si256(reinterpret_cast<const __m256i*>(&net->output_weights[HL_SIZE + 16 * i]));
|
||||
|
||||
const __m256i us_clamped = _mm256_min_epi16(_mm256_max_epi16(us, vec_zero), vec_qa);
|
||||
const __m256i them_clamped = _mm256_min_epi16(_mm256_max_epi16(them, vec_zero), vec_qa);
|
||||
|
||||
const __m256i us_results = _mm256_madd_epi16(_mm256_mullo_epi16(us_weights, us_clamped), us_clamped);
|
||||
const __m256i them_results = _mm256_madd_epi16(_mm256_mullo_epi16(them_weights, them_clamped), them_clamped);
|
||||
|
||||
sum = _mm256_add_epi32(sum, us_results);
|
||||
sum = _mm256_add_epi32(sum, them_results);
|
||||
}
|
||||
|
||||
__m256i v1 = _mm256_hadd_epi32(sum, sum);
|
||||
__m256i v2 = _mm256_hadd_epi32(v1, v1);
|
||||
|
||||
eval = _mm256_extract_epi32(v2, 0) + _mm256_extract_epi32(v2, 4);
|
||||
|
||||
eval /= QA;
|
||||
|
||||
eval += net->output_bias;
|
||||
|
||||
return (eval * SCALE) / (QA * QB);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace NNUE {
|
||||
|
||||
constexpr int INPUT_SIZE = 768;
|
||||
constexpr int HL_SIZE = 64;
|
||||
|
||||
constexpr int SCALE = 400;
|
||||
constexpr int QA = 255;
|
||||
constexpr int QB = 64;
|
||||
|
||||
struct Network {
|
||||
int16_t accumulator_weights[INPUT_SIZE][HL_SIZE];
|
||||
int16_t accumulator_biases[HL_SIZE];
|
||||
int16_t output_weights[2 * HL_SIZE];
|
||||
int16_t output_bias;
|
||||
|
||||
void Load(const std::string& path);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <array>
|
||||
#include <string_view>
|
||||
|
||||
using U64 = uint64_t;
|
||||
|
||||
|
||||
Reference in new issue
Block a user