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