io-chess
UCI chess engine
Loading...
Searching...
No Matches
EvalContextMoECache.h
Go to the documentation of this file.
1#pragma once
2
10
11#include "IEvaluator.h"
12#include "WDLConverter.hpp"
13#include "MoECacheModel.hpp"
14
15#include <algorithm>
16#include <array>
17#include <atomic>
18#include <fstream>
19#include <memory>
20#include <stdexcept>
21#include <string>
22
31
39public:
40 explicit EvalContextMoECache(const std::string &weightsPath);
41 explicit EvalContextMoECache(
42 std::shared_ptr<const EvalContextMoECacheSharedModel> sharedModel);
43
47 static std::shared_ptr<const EvalContextMoECacheSharedModel>
48 loadSharedModel(const std::string &weightsPath);
49
50 float evaluate(const Board &board, int ply = 0) override;
51 WDLConverter::WDL evaluateWDL(const Board &board, int ply = 0) override;
52
53 void setAggression(float aggression) override { wdlConverter_.aggression = aggression; }
54 void setEvalScale(int base, int weight) override {
55 evalScaleBase_ = base;
56 evalScaleWeight_ = weight;
57 }
58 void setEvalNormalization(bool enable) override {
60 }
61 void setIncrementalRebuildInterval(int interval) override {
62 rebuildEveryNEvals_ = std::max(0, interval);
63 }
64 uint64_t getFullRebuilds() const override {
65 return totalRebuilds_.load(std::memory_order_relaxed);
66 }
67
68private:
69 static constexpr uint32_t kMagicWeights = 0x32454F4D; // MOE2
70 static constexpr uint32_t kVersion = 1;
71
76 std::shared_ptr<const EvalContextMoECacheSharedModel> shared);
77
79 std::shared_ptr<const EvalContextMoECacheSharedModel> sharedModel_{};
80 std::array<MoEDoubleAccumulator, 2> models_{};
81
82 // Large feature tensor scratch reused across calls to avoid deep-recursion
83 // stack growth in alpha-beta.
85
86 std::array<FactorizedInput, 2> prevInputByStm_{};
87 std::array<bool, 2> hasPrevByStm_{{false, false}};
88
90
91 int evalScaleBase_ = 750;
95 std::array<uint32_t, 2> evalsSinceFullByStm_{{0, 0}};
96 std::atomic<uint64_t> totalRebuilds_{0};
97
101 static uint32_t read_u32(std::ifstream &in);
102
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); }) {
109 dst.resize(n);
110 } else {
111 if (n > dst.size())
112 throw std::runtime_error("Fixed-size tensor shape mismatch while reading");
113 }
114 in.read(reinterpret_cast<char *>(dst.data()),
115 static_cast<std::streamsize>(n * sizeof(float)));
116 if (!in.good())
117 throw std::runtime_error("Failed reading float block");
118 }
119
123 static void read_floats_raw(std::ifstream &in, float *dst, size_t n);
124
128 static ExpertPoolMode pool_mode_from_code(int code);
129
133 template <typename SrcContainer, typename DstContainer>
134 static void transpose_copy(const SrcContainer &src, DstContainer &dst,
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); }) {
138 dst.resize(n);
139 } else {
140 if (dst.size() != n)
141 throw std::runtime_error("Fixed-size tensor shape mismatch while transposing");
142 }
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];
146 }
147 }
148 }
149
153 static void load_weights_into_target(const std::string &weightsPath,
154 BenchConfig &cfg,
155 SharedMoEWeights &weights);
156
160 void load_weights_into_model(const std::string &weightsPath);
161};
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
Definition FactorizedFeatureExtractor.hpp:47
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