io-chess
UCI chess engine
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
47struct 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
56public:
57 // --- Branch group indices ---
72
73 // --- Common sub-plane indices (shared across all piece types) ---
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
104
105 // --- Bypass plane indices ---
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) ---
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};
Definition FactorizedFeatureExtractor.hpp:55
static void fill_input_rich(const chess::Board &board, FactorizedInput &out)
Definition FactorizedFeatureExtractor.cpp:873
static constexpr int NUM_KNIGHT_PLANES
Definition FactorizedFeatureExtractor.hpp:83
BranchIndex
Definition FactorizedFeatureExtractor.hpp:58
@ THEM_BISHOP
Definition FactorizedFeatureExtractor.hpp:67
@ THEM_KNIGHT
Definition FactorizedFeatureExtractor.hpp:66
@ US_KING
Definition FactorizedFeatureExtractor.hpp:64
@ US_BISHOP
Definition FactorizedFeatureExtractor.hpp:61
@ THEM_ROOK
Definition FactorizedFeatureExtractor.hpp:68
@ US_PAWN
Definition FactorizedFeatureExtractor.hpp:59
@ THEM_QUEEN
Definition FactorizedFeatureExtractor.hpp:69
@ US_QUEEN
Definition FactorizedFeatureExtractor.hpp:63
@ THEM_PAWN
Definition FactorizedFeatureExtractor.hpp:65
@ THEM_KING
Definition FactorizedFeatureExtractor.hpp:70
@ US_ROOK
Definition FactorizedFeatureExtractor.hpp:62
@ US_KNIGHT
Definition FactorizedFeatureExtractor.hpp:60
BypassIndex
Definition FactorizedFeatureExtractor.hpp:106
@ SAFE_MOBILITY_MAP
Definition FactorizedFeatureExtractor.hpp:114
@ TOTAL_CONTROL
Definition FactorizedFeatureExtractor.hpp:111
@ OPEN_LINES_MAP
Definition FactorizedFeatureExtractor.hpp:118
@ PASSED_PAWN_MAP
Definition FactorizedFeatureExtractor.hpp:116
@ EN_PRISE
Definition FactorizedFeatureExtractor.hpp:113
@ ABSOLUTE_PINS
Definition FactorizedFeatureExtractor.hpp:112
@ KING_RING_MAP
Definition FactorizedFeatureExtractor.hpp:115
@ THEM_KING_DIST
Definition FactorizedFeatureExtractor.hpp:108
@ US_KING_DIST
Definition FactorizedFeatureExtractor.hpp:107
@ OUTPOST_MAP
Definition FactorizedFeatureExtractor.hpp:117
@ US_PAWN_ATTACKS
Definition FactorizedFeatureExtractor.hpp:109
@ THEM_PAWN_ATTACKS
Definition FactorizedFeatureExtractor.hpp:110
static constexpr int PLANES_PER_TYPE[6]
Definition FactorizedFeatureExtractor.hpp:94
static constexpr int NUM_BISHOP_PLANES
Definition FactorizedFeatureExtractor.hpp:84
PlaneIndex
Definition FactorizedFeatureExtractor.hpp:74
@ PRESENCE
Definition FactorizedFeatureExtractor.hpp:75
@ MOBILITY
Definition FactorizedFeatureExtractor.hpp:76
@ X_RAY
Definition FactorizedFeatureExtractor.hpp:79
@ DEFENDS
Definition FactorizedFeatureExtractor.hpp:78
@ ATTACKS
Definition FactorizedFeatureExtractor.hpp:77
GlobalIndices
Definition FactorizedFeatureExtractor.hpp:126
@ THEM_OO
Definition FactorizedFeatureExtractor.hpp:139
@ US_ROOK_VS_MINORS
Definition FactorizedFeatureExtractor.hpp:145
@ THEM_OOO
Definition FactorizedFeatureExtractor.hpp:140
@ US_OOO
Definition FactorizedFeatureExtractor.hpp:138
@ US_MAT_QUEEN
Definition FactorizedFeatureExtractor.hpp:135
@ OPPOSITE_BISHOPS
Definition FactorizedFeatureExtractor.hpp:144
@ US_OO
Definition FactorizedFeatureExtractor.hpp:137
@ US_MAT_BISHOP
Definition FactorizedFeatureExtractor.hpp:131
@ US_MAT_ROOK
Definition FactorizedFeatureExtractor.hpp:133
@ THEM_BISHOP_PAIR
Definition FactorizedFeatureExtractor.hpp:143
@ US_MAT_KNIGHT
Definition FactorizedFeatureExtractor.hpp:129
@ THEM_MAT_ROOK
Definition FactorizedFeatureExtractor.hpp:134
@ THEM_MAT_QUEEN
Definition FactorizedFeatureExtractor.hpp:136
@ THEM_MAT_BISHOP
Definition FactorizedFeatureExtractor.hpp:132
@ US_MAT_PAWN
Definition FactorizedFeatureExtractor.hpp:127
@ THEM_MAT_PAWN
Definition FactorizedFeatureExtractor.hpp:128
@ US_BISHOP_PAIR
Definition FactorizedFeatureExtractor.hpp:142
@ IS_WHITE_TO_MOVE
Definition FactorizedFeatureExtractor.hpp:147
@ PHASE
Definition FactorizedFeatureExtractor.hpp:141
@ THEM_ROOK_VS_MINORS
Definition FactorizedFeatureExtractor.hpp:146
@ THEM_MAT_KNIGHT
Definition FactorizedFeatureExtractor.hpp:130
static void fill_input(const chess::Board &board, FactorizedInput &out)
Definition FactorizedFeatureExtractor.cpp:878
static constexpr int NUM_ROOK_PLANES
Definition FactorizedFeatureExtractor.hpp:85
static constexpr int NUM_KING_PLANES
Definition FactorizedFeatureExtractor.hpp:91
static constexpr int NUM_QUEEN_PLANES
Definition FactorizedFeatureExtractor.hpp:88
static constexpr int NUM_PAWN_PLANES
Definition FactorizedFeatureExtractor.hpp:82
static constexpr int NUM_BRANCH_PLANES_RICH
Definition FactorizedFeatureExtractor.hpp:103
static constexpr int NUM_BYPASS_PLANES
Definition FactorizedFeatureExtractor.hpp:121
Definition FactorizedFeatureExtractor.hpp:47
float branches[12][MAX_BRANCH_PLANES][64]
Definition FactorizedFeatureExtractor.hpp:50
float bypass[12][64]
Definition FactorizedFeatureExtractor.hpp:51
static constexpr int MAX_BRANCH_PLANES
Definition FactorizedFeatureExtractor.hpp:48
float global[32]
Definition FactorizedFeatureExtractor.hpp:52