]> git.sesse.net Git - stockfish/blob - src/nnue/layers/clipped_relu.h
65455df4944324a12870ca29d68c9ff5e0b379b1
[stockfish] / src / nnue / layers / clipped_relu.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2021 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 Stockfish::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 InputDimensions =
39         PreviousLayer::OutputDimensions;
40     static constexpr IndexType OutputDimensions = InputDimensions;
41
42     // Size of forward propagation buffer used in this layer
43     static constexpr std::size_t SelfBufferSize =
44         ceil_to_multiple(OutputDimensions * sizeof(OutputType), CacheLineSize);
45
46     // Size of the forward propagation buffer used from the input layer to this layer
47     static constexpr std::size_t BufferSize =
48         PreviousLayer::BufferSize + SelfBufferSize;
49
50     // Hash value embedded in the evaluation file
51     static constexpr std::uint32_t get_hash_value() {
52       std::uint32_t hashValue = 0x538D24C7u;
53       hashValue += PreviousLayer::get_hash_value();
54       return hashValue;
55     }
56
57     // Read network parameters
58     bool read_parameters(std::istream& stream) {
59       return previousLayer.read_parameters(stream);
60     }
61
62     // Write network parameters
63     bool write_parameters(std::ostream& stream) const {
64       return previousLayer.write_parameters(stream);
65     }
66
67     // Forward propagation
68     const OutputType* propagate(
69         const TransformedFeatureType* transformedFeatures, char* buffer) const {
70       const auto input = previousLayer.propagate(
71           transformedFeatures, buffer + SelfBufferSize);
72       const auto output = reinterpret_cast<OutputType*>(buffer);
73
74   #if defined(USE_AVX2)
75       if constexpr (InputDimensions % SimdWidth == 0) {
76         constexpr IndexType NumChunks = InputDimensions / SimdWidth;
77         const __m256i Zero = _mm256_setzero_si256();
78         const __m256i Offsets = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
79         const auto in = reinterpret_cast<const __m256i*>(input);
80         const auto out = reinterpret_cast<__m256i*>(output);
81         for (IndexType i = 0; i < NumChunks; ++i) {
82           const __m256i words0 = _mm256_srai_epi16(_mm256_packs_epi32(
83               _mm256_load_si256(&in[i * 4 + 0]),
84               _mm256_load_si256(&in[i * 4 + 1])), WeightScaleBits);
85           const __m256i words1 = _mm256_srai_epi16(_mm256_packs_epi32(
86               _mm256_load_si256(&in[i * 4 + 2]),
87               _mm256_load_si256(&in[i * 4 + 3])), WeightScaleBits);
88           _mm256_store_si256(&out[i], _mm256_permutevar8x32_epi32(_mm256_max_epi8(
89               _mm256_packs_epi16(words0, words1), Zero), Offsets));
90         }
91       } else {
92         constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);
93         const __m128i Zero = _mm_setzero_si128();
94         const auto in = reinterpret_cast<const __m128i*>(input);
95         const auto out = reinterpret_cast<__m128i*>(output);
96         for (IndexType i = 0; i < NumChunks; ++i) {
97           const __m128i words0 = _mm_srai_epi16(_mm_packs_epi32(
98               _mm_load_si128(&in[i * 4 + 0]),
99               _mm_load_si128(&in[i * 4 + 1])), WeightScaleBits);
100           const __m128i words1 = _mm_srai_epi16(_mm_packs_epi32(
101               _mm_load_si128(&in[i * 4 + 2]),
102               _mm_load_si128(&in[i * 4 + 3])), WeightScaleBits);
103           const __m128i packedbytes = _mm_packs_epi16(words0, words1);
104           _mm_store_si128(&out[i], _mm_max_epi8(packedbytes, Zero));
105         }
106       }
107       constexpr IndexType Start =
108         InputDimensions % SimdWidth == 0
109         ? InputDimensions / SimdWidth * SimdWidth
110         : InputDimensions / (SimdWidth / 2) * (SimdWidth / 2);
111
112   #elif defined(USE_SSE2)
113       constexpr IndexType NumChunks = InputDimensions / SimdWidth;
114
115   #ifdef USE_SSE41
116       const __m128i Zero = _mm_setzero_si128();
117   #else
118       const __m128i k0x80s = _mm_set1_epi8(-128);
119   #endif
120
121       const auto in = reinterpret_cast<const __m128i*>(input);
122       const auto out = reinterpret_cast<__m128i*>(output);
123       for (IndexType i = 0; i < NumChunks; ++i) {
124         const __m128i words0 = _mm_srai_epi16(_mm_packs_epi32(
125             _mm_load_si128(&in[i * 4 + 0]),
126             _mm_load_si128(&in[i * 4 + 1])), WeightScaleBits);
127         const __m128i words1 = _mm_srai_epi16(_mm_packs_epi32(
128             _mm_load_si128(&in[i * 4 + 2]),
129             _mm_load_si128(&in[i * 4 + 3])), WeightScaleBits);
130         const __m128i packedbytes = _mm_packs_epi16(words0, words1);
131         _mm_store_si128(&out[i],
132
133   #ifdef USE_SSE41
134           _mm_max_epi8(packedbytes, Zero)
135   #else
136           _mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s)
137   #endif
138
139         );
140       }
141       constexpr IndexType Start = NumChunks * SimdWidth;
142
143   #elif defined(USE_MMX)
144       constexpr IndexType NumChunks = InputDimensions / SimdWidth;
145       const __m64 k0x80s = _mm_set1_pi8(-128);
146       const auto in = reinterpret_cast<const __m64*>(input);
147       const auto out = reinterpret_cast<__m64*>(output);
148       for (IndexType i = 0; i < NumChunks; ++i) {
149         const __m64 words0 = _mm_srai_pi16(
150             _mm_packs_pi32(in[i * 4 + 0], in[i * 4 + 1]),
151             WeightScaleBits);
152         const __m64 words1 = _mm_srai_pi16(
153             _mm_packs_pi32(in[i * 4 + 2], in[i * 4 + 3]),
154             WeightScaleBits);
155         const __m64 packedbytes = _mm_packs_pi16(words0, words1);
156         out[i] = _mm_subs_pi8(_mm_adds_pi8(packedbytes, k0x80s), k0x80s);
157       }
158       _mm_empty();
159       constexpr IndexType Start = NumChunks * SimdWidth;
160
161   #elif defined(USE_NEON)
162       constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);
163       const int8x8_t Zero = {0};
164       const auto in = reinterpret_cast<const int32x4_t*>(input);
165       const auto out = reinterpret_cast<int8x8_t*>(output);
166       for (IndexType i = 0; i < NumChunks; ++i) {
167         int16x8_t shifted;
168         const auto pack = reinterpret_cast<int16x4_t*>(&shifted);
169         pack[0] = vqshrn_n_s32(in[i * 2 + 0], WeightScaleBits);
170         pack[1] = vqshrn_n_s32(in[i * 2 + 1], WeightScaleBits);
171         out[i] = vmax_s8(vqmovn_s16(shifted), Zero);
172       }
173       constexpr IndexType Start = NumChunks * (SimdWidth / 2);
174   #else
175       constexpr IndexType Start = 0;
176   #endif
177
178       for (IndexType i = Start; i < InputDimensions; ++i) {
179         output[i] = static_cast<OutputType>(
180             std::max(0, std::min(127, input[i] >> WeightScaleBits)));
181       }
182       return output;
183     }
184
185    private:
186     PreviousLayer previousLayer;
187   };
188
189 }  // namespace Stockfish::Eval::NNUE::Layers
190
191 #endif // NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED