Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/nnue/nnue_common.h
375 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2025 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 WeightType = std::int16_t;
53
using PSQTWeightType = std::int32_t;
54
using IndexType = std::uint32_t;
55
56
// Version of the evaluation file
57
constexpr std::uint32_t Version = 0x7AF32F20u;
58
59
// Constant used in evaluation value calculation
60
constexpr int OutputScale = 16;
61
constexpr int WeightScaleBits = 6;
62
63
// Size of cache line (in bytes)
64
constexpr std::size_t CacheLineSize = 64;
65
66
constexpr const char Leb128MagicString[] = "COMPRESSED_LEB128";
67
constexpr const std::size_t Leb128MagicStringSize = sizeof(Leb128MagicString) - 1;
68
69
// SIMD width (in bytes)
70
#if defined(USE_AVX2)
71
constexpr std::size_t SimdWidth = 32;
72
73
#elif defined(USE_SSE2)
74
constexpr std::size_t SimdWidth = 16;
75
76
#elif defined(USE_NEON)
77
constexpr std::size_t SimdWidth = 16;
78
#endif
79
80
constexpr std::size_t MaxSimdWidth = 32;
81
82
// Type of input feature after conversion
83
using TransformedFeatureType = std::uint8_t;
84
85
// Round n up to be a multiple of base
86
template<typename IntType>
87
constexpr IntType ceil_to_multiple(IntType n, IntType base) {
88
return (n + base - 1) / base * base;
89
}
90
91
92
// Utility to read an integer (signed or unsigned, any size)
93
// from a stream in little-endian order. We swap the byte order after the read if
94
// necessary to return a result with the byte ordering of the compiling machine.
95
template<typename IntType>
96
inline IntType read_little_endian(std::istream& stream) {
97
IntType result;
98
99
if (IsLittleEndian)
100
stream.read(reinterpret_cast<char*>(&result), sizeof(IntType));
101
else
102
{
103
std::uint8_t u[sizeof(IntType)];
104
std::make_unsigned_t<IntType> v = 0;
105
106
stream.read(reinterpret_cast<char*>(u), sizeof(IntType));
107
for (std::size_t i = 0; i < sizeof(IntType); ++i)
108
v = (v << 8) | u[sizeof(IntType) - i - 1];
109
110
std::memcpy(&result, &v, sizeof(IntType));
111
}
112
113
return result;
114
}
115
116
117
// Utility to write an integer (signed or unsigned, any size)
118
// to a stream in little-endian order. We swap the byte order before the write if
119
// necessary to always write in little-endian order, independently of the byte
120
// ordering of the compiling machine.
121
template<typename IntType>
122
inline void write_little_endian(std::ostream& stream, IntType value) {
123
124
if (IsLittleEndian)
125
stream.write(reinterpret_cast<const char*>(&value), sizeof(IntType));
126
else
127
{
128
std::uint8_t u[sizeof(IntType)];
129
std::make_unsigned_t<IntType> v = value;
130
131
std::size_t i = 0;
132
// if constexpr to silence the warning about shift by 8
133
if constexpr (sizeof(IntType) > 1)
134
{
135
for (; i + 1 < sizeof(IntType); ++i)
136
{
137
u[i] = std::uint8_t(v);
138
v >>= 8;
139
}
140
}
141
u[i] = std::uint8_t(v);
142
143
stream.write(reinterpret_cast<char*>(u), sizeof(IntType));
144
}
145
}
146
147
148
// Read integers in bulk from a little-endian stream.
149
// This reads N integers from stream s and puts them in array out.
150
template<typename IntType>
151
inline void read_little_endian(std::istream& stream, IntType* out, std::size_t count) {
152
if (IsLittleEndian)
153
stream.read(reinterpret_cast<char*>(out), sizeof(IntType) * count);
154
else
155
for (std::size_t i = 0; i < count; ++i)
156
out[i] = read_little_endian<IntType>(stream);
157
}
158
159
160
// Write integers in bulk to a little-endian stream.
161
// This takes N integers from array values and writes them on stream s.
162
template<typename IntType>
163
inline void write_little_endian(std::ostream& stream, const IntType* values, std::size_t count) {
164
if (IsLittleEndian)
165
stream.write(reinterpret_cast<const char*>(values), sizeof(IntType) * count);
166
else
167
for (std::size_t i = 0; i < count; ++i)
168
write_little_endian<IntType>(stream, values[i]);
169
}
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 IntType>
176
inline void read_leb_128(std::istream& stream, IntType* out, std::size_t count) {
177
178
// Check the presence of our LEB128 magic string
179
char leb128MagicString[Leb128MagicStringSize];
180
stream.read(leb128MagicString, Leb128MagicStringSize);
181
assert(strncmp(Leb128MagicString, leb128MagicString, Leb128MagicStringSize) == 0);
182
183
static_assert(std::is_signed_v<IntType>, "Not implemented for unsigned types");
184
185
const std::uint32_t BUF_SIZE = 4096;
186
std::uint8_t buf[BUF_SIZE];
187
188
auto bytes_left = read_little_endian<std::uint32_t>(stream);
189
190
std::uint32_t buf_pos = BUF_SIZE;
191
for (std::size_t i = 0; i < count; ++i)
192
{
193
IntType result = 0;
194
size_t shift = 0;
195
do
196
{
197
if (buf_pos == BUF_SIZE)
198
{
199
stream.read(reinterpret_cast<char*>(buf), std::min(bytes_left, BUF_SIZE));
200
buf_pos = 0;
201
}
202
203
std::uint8_t byte = buf[buf_pos++];
204
--bytes_left;
205
result |= (byte & 0x7f) << shift;
206
shift += 7;
207
208
if ((byte & 0x80) == 0)
209
{
210
out[i] = (sizeof(IntType) * 8 <= shift || (byte & 0x40) == 0)
211
? result
212
: result | ~((1 << shift) - 1);
213
break;
214
}
215
} while (shift < sizeof(IntType) * 8);
216
}
217
218
assert(bytes_left == 0);
219
}
220
221
222
// Write signed integers to a stream with LEB128 compression.
223
// This takes N integers from array values, compresses them with
224
// the LEB128 algorithm and writes the result on the stream s.
225
// See https://en.wikipedia.org/wiki/LEB128 for a description of the compression scheme.
226
template<typename IntType>
227
inline void write_leb_128(std::ostream& stream, const IntType* values, std::size_t count) {
228
229
// Write our LEB128 magic string
230
stream.write(Leb128MagicString, Leb128MagicStringSize);
231
232
static_assert(std::is_signed_v<IntType>, "Not implemented for unsigned types");
233
234
std::uint32_t byte_count = 0;
235
for (std::size_t i = 0; i < count; ++i)
236
{
237
IntType value = values[i];
238
std::uint8_t byte;
239
do
240
{
241
byte = value & 0x7f;
242
value >>= 7;
243
++byte_count;
244
} while ((byte & 0x40) == 0 ? value != 0 : value != -1);
245
}
246
247
write_little_endian(stream, byte_count);
248
249
const std::uint32_t BUF_SIZE = 4096;
250
std::uint8_t buf[BUF_SIZE];
251
std::uint32_t buf_pos = 0;
252
253
auto flush = [&]() {
254
if (buf_pos > 0)
255
{
256
stream.write(reinterpret_cast<char*>(buf), buf_pos);
257
buf_pos = 0;
258
}
259
};
260
261
auto write = [&](std::uint8_t byte) {
262
buf[buf_pos++] = byte;
263
if (buf_pos == BUF_SIZE)
264
flush();
265
};
266
267
for (std::size_t i = 0; i < count; ++i)
268
{
269
IntType value = values[i];
270
while (true)
271
{
272
std::uint8_t byte = value & 0x7f;
273
value >>= 7;
274
if ((byte & 0x40) == 0 ? value == 0 : value == -1)
275
{
276
write(byte);
277
break;
278
}
279
write(byte | 0x80);
280
}
281
}
282
283
flush();
284
}
285
286
} // namespace Stockfish::Eval::NNUE
287
288
#endif // #ifndef NNUE_COMMON_H_INCLUDED
289
290