Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/nnue/layers/clipped_relu.h
648 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_CLIPPED_RELU_H_INCLUDED
22
#define NNUE_LAYERS_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 ClippedReLU {
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_AVX2)
71
if constexpr (InputDimensions % SimdWidth == 0)
72
{
73
constexpr IndexType NumChunks = InputDimensions / SimdWidth;
74
const __m256i Offsets = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
75
const auto in = reinterpret_cast<const __m256i*>(input);
76
const auto out = reinterpret_cast<__m256i*>(output);
77
for (IndexType i = 0; i < NumChunks; ++i)
78
{
79
const __m256i words0 =
80
_mm256_srli_epi16(_mm256_packus_epi32(_mm256_load_si256(&in[i * 4 + 0]),
81
_mm256_load_si256(&in[i * 4 + 1])),
82
WeightScaleBits);
83
const __m256i words1 =
84
_mm256_srli_epi16(_mm256_packus_epi32(_mm256_load_si256(&in[i * 4 + 2]),
85
_mm256_load_si256(&in[i * 4 + 3])),
86
WeightScaleBits);
87
_mm256_store_si256(&out[i], _mm256_permutevar8x32_epi32(
88
_mm256_packs_epi16(words0, words1), Offsets));
89
}
90
}
91
else
92
{
93
constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);
94
const auto in = reinterpret_cast<const __m128i*>(input);
95
const auto out = reinterpret_cast<__m128i*>(output);
96
for (IndexType i = 0; i < NumChunks; ++i)
97
{
98
const __m128i words0 = _mm_srli_epi16(
99
_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1])),
100
WeightScaleBits);
101
const __m128i words1 = _mm_srli_epi16(
102
_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3])),
103
WeightScaleBits);
104
_mm_store_si128(&out[i], _mm_packs_epi16(words0, words1));
105
}
106
}
107
constexpr IndexType Start = InputDimensions % SimdWidth == 0
108
? InputDimensions / SimdWidth * SimdWidth
109
: InputDimensions / (SimdWidth / 2) * (SimdWidth / 2);
110
111
#elif defined(USE_SSE2)
112
constexpr IndexType NumChunks = InputDimensions / SimdWidth;
113
114
#ifndef USE_SSE41
115
const __m128i k0x80s = _mm_set1_epi8(-128);
116
#endif
117
118
const auto in = reinterpret_cast<const __m128i*>(input);
119
const auto out = reinterpret_cast<__m128i*>(output);
120
for (IndexType i = 0; i < NumChunks; ++i)
121
{
122
#if defined(USE_SSE41)
123
const __m128i words0 = _mm_srli_epi16(
124
_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1])),
125
WeightScaleBits);
126
const __m128i words1 = _mm_srli_epi16(
127
_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3])),
128
WeightScaleBits);
129
_mm_store_si128(&out[i], _mm_packs_epi16(words0, words1));
130
#else
131
const __m128i words0 = _mm_srai_epi16(
132
_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1])),
133
WeightScaleBits);
134
const __m128i words1 = _mm_srai_epi16(
135
_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3])),
136
WeightScaleBits);
137
const __m128i packedbytes = _mm_packs_epi16(words0, words1);
138
_mm_store_si128(&out[i], _mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s));
139
#endif
140
}
141
constexpr IndexType Start = NumChunks * SimdWidth;
142
143
#elif defined(USE_NEON)
144
constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);
145
const SIMD::vec_i8x8_t Zero = {0};
146
const auto in = reinterpret_cast<const SIMD::vec_i32x4_t*>(input);
147
const auto out = reinterpret_cast<SIMD::vec_i8x8_t*>(output);
148
for (IndexType i = 0; i < NumChunks; ++i)
149
{
150
int16x8_t shifted;
151
const auto pack = reinterpret_cast<int16x4_t*>(&shifted);
152
pack[0] = vqshrn_n_s32(in[i * 2 + 0], WeightScaleBits);
153
pack[1] = vqshrn_n_s32(in[i * 2 + 1], WeightScaleBits);
154
out[i] = vmax_s8(vqmovn_s16(shifted), Zero);
155
}
156
constexpr IndexType Start = NumChunks * (SimdWidth / 2);
157
#else
158
constexpr IndexType Start = 0;
159
#endif
160
161
for (IndexType i = Start; i < InputDimensions; ++i)
162
{
163
output[i] = static_cast<OutputType>(std::clamp(input[i] >> WeightScaleBits, 0, 127));
164
}
165
}
166
};
167
168
} // namespace Stockfish::Eval::NNUE::Layers
169
170
#endif // NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED
171
172