Path: blob/master/src/nnue/layers/sqr_clipped_relu.h
653 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_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; }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_SSE2)70constexpr IndexType NumChunks = InputDimensions / 16;7172static_assert(WeightScaleBits == 6);73const auto in = reinterpret_cast<const __m128i*>(input);74const auto out = reinterpret_cast<__m128i*>(output);75for (IndexType i = 0; i < NumChunks; ++i)76{77__m128i words0 =78_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1]));79__m128i words1 =80_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3]));8182// We shift by WeightScaleBits * 2 = 12 and divide by 12883// which is an additional shift-right of 7, meaning 19 in total.84// MulHi strips the lower 16 bits so we need to shift out 3 more to match.85words0 = _mm_srli_epi16(_mm_mulhi_epi16(words0, words0), 3);86words1 = _mm_srli_epi16(_mm_mulhi_epi16(words1, words1), 3);8788_mm_store_si128(&out[i], _mm_packs_epi16(words0, words1));89}90constexpr IndexType Start = NumChunks * 16;9192#else93constexpr IndexType Start = 0;94#endif9596for (IndexType i = Start; i < InputDimensions; ++i)97{98output[i] = static_cast<OutputType>(99// Really should be /127 but we need to make it fast so we right-shift100// by an extra 7 bits instead. Needs to be accounted for in the trainer.101std::min(127ll, ((long long) (input[i]) * input[i]) >> (2 * WeightScaleBits + 7)));102}103}104};105106} // namespace Stockfish::Eval::NNUE::Layers107108#endif // NNUE_LAYERS_SQR_CLIPPED_RELU_H_INCLUDED109110111