]> git.sesse.net Git - stockfish/blob - src/nnue/layers/clipped_relu.h
Merge remote-tracking branch 'upstream/master'
[stockfish] / src / nnue / layers / clipped_relu.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 // 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 <IndexType InDims>
30   class ClippedReLU {
31    public:
32     // Input/output type
33     using InputType = std::int32_t;
34     using OutputType = std::uint8_t;
35
36     // Number of input/output dimensions
37     static constexpr IndexType InputDimensions = InDims;
38     static constexpr IndexType OutputDimensions = InputDimensions;
39     static constexpr IndexType PaddedOutputDimensions =
40         ceil_to_multiple<IndexType>(OutputDimensions, 32);
41
42     using OutputBuffer = OutputType[PaddedOutputDimensions];
43
44     // Hash value embedded in the evaluation file
45     static constexpr std::uint32_t get_hash_value(std::uint32_t prevHash) {
46       std::uint32_t hashValue = 0x538D24C7u;
47       hashValue += prevHash;
48       return hashValue;
49     }
50
51     // Read network parameters
52     bool read_parameters(std::istream&) {
53       return true;
54     }
55
56     // Write network parameters
57     bool write_parameters(std::ostream&) const {
58       return true;
59     }
60
61     // Forward propagation
62     const OutputType* propagate(
63         const InputType* input, OutputType* output) const {
64
65   #if defined(USE_AVX2)
66       if constexpr (InputDimensions % SimdWidth == 0) {
67         constexpr IndexType NumChunks = InputDimensions / SimdWidth;
68         const __m256i Zero = _mm256_setzero_si256();
69         const __m256i Offsets = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
70         const auto in = reinterpret_cast<const __m256i*>(input);
71         const auto out = reinterpret_cast<__m256i*>(output);
72         for (IndexType i = 0; i < NumChunks; ++i) {
73           const __m256i words0 = _mm256_srai_epi16(_mm256_packs_epi32(
74               _mm256_load_si256(&in[i * 4 + 0]),
75               _mm256_load_si256(&in[i * 4 + 1])), WeightScaleBits);
76           const __m256i words1 = _mm256_srai_epi16(_mm256_packs_epi32(
77               _mm256_load_si256(&in[i * 4 + 2]),
78               _mm256_load_si256(&in[i * 4 + 3])), WeightScaleBits);
79           _mm256_store_si256(&out[i], _mm256_permutevar8x32_epi32(_mm256_max_epi8(
80               _mm256_packs_epi16(words0, words1), Zero), Offsets));
81         }
82       } else {
83         constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);
84         const __m128i Zero = _mm_setzero_si128();
85         const auto in = reinterpret_cast<const __m128i*>(input);
86         const auto out = reinterpret_cast<__m128i*>(output);
87         for (IndexType i = 0; i < NumChunks; ++i) {
88           const __m128i words0 = _mm_srai_epi16(_mm_packs_epi32(
89               _mm_load_si128(&in[i * 4 + 0]),
90               _mm_load_si128(&in[i * 4 + 1])), WeightScaleBits);
91           const __m128i words1 = _mm_srai_epi16(_mm_packs_epi32(
92               _mm_load_si128(&in[i * 4 + 2]),
93               _mm_load_si128(&in[i * 4 + 3])), WeightScaleBits);
94           const __m128i packedbytes = _mm_packs_epi16(words0, words1);
95           _mm_store_si128(&out[i], _mm_max_epi8(packedbytes, Zero));
96         }
97       }
98       constexpr IndexType Start =
99         InputDimensions % SimdWidth == 0
100         ? InputDimensions / SimdWidth * SimdWidth
101         : InputDimensions / (SimdWidth / 2) * (SimdWidth / 2);
102
103   #elif defined(USE_SSE2)
104       constexpr IndexType NumChunks = InputDimensions / SimdWidth;
105
106   #ifdef USE_SSE41
107       const __m128i Zero = _mm_setzero_si128();
108   #else
109       const __m128i k0x80s = _mm_set1_epi8(-128);
110   #endif
111
112       const auto in = reinterpret_cast<const __m128i*>(input);
113       const auto out = reinterpret_cast<__m128i*>(output);
114       for (IndexType i = 0; i < NumChunks; ++i) {
115         const __m128i words0 = _mm_srai_epi16(_mm_packs_epi32(
116             _mm_load_si128(&in[i * 4 + 0]),
117             _mm_load_si128(&in[i * 4 + 1])), WeightScaleBits);
118         const __m128i words1 = _mm_srai_epi16(_mm_packs_epi32(
119             _mm_load_si128(&in[i * 4 + 2]),
120             _mm_load_si128(&in[i * 4 + 3])), WeightScaleBits);
121         const __m128i packedbytes = _mm_packs_epi16(words0, words1);
122         _mm_store_si128(&out[i],
123
124   #ifdef USE_SSE41
125           _mm_max_epi8(packedbytes, Zero)
126   #else
127           _mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s)
128   #endif
129
130         );
131       }
132       constexpr IndexType Start = NumChunks * SimdWidth;
133
134   #elif defined(USE_MMX)
135       constexpr IndexType NumChunks = InputDimensions / SimdWidth;
136       const __m64 k0x80s = _mm_set1_pi8(-128);
137       const auto in = reinterpret_cast<const __m64*>(input);
138       const auto out = reinterpret_cast<__m64*>(output);
139       for (IndexType i = 0; i < NumChunks; ++i) {
140         const __m64 words0 = _mm_srai_pi16(
141             _mm_packs_pi32(in[i * 4 + 0], in[i * 4 + 1]),
142             WeightScaleBits);
143         const __m64 words1 = _mm_srai_pi16(
144             _mm_packs_pi32(in[i * 4 + 2], in[i * 4 + 3]),
145             WeightScaleBits);
146         const __m64 packedbytes = _mm_packs_pi16(words0, words1);
147         out[i] = _mm_subs_pi8(_mm_adds_pi8(packedbytes, k0x80s), k0x80s);
148       }
149       _mm_empty();
150       constexpr IndexType Start = NumChunks * SimdWidth;
151
152   #elif defined(USE_NEON)
153       constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);
154       const int8x8_t Zero = {0};
155       const auto in = reinterpret_cast<const int32x4_t*>(input);
156       const auto out = reinterpret_cast<int8x8_t*>(output);
157       for (IndexType i = 0; i < NumChunks; ++i) {
158         int16x8_t shifted;
159         const auto pack = reinterpret_cast<int16x4_t*>(&shifted);
160         pack[0] = vqshrn_n_s32(in[i * 2 + 0], WeightScaleBits);
161         pack[1] = vqshrn_n_s32(in[i * 2 + 1], WeightScaleBits);
162         out[i] = vmax_s8(vqmovn_s16(shifted), Zero);
163       }
164       constexpr IndexType Start = NumChunks * (SimdWidth / 2);
165   #else
166       constexpr IndexType Start = 0;
167   #endif
168
169       for (IndexType i = Start; i < InputDimensions; ++i) {
170         output[i] = static_cast<OutputType>(
171             std::max(0, std::min(127, input[i] >> WeightScaleBits)));
172       }
173
174       return output;
175     }
176   };
177
178 }  // namespace Stockfish::Eval::NNUE::Layers
179
180 #endif // NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED