2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
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.
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.
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/>.
19 // Definition of layer ClippedReLU of NNUE evaluation function
21 #ifndef NNUE_LAYERS_SQR_CLIPPED_RELU_H_INCLUDED
22 #define NNUE_LAYERS_SQR_CLIPPED_RELU_H_INCLUDED
28 #include "../nnue_common.h"
30 namespace Stockfish::Eval::NNUE::Layers {
33 template<IndexType InDims>
34 class SqrClippedReLU {
37 using InputType = std::int32_t;
38 using OutputType = std::uint8_t;
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);
46 using OutputBuffer = OutputType[PaddedOutputDimensions];
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;
55 // Read network parameters
56 bool read_parameters(std::istream&) { return true; }
58 // Write network parameters
59 bool write_parameters(std::ostream&) const { return true; }
61 // Forward propagation
62 void propagate(const InputType* input, OutputType* output) const {
65 constexpr IndexType NumChunks = InputDimensions / 16;
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)
73 _mm_packs_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1]));
75 _mm_packs_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3]));
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);
83 _mm_store_si128(&out[i], _mm_packs_epi16(words0, words1));
85 constexpr IndexType Start = NumChunks * 16;
88 constexpr IndexType Start = 0;
91 for (IndexType i = Start; i < InputDimensions; ++i)
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)));
101 } // namespace Stockfish::Eval::NNUE::Layers
103 #endif // NNUE_LAYERS_SQR_CLIPPED_RELU_H_INCLUDED