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