Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/nnue/nnue_common.h
637 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2026 The Stockfish developers (see AUTHORS file)
4
5
Stockfish is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9
10
Stockfish is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
// Constants used in NNUE evaluation function
20
21
#ifndef NNUE_COMMON_H_INCLUDED
22
#define NNUE_COMMON_H_INCLUDED
23
24
#include <algorithm>
25
#include <cassert>
26
#include <cstdint>
27
#include <cstring>
28
#include <iostream>
29
#include <type_traits>
30
31
#include "../misc.h"
32
33
#if defined(USE_AVX2)
34
#include <immintrin.h>
35
36
#elif defined(USE_SSE41)
37
#include <smmintrin.h>
38
39
#elif defined(USE_SSSE3)
40
#include <tmmintrin.h>
41
42
#elif defined(USE_SSE2)
43
#include <emmintrin.h>
44
45
#elif defined(USE_NEON)
46
#include <arm_neon.h>
47
#endif
48
49
namespace Stockfish::Eval::NNUE {
50
51
using BiasType = std::int16_t;
52
using ThreatWeightType = std::int8_t;
53
using WeightType = std::int16_t;
54
using PSQTWeightType = std::int32_t;
55
using IndexType = std::uint32_t;
56
57
// Version of the evaluation file
58
constexpr std::uint32_t Version = 0x7AF32F20u;
59
60
// Constant used in evaluation value calculation
61
constexpr int OutputScale = 16;
62
constexpr int WeightScaleBits = 6;
63
64
// Size of cache line (in bytes)
65
constexpr std::size_t CacheLineSize = 64;
66
67
constexpr const char Leb128MagicString[] = "COMPRESSED_LEB128";
68
constexpr const std::size_t Leb128MagicStringSize = sizeof(Leb128MagicString) - 1;
69
70
// SIMD width (in bytes)
71
#if defined(USE_AVX2)
72
constexpr std::size_t SimdWidth = 32;
73
74
#elif defined(USE_SSE2)
75
constexpr std::size_t SimdWidth = 16;
76
77
#elif defined(USE_NEON)
78
constexpr std::size_t SimdWidth = 16;
79
#endif
80
81
constexpr std::size_t MaxSimdWidth = 32;
82
83
// Type of input feature after conversion
84
using TransformedFeatureType = std::uint8_t;
85
86
// Round n up to be a multiple of base
87
template<typename IntType>
88
constexpr IntType ceil_to_multiple(IntType n, IntType base) {
89
return (n + base - 1) / base * base;
90
}
91
92
93
// Utility to read an integer (signed or unsigned, any size)
94
// from a stream in little-endian order. We swap the byte order after the read if
95
// necessary to return a result with the byte ordering of the compiling machine.
96
template<typename IntType>
97
inline IntType read_little_endian(std::istream& stream) {
98
IntType result;
99
100
if (IsLittleEndian)
101
stream.read(reinterpret_cast<char*>(&result), sizeof(IntType));
102
else
103
{
104
std::uint8_t u[sizeof(IntType)];
105
std::make_unsigned_t<IntType> v = 0;
106
107
stream.read(reinterpret_cast<char*>(u), sizeof(IntType));
108
for (std::size_t i = 0; i < sizeof(IntType); ++i)
109
v = (v << 8) | u[sizeof(IntType) - i - 1];
110
111
std::memcpy(&result, &v, sizeof(IntType));
112
}
113
114
return result;
115
}
116
117
118
// Utility to write an integer (signed or unsigned, any size)
119
// to a stream in little-endian order. We swap the byte order before the write if
120
// necessary to always write in little-endian order, independently of the byte
121
// ordering of the compiling machine.
122
template<typename IntType>
123
inline void write_little_endian(std::ostream& stream, IntType value) {
124
125
if (IsLittleEndian)
126
stream.write(reinterpret_cast<const char*>(&value), sizeof(IntType));
127
else
128
{
129
std::uint8_t u[sizeof(IntType)];
130
std::make_unsigned_t<IntType> v = value;
131
132
std::size_t i = 0;
133
// if constexpr to silence the warning about shift by 8
134
if constexpr (sizeof(IntType) > 1)
135
{
136
for (; i + 1 < sizeof(IntType); ++i)
137
{
138
u[i] = std::uint8_t(v);
139
v >>= 8;
140
}
141
}
142
u[i] = std::uint8_t(v);
143
144
stream.write(reinterpret_cast<char*>(u), sizeof(IntType));
145
}
146
}
147
148
149
// Read integers in bulk from a little-endian stream.
150
// This reads N integers from stream s and puts them in array out.
151
template<typename IntType>
152
inline void read_little_endian(std::istream& stream, IntType* out, std::size_t count) {
153
if (IsLittleEndian)
154
stream.read(reinterpret_cast<char*>(out), sizeof(IntType) * count);
155
else
156
for (std::size_t i = 0; i < count; ++i)
157
out[i] = read_little_endian<IntType>(stream);
158
}
159
160
161
// Write integers in bulk to a little-endian stream.
162
// This takes N integers from array values and writes them on stream s.
163
template<typename IntType>
164
inline void write_little_endian(std::ostream& stream, const IntType* values, std::size_t count) {
165
if (IsLittleEndian)
166
stream.write(reinterpret_cast<const char*>(values), sizeof(IntType) * count);
167
else
168
for (std::size_t i = 0; i < count; ++i)
169
write_little_endian<IntType>(stream, values[i]);
170
}
171
172
// Read N signed integers from the stream s, putting them in the array out.
173
// The stream is assumed to be compressed using the signed LEB128 format.
174
// See https://en.wikipedia.org/wiki/LEB128 for a description of the compression scheme.
175
template<typename BufType, typename IntType, std::size_t Count>
176
inline void read_leb_128_detail(std::istream& stream,
177
std::array<IntType, Count>& out,
178
std::uint32_t& bytes_left,
179
BufType& buf,
180
std::uint32_t& buf_pos) {
181
182
static_assert(std::is_signed_v<IntType>, "Not implemented for unsigned types");
183
static_assert(sizeof(IntType) <= 4, "Not implemented for types larger than 32 bit");
184
185
IntType result = 0;
186
size_t shift = 0, i = 0;
187
while (i < Count)
188
{
189
if (buf_pos == buf.size())
190
{
191
stream.read(reinterpret_cast<char*>(buf.data()),
192
std::min(std::size_t(bytes_left), buf.size()));
193
buf_pos = 0;
194
}
195
196
std::uint8_t byte = buf[buf_pos++];
197
--bytes_left;
198
result |= (byte & 0x7f) << (shift % 32);
199
shift += 7;
200
201
if ((byte & 0x80) == 0)
202
{
203
out[i++] = (shift >= 32 || (byte & 0x40) == 0) ? result : result | ~((1 << shift) - 1);
204
result = 0;
205
shift = 0;
206
}
207
}
208
}
209
210
template<typename... Arrays>
211
inline void read_leb_128(std::istream& stream, Arrays&... outs) {
212
// Check the presence of our LEB128 magic string
213
char leb128MagicString[Leb128MagicStringSize];
214
stream.read(leb128MagicString, Leb128MagicStringSize);
215
assert(strncmp(Leb128MagicString, leb128MagicString, Leb128MagicStringSize) == 0);
216
217
auto bytes_left = read_little_endian<std::uint32_t>(stream);
218
std::array<std::uint8_t, 8192> buf;
219
std::uint32_t buf_pos = std::uint32_t(buf.size());
220
221
(read_leb_128_detail(stream, outs, bytes_left, buf, buf_pos), ...);
222
223
assert(bytes_left == 0);
224
}
225
226
227
// Write signed integers to a stream with LEB128 compression.
228
// This takes N integers from array values, compresses them with
229
// the LEB128 algorithm and writes the result on the stream s.
230
// See https://en.wikipedia.org/wiki/LEB128 for a description of the compression scheme.
231
template<typename IntType, std::size_t Count>
232
inline void write_leb_128(std::ostream& stream, const std::array<IntType, Count>& values) {
233
234
// Write our LEB128 magic string
235
stream.write(Leb128MagicString, Leb128MagicStringSize);
236
237
static_assert(std::is_signed_v<IntType>, "Not implemented for unsigned types");
238
239
std::uint32_t byte_count = 0;
240
for (std::size_t i = 0; i < Count; ++i)
241
{
242
IntType value = values[i];
243
std::uint8_t byte;
244
do
245
{
246
byte = value & 0x7f;
247
value >>= 7;
248
++byte_count;
249
} while ((byte & 0x40) == 0 ? value != 0 : value != -1);
250
}
251
252
write_little_endian(stream, byte_count);
253
254
const std::uint32_t BUF_SIZE = 4096;
255
std::uint8_t buf[BUF_SIZE];
256
std::uint32_t buf_pos = 0;
257
258
auto flush = [&]() {
259
if (buf_pos > 0)
260
{
261
stream.write(reinterpret_cast<char*>(buf), buf_pos);
262
buf_pos = 0;
263
}
264
};
265
266
auto write = [&](std::uint8_t b) {
267
buf[buf_pos++] = b;
268
if (buf_pos == BUF_SIZE)
269
flush();
270
};
271
272
for (std::size_t i = 0; i < Count; ++i)
273
{
274
IntType value = values[i];
275
while (true)
276
{
277
std::uint8_t byte = value & 0x7f;
278
value >>= 7;
279
if ((byte & 0x40) == 0 ? value == 0 : value == -1)
280
{
281
write(byte);
282
break;
283
}
284
write(byte | 0x80);
285
}
286
}
287
288
flush();
289
}
290
291
} // namespace Stockfish::Eval::NNUE
292
293
#endif // #ifndef NNUE_COMMON_H_INCLUDED
294
295