2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
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.
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.
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/>.
19 // Definition of layer AffineTransformSparseInput of NNUE evaluation function
21 #ifndef NNUE_LAYERS_AFFINE_TRANSFORM_SPARSE_INPUT_H_INCLUDED
22 #define NNUE_LAYERS_AFFINE_TRANSFORM_SPARSE_INPUT_H_INCLUDED
27 #include <type_traits>
28 #include "../nnue_common.h"
29 #include "affine_transform.h"
33 This file contains the definition for a fully connected layer (aka affine transform) with block sparse input.
36 namespace Stockfish::Eval::NNUE::Layers {
37 #if defined(__GNUC__) // GCC, Clang, ICC
39 static inline IndexType lsb_(std::uint32_t b) {
41 return IndexType(__builtin_ctzl(b));
44 #elif defined(_MSC_VER) // MSVC
46 static inline IndexType lsb_(std::uint32_t b) {
49 _BitScanForward(&idx, b);
50 return (IndexType) idx;
53 #else // Compiler is neither GCC nor MSVC compatible
55 #error "Compiler not supported."
60 #if defined(USE_SSSE3)
61 alignas(CacheLineSize) static inline const std::array<std::array<std::uint16_t, 8>, 256> lookup_indices = [](){
62 std::array<std::array<std::uint16_t, 8>, 256> v{};
63 for (int i = 0; i < 256; ++i)
69 const IndexType lsbIndex = lsb_(std::uint32_t(j));
77 alignas(CacheLineSize) static inline const std::array<unsigned, 256> lookup_count = [](){
78 std::array<unsigned, 256> v;
79 for (int i = 0; i < 256; ++i)
93 // Find indices of nonzero numbers in an int32_t array
94 template<const IndexType InputDimensions>
95 void find_nnz(const std::int32_t* input, std::uint16_t* out, IndexType& count_out) {
96 #if defined (USE_AVX512)
97 using vec_t = __m512i;
98 #define vec_nnz(a) _mm512_cmpgt_epi32_mask(a, _mm512_setzero_si512())
99 #elif defined (USE_AVX2)
100 using vec_t = __m256i;
101 #define vec_nnz(a) _mm256_movemask_ps(_mm256_castsi256_ps(_mm256_cmpgt_epi32(a, _mm256_setzero_si256())))
102 #elif defined (USE_SSSE3)
103 using vec_t = __m128i;
104 #define vec_nnz(a) _mm_movemask_ps(_mm_castsi128_ps(_mm_cmpgt_epi32(a, _mm_setzero_si128())))
106 constexpr IndexType InputSimdWidth = sizeof(vec_t) / sizeof(std::int32_t);
107 // Inputs are processed InputSimdWidth at a time and outputs are processed 8 at a time so we process in chunks of max(InputSimdWidth, 8)
108 constexpr IndexType ChunkSize = std::max<IndexType>(InputSimdWidth, 8);
109 constexpr IndexType NumChunks = InputDimensions / ChunkSize;
110 constexpr IndexType InputsPerChunk = ChunkSize / InputSimdWidth;
111 constexpr IndexType OutputsPerChunk = ChunkSize / 8;
113 const auto inputVector = reinterpret_cast<const vec_t*>(input);
115 __m128i base = _mm_set1_epi16(0);
116 __m128i increment = _mm_set1_epi16(8);
117 for (IndexType i = 0; i < NumChunks; ++i)
119 // bitmask of nonzero values in this chunk
121 for (IndexType j = 0; j < InputsPerChunk; ++j)
123 const vec_t inputChunk = inputVector[i * InputsPerChunk + j];
124 nnz |= (unsigned)vec_nnz(inputChunk) << (j * InputSimdWidth);
126 for (IndexType j = 0; j < OutputsPerChunk; ++j)
128 const auto lookup = (nnz >> (j * 8)) & 0xFF;
129 const auto offsets = _mm_loadu_si128(reinterpret_cast<const __m128i*>(&lookup_indices[lookup]));
130 _mm_storeu_si128(reinterpret_cast<__m128i*>(out + count), _mm_add_epi16(base, offsets));
131 count += lookup_count[lookup];
132 base = _mm_add_epi16(base, increment);
140 // Sparse input implementation
141 template <IndexType InDims, IndexType OutDims>
142 class AffineTransformSparseInput {
146 using InputType = std::uint8_t;
147 using OutputType = std::int32_t;
149 // Number of input/output dimensions
150 static constexpr IndexType InputDimensions = InDims;
151 static constexpr IndexType OutputDimensions = OutDims;
153 static_assert(OutputDimensions % 16 == 0, "Only implemented for OutputDimensions divisible by 16.");
155 static constexpr IndexType PaddedInputDimensions =
156 ceil_to_multiple<IndexType>(InputDimensions, MaxSimdWidth);
157 static constexpr IndexType PaddedOutputDimensions =
158 ceil_to_multiple<IndexType>(OutputDimensions, MaxSimdWidth);
160 #if defined (USE_SSSE3)
161 static constexpr IndexType ChunkSize = 4;
163 static constexpr IndexType ChunkSize = 1;
166 using OutputBuffer = OutputType[PaddedOutputDimensions];
168 // Hash value embedded in the evaluation file
169 static constexpr std::uint32_t get_hash_value(std::uint32_t prevHash) {
170 std::uint32_t hashValue = 0xCC03DAE4u;
171 hashValue += OutputDimensions;
172 hashValue ^= prevHash >> 1;
173 hashValue ^= prevHash << 31;
177 static IndexType get_weight_index_scrambled(IndexType i)
180 (i / ChunkSize) % (PaddedInputDimensions / ChunkSize) * OutputDimensions * ChunkSize +
181 i / PaddedInputDimensions * ChunkSize +
185 static IndexType get_weight_index(IndexType i)
187 #if defined (USE_SSSE3)
188 return get_weight_index_scrambled(i);
194 // Read network parameters
195 bool read_parameters(std::istream& stream) {
196 read_little_endian<BiasType>(stream, biases, OutputDimensions);
197 for (IndexType i = 0; i < OutputDimensions * PaddedInputDimensions; ++i)
198 weights[get_weight_index(i)] = read_little_endian<WeightType>(stream);
200 return !stream.fail();
203 // Write network parameters
204 bool write_parameters(std::ostream& stream) const {
205 write_little_endian<BiasType>(stream, biases, OutputDimensions);
207 for (IndexType i = 0; i < OutputDimensions * PaddedInputDimensions; ++i)
208 write_little_endian<WeightType>(stream, weights[get_weight_index(i)]);
210 return !stream.fail();
212 // Forward propagation
213 const OutputType* propagate(
214 const InputType* input, OutputType* output) const {
216 #if defined (USE_SSSE3)
217 #if defined (USE_AVX512)
218 using vec_t = __m512i;
219 #define vec_setzero _mm512_setzero_si512
220 #define vec_set_32 _mm512_set1_epi32
221 #define vec_add_dpbusd_32 Simd::m512_add_dpbusd_epi32
222 #elif defined (USE_AVX2)
223 using vec_t = __m256i;
224 #define vec_setzero _mm256_setzero_si256
225 #define vec_set_32 _mm256_set1_epi32
226 #define vec_add_dpbusd_32 Simd::m256_add_dpbusd_epi32
227 #elif defined (USE_SSSE3)
228 using vec_t = __m128i;
229 #define vec_setzero _mm_setzero_si128
230 #define vec_set_32 _mm_set1_epi32
231 #define vec_add_dpbusd_32 Simd::m128_add_dpbusd_epi32
233 static constexpr IndexType OutputSimdWidth = sizeof(vec_t) / sizeof(OutputType);
235 constexpr IndexType NumChunks = ceil_to_multiple<IndexType>(InputDimensions, 8) / ChunkSize;
236 constexpr IndexType NumRegs = OutputDimensions / OutputSimdWidth;
237 std::uint16_t nnz[NumChunks];
240 const auto input32 = reinterpret_cast<const std::int32_t*>(input);
242 // Find indices of nonzero 32bit blocks
243 find_nnz<NumChunks>(input32, nnz, count);
245 const vec_t* biasvec = reinterpret_cast<const vec_t*>(biases);
247 for (IndexType k = 0; k < NumRegs; ++k)
250 for (IndexType j = 0; j < count; ++j)
252 const auto i = nnz[j];
253 const vec_t in = vec_set_32(input32[i]);
254 const auto col = reinterpret_cast<const vec_t*>(&weights[i * OutputDimensions * ChunkSize]);
255 for (IndexType k = 0; k < NumRegs; ++k)
256 vec_add_dpbusd_32(acc[k], in, col[k]);
259 vec_t* outptr = reinterpret_cast<vec_t*>(output);
260 for (IndexType k = 0; k < NumRegs; ++k)
264 # undef vec_add_dpbusd_32
266 // Use dense implementation for the other architectures.
267 affine_transform_non_ssse3<
269 PaddedInputDimensions,
270 OutputDimensions>(output, weights, biases, input);
277 using BiasType = OutputType;
278 using WeightType = std::int8_t;
280 alignas(CacheLineSize) BiasType biases[OutputDimensions];
281 alignas(CacheLineSize) WeightType weights[OutputDimensions * PaddedInputDimensions];
284 } // namespace Stockfish::Eval::NNUE::Layers
286 #endif // #ifndef NNUE_LAYERS_AFFINE_TRANSFORM_SPARSE_INPUT_H_INCLUDED