]> git.sesse.net Git - stockfish/blob - src/nnue/layers/clipped_relu.h
Adjust NNUE usage based on number of pawns in position
[stockfish] / src / nnue / layers / clipped_relu.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2020 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 // Definition of layer ClippedReLU of NNUE evaluation function
20
21 #ifndef NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED
22 #define NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED
23
24 #include "../nnue_common.h"
25
26 namespace Eval::NNUE::Layers {
27
28   // Clipped ReLU
29   template <typename PreviousLayer>
30   class ClippedReLU {
31    public:
32     // Input/output type
33     using InputType = typename PreviousLayer::OutputType;
34     using OutputType = std::uint8_t;
35     static_assert(std::is_same<InputType, std::int32_t>::value, "");
36
37     // Number of input/output dimensions
38     static constexpr IndexType kInputDimensions =
39         PreviousLayer::kOutputDimensions;
40     static constexpr IndexType kOutputDimensions = kInputDimensions;
41
42     // Size of forward propagation buffer used in this layer
43     static constexpr std::size_t kSelfBufferSize =
44         CeilToMultiple(kOutputDimensions * sizeof(OutputType), kCacheLineSize);
45
46     // Size of the forward propagation buffer used from the input layer to this layer
47     static constexpr std::size_t kBufferSize =
48         PreviousLayer::kBufferSize + kSelfBufferSize;
49
50     // Hash value embedded in the evaluation file
51     static constexpr std::uint32_t GetHashValue() {
52       std::uint32_t hash_value = 0x538D24C7u;
53       hash_value += PreviousLayer::GetHashValue();
54       return hash_value;
55     }
56
57     // Read network parameters
58     bool ReadParameters(std::istream& stream) {
59       return previous_layer_.ReadParameters(stream);
60     }
61
62     // Forward propagation
63     const OutputType* Propagate(
64         const TransformedFeatureType* transformed_features, char* buffer) const {
65       const auto input = previous_layer_.Propagate(
66           transformed_features, buffer + kSelfBufferSize);
67       const auto output = reinterpret_cast<OutputType*>(buffer);
68
69   #if defined(USE_AVX2)
70       constexpr IndexType kNumChunks = kInputDimensions / kSimdWidth;
71       const __m256i kZero = _mm256_setzero_si256();
72       const __m256i kOffsets = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
73       const auto in = reinterpret_cast<const __m256i*>(input);
74       const auto out = reinterpret_cast<__m256i*>(output);
75       for (IndexType i = 0; i < kNumChunks; ++i) {
76         const __m256i words0 = _mm256_srai_epi16(_mm256_packs_epi32(
77
78   #if defined(__MINGW32__) || defined(__MINGW64__)
79           // HACK: Use _mm256_loadu_si256() instead of _mm256_load_si256. Because the binary
80           //       compiled with g++ in MSYS2 crashes here because the output memory is not aligned
81           //       even though alignas is specified.
82           _mm256_loadu_si256
83   #else
84           _mm256_load_si256
85   #endif
86
87           (&in[i * 4 + 0]),
88
89   #if defined(__MINGW32__) || defined(__MINGW64__)
90           _mm256_loadu_si256
91   #else
92           _mm256_load_si256
93   #endif
94
95           (&in[i * 4 + 1])), kWeightScaleBits);
96         const __m256i words1 = _mm256_srai_epi16(_mm256_packs_epi32(
97
98   #if defined(__MINGW32__) || defined(__MINGW64__)
99           _mm256_loadu_si256
100   #else
101           _mm256_load_si256
102   #endif
103
104           (&in[i * 4 + 2]),
105
106   #if defined(__MINGW32__) || defined(__MINGW64__)
107           _mm256_loadu_si256
108   #else
109           _mm256_load_si256
110   #endif
111
112           (&in[i * 4 + 3])), kWeightScaleBits);
113
114   #if defined(__MINGW32__) || defined(__MINGW64__)
115         _mm256_storeu_si256
116   #else
117         _mm256_store_si256
118   #endif
119
120           (&out[i], _mm256_permutevar8x32_epi32(_mm256_max_epi8(
121             _mm256_packs_epi16(words0, words1), kZero), kOffsets));
122       }
123       constexpr IndexType kStart = kNumChunks * kSimdWidth;
124
125   #elif defined(USE_SSSE3)
126       constexpr IndexType kNumChunks = kInputDimensions / kSimdWidth;
127
128   #ifdef USE_SSE41
129       const __m128i kZero = _mm_setzero_si128();
130   #else
131       const __m128i k0x80s = _mm_set1_epi8(-128);
132   #endif
133
134       const auto in = reinterpret_cast<const __m128i*>(input);
135       const auto out = reinterpret_cast<__m128i*>(output);
136       for (IndexType i = 0; i < kNumChunks; ++i) {
137         const __m128i words0 = _mm_srai_epi16(_mm_packs_epi32(
138             _mm_load_si128(&in[i * 4 + 0]),
139             _mm_load_si128(&in[i * 4 + 1])), kWeightScaleBits);
140         const __m128i words1 = _mm_srai_epi16(_mm_packs_epi32(
141             _mm_load_si128(&in[i * 4 + 2]),
142             _mm_load_si128(&in[i * 4 + 3])), kWeightScaleBits);
143         const __m128i packedbytes = _mm_packs_epi16(words0, words1);
144         _mm_store_si128(&out[i],
145
146   #ifdef USE_SSE41
147           _mm_max_epi8(packedbytes, kZero)
148   #else
149           _mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s)
150   #endif
151
152         );
153       }
154       constexpr IndexType kStart = kNumChunks * kSimdWidth;
155
156   #elif defined(USE_NEON)
157       constexpr IndexType kNumChunks = kInputDimensions / (kSimdWidth / 2);
158       const int8x8_t kZero = {0};
159       const auto in = reinterpret_cast<const int32x4_t*>(input);
160       const auto out = reinterpret_cast<int8x8_t*>(output);
161       for (IndexType i = 0; i < kNumChunks; ++i) {
162         int16x8_t shifted;
163         const auto pack = reinterpret_cast<int16x4_t*>(&shifted);
164         pack[0] = vqshrn_n_s32(in[i * 2 + 0], kWeightScaleBits);
165         pack[1] = vqshrn_n_s32(in[i * 2 + 1], kWeightScaleBits);
166         out[i] = vmax_s8(vqmovn_s16(shifted), kZero);
167       }
168       constexpr IndexType kStart = kNumChunks * (kSimdWidth / 2);
169   #else
170       constexpr IndexType kStart = 0;
171   #endif
172
173       for (IndexType i = kStart; i < kInputDimensions; ++i) {
174         output[i] = static_cast<OutputType>(
175             std::max(0, std::min(127, input[i] >> kWeightScaleBits)));
176       }
177       return output;
178     }
179
180    private:
181     PreviousLayer previous_layer_;
182   };
183
184 }  // namespace Eval::NNUE::Layers
185
186 #endif // NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED