42 std::shared_ptr<const EvalContextMoECacheSharedModel> sharedModel);
47 static std::shared_ptr<const EvalContextMoECacheSharedModel>
76 std::shared_ptr<const EvalContextMoECacheSharedModel> shared);
79 std::shared_ptr<const EvalContextMoECacheSharedModel>
sharedModel_{};
80 std::array<MoEDoubleAccumulator, 2>
models_{};
101 static uint32_t
read_u32(std::ifstream &in);
106 template <
typename FloatContainer>
107 static void read_floats(std::ifstream &in, FloatContainer &dst,
size_t n) {
108 if constexpr (
requires(FloatContainer &c,
size_t m) { c.resize(m); }) {
112 throw std::runtime_error(
"Fixed-size tensor shape mismatch while reading");
114 in.read(
reinterpret_cast<char *
>(dst.data()),
115 static_cast<std::streamsize
>(n *
sizeof(
float)));
117 throw std::runtime_error(
"Failed reading float block");
133 template <
typename SrcContainer,
typename DstContainer>
135 int rows,
int cols) {
136 const size_t n =
static_cast<size_t>(rows) * cols;
137 if constexpr (
requires(DstContainer &c,
size_t m) { c.resize(m); }) {
141 throw std::runtime_error(
"Fixed-size tensor shape mismatch while transposing");
143 for (
int r = 0; r < rows; ++r) {
144 for (
int c = 0; c < cols; ++c) {
145 dst[(size_t)c * rows + r] = src[(
size_t)r * cols + c];
Abstract interface for board evaluation algorithms.
Factorized Mixture of Experts (MoE) neural network architecture and inference components.
ExpertPoolMode
Defines how spatial features are pooled before entering the fully-connected expert networks.
Definition MoECacheModel.hpp:826
chess::Board Board
Alias for chess::Board.
Definition Types.h:14
Converts neural network WDL outputs to centipawns.
void setEvalNormalization(bool enable) override
Enables or disables dynamic evaluation normalization.
Definition EvalContextMoECache.h:58
float evaluate(const Board &board, int ply=0) override
Evaluates the board from the perspective of the side to move.
Definition EvalContextMoECache.cpp:286
bool enableEvalNormalization_
Definition EvalContextMoECache.h:93
WDLConverter::WDL evaluateWDL(const Board &board, int ply=0) override
Evaluates the board and returns Win/Draw/Loss probabilities.
Definition EvalContextMoECache.cpp:218
static uint32_t read_u32(std::ifstream &in)
Reads a 32-bit unsigned integer from the binary weights stream.
Definition EvalContextMoECache.cpp:22
std::array< MoEDoubleAccumulator, 2 > models_
Definition EvalContextMoECache.h:80
uint64_t getFullRebuilds() const override
Retrieves the number of full feature rebuilds performed (for profiling).
Definition EvalContextMoECache.h:64
static void transpose_copy(const SrcContainer &src, DstContainer &dst, int rows, int cols)
Copies and transposes a matrix tensor (used to align weights for faster cache hits during inference).
Definition EvalContextMoECache.h:134
int rebuildEveryNEvals_
Definition EvalContextMoECache.h:94
void init_from_shared_model(std::shared_ptr< const EvalContextMoECacheSharedModel > shared)
Initializes the thread-local context from the pre-loaded shared weights.
Definition EvalContextMoECache.cpp:190
static ExpertPoolMode pool_mode_from_code(int code)
Converts an integer ID stored in the weights file to an ExpertPoolMode enum.
Definition EvalContextMoECache.cpp:38
static void load_weights_into_target(const std::string &weightsPath, BenchConfig &cfg, SharedMoEWeights &weights)
Loads neural network weights from a binary file into a shared weights container.
Definition EvalContextMoECache.cpp:53
BenchConfig cfg_
Definition EvalContextMoECache.h:78
static std::shared_ptr< const EvalContextMoECacheSharedModel > loadSharedModel(const std::string &weightsPath)
Loads the shared model weights from disk.
Definition EvalContextMoECache.cpp:184
std::atomic< uint64_t > totalRebuilds_
Definition EvalContextMoECache.h:96
static void read_floats(std::ifstream &in, FloatContainer &dst, size_t n)
Reads a block of float values into a resizing container from the binary stream.
Definition EvalContextMoECache.h:107
std::array< FactorizedInput, 2 > prevInputByStm_
Definition EvalContextMoECache.h:86
static constexpr uint32_t kVersion
Definition EvalContextMoECache.h:70
EvalContextMoECache(const std::string &weightsPath)
Definition EvalContextMoECache.cpp:205
static constexpr uint32_t kMagicWeights
Definition EvalContextMoECache.h:69
std::array< bool, 2 > hasPrevByStm_
Definition EvalContextMoECache.h:87
std::shared_ptr< const EvalContextMoECacheSharedModel > sharedModel_
Definition EvalContextMoECache.h:79
static void read_floats_raw(std::ifstream &in, float *dst, size_t n)
Reads a raw block of floats into a contiguous C-style array.
Definition EvalContextMoECache.cpp:30
WDLConverter wdlConverter_
Definition EvalContextMoECache.h:89
void load_weights_into_model(const std::string &weightsPath)
Loads neural network weights directly into the thread-local model (legacy usage).
Definition EvalContextMoECache.cpp:179
FactorizedInput scratchInput_
Definition EvalContextMoECache.h:84
int evalScaleWeight_
Definition EvalContextMoECache.h:92
void setAggression(float aggression) override
Sets the contempt or aggression factor for the evaluator.
Definition EvalContextMoECache.h:53
void setIncrementalRebuildInterval(int interval) override
Sets the interval for forcing full feature rebuilds (to correct accumulation errors).
Definition EvalContextMoECache.h:61
int evalScaleBase_
Definition EvalContextMoECache.h:91
std::array< uint32_t, 2 > evalsSinceFullByStm_
Definition EvalContextMoECache.h:95
void setEvalScale(int base, int weight) override
Sets the scaling parameters for the evaluation score.
Definition EvalContextMoECache.h:54
Abstract interface for evaluators.
Definition IEvaluator.h:25
Handles conversion between Win-Draw-Loss probabilities and centipawn scores.
Definition WDLConverter.hpp:34
Definition MoECacheModel.hpp:860
Contains the shared neural network weights.
Definition EvalContextMoECache.h:27
BenchConfig cfg
Configuration of the MoE architecture.
Definition EvalContextMoECache.h:28
SharedMoEWeights weights
Thread-safe shared weights container.
Definition EvalContextMoECache.h:29
Contains the globally shared, read-only weights for the Factorized MoE network.
Definition MoECacheModel.hpp:986
Holds Win, Draw, and Loss probabilities.
Definition WDLConverter.hpp:40