Path: blob/master/src/nnue/layers/sqr_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_SQR_CLIPPED_RELU_H_INCLUDED21#define NNUE_LAYERS_SQR_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 SqrClippedReLU {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_SSE2)64constexpr IndexType NumChunks = InputDimensions / 16;6566static_assert(WeightScaleBits == 6);67const auto in = reinterpret_cast<const __m128i*>(input);68const auto out = reinterpret_cast<__m128i*>(output);69for (IndexType i = 0; i < NumChunks; ++i)70{71__m128i words0 =72_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1]));73__m128i words1 =74_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3]));7576// We shift by WeightScaleBits * 2 = 12 and divide by 12877// which is an additional shift-right of 7, meaning 19 in total.78// MulHi strips the lower 16 bits so we need to shift out 3 more to match.79words0 = _mm_srli_epi16(_mm_mulhi_epi16(words0, words0), 3);80words1 = _mm_srli_epi16(_mm_mulhi_epi16(words1, words1), 3);8182_mm_store_si128(&out[i], _mm_packs_epi16(words0, words1));83}84constexpr IndexType Start = NumChunks * 16;8586#else87constexpr IndexType Start = 0;88#endif8990for (IndexType i = Start; i < InputDimensions; ++i)91{92output[i] = static_cast<OutputType>(93// Really should be /127 but we need to make it fast so we right-shift94// by an extra 7 bits instead. Needs to be accounted for in the trainer.95std::min(127ll, ((long long) (input[i]) * input[i]) >> (2 * WeightScaleBits + 7)));96}97}98};99100} // namespace Stockfish::Eval::NNUE::Layers101102#endif // NNUE_LAYERS_SQR_CLIPPED_RELU_H_INCLUDED103104105