2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2022 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
24 #include "../nnue_common.h"
26 namespace Stockfish::Eval::NNUE::Layers {
29 template <IndexType InDims>
30 class SqrClippedReLU {
33 using InputType = std::int32_t;
34 using OutputType = std::uint8_t;
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);
42 using OutputBuffer = OutputType[PaddedOutputDimensions];
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;
51 // Read network parameters
52 bool read_parameters(std::istream&) {
56 // Write network parameters
57 bool write_parameters(std::ostream&) const {
61 // Forward propagation
62 const OutputType* propagate(
63 const InputType* input, OutputType* output) const {
66 constexpr IndexType NumChunks = InputDimensions / 16;
69 const __m128i Zero = _mm_setzero_si128();
71 const __m128i k0x80s = _mm_set1_epi8(-128);
74 static_assert(WeightScaleBits == 6);
75 const auto in = reinterpret_cast<const __m128i*>(input);
76 const auto out = reinterpret_cast<__m128i*>(output);
77 for (IndexType i = 0; i < NumChunks; ++i) {
78 __m128i words0 = _mm_packs_epi32(
79 _mm_load_si128(&in[i * 4 + 0]),
80 _mm_load_si128(&in[i * 4 + 1]));
81 __m128i words1 = _mm_packs_epi32(
82 _mm_load_si128(&in[i * 4 + 2]),
83 _mm_load_si128(&in[i * 4 + 3]));
86 words0 = _mm_srli_epi16(_mm_mulhi_epi16(words0, words0), 3);
87 words1 = _mm_srli_epi16(_mm_mulhi_epi16(words1, words1), 3);
89 const __m128i packedbytes = _mm_packs_epi16(words0, words1);
91 _mm_store_si128(&out[i],
94 _mm_max_epi8(packedbytes, Zero)
96 _mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s)
101 constexpr IndexType Start = NumChunks * 16;
104 constexpr IndexType Start = 0;
107 for (IndexType i = Start; i < InputDimensions; ++i) {
108 output[i] = static_cast<OutputType>(
109 // realy should be /127 but we need to make it fast
110 // needs to be accounted for in the trainer
111 std::max(0ll, std::min(127ll, (((long long)input[i] * input[i]) >> (2 * WeightScaleBits)) / 128)));
118 } // namespace Stockfish::Eval::NNUE::Layers
120 #endif // NNUE_LAYERS_SQR_CLIPPED_RELU_H_INCLUDED