Path: blob/master/src/nnue/layers/clipped_relu.h
376 views
/*1Stockfish, a UCI chess playing engine derived from Glaurung 2.12Copyright (C) 2004-2025 The Stockfish developers (see AUTHORS file)34Stockfish is free software: you can redistribute it and/or modify5it under the terms of the GNU General Public License as published by6the Free Software Foundation, either version 3 of the License, or7(at your option) any later version.89Stockfish is distributed in the hope that it will be useful,10but WITHOUT ANY WARRANTY; without even the implied warranty of11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12GNU General Public License for more details.1314You should have received a copy of the GNU General Public License15along with this program. If not, see <http://www.gnu.org/licenses/>.16*/1718// Definition of layer ClippedReLU of NNUE evaluation function1920#ifndef NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED21#define NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED2223#include <algorithm>24#include <cstdint>25#include <iosfwd>2627#include "../nnue_common.h"2829namespace Stockfish::Eval::NNUE::Layers {3031// Clipped ReLU32template<IndexType InDims>33class ClippedReLU {34public:35// Input/output type36using InputType = std::int32_t;37using OutputType = std::uint8_t;3839// Number of input/output dimensions40static constexpr IndexType InputDimensions = InDims;41static constexpr IndexType OutputDimensions = InputDimensions;42static constexpr IndexType PaddedOutputDimensions =43ceil_to_multiple<IndexType>(OutputDimensions, 32);4445using OutputBuffer = OutputType[PaddedOutputDimensions];4647// Hash value embedded in the evaluation file48static constexpr std::uint32_t get_hash_value(std::uint32_t prevHash) {49std::uint32_t hashValue = 0x538D24C7u;50hashValue += prevHash;51return hashValue;52}5354// Read network parameters55bool read_parameters(std::istream&) { return true; }5657// Write network parameters58bool write_parameters(std::ostream&) const { return true; }5960// Forward propagation61void propagate(const InputType* input, OutputType* output) const {6263#if defined(USE_AVX2)64if constexpr (InputDimensions % SimdWidth == 0)65{66constexpr IndexType NumChunks = InputDimensions / SimdWidth;67const __m256i Offsets = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);68const auto in = reinterpret_cast<const __m256i*>(input);69const auto out = reinterpret_cast<__m256i*>(output);70for (IndexType i = 0; i < NumChunks; ++i)71{72const __m256i words0 =73_mm256_srli_epi16(_mm256_packus_epi32(_mm256_load_si256(&in[i * 4 + 0]),74_mm256_load_si256(&in[i * 4 + 1])),75WeightScaleBits);76const __m256i words1 =77_mm256_srli_epi16(_mm256_packus_epi32(_mm256_load_si256(&in[i * 4 + 2]),78_mm256_load_si256(&in[i * 4 + 3])),79WeightScaleBits);80_mm256_store_si256(&out[i], _mm256_permutevar8x32_epi32(81_mm256_packs_epi16(words0, words1), Offsets));82}83}84else85{86constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);87const auto in = reinterpret_cast<const __m128i*>(input);88const auto out = reinterpret_cast<__m128i*>(output);89for (IndexType i = 0; i < NumChunks; ++i)90{91const __m128i words0 = _mm_srli_epi16(92_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1])),93WeightScaleBits);94const __m128i words1 = _mm_srli_epi16(95_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3])),96WeightScaleBits);97_mm_store_si128(&out[i], _mm_packs_epi16(words0, words1));98}99}100constexpr IndexType Start = InputDimensions % SimdWidth == 0101? InputDimensions / SimdWidth * SimdWidth102: InputDimensions / (SimdWidth / 2) * (SimdWidth / 2);103104#elif defined(USE_SSE2)105constexpr IndexType NumChunks = InputDimensions / SimdWidth;106107#ifndef USE_SSE41108const __m128i k0x80s = _mm_set1_epi8(-128);109#endif110111const auto in = reinterpret_cast<const __m128i*>(input);112const auto out = reinterpret_cast<__m128i*>(output);113for (IndexType i = 0; i < NumChunks; ++i)114{115#if defined(USE_SSE41)116const __m128i words0 = _mm_srli_epi16(117_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1])),118WeightScaleBits);119const __m128i words1 = _mm_srli_epi16(120_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3])),121WeightScaleBits);122_mm_store_si128(&out[i], _mm_packs_epi16(words0, words1));123#else124const __m128i words0 = _mm_srai_epi16(125_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1])),126WeightScaleBits);127const __m128i words1 = _mm_srai_epi16(128_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3])),129WeightScaleBits);130const __m128i packedbytes = _mm_packs_epi16(words0, words1);131_mm_store_si128(&out[i], _mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s));132#endif133}134constexpr IndexType Start = NumChunks * SimdWidth;135136#elif defined(USE_NEON)137constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);138const int8x8_t Zero = {0};139const auto in = reinterpret_cast<const int32x4_t*>(input);140const auto out = reinterpret_cast<int8x8_t*>(output);141for (IndexType i = 0; i < NumChunks; ++i)142{143int16x8_t shifted;144const auto pack = reinterpret_cast<int16x4_t*>(&shifted);145pack[0] = vqshrn_n_s32(in[i * 2 + 0], WeightScaleBits);146pack[1] = vqshrn_n_s32(in[i * 2 + 1], WeightScaleBits);147out[i] = vmax_s8(vqmovn_s16(shifted), Zero);148}149constexpr IndexType Start = NumChunks * (SimdWidth / 2);150#else151constexpr IndexType Start = 0;152#endif153154for (IndexType i = Start; i < InputDimensions; ++i)155{156output[i] = static_cast<OutputType>(std::clamp(input[i] >> WeightScaleBits, 0, 127));157}158}159};160161} // namespace Stockfish::Eval::NNUE::Layers162163#endif // NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED164165166