Path: blob/master/src/nnue/layers/clipped_relu.h
648 views
/*1Stockfish, a UCI chess playing engine derived from Glaurung 2.12Copyright (C) 2004-2026 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; }5960std::size_t get_content_hash() const {61std::size_t h = 0;62hash_combine(h, get_hash_value(0));63return h;64}6566// Forward propagation67void propagate(const InputType* input, OutputType* output) const {6869#if defined(USE_AVX2)70if constexpr (InputDimensions % SimdWidth == 0)71{72constexpr IndexType NumChunks = InputDimensions / SimdWidth;73const __m256i Offsets = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);74const auto in = reinterpret_cast<const __m256i*>(input);75const auto out = reinterpret_cast<__m256i*>(output);76for (IndexType i = 0; i < NumChunks; ++i)77{78const __m256i words0 =79_mm256_srli_epi16(_mm256_packus_epi32(_mm256_load_si256(&in[i * 4 + 0]),80_mm256_load_si256(&in[i * 4 + 1])),81WeightScaleBits);82const __m256i words1 =83_mm256_srli_epi16(_mm256_packus_epi32(_mm256_load_si256(&in[i * 4 + 2]),84_mm256_load_si256(&in[i * 4 + 3])),85WeightScaleBits);86_mm256_store_si256(&out[i], _mm256_permutevar8x32_epi32(87_mm256_packs_epi16(words0, words1), Offsets));88}89}90else91{92constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);93const auto in = reinterpret_cast<const __m128i*>(input);94const auto out = reinterpret_cast<__m128i*>(output);95for (IndexType i = 0; i < NumChunks; ++i)96{97const __m128i words0 = _mm_srli_epi16(98_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1])),99WeightScaleBits);100const __m128i words1 = _mm_srli_epi16(101_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3])),102WeightScaleBits);103_mm_store_si128(&out[i], _mm_packs_epi16(words0, words1));104}105}106constexpr IndexType Start = InputDimensions % SimdWidth == 0107? InputDimensions / SimdWidth * SimdWidth108: InputDimensions / (SimdWidth / 2) * (SimdWidth / 2);109110#elif defined(USE_SSE2)111constexpr IndexType NumChunks = InputDimensions / SimdWidth;112113#ifndef USE_SSE41114const __m128i k0x80s = _mm_set1_epi8(-128);115#endif116117const auto in = reinterpret_cast<const __m128i*>(input);118const auto out = reinterpret_cast<__m128i*>(output);119for (IndexType i = 0; i < NumChunks; ++i)120{121#if defined(USE_SSE41)122const __m128i words0 = _mm_srli_epi16(123_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1])),124WeightScaleBits);125const __m128i words1 = _mm_srli_epi16(126_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3])),127WeightScaleBits);128_mm_store_si128(&out[i], _mm_packs_epi16(words0, words1));129#else130const __m128i words0 = _mm_srai_epi16(131_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1])),132WeightScaleBits);133const __m128i words1 = _mm_srai_epi16(134_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3])),135WeightScaleBits);136const __m128i packedbytes = _mm_packs_epi16(words0, words1);137_mm_store_si128(&out[i], _mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s));138#endif139}140constexpr IndexType Start = NumChunks * SimdWidth;141142#elif defined(USE_NEON)143constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);144const SIMD::vec_i8x8_t Zero = {0};145const auto in = reinterpret_cast<const SIMD::vec_i32x4_t*>(input);146const auto out = reinterpret_cast<SIMD::vec_i8x8_t*>(output);147for (IndexType i = 0; i < NumChunks; ++i)148{149int16x8_t shifted;150const auto pack = reinterpret_cast<int16x4_t*>(&shifted);151pack[0] = vqshrn_n_s32(in[i * 2 + 0], WeightScaleBits);152pack[1] = vqshrn_n_s32(in[i * 2 + 1], WeightScaleBits);153out[i] = vmax_s8(vqmovn_s16(shifted), Zero);154}155constexpr IndexType Start = NumChunks * (SimdWidth / 2);156#else157constexpr IndexType Start = 0;158#endif159160for (IndexType i = Start; i < InputDimensions; ++i)161{162output[i] = static_cast<OutputType>(std::clamp(input[i] >> WeightScaleBits, 0, 127));163}164}165};166167} // namespace Stockfish::Eval::NNUE::Layers168169#endif // NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED170171172