-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheval.h
135 lines (114 loc) · 4.63 KB
/
eval.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Clarity
Copyright (C) 2024 Joseph Pasfield
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "globals.h"
/*
Current Net: cn_028
Arch: (768x6->1024)x2->1x8
Activation: SCReLU
Special Details:
- Horizontal Mirroring
- Few more buckets, let's see if we can get some improvement from finny tables now
*/
constexpr int inputSize = 768;
constexpr int inputBucketCount = 6;
constexpr int layer1Size = 1024;
constexpr int outputBucketCount = 8;
constexpr std::array<int, 64> inputBuckets = []{
constexpr std::array<int, 32> rawInputBuckets = {
0, 0, 1, 1,
2, 2, 3, 3,
4, 4, 4, 4,
4, 4, 4, 4,
5, 5, 5, 5,
5, 5, 5, 5,
5, 5, 5, 5,
5, 5, 5, 5
};
std::array<int, 64> result = {};
for(int rank = 0; rank < 8; rank++) {
for(int file = 0; file < 4; file++) {
const int src = rank * 4 + file;
const int dst = rank * 8 + file;
result[dst] = rawInputBuckets[src];
result[dst ^ 7] = rawInputBuckets[src] + inputBucketCount;
}
}
return result;
}();
// organizing this somewhat similarly to code I've seen, mostly from clarity_sp_nnue, made by Ciekce.
struct Network {
alignas(64) std::array<std::int16_t, inputSize * inputBucketCount * layer1Size> featureWeights;
alignas(64) std::array<std::int16_t, layer1Size> featureBiases;
alignas(64) std::array<std::int16_t, layer1Size * 2 * outputBucketCount> outputWeights;
alignas(64) std::array<std::int16_t, outputBucketCount> outputBiases;
};
struct Accumulator {
alignas(64) std::array<std::int16_t, layer1Size> black;
alignas(64) std::array<std::int16_t, layer1Size> white;
void initialize(std::span<const std::int16_t, layer1Size> bias);
void initHalf(std::span<const std::int16_t, layer1Size> bias, int color);
};
struct RefreshTableEntry {
Accumulator accumulator;
std::array<BoardState, 2> boards{};
BoardState &colorBoards(int c) {
return boards[c];
}
};
struct RefreshTable {
std::vector<RefreshTableEntry> table;
void init();
};
class NetworkState {
public:
NetworkState() {
stack.resize(128);
reset();
}
inline void push() {
stack[current + 1] = stack[current];
current++;
}
void performUpdates(NetworkUpdates updates, int blackKing, int whiteKing, const BoardState &state);
void performUpdatesAndPush(NetworkUpdates updates, int blackKing, int whiteKing, const BoardState &state);
inline void pop() {
current--;
}
void reset();
void activateFeature(int square, int type, int blackKing, int whiteKing);
void activateFeatureSingle(int square, int type, int color, int king);
void activateFeatureAndPush(int square, int type, int blackKing, int whiteKing);
void disableFeature(int square, int type, int blackKing, int whiteKing);
void disableFeatureSingle(int square, int type, int color, int king);
void refreshAccumulator(int color, const BoardState &state, int king);
int evaluate(int colorToMove, int materialCount);
void fullRefresh(const BoardState &state, int blackKing, int whiteKing);
void halfRefresh(int color, const BoardState &state, int king);
private:
RefreshTable refreshTable;
int current;
std::vector<Accumulator> stack;
static std::pair<uint32_t, uint32_t> getFeatureIndices(int square, int type, int blackKing, int whiteKing);
static int getFeatureIndex(int square, int type, int color, int king);
int forward(const int bucket, const std::span<std::int16_t, layer1Size> us, const std::span<std::int16_t, layer1Size> them, const std::span<const std::int16_t, layer1Size * 2 * outputBucketCount> weights);
};
constexpr bool refreshRequired(int color, int oldKingSquare, int newKingSquare) {
if(color == 0) {
oldKingSquare ^= 56;
newKingSquare ^= 56;
}
return inputBuckets[oldKingSquare] != inputBuckets[newKingSquare];
}