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