Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/nnue/layers/sqr_clipped_relu.h
653 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2026 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
std::size_t get_content_hash() const {
62
std::size_t h = 0;
63
hash_combine(h, get_hash_value(0));
64
return h;
65
}
66
67
// Forward propagation
68
void propagate(const InputType* input, OutputType* output) const {
69
70
#if defined(USE_SSE2)
71
constexpr IndexType NumChunks = InputDimensions / 16;
72
73
static_assert(WeightScaleBits == 6);
74
const auto in = reinterpret_cast<const __m128i*>(input);
75
const auto out = reinterpret_cast<__m128i*>(output);
76
for (IndexType i = 0; i < NumChunks; ++i)
77
{
78
__m128i words0 =
79
_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1]));
80
__m128i words1 =
81
_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 2]), _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
{
99
output[i] = static_cast<OutputType>(
100
// Really should be /127 but we need to make it fast so we right-shift
101
// by an extra 7 bits instead. Needs to be accounted for in the trainer.
102
std::min(127ll, ((long long) (input[i]) * input[i]) >> (2 * WeightScaleBits + 7)));
103
}
104
}
105
};
106
107
} // namespace Stockfish::Eval::NNUE::Layers
108
109
#endif // NNUE_LAYERS_SQR_CLIPPED_RELU_H_INCLUDED
110
111