]> git.sesse.net Git - stockfish/blob - src/nnue/layers/sqr_clipped_relu.h
Cleanup includes
[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 <algorithm>
25 #include <cstdint>
26 #include <iosfwd>
27
28 #include "../nnue_common.h"
29
30 namespace Stockfish::Eval::NNUE::Layers {
31
32   // Clipped ReLU
33   template <IndexType InDims>
34   class SqrClippedReLU {
35    public:
36     // Input/output type
37     using InputType = std::int32_t;
38     using OutputType = std::uint8_t;
39
40     // Number of input/output dimensions
41     static constexpr IndexType InputDimensions = InDims;
42     static constexpr IndexType OutputDimensions = InputDimensions;
43     static constexpr IndexType PaddedOutputDimensions =
44         ceil_to_multiple<IndexType>(OutputDimensions, 32);
45
46     using OutputBuffer = OutputType[PaddedOutputDimensions];
47
48     // Hash value embedded in the evaluation file
49     static constexpr std::uint32_t get_hash_value(std::uint32_t prevHash) {
50       std::uint32_t hashValue = 0x538D24C7u;
51       hashValue += prevHash;
52       return hashValue;
53     }
54
55     // Read network parameters
56     bool read_parameters(std::istream&) {
57       return true;
58     }
59
60     // Write network parameters
61     bool write_parameters(std::ostream&) const {
62       return true;
63     }
64
65     // Forward propagation
66     void propagate(
67         const InputType* input, OutputType* output) const {
68
69   #if defined(USE_SSE2)
70       constexpr IndexType NumChunks = InputDimensions / 16;
71
72       static_assert(WeightScaleBits == 6);
73       const auto in = reinterpret_cast<const __m128i*>(input);
74       const auto out = reinterpret_cast<__m128i*>(output);
75       for (IndexType i = 0; i < NumChunks; ++i) {
76         __m128i words0 = _mm_packs_epi32(
77             _mm_load_si128(&in[i * 4 + 0]),
78             _mm_load_si128(&in[i * 4 + 1]));
79         __m128i words1 = _mm_packs_epi32(
80             _mm_load_si128(&in[i * 4 + 2]),
81             _mm_load_si128(&in[i * 4 + 3]));
82
83         // We shift by WeightScaleBits * 2 = 12 and divide by 128
84         // which is an additional shift-right of 7, meaning 19 in total.
85         // MulHi strips the lower 16 bits so we need to shift out 3 more to match.
86         words0 = _mm_srli_epi16(_mm_mulhi_epi16(words0, words0), 3);
87         words1 = _mm_srli_epi16(_mm_mulhi_epi16(words1, words1), 3);
88
89         _mm_store_si128(&out[i], _mm_packs_epi16(words0, words1));
90       }
91       constexpr IndexType Start = NumChunks * 16;
92
93   #else
94       constexpr IndexType Start = 0;
95   #endif
96
97       for (IndexType i = Start; i < InputDimensions; ++i) {
98         output[i] = static_cast<OutputType>(
99             // really should be /127 but we need to make it fast
100             // needs to be accounted for in the trainer
101             std::min(127ll, (((long long)input[i] * input[i]) >> (2 * WeightScaleBits)) / 128));
102       }
103     }
104   };
105
106 }  // namespace Stockfish::Eval::NNUE::Layers
107
108 #endif // NNUE_LAYERS_SQR_CLIPPED_RELU_H_INCLUDED