Path: blob/master/src/nnue/nnue_architecture.h
647 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// 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 "features/full_threats.h"29#include "layers/affine_transform.h"30#include "layers/affine_transform_sparse_input.h"31#include "layers/clipped_relu.h"32#include "layers/sqr_clipped_relu.h"33#include "nnue_common.h"3435namespace Stockfish::Eval::NNUE {3637// Input features used in evaluation function38using ThreatFeatureSet = Features::FullThreats;39using PSQFeatureSet = Features::HalfKAv2_hm;4041// Number of input feature dimensions after conversion42constexpr IndexType TransformedFeatureDimensionsBig = 1024;43constexpr int L2Big = 15;44constexpr int L3Big = 32;4546constexpr IndexType TransformedFeatureDimensionsSmall = 128;47constexpr int L2Small = 15;48constexpr int L3Small = 32;4950constexpr IndexType PSQTBuckets = 8;51constexpr IndexType LayerStacks = 8;5253// If vector instructions are enabled, we update and refresh the54// accumulator tile by tile such that each tile fits in the CPU's55// vector registers.56static_assert(PSQTBuckets % 8 == 0,57"Per feature PSQT values cannot be processed at granularity lower than 8 at a time.");5859template<IndexType L1, int L2, int L3>60struct NetworkArchitecture {61static constexpr IndexType TransformedFeatureDimensions = L1;62static constexpr int FC_0_OUTPUTS = L2;63static constexpr int FC_1_OUTPUTS = L3;6465Layers::AffineTransformSparseInput<TransformedFeatureDimensions, FC_0_OUTPUTS + 1> fc_0;66Layers::SqrClippedReLU<FC_0_OUTPUTS + 1> ac_sqr_0;67Layers::ClippedReLU<FC_0_OUTPUTS + 1> ac_0;68Layers::AffineTransform<FC_0_OUTPUTS * 2, FC_1_OUTPUTS> fc_1;69Layers::ClippedReLU<FC_1_OUTPUTS> ac_1;70Layers::AffineTransform<FC_1_OUTPUTS, 1> fc_2;7172// Hash value embedded in the evaluation file73static constexpr std::uint32_t get_hash_value() {74// input slice hash75std::uint32_t hashValue = 0xEC42E90Du;76hashValue ^= TransformedFeatureDimensions * 2;7778hashValue = decltype(fc_0)::get_hash_value(hashValue);79hashValue = decltype(ac_0)::get_hash_value(hashValue);80hashValue = decltype(fc_1)::get_hash_value(hashValue);81hashValue = decltype(ac_1)::get_hash_value(hashValue);82hashValue = decltype(fc_2)::get_hash_value(hashValue);8384return hashValue;85}8687// Read network parameters88bool read_parameters(std::istream& stream) {89return fc_0.read_parameters(stream) && ac_0.read_parameters(stream)90&& fc_1.read_parameters(stream) && ac_1.read_parameters(stream)91&& fc_2.read_parameters(stream);92}9394// Write network parameters95bool write_parameters(std::ostream& stream) const {96return fc_0.write_parameters(stream) && ac_0.write_parameters(stream)97&& fc_1.write_parameters(stream) && ac_1.write_parameters(stream)98&& fc_2.write_parameters(stream);99}100101std::int32_t propagate(const TransformedFeatureType* transformedFeatures) const {102struct alignas(CacheLineSize) Buffer {103alignas(CacheLineSize) typename decltype(fc_0)::OutputBuffer fc_0_out;104alignas(CacheLineSize) typename decltype(ac_sqr_0)::OutputType105ac_sqr_0_out[ceil_to_multiple<IndexType>(FC_0_OUTPUTS * 2, 32)];106alignas(CacheLineSize) typename decltype(ac_0)::OutputBuffer ac_0_out;107alignas(CacheLineSize) typename decltype(fc_1)::OutputBuffer fc_1_out;108alignas(CacheLineSize) typename decltype(ac_1)::OutputBuffer ac_1_out;109alignas(CacheLineSize) typename decltype(fc_2)::OutputBuffer fc_2_out;110111Buffer() { std::memset(this, 0, sizeof(*this)); }112};113114#if defined(__clang__) && (__APPLE__)115// workaround for a bug reported with xcode 12116static thread_local auto tlsBuffer = std::make_unique<Buffer>();117// Access TLS only once, cache result.118Buffer& buffer = *tlsBuffer;119#else120alignas(CacheLineSize) static thread_local Buffer buffer;121#endif122123fc_0.propagate(transformedFeatures, buffer.fc_0_out);124ac_sqr_0.propagate(buffer.fc_0_out, buffer.ac_sqr_0_out);125ac_0.propagate(buffer.fc_0_out, buffer.ac_0_out);126std::memcpy(buffer.ac_sqr_0_out + FC_0_OUTPUTS, buffer.ac_0_out,127FC_0_OUTPUTS * sizeof(typename decltype(ac_0)::OutputType));128fc_1.propagate(buffer.ac_sqr_0_out, buffer.fc_1_out);129ac_1.propagate(buffer.fc_1_out, buffer.ac_1_out);130fc_2.propagate(buffer.ac_1_out, buffer.fc_2_out);131132// buffer.fc_0_out[FC_0_OUTPUTS] is such that 1.0 is equal to 127*(1<<WeightScaleBits) in133// quantized form, but we want 1.0 to be equal to 600*OutputScale134std::int32_t fwdOut =135(buffer.fc_0_out[FC_0_OUTPUTS]) * (600 * OutputScale) / (127 * (1 << WeightScaleBits));136std::int32_t outputValue = buffer.fc_2_out[0] + fwdOut;137138return outputValue;139}140141std::size_t get_content_hash() const {142std::size_t h = 0;143hash_combine(h, fc_0.get_content_hash());144hash_combine(h, ac_sqr_0.get_content_hash());145hash_combine(h, ac_0.get_content_hash());146hash_combine(h, fc_1.get_content_hash());147hash_combine(h, ac_1.get_content_hash());148hash_combine(h, fc_2.get_content_hash());149hash_combine(h, get_hash_value());150return h;151}152};153154} // namespace Stockfish::Eval::NNUE155156template<Stockfish::Eval::NNUE::IndexType L1, int L2, int L3>157struct std::hash<Stockfish::Eval::NNUE::NetworkArchitecture<L1, L2, L3>> {158std::size_t159operator()(const Stockfish::Eval::NNUE::NetworkArchitecture<L1, L2, L3>& arch) const noexcept {160return arch.get_content_hash();161}162};163164#endif // #ifndef NNUE_ARCHITECTURE_H_INCLUDED165166167