Path: blob/master/src/nnue/nnue_architecture.h
375 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// Input features and network structure used in NNUE evaluation function1920#ifndef NNUE_ARCHITECTURE_H_INCLUDED21#define NNUE_ARCHITECTURE_H_INCLUDED2223#include <cstdint>24#include <cstring>25#include <iosfwd>2627#include "features/half_ka_v2_hm.h"28#include "layers/affine_transform.h"29#include "layers/affine_transform_sparse_input.h"30#include "layers/clipped_relu.h"31#include "layers/sqr_clipped_relu.h"32#include "nnue_common.h"3334namespace Stockfish::Eval::NNUE {3536// Input features used in evaluation function37using FeatureSet = Features::HalfKAv2_hm;3839// Number of input feature dimensions after conversion40constexpr IndexType TransformedFeatureDimensionsBig = 3072;41constexpr int L2Big = 15;42constexpr int L3Big = 32;4344constexpr IndexType TransformedFeatureDimensionsSmall = 128;45constexpr int L2Small = 15;46constexpr int L3Small = 32;4748constexpr IndexType PSQTBuckets = 8;49constexpr IndexType LayerStacks = 8;5051// If vector instructions are enabled, we update and refresh the52// accumulator tile by tile such that each tile fits in the CPU's53// vector registers.54static_assert(PSQTBuckets % 8 == 0,55"Per feature PSQT values cannot be processed at granularity lower than 8 at a time.");5657template<IndexType L1, int L2, int L3>58struct NetworkArchitecture {59static constexpr IndexType TransformedFeatureDimensions = L1;60static constexpr int FC_0_OUTPUTS = L2;61static constexpr int FC_1_OUTPUTS = L3;6263Layers::AffineTransformSparseInput<TransformedFeatureDimensions, FC_0_OUTPUTS + 1> fc_0;64Layers::SqrClippedReLU<FC_0_OUTPUTS + 1> ac_sqr_0;65Layers::ClippedReLU<FC_0_OUTPUTS + 1> ac_0;66Layers::AffineTransform<FC_0_OUTPUTS * 2, FC_1_OUTPUTS> fc_1;67Layers::ClippedReLU<FC_1_OUTPUTS> ac_1;68Layers::AffineTransform<FC_1_OUTPUTS, 1> fc_2;6970// Hash value embedded in the evaluation file71static constexpr std::uint32_t get_hash_value() {72// input slice hash73std::uint32_t hashValue = 0xEC42E90Du;74hashValue ^= TransformedFeatureDimensions * 2;7576hashValue = decltype(fc_0)::get_hash_value(hashValue);77hashValue = decltype(ac_0)::get_hash_value(hashValue);78hashValue = decltype(fc_1)::get_hash_value(hashValue);79hashValue = decltype(ac_1)::get_hash_value(hashValue);80hashValue = decltype(fc_2)::get_hash_value(hashValue);8182return hashValue;83}8485// Read network parameters86bool read_parameters(std::istream& stream) {87return fc_0.read_parameters(stream) && ac_0.read_parameters(stream)88&& fc_1.read_parameters(stream) && ac_1.read_parameters(stream)89&& fc_2.read_parameters(stream);90}9192// Write network parameters93bool write_parameters(std::ostream& stream) const {94return fc_0.write_parameters(stream) && ac_0.write_parameters(stream)95&& fc_1.write_parameters(stream) && ac_1.write_parameters(stream)96&& fc_2.write_parameters(stream);97}9899std::int32_t propagate(const TransformedFeatureType* transformedFeatures) {100struct alignas(CacheLineSize) Buffer {101alignas(CacheLineSize) typename decltype(fc_0)::OutputBuffer fc_0_out;102alignas(CacheLineSize) typename decltype(ac_sqr_0)::OutputType103ac_sqr_0_out[ceil_to_multiple<IndexType>(FC_0_OUTPUTS * 2, 32)];104alignas(CacheLineSize) typename decltype(ac_0)::OutputBuffer ac_0_out;105alignas(CacheLineSize) typename decltype(fc_1)::OutputBuffer fc_1_out;106alignas(CacheLineSize) typename decltype(ac_1)::OutputBuffer ac_1_out;107alignas(CacheLineSize) typename decltype(fc_2)::OutputBuffer fc_2_out;108109Buffer() { std::memset(this, 0, sizeof(*this)); }110};111112#if defined(__clang__) && (__APPLE__)113// workaround for a bug reported with xcode 12114static thread_local auto tlsBuffer = std::make_unique<Buffer>();115// Access TLS only once, cache result.116Buffer& buffer = *tlsBuffer;117#else118alignas(CacheLineSize) static thread_local Buffer buffer;119#endif120121fc_0.propagate(transformedFeatures, buffer.fc_0_out);122ac_sqr_0.propagate(buffer.fc_0_out, buffer.ac_sqr_0_out);123ac_0.propagate(buffer.fc_0_out, buffer.ac_0_out);124std::memcpy(buffer.ac_sqr_0_out + FC_0_OUTPUTS, buffer.ac_0_out,125FC_0_OUTPUTS * sizeof(typename decltype(ac_0)::OutputType));126fc_1.propagate(buffer.ac_sqr_0_out, buffer.fc_1_out);127ac_1.propagate(buffer.fc_1_out, buffer.ac_1_out);128fc_2.propagate(buffer.ac_1_out, buffer.fc_2_out);129130// buffer.fc_0_out[FC_0_OUTPUTS] is such that 1.0 is equal to 127*(1<<WeightScaleBits) in131// quantized form, but we want 1.0 to be equal to 600*OutputScale132std::int32_t fwdOut =133(buffer.fc_0_out[FC_0_OUTPUTS]) * (600 * OutputScale) / (127 * (1 << WeightScaleBits));134std::int32_t outputValue = buffer.fc_2_out[0] + fwdOut;135136return outputValue;137}138};139140} // namespace Stockfish::Eval::NNUE141142#endif // #ifndef NNUE_ARCHITECTURE_H_INCLUDED143144145