mirror of
https://github.com/official-stockfish/Stockfish.git
synced 2026-07-22 12:47:08 +00:00
remove AVX2 special casing based on earlier https://github.com/official-stockfish/Stockfish/pull/6824 that passed non-regression STC: LLR: 3.56 (-2.94,2.94) <-1.75,0.25> Total: 49888 W: 12683 L: 12439 D: 24766 Ptnml(0-2): 104, 5170, 14153, 5412, 105 https://tests.stockfishchess.org/tests/view/6a0702c58d9bd4cd7cd69462 No change on non-AVX2 architectures. closes https://github.com/official-stockfish/Stockfish/pull/6825 No functional change
162 lines
6.3 KiB
C++
162 lines
6.3 KiB
C++
/*
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
Copyright (C) 2004-2026 The Stockfish developers (see AUTHORS file)
|
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
// Definition of layer ClippedReLU of NNUE evaluation function
|
|
|
|
#ifndef NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED
|
|
#define NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED
|
|
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <iosfwd>
|
|
|
|
#include "../nnue_common.h"
|
|
|
|
namespace Stockfish::Eval::NNUE::Layers {
|
|
|
|
// Clipped ReLU
|
|
template<IndexType InDims>
|
|
class ClippedReLU {
|
|
public:
|
|
// Input/output type
|
|
using InputType = std::int32_t;
|
|
using OutputType = std::uint8_t;
|
|
|
|
// Number of input/output dimensions
|
|
static constexpr IndexType InputDimensions = InDims;
|
|
static constexpr IndexType OutputDimensions = InputDimensions;
|
|
static constexpr IndexType PaddedOutputDimensions =
|
|
ceil_to_multiple<IndexType>(OutputDimensions, 32);
|
|
|
|
using OutputBuffer = OutputType[PaddedOutputDimensions];
|
|
|
|
// Hash value embedded in the evaluation file
|
|
static constexpr std::uint32_t get_hash_value(std::uint32_t prevHash) {
|
|
std::uint32_t hashValue = 0x538D24C7u;
|
|
hashValue += prevHash;
|
|
return hashValue;
|
|
}
|
|
|
|
// Read network parameters
|
|
bool read_parameters(std::istream&) { return true; }
|
|
|
|
// Write network parameters
|
|
bool write_parameters(std::ostream&) const { return true; }
|
|
|
|
std::size_t get_content_hash() const {
|
|
std::size_t h = 0;
|
|
hash_combine(h, get_hash_value(0));
|
|
return h;
|
|
}
|
|
|
|
// Forward propagation
|
|
void propagate(const InputType* input, OutputType* output) const {
|
|
|
|
|
|
#if defined(USE_SSE2)
|
|
constexpr IndexType NumChunks = InputDimensions / 16;
|
|
|
|
#ifndef USE_SSE41
|
|
const __m128i k0x80s = _mm_set1_epi8(-128);
|
|
#endif
|
|
|
|
const auto in = reinterpret_cast<const __m128i*>(input);
|
|
const auto out = reinterpret_cast<__m128i*>(output);
|
|
for (IndexType i = 0; i < NumChunks; ++i)
|
|
{
|
|
#if defined(USE_SSE41)
|
|
const __m128i words0 = _mm_srli_epi16(
|
|
_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1])),
|
|
WeightScaleBits);
|
|
const __m128i words1 = _mm_srli_epi16(
|
|
_mm_packus_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3])),
|
|
WeightScaleBits);
|
|
_mm_store_si128(&out[i], _mm_packs_epi16(words0, words1));
|
|
#else
|
|
const __m128i words0 = _mm_srai_epi16(
|
|
_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1])),
|
|
WeightScaleBits);
|
|
const __m128i words1 = _mm_srai_epi16(
|
|
_mm_packs_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3])),
|
|
WeightScaleBits);
|
|
const __m128i packedbytes = _mm_packs_epi16(words0, words1);
|
|
_mm_store_si128(&out[i], _mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s));
|
|
#endif
|
|
}
|
|
constexpr IndexType Start = NumChunks * 16;
|
|
|
|
#elif defined(USE_NEON)
|
|
constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);
|
|
const SIMD::vec_i8x8_t Zero = {0};
|
|
const auto in = reinterpret_cast<const SIMD::vec_i32x4_t*>(input);
|
|
const auto out = reinterpret_cast<SIMD::vec_i8x8_t*>(output);
|
|
for (IndexType i = 0; i < NumChunks; ++i)
|
|
{
|
|
int16x8_t shifted;
|
|
const auto pack = reinterpret_cast<int16x4_t*>(&shifted);
|
|
pack[0] = vqshrn_n_s32(in[i * 2 + 0], WeightScaleBits);
|
|
pack[1] = vqshrn_n_s32(in[i * 2 + 1], WeightScaleBits);
|
|
out[i] = vmax_s8(vqmovn_s16(shifted), Zero);
|
|
}
|
|
constexpr IndexType Start = NumChunks * (SimdWidth / 2);
|
|
|
|
#elif defined(USE_LASX)
|
|
constexpr IndexType NumChunks = InputDimensions / 32;
|
|
const auto in = reinterpret_cast<const __m256i*>(input);
|
|
const auto out = reinterpret_cast<__m256i*>(output);
|
|
for (IndexType i = 0; i < NumChunks; ++i)
|
|
{
|
|
const __m256i packed0 = SIMD::lasx_packus_32(in[i * 4 + 0], in[i * 4 + 1]);
|
|
const __m256i packed1 = SIMD::lasx_packus_32(in[i * 4 + 2], in[i * 4 + 3]);
|
|
const __m256i words0 = __lasx_xvsrli_h(packed0, WeightScaleBits);
|
|
const __m256i words1 = __lasx_xvsrli_h(packed1, WeightScaleBits);
|
|
const __m256i packed = __lasx_xvssrani_b_h(words1, words0, 0);
|
|
const __m256i swaped = __lasx_xvpermi_d(packed, 0xD8);
|
|
__lasx_xvst(__lasx_xvshuf4i_w(swaped, 0xD8), out + i, 0);
|
|
}
|
|
constexpr IndexType Start = NumChunks * 32;
|
|
|
|
#elif defined(USE_LSX)
|
|
constexpr IndexType NumChunks = InputDimensions / 16;
|
|
const auto in = reinterpret_cast<const __m128i*>(input);
|
|
const auto out = reinterpret_cast<__m128i*>(output);
|
|
for (IndexType i = 0; i < NumChunks; ++i)
|
|
{
|
|
const __m128i packed0 = SIMD::lsx_packus_32(in[i * 4 + 0], in[i * 4 + 1]);
|
|
const __m128i packed1 = SIMD::lsx_packus_32(in[i * 4 + 2], in[i * 4 + 3]);
|
|
const __m128i words0 = __lsx_vsrli_h(packed0, WeightScaleBits);
|
|
const __m128i words1 = __lsx_vsrli_h(packed1, WeightScaleBits);
|
|
out[i] = __lsx_vssrani_b_h(words1, words0, 0);
|
|
}
|
|
constexpr IndexType Start = NumChunks * 16;
|
|
|
|
#else
|
|
constexpr IndexType Start = 0;
|
|
#endif
|
|
|
|
for (IndexType i = Start; i < InputDimensions; ++i)
|
|
{
|
|
output[i] = static_cast<OutputType>(std::clamp(input[i] >> WeightScaleBits, 0, 127));
|
|
}
|
|
}
|
|
};
|
|
|
|
} // namespace Stockfish::Eval::NNUE::Layers
|
|
|
|
#endif // NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED
|