io-chess
UCI chess engine
Main Page
Namespaces
Classes
Files
File List
File Members
Loading...
Searching...
No Matches
FactorizedFeatureExtractor.hpp
Go to the documentation of this file.
1
5
#pragma once
6
#include "chess.hpp"
7
#include <array>
8
#include <cstdint>
9
10
// ============================================================================
11
// FactorizedInput — Output struct for ChessNetFactorizedMoE
12
// ============================================================================
13
//
14
// Layout (all float, no uint8→float conversion needed at inference time):
15
//
16
// branches[12][MAX_BRANCH_PLANES][64]
17
// 12 piece groups × up to 10 branch planes × 64 squares.
18
// Each piece type uses a different number of planes (see below);
19
// unused trailing planes are always zero.
20
//
21
// bypass[12][64] — Global heatmaps + volatile tactical maps
22
// global[32] — Scalar features (Material, Flags, etc.)
23
//
24
// Piece-group ordering (side-to-move relative):
25
//
26
// 0 US Pawns 6 THEM Pawns
27
// 1 US Knights 7 THEM Knights
28
// 2 US Bishops 8 THEM Bishops
29
// 3 US Rooks 9 THEM Rooks
30
// 4 US Queens 10 THEM Queens
31
// 5 US King 11 THEM King
32
//
33
// Legacy WHITE/BLACK enum names are kept for compatibility, but at runtime
34
// these groups are filled relative to side-to-move.
35
//
36
// Per-group planes vary by piece type:
37
//
38
// Pawns (4): Presence, Mobility, Attacks, Defends
39
// Knights (4): Presence, Mobility, Attacks, Defends
40
// Bishops (5): Presence, Mobility, Attacks, Defends, X-Ray
41
// Rooks (5): Presence, Mobility, Attacks, Defends, X-Ray
42
// Queens (5): Presence, Mobility, Attacks, Defends, X-Ray
43
// Kings (4): Presence, Mobility, Attacks, Defends
44
//
45
// ============================================================================
46
47
struct
alignas
(64)
FactorizedInput
{
48
static
constexpr
int
MAX_BRANCH_PLANES
= 10;
// Logical max across supported schemas
49
50
float
branches
[12][
MAX_BRANCH_PLANES
][64];
// 12 groups × up to 10 planes × 64 squares
51
float
bypass
[12][64];
// 12 bypass planes
52
float
global
[32];
// 32 scalars
53
};
54
55
class
FactorizedFeatureExtractor
{
56
public
:
57
// --- Branch group indices ---
58
enum
BranchIndex
{
59
US_PAWN
= 0,
60
US_KNIGHT
,
61
US_BISHOP
,
62
US_ROOK
,
63
US_QUEEN
,
64
US_KING
,
65
THEM_PAWN
,
66
THEM_KNIGHT
,
67
THEM_BISHOP
,
68
THEM_ROOK
,
69
THEM_QUEEN
,
70
THEM_KING
,
71
};
72
73
// --- Common sub-plane indices (shared across all piece types) ---
74
enum
PlaneIndex
{
75
PRESENCE
= 0,
76
MOBILITY
= 1,
77
ATTACKS
= 2,
78
DEFENDS
= 3,
79
X_RAY
= 4,
80
};
81
82
static
constexpr
int
NUM_PAWN_PLANES
= 4;
83
static
constexpr
int
NUM_KNIGHT_PLANES
= 4;
84
static
constexpr
int
NUM_BISHOP_PLANES
= 5;
85
static
constexpr
int
NUM_ROOK_PLANES
= 5;
86
87
// Queens (5 planes total — unchanged)
88
static
constexpr
int
NUM_QUEEN_PLANES
= 5;
89
90
// Kings (4 planes total)
91
static
constexpr
int
NUM_KING_PLANES
= 4;
92
93
// Plane count per piece type (indexed by piece_offset 0-5)
94
static
constexpr
int
PLANES_PER_TYPE
[6] = {
95
NUM_PAWN_PLANES
,
// 0: Pawn = 4
96
NUM_KNIGHT_PLANES
,
// 1: Knight = 4
97
NUM_BISHOP_PLANES
,
// 2: Bishop = 5
98
NUM_ROOK_PLANES
,
// 3: Rook = 5
99
NUM_QUEEN_PLANES
,
// 4: Queen = 5
100
NUM_KING_PLANES
,
// 5: King = 4
101
};
102
103
static
constexpr
int
NUM_BRANCH_PLANES_RICH
=
FactorizedInput::MAX_BRANCH_PLANES
;
104
105
// --- Bypass plane indices ---
106
enum
BypassIndex
{
107
US_KING_DIST
= 0,
108
THEM_KING_DIST
= 1,
109
US_PAWN_ATTACKS
= 2,
110
THEM_PAWN_ATTACKS
= 3,
111
TOTAL_CONTROL
= 4,
112
ABSOLUTE_PINS
= 5,
113
EN_PRISE
= 6,
114
SAFE_MOBILITY_MAP
= 7,
115
KING_RING_MAP
= 8,
116
PASSED_PAWN_MAP
= 9,
117
OUTPOST_MAP
= 10,
118
OPEN_LINES_MAP
= 11,
119
};
120
121
static
constexpr
int
NUM_BYPASS_PLANES
= 12;
122
static_assert
(
OPEN_LINES_MAP
<
NUM_BYPASS_PLANES
,
123
"Bypass index exceeds bypass plane storage"
);
124
125
// --- Global scalar indices (identical to FeatureExtractor) ---
126
enum
GlobalIndices
{
127
US_MAT_PAWN
= 0,
128
THEM_MAT_PAWN
,
129
US_MAT_KNIGHT
,
130
THEM_MAT_KNIGHT
,
131
US_MAT_BISHOP
,
132
THEM_MAT_BISHOP
,
133
US_MAT_ROOK
,
134
THEM_MAT_ROOK
,
135
US_MAT_QUEEN
,
136
THEM_MAT_QUEEN
,
137
US_OO
,
138
US_OOO
,
139
THEM_OO
,
140
THEM_OOO
,
141
PHASE
,
142
US_BISHOP_PAIR
,
143
THEM_BISHOP_PAIR
,
144
OPPOSITE_BISHOPS
,
145
US_ROOK_VS_MINORS
,
146
THEM_ROOK_VS_MINORS
,
147
IS_WHITE_TO_MOVE
,
148
};
149
150
// --- Main entry points ---
151
static
void
fill_input
(
const
chess::Board &board,
FactorizedInput
&out);
152
static
void
fill_input_rich
(
const
chess::Board &board,
FactorizedInput
&out);
153
};
FactorizedFeatureExtractor
Definition
FactorizedFeatureExtractor.hpp:55
FactorizedFeatureExtractor::fill_input_rich
static void fill_input_rich(const chess::Board &board, FactorizedInput &out)
Definition
FactorizedFeatureExtractor.cpp:873
FactorizedFeatureExtractor::NUM_KNIGHT_PLANES
static constexpr int NUM_KNIGHT_PLANES
Definition
FactorizedFeatureExtractor.hpp:83
FactorizedFeatureExtractor::BranchIndex
BranchIndex
Definition
FactorizedFeatureExtractor.hpp:58
FactorizedFeatureExtractor::THEM_BISHOP
@ THEM_BISHOP
Definition
FactorizedFeatureExtractor.hpp:67
FactorizedFeatureExtractor::THEM_KNIGHT
@ THEM_KNIGHT
Definition
FactorizedFeatureExtractor.hpp:66
FactorizedFeatureExtractor::US_KING
@ US_KING
Definition
FactorizedFeatureExtractor.hpp:64
FactorizedFeatureExtractor::US_BISHOP
@ US_BISHOP
Definition
FactorizedFeatureExtractor.hpp:61
FactorizedFeatureExtractor::THEM_ROOK
@ THEM_ROOK
Definition
FactorizedFeatureExtractor.hpp:68
FactorizedFeatureExtractor::US_PAWN
@ US_PAWN
Definition
FactorizedFeatureExtractor.hpp:59
FactorizedFeatureExtractor::THEM_QUEEN
@ THEM_QUEEN
Definition
FactorizedFeatureExtractor.hpp:69
FactorizedFeatureExtractor::US_QUEEN
@ US_QUEEN
Definition
FactorizedFeatureExtractor.hpp:63
FactorizedFeatureExtractor::THEM_PAWN
@ THEM_PAWN
Definition
FactorizedFeatureExtractor.hpp:65
FactorizedFeatureExtractor::THEM_KING
@ THEM_KING
Definition
FactorizedFeatureExtractor.hpp:70
FactorizedFeatureExtractor::US_ROOK
@ US_ROOK
Definition
FactorizedFeatureExtractor.hpp:62
FactorizedFeatureExtractor::US_KNIGHT
@ US_KNIGHT
Definition
FactorizedFeatureExtractor.hpp:60
FactorizedFeatureExtractor::BypassIndex
BypassIndex
Definition
FactorizedFeatureExtractor.hpp:106
FactorizedFeatureExtractor::SAFE_MOBILITY_MAP
@ SAFE_MOBILITY_MAP
Definition
FactorizedFeatureExtractor.hpp:114
FactorizedFeatureExtractor::TOTAL_CONTROL
@ TOTAL_CONTROL
Definition
FactorizedFeatureExtractor.hpp:111
FactorizedFeatureExtractor::OPEN_LINES_MAP
@ OPEN_LINES_MAP
Definition
FactorizedFeatureExtractor.hpp:118
FactorizedFeatureExtractor::PASSED_PAWN_MAP
@ PASSED_PAWN_MAP
Definition
FactorizedFeatureExtractor.hpp:116
FactorizedFeatureExtractor::EN_PRISE
@ EN_PRISE
Definition
FactorizedFeatureExtractor.hpp:113
FactorizedFeatureExtractor::ABSOLUTE_PINS
@ ABSOLUTE_PINS
Definition
FactorizedFeatureExtractor.hpp:112
FactorizedFeatureExtractor::KING_RING_MAP
@ KING_RING_MAP
Definition
FactorizedFeatureExtractor.hpp:115
FactorizedFeatureExtractor::THEM_KING_DIST
@ THEM_KING_DIST
Definition
FactorizedFeatureExtractor.hpp:108
FactorizedFeatureExtractor::US_KING_DIST
@ US_KING_DIST
Definition
FactorizedFeatureExtractor.hpp:107
FactorizedFeatureExtractor::OUTPOST_MAP
@ OUTPOST_MAP
Definition
FactorizedFeatureExtractor.hpp:117
FactorizedFeatureExtractor::US_PAWN_ATTACKS
@ US_PAWN_ATTACKS
Definition
FactorizedFeatureExtractor.hpp:109
FactorizedFeatureExtractor::THEM_PAWN_ATTACKS
@ THEM_PAWN_ATTACKS
Definition
FactorizedFeatureExtractor.hpp:110
FactorizedFeatureExtractor::PLANES_PER_TYPE
static constexpr int PLANES_PER_TYPE[6]
Definition
FactorizedFeatureExtractor.hpp:94
FactorizedFeatureExtractor::NUM_BISHOP_PLANES
static constexpr int NUM_BISHOP_PLANES
Definition
FactorizedFeatureExtractor.hpp:84
FactorizedFeatureExtractor::PlaneIndex
PlaneIndex
Definition
FactorizedFeatureExtractor.hpp:74
FactorizedFeatureExtractor::PRESENCE
@ PRESENCE
Definition
FactorizedFeatureExtractor.hpp:75
FactorizedFeatureExtractor::MOBILITY
@ MOBILITY
Definition
FactorizedFeatureExtractor.hpp:76
FactorizedFeatureExtractor::X_RAY
@ X_RAY
Definition
FactorizedFeatureExtractor.hpp:79
FactorizedFeatureExtractor::DEFENDS
@ DEFENDS
Definition
FactorizedFeatureExtractor.hpp:78
FactorizedFeatureExtractor::ATTACKS
@ ATTACKS
Definition
FactorizedFeatureExtractor.hpp:77
FactorizedFeatureExtractor::GlobalIndices
GlobalIndices
Definition
FactorizedFeatureExtractor.hpp:126
FactorizedFeatureExtractor::THEM_OO
@ THEM_OO
Definition
FactorizedFeatureExtractor.hpp:139
FactorizedFeatureExtractor::US_ROOK_VS_MINORS
@ US_ROOK_VS_MINORS
Definition
FactorizedFeatureExtractor.hpp:145
FactorizedFeatureExtractor::THEM_OOO
@ THEM_OOO
Definition
FactorizedFeatureExtractor.hpp:140
FactorizedFeatureExtractor::US_OOO
@ US_OOO
Definition
FactorizedFeatureExtractor.hpp:138
FactorizedFeatureExtractor::US_MAT_QUEEN
@ US_MAT_QUEEN
Definition
FactorizedFeatureExtractor.hpp:135
FactorizedFeatureExtractor::OPPOSITE_BISHOPS
@ OPPOSITE_BISHOPS
Definition
FactorizedFeatureExtractor.hpp:144
FactorizedFeatureExtractor::US_OO
@ US_OO
Definition
FactorizedFeatureExtractor.hpp:137
FactorizedFeatureExtractor::US_MAT_BISHOP
@ US_MAT_BISHOP
Definition
FactorizedFeatureExtractor.hpp:131
FactorizedFeatureExtractor::US_MAT_ROOK
@ US_MAT_ROOK
Definition
FactorizedFeatureExtractor.hpp:133
FactorizedFeatureExtractor::THEM_BISHOP_PAIR
@ THEM_BISHOP_PAIR
Definition
FactorizedFeatureExtractor.hpp:143
FactorizedFeatureExtractor::US_MAT_KNIGHT
@ US_MAT_KNIGHT
Definition
FactorizedFeatureExtractor.hpp:129
FactorizedFeatureExtractor::THEM_MAT_ROOK
@ THEM_MAT_ROOK
Definition
FactorizedFeatureExtractor.hpp:134
FactorizedFeatureExtractor::THEM_MAT_QUEEN
@ THEM_MAT_QUEEN
Definition
FactorizedFeatureExtractor.hpp:136
FactorizedFeatureExtractor::THEM_MAT_BISHOP
@ THEM_MAT_BISHOP
Definition
FactorizedFeatureExtractor.hpp:132
FactorizedFeatureExtractor::US_MAT_PAWN
@ US_MAT_PAWN
Definition
FactorizedFeatureExtractor.hpp:127
FactorizedFeatureExtractor::THEM_MAT_PAWN
@ THEM_MAT_PAWN
Definition
FactorizedFeatureExtractor.hpp:128
FactorizedFeatureExtractor::US_BISHOP_PAIR
@ US_BISHOP_PAIR
Definition
FactorizedFeatureExtractor.hpp:142
FactorizedFeatureExtractor::IS_WHITE_TO_MOVE
@ IS_WHITE_TO_MOVE
Definition
FactorizedFeatureExtractor.hpp:147
FactorizedFeatureExtractor::PHASE
@ PHASE
Definition
FactorizedFeatureExtractor.hpp:141
FactorizedFeatureExtractor::THEM_ROOK_VS_MINORS
@ THEM_ROOK_VS_MINORS
Definition
FactorizedFeatureExtractor.hpp:146
FactorizedFeatureExtractor::THEM_MAT_KNIGHT
@ THEM_MAT_KNIGHT
Definition
FactorizedFeatureExtractor.hpp:130
FactorizedFeatureExtractor::fill_input
static void fill_input(const chess::Board &board, FactorizedInput &out)
Definition
FactorizedFeatureExtractor.cpp:878
FactorizedFeatureExtractor::NUM_ROOK_PLANES
static constexpr int NUM_ROOK_PLANES
Definition
FactorizedFeatureExtractor.hpp:85
FactorizedFeatureExtractor::NUM_KING_PLANES
static constexpr int NUM_KING_PLANES
Definition
FactorizedFeatureExtractor.hpp:91
FactorizedFeatureExtractor::NUM_QUEEN_PLANES
static constexpr int NUM_QUEEN_PLANES
Definition
FactorizedFeatureExtractor.hpp:88
FactorizedFeatureExtractor::NUM_PAWN_PLANES
static constexpr int NUM_PAWN_PLANES
Definition
FactorizedFeatureExtractor.hpp:82
FactorizedFeatureExtractor::NUM_BRANCH_PLANES_RICH
static constexpr int NUM_BRANCH_PLANES_RICH
Definition
FactorizedFeatureExtractor.hpp:103
FactorizedFeatureExtractor::NUM_BYPASS_PLANES
static constexpr int NUM_BYPASS_PLANES
Definition
FactorizedFeatureExtractor.hpp:121
FactorizedInput
Definition
FactorizedFeatureExtractor.hpp:47
FactorizedInput::branches
float branches[12][MAX_BRANCH_PLANES][64]
Definition
FactorizedFeatureExtractor.hpp:50
FactorizedInput::bypass
float bypass[12][64]
Definition
FactorizedFeatureExtractor.hpp:51
FactorizedInput::MAX_BRANCH_PLANES
static constexpr int MAX_BRANCH_PLANES
Definition
FactorizedFeatureExtractor.hpp:48
FactorizedInput::global
float global[32]
Definition
FactorizedFeatureExtractor.hpp:52
preprocessing
include
FactorizedFeatureExtractor.hpp
Generated by
1.17.0