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