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
73 std::shared_ptr<const EvalContextMoECacheSharedModel> shared);
74
76 std::shared_ptr<const EvalContextMoECacheSharedModel> sharedModel_{};
77 std::array<MoEDoubleAccumulator, 2> models_{};
78
79 // Large feature tensor scratch reused across calls to avoid deep-recursion
80 // stack growth in alpha-beta.
82
83 std::array<FactorizedInput, 2> prevInputByStm_{};
84 std::array<bool, 2> hasPrevByStm_{{false, false}};
85
87
88 int evalScaleBase_ = 750;
92 std::array<uint32_t, 2> evalsSinceFullByStm_{{0, 0}};
93 std::atomic<uint64_t> totalRebuilds_{0};
94
95 static uint32_t read_u32(std::ifstream &in);
96
97 template <typename FloatContainer>
98 static void read_floats(std::ifstream &in, FloatContainer &dst, size_t n) {
99 if constexpr (requires(FloatContainer &c, size_t m) { c.resize(m); }) {
100 dst.resize(n);
101 } else {
102 if (n > dst.size())
103 throw std::runtime_error("Fixed-size tensor shape mismatch while reading");
104 }
105 in.read(reinterpret_cast<char *>(dst.data()),
106 static_cast<std::streamsize>(n * sizeof(float)));
107 if (!in.good())
108 throw std::runtime_error("Failed reading float block");
109 }
110
111 static void read_floats_raw(std::ifstream &in, float *dst, size_t n);
112 static ExpertPoolMode pool_mode_from_code(int code);
113
114 template <typename SrcContainer, typename DstContainer>
115 static void transpose_copy(const SrcContainer &src, DstContainer &dst,
116 int rows, int cols) {
117 const size_t n = static_cast<size_t>(rows) * cols;
118 if constexpr (requires(DstContainer &c, size_t m) { c.resize(m); }) {
119 dst.resize(n);
120 } else {
121 if (dst.size() != n)
122 throw std::runtime_error("Fixed-size tensor shape mismatch while transposing");
123 }
124 for (int r = 0; r < rows; ++r) {
125 for (int c = 0; c < cols; ++c) {
126 dst[(size_t)c * rows + r] = src[(size_t)r * cols + c];
127 }
128 }
129 }
130
131 static void load_weights_into_target(const std::string &weightsPath,
132 BenchConfig &cfg,
133 SharedMoEWeights &weights);
134 void load_weights_into_model(const std::string &weightsPath);
135};
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:283
bool enableEvalNormalization_
Definition EvalContextMoECache.h:90
WDLConverter::WDL evaluateWDL(const Board &board, int ply=0) override
Evaluates the board and returns Win/Draw/Loss probabilities.
Definition EvalContextMoECache.cpp:215
static uint32_t read_u32(std::ifstream &in)
Definition EvalContextMoECache.cpp:19
std::array< MoEDoubleAccumulator, 2 > models_
Definition EvalContextMoECache.h:77
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)
Definition EvalContextMoECache.h:115
int rebuildEveryNEvals_
Definition EvalContextMoECache.h:91
void init_from_shared_model(std::shared_ptr< const EvalContextMoECacheSharedModel > shared)
Definition EvalContextMoECache.cpp:187
static ExpertPoolMode pool_mode_from_code(int code)
Definition EvalContextMoECache.cpp:35
static void load_weights_into_target(const std::string &weightsPath, BenchConfig &cfg, SharedMoEWeights &weights)
Definition EvalContextMoECache.cpp:50
BenchConfig cfg_
Definition EvalContextMoECache.h:75
static std::shared_ptr< const EvalContextMoECacheSharedModel > loadSharedModel(const std::string &weightsPath)
Loads the shared model weights from disk.
Definition EvalContextMoECache.cpp:181
std::atomic< uint64_t > totalRebuilds_
Definition EvalContextMoECache.h:93
static void read_floats(std::ifstream &in, FloatContainer &dst, size_t n)
Definition EvalContextMoECache.h:98
std::array< FactorizedInput, 2 > prevInputByStm_
Definition EvalContextMoECache.h:83
static constexpr uint32_t kVersion
Definition EvalContextMoECache.h:70
EvalContextMoECache(const std::string &weightsPath)
Definition EvalContextMoECache.cpp:202
static constexpr uint32_t kMagicWeights
Definition EvalContextMoECache.h:69
std::array< bool, 2 > hasPrevByStm_
Definition EvalContextMoECache.h:84
std::shared_ptr< const EvalContextMoECacheSharedModel > sharedModel_
Definition EvalContextMoECache.h:76
static void read_floats_raw(std::ifstream &in, float *dst, size_t n)
Definition EvalContextMoECache.cpp:27
WDLConverter wdlConverter_
Definition EvalContextMoECache.h:86
void load_weights_into_model(const std::string &weightsPath)
Definition EvalContextMoECache.cpp:176
FactorizedInput scratchInput_
Definition EvalContextMoECache.h:81
int evalScaleWeight_
Definition EvalContextMoECache.h:89
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:88
std::array< uint32_t, 2 > evalsSinceFullByStm_
Definition EvalContextMoECache.h:92
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