io-chess
UCI chess engine
Loading...
Searching...
No Matches
MoERouting.h
Go to the documentation of this file.
1#pragma once
2
10
11#include <ExpertRouter.hpp>
12
13#include <algorithm>
14
15namespace MoERouting {
16
26 float &top1Weight, float &top2Weight,
27 float dominantExpertCutoff = 0.8f) {
28 if (top1Weight > dominantExpertCutoff) {
29 // Renormalize to 1.0 when we intentionally skip the second expert.
30 top1Weight = 1.0f;
31 top2Weight = 0.0f;
32 return 1;
33 }
34 return 2;
35}
36
46inline void route_top2_base_experts(const FactorizedInput &inp, int &e0,
47 int &e1, float &w0, float &w1) {
49 ExpertRouter::compute_weights(inp, 0.0f, weights);
50
51 e0 = 0;
52 e1 = 1;
53 float s0 = weights.weights[0];
54 float s1 = weights.weights[1];
55 if (s1 > s0) {
56 std::swap(e0, e1);
57 std::swap(s0, s1);
58 }
59
60 for (int e = 2; e < ExpertRouter::NUM_BASE; ++e) {
61 const float s = weights.weights[e];
62 if (s > s0) {
63 s1 = s0;
64 e1 = e0;
65 s0 = s;
66 e0 = e;
67 } else if (s > s1) {
68 s1 = s;
69 e1 = e;
70 }
71 }
72
73 const float z = s0 + s1;
74 if (z > 1e-20f) {
75 w0 = s0 / z;
76 w1 = s1 / z;
77 } else {
78 w0 = 0.5f;
79 w1 = 0.5f;
80 }
81}
82
83} // namespace MoERouting
Computes expert weights for the Residual MoE architecture based on position features.
static constexpr int NUM_BASE
Definition ExpertRouter.hpp:33
static void compute_weights(const ChessInput &input, float eval_cp, ExpertWeights &out)
Definition ExpertRouter.hpp:106
Definition MoERouting.h:15
int collapse_top2_if_dominant(float &top1Weight, float &top2Weight, float dominantExpertCutoff=0.8f)
Collapses the top 2 experts into a single expert if the top one is highly dominant.
Definition MoERouting.h:25
void route_top2_base_experts(const FactorizedInput &inp, int &e0, int &e1, float &w0, float &w1)
Identifies the top 2 base experts for a given factorized input.
Definition MoERouting.h:46
Definition ExpertRouter.hpp:52
float weights[NUM_EXPERTS]
Definition ExpertRouter.hpp:53
Definition FactorizedFeatureExtractor.hpp:47