]> git.sesse.net Git - stockfish/blob - src/nnue/layers/sqr_clipped_relu.h
5c1b9e6cd060d642f16de063c097c0b3da61e505
[stockfish] / src / nnue / layers / sqr_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_SQR_CLIPPED_RELU_H_INCLUDED
22 #define NNUE_LAYERS_SQR_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 SqrClippedReLU {
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     void propagate(
63         const InputType* input, OutputType* output) const {
64
65   #if defined(USE_SSE2)
66       constexpr IndexType NumChunks = InputDimensions / 16;
67
68       static_assert(WeightScaleBits == 6);
69       const auto in = reinterpret_cast<const __m128i*>(input);
70       const auto out = reinterpret_cast<__m128i*>(output);
71       for (IndexType i = 0; i < NumChunks; ++i) {
72         __m128i words0 = _mm_packs_epi32(
73             _mm_load_si128(&in[i * 4 + 0]),
74             _mm_load_si128(&in[i * 4 + 1]));
75         __m128i words1 = _mm_packs_epi32(
76             _mm_load_si128(&in[i * 4 + 2]),
77             _mm_load_si128(&in[i * 4 + 3]));
78
79         // We shift by WeightScaleBits * 2 = 12 and divide by 128
80         // which is an additional shift-right of 7, meaning 19 in total.
81         // MulHi strips the lower 16 bits so we need to shift out 3 more to match.
82         words0 = _mm_srli_epi16(_mm_mulhi_epi16(words0, words0), 3);
83         words1 = _mm_srli_epi16(_mm_mulhi_epi16(words1, words1), 3);
84
85         _mm_store_si128(&out[i], _mm_packs_epi16(words0, words1));
86       }
87       constexpr IndexType Start = NumChunks * 16;
88
89   #else
90       constexpr IndexType Start = 0;
91   #endif
92
93       for (IndexType i = Start; i < InputDimensions; ++i) {
94         output[i] = static_cast<OutputType>(
95             // really should be /127 but we need to make it fast
96             // needs to be accounted for in the trainer
97             std::min(127ll, (((long long)input[i] * input[i]) >> (2 * WeightScaleBits)) / 128));
98       }
99     }
100   };
101
102 }  // namespace Stockfish::Eval::NNUE::Layers
103
104 #endif // NNUE_LAYERS_SQR_CLIPPED_RELU_H_INCLUDED