// Input features used in evaluation function
using RawFeatures = Features::FeatureSet<
- Features::HalfKP<Features::Side::kFriend>>;
+ Features::HalfKP<Features::Side::Friend>>;
// Number of input feature dimensions after conversion
-constexpr IndexType kTransformedFeatureDimensions = 256;
+constexpr IndexType TransformedFeatureDimensions = 256;
namespace Layers {
// Define network structure
-using InputLayer = InputSlice<kTransformedFeatureDimensions * 2>;
+using InputLayer = InputSlice<TransformedFeatureDimensions * 2>;
using HiddenLayer1 = ClippedReLU<AffineTransform<InputLayer, 32>>;
using HiddenLayer2 = ClippedReLU<AffineTransform<HiddenLayer1, 32>>;
using OutputLayer = AffineTransform<HiddenLayer2, 1>;
namespace Stockfish::Eval::NNUE {
// Input feature converter
- LargePagePtr<FeatureTransformer> feature_transformer;
+ LargePagePtr<FeatureTransformer> featureTransformer;
// Evaluation function
AlignedPtr<Network> network;
// Initialize the evaluation function parameters
template <typename T>
- void Initialize(AlignedPtr<T>& pointer) {
+ void initialize(AlignedPtr<T>& pointer) {
pointer.reset(reinterpret_cast<T*>(std_aligned_alloc(alignof(T), sizeof(T))));
std::memset(pointer.get(), 0, sizeof(T));
}
template <typename T>
- void Initialize(LargePagePtr<T>& pointer) {
+ void initialize(LargePagePtr<T>& pointer) {
static_assert(alignof(T) <= 4096, "aligned_large_pages_alloc() may fail for such a big alignment requirement of T");
pointer.reset(reinterpret_cast<T*>(aligned_large_pages_alloc(sizeof(T))));
// Read evaluation function parameters
template <typename T>
- bool ReadParameters(std::istream& stream, T& reference) {
+ bool read_parameters(std::istream& stream, T& reference) {
std::uint32_t header;
header = read_little_endian<std::uint32_t>(stream);
- if (!stream || header != T::GetHashValue()) return false;
- return reference.ReadParameters(stream);
+ if (!stream || header != T::get_hash_value()) return false;
+ return reference.read_parameters(stream);
}
} // namespace Detail
// Initialize the evaluation function parameters
- void Initialize() {
+ void initialize() {
- Detail::Initialize(feature_transformer);
- Detail::Initialize(network);
+ Detail::initialize(featureTransformer);
+ Detail::initialize(network);
}
// Read network header
- bool ReadHeader(std::istream& stream, std::uint32_t* hash_value, std::string* architecture)
+ bool read_header(std::istream& stream, std::uint32_t* hashValue, std::string* architecture)
{
std::uint32_t version, size;
version = read_little_endian<std::uint32_t>(stream);
- *hash_value = read_little_endian<std::uint32_t>(stream);
+ *hashValue = read_little_endian<std::uint32_t>(stream);
size = read_little_endian<std::uint32_t>(stream);
- if (!stream || version != kVersion) return false;
+ if (!stream || version != Version) return false;
architecture->resize(size);
stream.read(&(*architecture)[0], size);
return !stream.fail();
}
// Read network parameters
- bool ReadParameters(std::istream& stream) {
+ bool read_parameters(std::istream& stream) {
- std::uint32_t hash_value;
+ std::uint32_t hashValue;
std::string architecture;
- if (!ReadHeader(stream, &hash_value, &architecture)) return false;
- if (hash_value != kHashValue) return false;
- if (!Detail::ReadParameters(stream, *feature_transformer)) return false;
- if (!Detail::ReadParameters(stream, *network)) return false;
+ if (!read_header(stream, &hashValue, &architecture)) return false;
+ if (hashValue != HashValue) return false;
+ if (!Detail::read_parameters(stream, *featureTransformer)) return false;
+ if (!Detail::read_parameters(stream, *network)) return false;
return stream && stream.peek() == std::ios::traits_type::eof();
}
// We manually align the arrays on the stack because with gcc < 9.3
// overaligning stack variables with alignas() doesn't work correctly.
- constexpr uint64_t alignment = kCacheLineSize;
+ constexpr uint64_t alignment = CacheLineSize;
#if defined(ALIGNAS_ON_STACK_VARIABLES_BROKEN)
- TransformedFeatureType transformed_features_unaligned[
- FeatureTransformer::kBufferSize + alignment / sizeof(TransformedFeatureType)];
- char buffer_unaligned[Network::kBufferSize + alignment];
+ TransformedFeatureType transformedFeaturesUnaligned[
+ FeatureTransformer::BufferSize + alignment / sizeof(TransformedFeatureType)];
+ char bufferUnaligned[Network::BufferSize + alignment];
- auto* transformed_features = align_ptr_up<alignment>(&transformed_features_unaligned[0]);
- auto* buffer = align_ptr_up<alignment>(&buffer_unaligned[0]);
+ auto* transformedFeatures = align_ptr_up<alignment>(&transformedFeaturesUnaligned[0]);
+ auto* buffer = align_ptr_up<alignment>(&bufferUnaligned[0]);
#else
alignas(alignment)
- TransformedFeatureType transformed_features[FeatureTransformer::kBufferSize];
- alignas(alignment) char buffer[Network::kBufferSize];
+ TransformedFeatureType transformedFeatures[FeatureTransformer::BufferSize];
+ alignas(alignment) char buffer[Network::BufferSize];
#endif
- ASSERT_ALIGNED(transformed_features, alignment);
+ ASSERT_ALIGNED(transformedFeatures, alignment);
ASSERT_ALIGNED(buffer, alignment);
- feature_transformer->Transform(pos, transformed_features);
- const auto output = network->Propagate(transformed_features, buffer);
+ featureTransformer->transform(pos, transformedFeatures);
+ const auto output = network->propagate(transformedFeatures, buffer);
- return static_cast<Value>(output[0] / FV_SCALE);
+ return static_cast<Value>(output[0] / OutputScale);
}
// Load eval, from a file stream or a memory stream
bool load_eval(std::string name, std::istream& stream) {
- Initialize();
+ initialize();
fileName = name;
- return ReadParameters(stream);
+ return read_parameters(stream);
}
} // namespace Stockfish::Eval::NNUE
namespace Stockfish::Eval::NNUE {
// Hash value of evaluation function structure
- constexpr std::uint32_t kHashValue =
- FeatureTransformer::GetHashValue() ^ Network::GetHashValue();
+ constexpr std::uint32_t HashValue =
+ FeatureTransformer::get_hash_value() ^ Network::get_hash_value();
// Deleter for automating release of memory area
template <typename T>
return value == First || CompileTimeList<T, Remaining...>::Contains(value);
}
static constexpr std::array<T, sizeof...(Remaining) + 1>
- kValues = {{First, Remaining...}};
+ Values = {{First, Remaining...}};
};
// Base class of feature set
public:
// Hash value embedded in the evaluation file
- static constexpr std::uint32_t kHashValue = FeatureType::kHashValue;
+ static constexpr std::uint32_t HashValue = FeatureType::HashValue;
// Number of feature dimensions
- static constexpr IndexType kDimensions = FeatureType::kDimensions;
+ static constexpr IndexType Dimensions = FeatureType::Dimensions;
// Maximum number of simultaneously active features
- static constexpr IndexType kMaxActiveDimensions =
- FeatureType::kMaxActiveDimensions;
+ static constexpr IndexType MaxActiveDimensions =
+ FeatureType::MaxActiveDimensions;
// Trigger for full calculation instead of difference calculation
using SortedTriggerSet =
- CompileTimeList<TriggerEvent, FeatureType::kRefreshTrigger>;
- static constexpr auto kRefreshTriggers = SortedTriggerSet::kValues;
+ CompileTimeList<TriggerEvent, FeatureType::RefreshTrigger>;
+ static constexpr auto RefreshTriggers = SortedTriggerSet::Values;
};
// Trigger to perform full calculations instead of difference only
enum class TriggerEvent {
- kFriendKingMoved // calculate full evaluation when own king moves
+ FriendKingMoved // calculate full evaluation when own king moves
};
enum class Side {
- kFriend // side to move
+ Friend // side to move
};
} // namespace Stockfish::Eval::NNUE::Features
// Index of a feature for a given king position and another piece on some square
inline IndexType make_index(Color perspective, Square s, Piece pc, Square ksq) {
- return IndexType(orient(perspective, s) + kpp_board_index[perspective][pc] + PS_END * ksq);
+ return IndexType(orient(perspective, s) + PieceSquareIndex[perspective][pc] + PS_NB * ksq);
}
// Get a list of indices for active features
template <Side AssociatedKing>
- void HalfKP<AssociatedKing>::AppendActiveIndices(
+ void HalfKP<AssociatedKing>::append_active_indices(
const Position& pos, Color perspective, IndexList* active) {
Square ksq = orient(perspective, pos.square<KING>(perspective));
}
- // AppendChangedIndices() : get a list of indices for recently changed features
+ // append_changed_indices() : get a list of indices for recently changed features
// IMPORTANT: The `pos` in this function is pretty much useless as it
// is not always the position the features are updated to. The feature
// the current leaf position (the position after the move).
template <Side AssociatedKing>
- void HalfKP<AssociatedKing>::AppendChangedIndices(
+ void HalfKP<AssociatedKing>::append_changed_indices(
const Position& pos, const DirtyPiece& dp, Color perspective,
IndexList* removed, IndexList* added) {
}
}
- template class HalfKP<Side::kFriend>;
+ template class HalfKP<Side::Friend>;
} // namespace Stockfish::Eval::NNUE::Features
public:
// Feature name
- static constexpr const char* kName = "HalfKP(Friend)";
+ static constexpr const char* Name = "HalfKP(Friend)";
// Hash value embedded in the evaluation file
- static constexpr std::uint32_t kHashValue =
- 0x5D69D5B9u ^ (AssociatedKing == Side::kFriend);
+ static constexpr std::uint32_t HashValue =
+ 0x5D69D5B9u ^ (AssociatedKing == Side::Friend);
// Number of feature dimensions
- static constexpr IndexType kDimensions =
- static_cast<IndexType>(SQUARE_NB) * static_cast<IndexType>(PS_END);
+ static constexpr IndexType Dimensions =
+ static_cast<IndexType>(SQUARE_NB) * static_cast<IndexType>(PS_NB);
// Maximum number of simultaneously active features
- static constexpr IndexType kMaxActiveDimensions = 30; // Kings don't count
+ static constexpr IndexType MaxActiveDimensions = 30; // Kings don't count
// Trigger for full calculation instead of difference calculation
- static constexpr TriggerEvent kRefreshTrigger = TriggerEvent::kFriendKingMoved;
+ static constexpr TriggerEvent RefreshTrigger = TriggerEvent::FriendKingMoved;
// Get a list of indices for active features
- static void AppendActiveIndices(const Position& pos, Color perspective,
- IndexList* active);
+ static void append_active_indices(const Position& pos, Color perspective,
+ IndexList* active);
// Get a list of indices for recently changed features
- static void AppendChangedIndices(const Position& pos, const DirtyPiece& dp, Color perspective,
- IndexList* removed, IndexList* added);
+ static void append_changed_indices(const Position& pos, const DirtyPiece& dp, Color perspective,
+ IndexList* removed, IndexList* added);
};
} // namespace Stockfish::Eval::NNUE::Features
//Type of feature index list
class IndexList
- : public ValueList<IndexType, RawFeatures::kMaxActiveDimensions> {
+ : public ValueList<IndexType, RawFeatures::MaxActiveDimensions> {
};
} // namespace Stockfish::Eval::NNUE::Features
namespace Stockfish::Eval::NNUE::Layers {
// Affine transformation layer
- template <typename PreviousLayer, IndexType OutputDimensions>
+ template <typename PreviousLayer, IndexType OutDims>
class AffineTransform {
public:
// Input/output type
static_assert(std::is_same<InputType, std::uint8_t>::value, "");
// Number of input/output dimensions
- static constexpr IndexType kInputDimensions =
- PreviousLayer::kOutputDimensions;
- static constexpr IndexType kOutputDimensions = OutputDimensions;
- static constexpr IndexType kPaddedInputDimensions =
- CeilToMultiple<IndexType>(kInputDimensions, kMaxSimdWidth);
+ static constexpr IndexType InputDimensions =
+ PreviousLayer::OutputDimensions;
+ static constexpr IndexType OutputDimensions = OutDims;
+ static constexpr IndexType PaddedInputDimensions =
+ ceil_to_multiple<IndexType>(InputDimensions, MaxSimdWidth);
#if defined (USE_AVX512)
- static constexpr const IndexType kOutputSimdWidth = kSimdWidth / 2;
+ static constexpr const IndexType OutputSimdWidth = SimdWidth / 2;
#elif defined (USE_SSSE3)
- static constexpr const IndexType kOutputSimdWidth = kSimdWidth / 4;
+ static constexpr const IndexType OutputSimdWidth = SimdWidth / 4;
#endif
// Size of forward propagation buffer used in this layer
- static constexpr std::size_t kSelfBufferSize =
- CeilToMultiple(kOutputDimensions * sizeof(OutputType), kCacheLineSize);
+ static constexpr std::size_t SelfBufferSize =
+ ceil_to_multiple(OutputDimensions * sizeof(OutputType), CacheLineSize);
// Size of the forward propagation buffer used from the input layer to this layer
- static constexpr std::size_t kBufferSize =
- PreviousLayer::kBufferSize + kSelfBufferSize;
+ static constexpr std::size_t BufferSize =
+ PreviousLayer::BufferSize + SelfBufferSize;
// Hash value embedded in the evaluation file
- static constexpr std::uint32_t GetHashValue() {
- std::uint32_t hash_value = 0xCC03DAE4u;
- hash_value += kOutputDimensions;
- hash_value ^= PreviousLayer::GetHashValue() >> 1;
- hash_value ^= PreviousLayer::GetHashValue() << 31;
- return hash_value;
+ static constexpr std::uint32_t get_hash_value() {
+ std::uint32_t hashValue = 0xCC03DAE4u;
+ hashValue += OutputDimensions;
+ hashValue ^= PreviousLayer::get_hash_value() >> 1;
+ hashValue ^= PreviousLayer::get_hash_value() << 31;
+ return hashValue;
}
- // Read network parameters
- bool ReadParameters(std::istream& stream) {
- if (!previous_layer_.ReadParameters(stream)) return false;
- for (std::size_t i = 0; i < kOutputDimensions; ++i)
- biases_[i] = read_little_endian<BiasType>(stream);
- for (std::size_t i = 0; i < kOutputDimensions * kPaddedInputDimensions; ++i)
+ // Read network parameters
+ bool read_parameters(std::istream& stream) {
+ if (!previousLayer.read_parameters(stream)) return false;
+ for (std::size_t i = 0; i < OutputDimensions; ++i)
+ biases[i] = read_little_endian<BiasType>(stream);
+ for (std::size_t i = 0; i < OutputDimensions * PaddedInputDimensions; ++i)
#if !defined (USE_SSSE3)
- weights_[i] = read_little_endian<WeightType>(stream);
+ weights[i] = read_little_endian<WeightType>(stream);
#else
- weights_[
- (i / 4) % (kPaddedInputDimensions / 4) * kOutputDimensions * 4 +
- i / kPaddedInputDimensions * 4 +
+ weights[
+ (i / 4) % (PaddedInputDimensions / 4) * OutputDimensions * 4 +
+ i / PaddedInputDimensions * 4 +
i % 4
] = read_little_endian<WeightType>(stream);
// Determine if eights of weight and input products can be summed using 16bits
// without saturation. We assume worst case combinations of 0 and 127 for all inputs.
- if (kOutputDimensions > 1 && !stream.fail())
+ if (OutputDimensions > 1 && !stream.fail())
{
canSaturate16.count = 0;
#if !defined(USE_VNNI)
- for (IndexType i = 0; i < kPaddedInputDimensions; i += 16)
- for (IndexType j = 0; j < kOutputDimensions; ++j)
+ for (IndexType i = 0; i < PaddedInputDimensions; i += 16)
+ for (IndexType j = 0; j < OutputDimensions; ++j)
for (int x = 0; x < 2; ++x)
{
- WeightType* w = &weights_[i * kOutputDimensions + j * 4 + x * 2];
+ WeightType* w = &weights[i * OutputDimensions + j * 4 + x * 2];
int sum[2] = {0, 0};
for (int k = 0; k < 8; ++k)
{
- IndexType idx = k / 2 * kOutputDimensions * 4 + k % 2;
+ IndexType idx = k / 2 * OutputDimensions * 4 + k % 2;
sum[w[idx] < 0] += w[idx];
}
for (int sign : { -1, 1 })
int maxK = 0, maxW = 0;
for (int k = 0; k < 8; ++k)
{
- IndexType idx = k / 2 * kOutputDimensions * 4 + k % 2;
+ IndexType idx = k / 2 * OutputDimensions * 4 + k % 2;
if (maxW < sign * w[idx])
maxK = k, maxW = sign * w[idx];
}
- IndexType idx = maxK / 2 * kOutputDimensions * 4 + maxK % 2;
+ IndexType idx = maxK / 2 * OutputDimensions * 4 + maxK % 2;
sum[sign == -1] -= w[idx];
canSaturate16.add(j, i + maxK / 2 * 4 + maxK % 2 + x * 2, w[idx]);
w[idx] = 0;
}
// Forward propagation
- const OutputType* Propagate(
- const TransformedFeatureType* transformed_features, char* buffer) const {
- const auto input = previous_layer_.Propagate(
- transformed_features, buffer + kSelfBufferSize);
+ const OutputType* propagate(
+ const TransformedFeatureType* transformedFeatures, char* buffer) const {
+ const auto input = previousLayer.propagate(
+ transformedFeatures, buffer + SelfBufferSize);
#if defined (USE_AVX512)
- [[maybe_unused]] const __m512i kOnes512 = _mm512_set1_epi16(1);
+ [[maybe_unused]] const __m512i Ones512 = _mm512_set1_epi16(1);
[[maybe_unused]] auto m512_hadd = [](__m512i sum, int bias) -> int {
return _mm512_reduce_add_epi32(sum) + bias;
acc = _mm512_dpbusd_epi32(acc, a, b);
#else
__m512i product0 = _mm512_maddubs_epi16(a, b);
- product0 = _mm512_madd_epi16(product0, kOnes512);
+ product0 = _mm512_madd_epi16(product0, Ones512);
acc = _mm512_add_epi32(acc, product0);
#endif
};
product0 = _mm512_add_epi16(product0, product1);
product2 = _mm512_add_epi16(product2, product3);
product0 = _mm512_add_epi16(product0, product2);
- product0 = _mm512_madd_epi16(product0, kOnes512);
+ product0 = _mm512_madd_epi16(product0, Ones512);
acc = _mm512_add_epi32(acc, product0);
#endif
};
#endif
#if defined (USE_AVX2)
- [[maybe_unused]] const __m256i kOnes256 = _mm256_set1_epi16(1);
+ [[maybe_unused]] const __m256i Ones256 = _mm256_set1_epi16(1);
[[maybe_unused]] auto m256_hadd = [](__m256i sum, int bias) -> int {
__m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(sum), _mm256_extracti128_si256(sum, 1));
acc = _mm256_dpbusd_epi32(acc, a, b);
#else
__m256i product0 = _mm256_maddubs_epi16(a, b);
- product0 = _mm256_madd_epi16(product0, kOnes256);
+ product0 = _mm256_madd_epi16(product0, Ones256);
acc = _mm256_add_epi32(acc, product0);
#endif
};
product0 = _mm256_add_epi16(product0, product1);
product2 = _mm256_add_epi16(product2, product3);
product0 = _mm256_add_epi16(product0, product2);
- product0 = _mm256_madd_epi16(product0, kOnes256);
+ product0 = _mm256_madd_epi16(product0, Ones256);
acc = _mm256_add_epi32(acc, product0);
#endif
};
#endif
#if defined (USE_SSSE3)
- [[maybe_unused]] const __m128i kOnes128 = _mm_set1_epi16(1);
+ [[maybe_unused]] const __m128i Ones128 = _mm_set1_epi16(1);
[[maybe_unused]] auto m128_hadd = [](__m128i sum, int bias) -> int {
sum = _mm_add_epi32(sum, _mm_shuffle_epi32(sum, 0x4E)); //_MM_PERM_BADC
[[maybe_unused]] auto m128_add_dpbusd_epi32 = [=](__m128i& acc, __m128i a, __m128i b) {
__m128i product0 = _mm_maddubs_epi16(a, b);
- product0 = _mm_madd_epi16(product0, kOnes128);
+ product0 = _mm_madd_epi16(product0, Ones128);
acc = _mm_add_epi32(acc, product0);
};
product0 = _mm_add_epi16(product0, product1);
product2 = _mm_add_epi16(product2, product3);
product0 = _mm_add_epi16(product0, product2);
- product0 = _mm_madd_epi16(product0, kOnes128);
+ product0 = _mm_madd_epi16(product0, Ones128);
acc = _mm_add_epi32(acc, product0);
};
#if defined (USE_SSSE3)
const auto output = reinterpret_cast<OutputType*>(buffer);
- const auto input_vector = reinterpret_cast<const vec_t*>(input);
+ const auto inputVector = reinterpret_cast<const vec_t*>(input);
- static_assert(kOutputDimensions % kOutputSimdWidth == 0 || kOutputDimensions == 1);
+ static_assert(OutputDimensions % OutputSimdWidth == 0 || OutputDimensions == 1);
- // kOutputDimensions is either 1 or a multiple of kSimdWidth
+ // OutputDimensions is either 1 or a multiple of SimdWidth
// because then it is also an input dimension.
- if constexpr (kOutputDimensions % kOutputSimdWidth == 0)
+ if constexpr (OutputDimensions % OutputSimdWidth == 0)
{
- constexpr IndexType kNumChunks = kPaddedInputDimensions / 4;
+ constexpr IndexType NumChunks = PaddedInputDimensions / 4;
const auto input32 = reinterpret_cast<const std::int32_t*>(input);
vec_t* outptr = reinterpret_cast<vec_t*>(output);
- std::memcpy(output, biases_, kOutputDimensions * sizeof(OutputType));
+ std::memcpy(output, biases, OutputDimensions * sizeof(OutputType));
- for (int i = 0; i < (int)kNumChunks - 3; i += 4)
+ for (int i = 0; i < (int)NumChunks - 3; i += 4)
{
const vec_t in0 = vec_set_32(input32[i + 0]);
const vec_t in1 = vec_set_32(input32[i + 1]);
const vec_t in2 = vec_set_32(input32[i + 2]);
const vec_t in3 = vec_set_32(input32[i + 3]);
- const auto col0 = reinterpret_cast<const vec_t*>(&weights_[(i + 0) * kOutputDimensions * 4]);
- const auto col1 = reinterpret_cast<const vec_t*>(&weights_[(i + 1) * kOutputDimensions * 4]);
- const auto col2 = reinterpret_cast<const vec_t*>(&weights_[(i + 2) * kOutputDimensions * 4]);
- const auto col3 = reinterpret_cast<const vec_t*>(&weights_[(i + 3) * kOutputDimensions * 4]);
- for (int j = 0; j * kOutputSimdWidth < kOutputDimensions; ++j)
+ const auto col0 = reinterpret_cast<const vec_t*>(&weights[(i + 0) * OutputDimensions * 4]);
+ const auto col1 = reinterpret_cast<const vec_t*>(&weights[(i + 1) * OutputDimensions * 4]);
+ const auto col2 = reinterpret_cast<const vec_t*>(&weights[(i + 2) * OutputDimensions * 4]);
+ const auto col3 = reinterpret_cast<const vec_t*>(&weights[(i + 3) * OutputDimensions * 4]);
+ for (int j = 0; j * OutputSimdWidth < OutputDimensions; ++j)
vec_add_dpbusd_32x4(outptr[j], in0, col0[j], in1, col1[j], in2, col2[j], in3, col3[j]);
}
for (int i = 0; i < canSaturate16.count; ++i)
output[canSaturate16.ids[i].out] += input[canSaturate16.ids[i].in] * canSaturate16.ids[i].w;
}
- else if constexpr (kOutputDimensions == 1)
+ else if constexpr (OutputDimensions == 1)
{
#if defined (USE_AVX512)
- if constexpr (kPaddedInputDimensions % (kSimdWidth * 2) != 0)
+ if constexpr (PaddedInputDimensions % (SimdWidth * 2) != 0)
{
- constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
- const auto input_vector256 = reinterpret_cast<const __m256i*>(input);
+ constexpr IndexType NumChunks = PaddedInputDimensions / SimdWidth;
+ const auto inputVector256 = reinterpret_cast<const __m256i*>(input);
__m256i sum0 = _mm256_setzero_si256();
- const auto row0 = reinterpret_cast<const __m256i*>(&weights_[0]);
+ const auto row0 = reinterpret_cast<const __m256i*>(&weights[0]);
- for (int j = 0; j < (int)kNumChunks; ++j)
+ for (int j = 0; j < (int)NumChunks; ++j)
{
- const __m256i in = input_vector256[j];
+ const __m256i in = inputVector256[j];
m256_add_dpbusd_epi32(sum0, in, row0[j]);
}
- output[0] = m256_hadd(sum0, biases_[0]);
+ output[0] = m256_hadd(sum0, biases[0]);
}
else
#endif
{
#if defined (USE_AVX512)
- constexpr IndexType kNumChunks = kPaddedInputDimensions / (kSimdWidth * 2);
+ constexpr IndexType NumChunks = PaddedInputDimensions / (SimdWidth * 2);
#else
- constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
+ constexpr IndexType NumChunks = PaddedInputDimensions / SimdWidth;
#endif
vec_t sum0 = vec_setzero();
- const auto row0 = reinterpret_cast<const vec_t*>(&weights_[0]);
+ const auto row0 = reinterpret_cast<const vec_t*>(&weights[0]);
- for (int j = 0; j < (int)kNumChunks; ++j)
+ for (int j = 0; j < (int)NumChunks; ++j)
{
- const vec_t in = input_vector[j];
+ const vec_t in = inputVector[j];
vec_add_dpbusd_32(sum0, in, row0[j]);
}
- output[0] = vec_hadd(sum0, biases_[0]);
+ output[0] = vec_hadd(sum0, biases[0]);
}
}
auto output = reinterpret_cast<OutputType*>(buffer);
#if defined(USE_SSE2)
- constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
- const __m128i kZeros = _mm_setzero_si128();
- const auto input_vector = reinterpret_cast<const __m128i*>(input);
+ constexpr IndexType NumChunks = PaddedInputDimensions / SimdWidth;
+ const __m128i Zeros = _mm_setzero_si128();
+ const auto inputVector = reinterpret_cast<const __m128i*>(input);
#elif defined(USE_MMX)
- constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
- const __m64 kZeros = _mm_setzero_si64();
- const auto input_vector = reinterpret_cast<const __m64*>(input);
+ constexpr IndexType NumChunks = PaddedInputDimensions / SimdWidth;
+ const __m64 Zeros = _mm_setzero_si64();
+ const auto inputVector = reinterpret_cast<const __m64*>(input);
#elif defined(USE_NEON)
- constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
- const auto input_vector = reinterpret_cast<const int8x8_t*>(input);
+ constexpr IndexType NumChunks = PaddedInputDimensions / SimdWidth;
+ const auto inputVector = reinterpret_cast<const int8x8_t*>(input);
#endif
- for (IndexType i = 0; i < kOutputDimensions; ++i) {
- const IndexType offset = i * kPaddedInputDimensions;
+ for (IndexType i = 0; i < OutputDimensions; ++i) {
+ const IndexType offset = i * PaddedInputDimensions;
#if defined(USE_SSE2)
- __m128i sum_lo = _mm_cvtsi32_si128(biases_[i]);
- __m128i sum_hi = kZeros;
- const auto row = reinterpret_cast<const __m128i*>(&weights_[offset]);
- for (IndexType j = 0; j < kNumChunks; ++j) {
+ __m128i sumLo = _mm_cvtsi32_si128(biases[i]);
+ __m128i sumHi = Zeros;
+ const auto row = reinterpret_cast<const __m128i*>(&weights[offset]);
+ for (IndexType j = 0; j < NumChunks; ++j) {
__m128i row_j = _mm_load_si128(&row[j]);
- __m128i input_j = _mm_load_si128(&input_vector[j]);
- __m128i extended_row_lo = _mm_srai_epi16(_mm_unpacklo_epi8(row_j, row_j), 8);
- __m128i extended_row_hi = _mm_srai_epi16(_mm_unpackhi_epi8(row_j, row_j), 8);
- __m128i extended_input_lo = _mm_unpacklo_epi8(input_j, kZeros);
- __m128i extended_input_hi = _mm_unpackhi_epi8(input_j, kZeros);
- __m128i product_lo = _mm_madd_epi16(extended_row_lo, extended_input_lo);
- __m128i product_hi = _mm_madd_epi16(extended_row_hi, extended_input_hi);
- sum_lo = _mm_add_epi32(sum_lo, product_lo);
- sum_hi = _mm_add_epi32(sum_hi, product_hi);
+ __m128i input_j = _mm_load_si128(&inputVector[j]);
+ __m128i extendedRowLo = _mm_srai_epi16(_mm_unpacklo_epi8(row_j, row_j), 8);
+ __m128i extendedRowHi = _mm_srai_epi16(_mm_unpackhi_epi8(row_j, row_j), 8);
+ __m128i extendedInputLo = _mm_unpacklo_epi8(input_j, Zeros);
+ __m128i extendedInputHi = _mm_unpackhi_epi8(input_j, Zeros);
+ __m128i productLo = _mm_madd_epi16(extendedRowLo, extendedInputLo);
+ __m128i productHi = _mm_madd_epi16(extendedRowHi, extendedInputHi);
+ sumLo = _mm_add_epi32(sumLo, productLo);
+ sumHi = _mm_add_epi32(sumHi, productHi);
}
- __m128i sum = _mm_add_epi32(sum_lo, sum_hi);
- __m128i sum_high_64 = _mm_shuffle_epi32(sum, _MM_SHUFFLE(1, 0, 3, 2));
- sum = _mm_add_epi32(sum, sum_high_64);
+ __m128i sum = _mm_add_epi32(sumLo, sumHi);
+ __m128i sumHigh_64 = _mm_shuffle_epi32(sum, _MM_SHUFFLE(1, 0, 3, 2));
+ sum = _mm_add_epi32(sum, sumHigh_64);
__m128i sum_second_32 = _mm_shufflelo_epi16(sum, _MM_SHUFFLE(1, 0, 3, 2));
sum = _mm_add_epi32(sum, sum_second_32);
output[i] = _mm_cvtsi128_si32(sum);
#elif defined(USE_MMX)
- __m64 sum_lo = _mm_cvtsi32_si64(biases_[i]);
- __m64 sum_hi = kZeros;
- const auto row = reinterpret_cast<const __m64*>(&weights_[offset]);
- for (IndexType j = 0; j < kNumChunks; ++j) {
+ __m64 sumLo = _mm_cvtsi32_si64(biases[i]);
+ __m64 sumHi = Zeros;
+ const auto row = reinterpret_cast<const __m64*>(&weights[offset]);
+ for (IndexType j = 0; j < NumChunks; ++j) {
__m64 row_j = row[j];
- __m64 input_j = input_vector[j];
- __m64 extended_row_lo = _mm_srai_pi16(_mm_unpacklo_pi8(row_j, row_j), 8);
- __m64 extended_row_hi = _mm_srai_pi16(_mm_unpackhi_pi8(row_j, row_j), 8);
- __m64 extended_input_lo = _mm_unpacklo_pi8(input_j, kZeros);
- __m64 extended_input_hi = _mm_unpackhi_pi8(input_j, kZeros);
- __m64 product_lo = _mm_madd_pi16(extended_row_lo, extended_input_lo);
- __m64 product_hi = _mm_madd_pi16(extended_row_hi, extended_input_hi);
- sum_lo = _mm_add_pi32(sum_lo, product_lo);
- sum_hi = _mm_add_pi32(sum_hi, product_hi);
+ __m64 input_j = inputVector[j];
+ __m64 extendedRowLo = _mm_srai_pi16(_mm_unpacklo_pi8(row_j, row_j), 8);
+ __m64 extendedRowHi = _mm_srai_pi16(_mm_unpackhi_pi8(row_j, row_j), 8);
+ __m64 extendedInputLo = _mm_unpacklo_pi8(input_j, Zeros);
+ __m64 extendedInputHi = _mm_unpackhi_pi8(input_j, Zeros);
+ __m64 productLo = _mm_madd_pi16(extendedRowLo, extendedInputLo);
+ __m64 productHi = _mm_madd_pi16(extendedRowHi, extendedInputHi);
+ sumLo = _mm_add_pi32(sumLo, productLo);
+ sumHi = _mm_add_pi32(sumHi, productHi);
}
- __m64 sum = _mm_add_pi32(sum_lo, sum_hi);
+ __m64 sum = _mm_add_pi32(sumLo, sumHi);
sum = _mm_add_pi32(sum, _mm_unpackhi_pi32(sum, sum));
output[i] = _mm_cvtsi64_si32(sum);
#elif defined(USE_NEON)
- int32x4_t sum = {biases_[i]};
- const auto row = reinterpret_cast<const int8x8_t*>(&weights_[offset]);
- for (IndexType j = 0; j < kNumChunks; ++j) {
- int16x8_t product = vmull_s8(input_vector[j * 2], row[j * 2]);
- product = vmlal_s8(product, input_vector[j * 2 + 1], row[j * 2 + 1]);
+ int32x4_t sum = {biases[i]};
+ const auto row = reinterpret_cast<const int8x8_t*>(&weights[offset]);
+ for (IndexType j = 0; j < NumChunks; ++j) {
+ int16x8_t product = vmull_s8(inputVector[j * 2], row[j * 2]);
+ product = vmlal_s8(product, inputVector[j * 2 + 1], row[j * 2 + 1]);
sum = vpadalq_s16(sum, product);
}
output[i] = sum[0] + sum[1] + sum[2] + sum[3];
#else
- OutputType sum = biases_[i];
- for (IndexType j = 0; j < kInputDimensions; ++j) {
- sum += weights_[offset + j] * input[j];
+ OutputType sum = biases[i];
+ for (IndexType j = 0; j < InputDimensions; ++j) {
+ sum += weights[offset + j] * input[j];
}
output[i] = sum;
#endif
using BiasType = OutputType;
using WeightType = std::int8_t;
- PreviousLayer previous_layer_;
+ PreviousLayer previousLayer;
- alignas(kCacheLineSize) BiasType biases_[kOutputDimensions];
- alignas(kCacheLineSize) WeightType weights_[kOutputDimensions * kPaddedInputDimensions];
+ alignas(CacheLineSize) BiasType biases[OutputDimensions];
+ alignas(CacheLineSize) WeightType weights[OutputDimensions * PaddedInputDimensions];
#if defined (USE_SSSE3)
struct CanSaturate {
int count;
uint16_t out;
uint16_t in;
int8_t w;
- } ids[kPaddedInputDimensions * kOutputDimensions * 3 / 4];
+ } ids[PaddedInputDimensions * OutputDimensions * 3 / 4];
void add(int i, int j, int8_t w) {
ids[count].out = i;
static_assert(std::is_same<InputType, std::int32_t>::value, "");
// Number of input/output dimensions
- static constexpr IndexType kInputDimensions =
- PreviousLayer::kOutputDimensions;
- static constexpr IndexType kOutputDimensions = kInputDimensions;
+ static constexpr IndexType InputDimensions =
+ PreviousLayer::OutputDimensions;
+ static constexpr IndexType OutputDimensions = InputDimensions;
// Size of forward propagation buffer used in this layer
- static constexpr std::size_t kSelfBufferSize =
- CeilToMultiple(kOutputDimensions * sizeof(OutputType), kCacheLineSize);
+ static constexpr std::size_t SelfBufferSize =
+ ceil_to_multiple(OutputDimensions * sizeof(OutputType), CacheLineSize);
// Size of the forward propagation buffer used from the input layer to this layer
- static constexpr std::size_t kBufferSize =
- PreviousLayer::kBufferSize + kSelfBufferSize;
+ static constexpr std::size_t BufferSize =
+ PreviousLayer::BufferSize + SelfBufferSize;
// Hash value embedded in the evaluation file
- static constexpr std::uint32_t GetHashValue() {
- std::uint32_t hash_value = 0x538D24C7u;
- hash_value += PreviousLayer::GetHashValue();
- return hash_value;
+ static constexpr std::uint32_t get_hash_value() {
+ std::uint32_t hashValue = 0x538D24C7u;
+ hashValue += PreviousLayer::get_hash_value();
+ return hashValue;
}
// Read network parameters
- bool ReadParameters(std::istream& stream) {
- return previous_layer_.ReadParameters(stream);
+ bool read_parameters(std::istream& stream) {
+ return previousLayer.read_parameters(stream);
}
// Forward propagation
- const OutputType* Propagate(
- const TransformedFeatureType* transformed_features, char* buffer) const {
- const auto input = previous_layer_.Propagate(
- transformed_features, buffer + kSelfBufferSize);
+ const OutputType* propagate(
+ const TransformedFeatureType* transformedFeatures, char* buffer) const {
+ const auto input = previousLayer.propagate(
+ transformedFeatures, buffer + SelfBufferSize);
const auto output = reinterpret_cast<OutputType*>(buffer);
#if defined(USE_AVX2)
- constexpr IndexType kNumChunks = kInputDimensions / kSimdWidth;
- const __m256i kZero = _mm256_setzero_si256();
- const __m256i kOffsets = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
+ constexpr IndexType NumChunks = InputDimensions / SimdWidth;
+ const __m256i Zero = _mm256_setzero_si256();
+ const __m256i Offsets = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
const auto in = reinterpret_cast<const __m256i*>(input);
const auto out = reinterpret_cast<__m256i*>(output);
- for (IndexType i = 0; i < kNumChunks; ++i) {
+ for (IndexType i = 0; i < NumChunks; ++i) {
const __m256i words0 = _mm256_srai_epi16(_mm256_packs_epi32(
_mm256_load_si256(&in[i * 4 + 0]),
- _mm256_load_si256(&in[i * 4 + 1])), kWeightScaleBits);
+ _mm256_load_si256(&in[i * 4 + 1])), WeightScaleBits);
const __m256i words1 = _mm256_srai_epi16(_mm256_packs_epi32(
_mm256_load_si256(&in[i * 4 + 2]),
- _mm256_load_si256(&in[i * 4 + 3])), kWeightScaleBits);
+ _mm256_load_si256(&in[i * 4 + 3])), WeightScaleBits);
_mm256_store_si256(&out[i], _mm256_permutevar8x32_epi32(_mm256_max_epi8(
- _mm256_packs_epi16(words0, words1), kZero), kOffsets));
+ _mm256_packs_epi16(words0, words1), Zero), Offsets));
}
- constexpr IndexType kStart = kNumChunks * kSimdWidth;
+ constexpr IndexType Start = NumChunks * SimdWidth;
#elif defined(USE_SSE2)
- constexpr IndexType kNumChunks = kInputDimensions / kSimdWidth;
+ constexpr IndexType NumChunks = InputDimensions / SimdWidth;
#ifdef USE_SSE41
- const __m128i kZero = _mm_setzero_si128();
+ const __m128i Zero = _mm_setzero_si128();
#else
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 < kNumChunks; ++i) {
+ for (IndexType i = 0; i < NumChunks; ++i) {
const __m128i words0 = _mm_srai_epi16(_mm_packs_epi32(
_mm_load_si128(&in[i * 4 + 0]),
- _mm_load_si128(&in[i * 4 + 1])), kWeightScaleBits);
+ _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])), kWeightScaleBits);
+ _mm_load_si128(&in[i * 4 + 3])), WeightScaleBits);
const __m128i packedbytes = _mm_packs_epi16(words0, words1);
_mm_store_si128(&out[i],
#ifdef USE_SSE41
- _mm_max_epi8(packedbytes, kZero)
+ _mm_max_epi8(packedbytes, Zero)
#else
_mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s)
#endif
);
}
- constexpr IndexType kStart = kNumChunks * kSimdWidth;
+ constexpr IndexType Start = NumChunks * SimdWidth;
#elif defined(USE_MMX)
- constexpr IndexType kNumChunks = kInputDimensions / kSimdWidth;
+ constexpr IndexType NumChunks = InputDimensions / SimdWidth;
const __m64 k0x80s = _mm_set1_pi8(-128);
const auto in = reinterpret_cast<const __m64*>(input);
const auto out = reinterpret_cast<__m64*>(output);
- for (IndexType i = 0; i < kNumChunks; ++i) {
+ for (IndexType i = 0; i < NumChunks; ++i) {
const __m64 words0 = _mm_srai_pi16(
_mm_packs_pi32(in[i * 4 + 0], in[i * 4 + 1]),
- kWeightScaleBits);
+ WeightScaleBits);
const __m64 words1 = _mm_srai_pi16(
_mm_packs_pi32(in[i * 4 + 2], in[i * 4 + 3]),
- kWeightScaleBits);
+ WeightScaleBits);
const __m64 packedbytes = _mm_packs_pi16(words0, words1);
out[i] = _mm_subs_pi8(_mm_adds_pi8(packedbytes, k0x80s), k0x80s);
}
_mm_empty();
- constexpr IndexType kStart = kNumChunks * kSimdWidth;
+ constexpr IndexType Start = NumChunks * SimdWidth;
#elif defined(USE_NEON)
- constexpr IndexType kNumChunks = kInputDimensions / (kSimdWidth / 2);
- const int8x8_t kZero = {0};
+ constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);
+ const int8x8_t Zero = {0};
const auto in = reinterpret_cast<const int32x4_t*>(input);
const auto out = reinterpret_cast<int8x8_t*>(output);
- for (IndexType i = 0; i < kNumChunks; ++i) {
+ 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], kWeightScaleBits);
- pack[1] = vqshrn_n_s32(in[i * 2 + 1], kWeightScaleBits);
- out[i] = vmax_s8(vqmovn_s16(shifted), kZero);
+ 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 kStart = kNumChunks * (kSimdWidth / 2);
+ constexpr IndexType Start = NumChunks * (SimdWidth / 2);
#else
- constexpr IndexType kStart = 0;
+ constexpr IndexType Start = 0;
#endif
- for (IndexType i = kStart; i < kInputDimensions; ++i) {
+ for (IndexType i = Start; i < InputDimensions; ++i) {
output[i] = static_cast<OutputType>(
- std::max(0, std::min(127, input[i] >> kWeightScaleBits)));
+ std::max(0, std::min(127, input[i] >> WeightScaleBits)));
}
return output;
}
private:
- PreviousLayer previous_layer_;
+ PreviousLayer previousLayer;
};
} // namespace Stockfish::Eval::NNUE::Layers
namespace Stockfish::Eval::NNUE::Layers {
// Input layer
-template <IndexType OutputDimensions, IndexType Offset = 0>
+template <IndexType OutDims, IndexType Offset = 0>
class InputSlice {
public:
// Need to maintain alignment
- static_assert(Offset % kMaxSimdWidth == 0, "");
+ static_assert(Offset % MaxSimdWidth == 0, "");
// Output type
using OutputType = TransformedFeatureType;
// Output dimensionality
- static constexpr IndexType kOutputDimensions = OutputDimensions;
+ static constexpr IndexType OutputDimensions = OutDims;
// Size of forward propagation buffer used from the input layer to this layer
- static constexpr std::size_t kBufferSize = 0;
+ static constexpr std::size_t BufferSize = 0;
// Hash value embedded in the evaluation file
- static constexpr std::uint32_t GetHashValue() {
- std::uint32_t hash_value = 0xEC42E90Du;
- hash_value ^= kOutputDimensions ^ (Offset << 10);
- return hash_value;
+ static constexpr std::uint32_t get_hash_value() {
+ std::uint32_t hashValue = 0xEC42E90Du;
+ hashValue ^= OutputDimensions ^ (Offset << 10);
+ return hashValue;
}
// Read network parameters
- bool ReadParameters(std::istream& /*stream*/) {
+ bool read_parameters(std::istream& /*stream*/) {
return true;
}
// Forward propagation
- const OutputType* Propagate(
- const TransformedFeatureType* transformed_features,
+ const OutputType* propagate(
+ const TransformedFeatureType* transformedFeatures,
char* /*buffer*/) const {
- return transformed_features + Offset;
+ return transformedFeatures + Offset;
}
private:
enum AccumulatorState { EMPTY, COMPUTED, INIT };
// Class that holds the result of affine transformation of input features
- struct alignas(kCacheLineSize) Accumulator {
+ struct alignas(CacheLineSize) Accumulator {
std::int16_t
- accumulation[2][kRefreshTriggers.size()][kTransformedFeatureDimensions];
+ accumulation[2][RefreshTriggers.size()][TransformedFeatureDimensions];
AccumulatorState state[2];
};
namespace Stockfish::Eval::NNUE {
- static_assert(kTransformedFeatureDimensions % kMaxSimdWidth == 0, "");
- static_assert(Network::kOutputDimensions == 1, "");
+ static_assert(TransformedFeatureDimensions % MaxSimdWidth == 0, "");
+ static_assert(Network::OutputDimensions == 1, "");
static_assert(std::is_same<Network::OutputType, std::int32_t>::value, "");
// Trigger for full calculation instead of difference calculation
- constexpr auto kRefreshTriggers = RawFeatures::kRefreshTriggers;
+ constexpr auto RefreshTriggers = RawFeatures::RefreshTriggers;
} // namespace Stockfish::Eval::NNUE
namespace Stockfish::Eval::NNUE {
// Version of the evaluation file
- constexpr std::uint32_t kVersion = 0x7AF32F16u;
+ constexpr std::uint32_t Version = 0x7AF32F16u;
// Constant used in evaluation value calculation
- constexpr int FV_SCALE = 16;
- constexpr int kWeightScaleBits = 6;
+ constexpr int OutputScale = 16;
+ constexpr int WeightScaleBits = 6;
// Size of cache line (in bytes)
- constexpr std::size_t kCacheLineSize = 64;
+ constexpr std::size_t CacheLineSize = 64;
// SIMD width (in bytes)
#if defined(USE_AVX2)
- constexpr std::size_t kSimdWidth = 32;
+ constexpr std::size_t SimdWidth = 32;
#elif defined(USE_SSE2)
- constexpr std::size_t kSimdWidth = 16;
+ constexpr std::size_t SimdWidth = 16;
#elif defined(USE_MMX)
- constexpr std::size_t kSimdWidth = 8;
+ constexpr std::size_t SimdWidth = 8;
#elif defined(USE_NEON)
- constexpr std::size_t kSimdWidth = 16;
+ constexpr std::size_t SimdWidth = 16;
#endif
- constexpr std::size_t kMaxSimdWidth = 32;
+ constexpr std::size_t MaxSimdWidth = 32;
// unique number for each piece type on each square
enum {
PS_B_ROOK = 7 * SQUARE_NB + 1,
PS_W_QUEEN = 8 * SQUARE_NB + 1,
PS_B_QUEEN = 9 * SQUARE_NB + 1,
- PS_W_KING = 10 * SQUARE_NB + 1,
- PS_END = PS_W_KING, // pieces without kings (pawns included)
- PS_B_KING = 11 * SQUARE_NB + 1,
- PS_END2 = 12 * SQUARE_NB + 1
+ PS_NB = 10 * SQUARE_NB + 1
};
- constexpr uint32_t kpp_board_index[COLOR_NB][PIECE_NB] = {
+ constexpr uint32_t PieceSquareIndex[COLOR_NB][PIECE_NB] = {
// convention: W - us, B - them
// viewed from other side, W and B are reversed
- { PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_W_KING, PS_NONE,
- PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_B_KING, PS_NONE },
- { PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_B_KING, PS_NONE,
- PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_W_KING, PS_NONE }
+ { PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_NONE, PS_NONE,
+ PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_NONE, PS_NONE },
+ { PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_NONE, PS_NONE,
+ PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_NONE, PS_NONE }
};
// Type of input feature after conversion
// Round n up to be a multiple of base
template <typename IntType>
- constexpr IntType CeilToMultiple(IntType n, IntType base) {
+ constexpr IntType ceil_to_multiple(IntType n, IntType base) {
return (n + base - 1) / base * base;
}
#define vec_store(a,b) _mm512_store_si512(a,b)
#define vec_add_16(a,b) _mm512_add_epi16(a,b)
#define vec_sub_16(a,b) _mm512_sub_epi16(a,b)
- static constexpr IndexType kNumRegs = 8; // only 8 are needed
+ static constexpr IndexType NumRegs = 8; // only 8 are needed
#elif USE_AVX2
typedef __m256i vec_t;
#define vec_store(a,b) _mm256_store_si256(a,b)
#define vec_add_16(a,b) _mm256_add_epi16(a,b)
#define vec_sub_16(a,b) _mm256_sub_epi16(a,b)
- static constexpr IndexType kNumRegs = 16;
+ static constexpr IndexType NumRegs = 16;
#elif USE_SSE2
typedef __m128i vec_t;
#define vec_store(a,b) *(a)=(b)
#define vec_add_16(a,b) _mm_add_epi16(a,b)
#define vec_sub_16(a,b) _mm_sub_epi16(a,b)
- static constexpr IndexType kNumRegs = Is64Bit ? 16 : 8;
+ static constexpr IndexType NumRegs = Is64Bit ? 16 : 8;
#elif USE_MMX
typedef __m64 vec_t;
#define vec_store(a,b) *(a)=(b)
#define vec_add_16(a,b) _mm_add_pi16(a,b)
#define vec_sub_16(a,b) _mm_sub_pi16(a,b)
- static constexpr IndexType kNumRegs = 8;
+ static constexpr IndexType NumRegs = 8;
#elif USE_NEON
typedef int16x8_t vec_t;
#define vec_store(a,b) *(a)=(b)
#define vec_add_16(a,b) vaddq_s16(a,b)
#define vec_sub_16(a,b) vsubq_s16(a,b)
- static constexpr IndexType kNumRegs = 16;
+ static constexpr IndexType NumRegs = 16;
#else
#undef VECTOR
private:
// Number of output dimensions for one side
- static constexpr IndexType kHalfDimensions = kTransformedFeatureDimensions;
+ static constexpr IndexType HalfDimensions = TransformedFeatureDimensions;
#ifdef VECTOR
- static constexpr IndexType kTileHeight = kNumRegs * sizeof(vec_t) / 2;
- static_assert(kHalfDimensions % kTileHeight == 0, "kTileHeight must divide kHalfDimensions");
+ static constexpr IndexType TileHeight = NumRegs * sizeof(vec_t) / 2;
+ static_assert(HalfDimensions % TileHeight == 0, "TileHeight must divide HalfDimensions");
#endif
public:
using OutputType = TransformedFeatureType;
// Number of input/output dimensions
- static constexpr IndexType kInputDimensions = RawFeatures::kDimensions;
- static constexpr IndexType kOutputDimensions = kHalfDimensions * 2;
+ static constexpr IndexType InputDimensions = RawFeatures::Dimensions;
+ static constexpr IndexType OutputDimensions = HalfDimensions * 2;
// Size of forward propagation buffer
- static constexpr std::size_t kBufferSize =
- kOutputDimensions * sizeof(OutputType);
+ static constexpr std::size_t BufferSize =
+ OutputDimensions * sizeof(OutputType);
// Hash value embedded in the evaluation file
- static constexpr std::uint32_t GetHashValue() {
-
- return RawFeatures::kHashValue ^ kOutputDimensions;
+ static constexpr std::uint32_t get_hash_value() {
+ return RawFeatures::HashValue ^ OutputDimensions;
}
// Read network parameters
- bool ReadParameters(std::istream& stream) {
-
- for (std::size_t i = 0; i < kHalfDimensions; ++i)
- biases_[i] = read_little_endian<BiasType>(stream);
- for (std::size_t i = 0; i < kHalfDimensions * kInputDimensions; ++i)
- weights_[i] = read_little_endian<WeightType>(stream);
+ bool read_parameters(std::istream& stream) {
+ for (std::size_t i = 0; i < HalfDimensions; ++i)
+ biases[i] = read_little_endian<BiasType>(stream);
+ for (std::size_t i = 0; i < HalfDimensions * InputDimensions; ++i)
+ weights[i] = read_little_endian<WeightType>(stream);
return !stream.fail();
}
// Convert input features
- void Transform(const Position& pos, OutputType* output) const {
-
- UpdateAccumulator(pos, WHITE);
- UpdateAccumulator(pos, BLACK);
+ void transform(const Position& pos, OutputType* output) const {
+ update_accumulator(pos, WHITE);
+ update_accumulator(pos, BLACK);
const auto& accumulation = pos.state()->accumulator.accumulation;
#if defined(USE_AVX512)
- constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth * 2);
- static_assert(kHalfDimensions % (kSimdWidth * 2) == 0);
- const __m512i kControl = _mm512_setr_epi64(0, 2, 4, 6, 1, 3, 5, 7);
- const __m512i kZero = _mm512_setzero_si512();
+ constexpr IndexType NumChunks = HalfDimensions / (SimdWidth * 2);
+ static_assert(HalfDimensions % (SimdWidth * 2) == 0);
+ const __m512i Control = _mm512_setr_epi64(0, 2, 4, 6, 1, 3, 5, 7);
+ const __m512i Zero = _mm512_setzero_si512();
#elif defined(USE_AVX2)
- constexpr IndexType kNumChunks = kHalfDimensions / kSimdWidth;
- constexpr int kControl = 0b11011000;
- const __m256i kZero = _mm256_setzero_si256();
+ constexpr IndexType NumChunks = HalfDimensions / SimdWidth;
+ constexpr int Control = 0b11011000;
+ const __m256i Zero = _mm256_setzero_si256();
#elif defined(USE_SSE2)
- constexpr IndexType kNumChunks = kHalfDimensions / kSimdWidth;
+ constexpr IndexType NumChunks = HalfDimensions / SimdWidth;
#ifdef USE_SSE41
- const __m128i kZero = _mm_setzero_si128();
+ const __m128i Zero = _mm_setzero_si128();
#else
const __m128i k0x80s = _mm_set1_epi8(-128);
#endif
#elif defined(USE_MMX)
- constexpr IndexType kNumChunks = kHalfDimensions / kSimdWidth;
+ constexpr IndexType NumChunks = HalfDimensions / SimdWidth;
const __m64 k0x80s = _mm_set1_pi8(-128);
#elif defined(USE_NEON)
- constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
- const int8x8_t kZero = {0};
+ constexpr IndexType NumChunks = HalfDimensions / (SimdWidth / 2);
+ const int8x8_t Zero = {0};
#endif
const Color perspectives[2] = {pos.side_to_move(), ~pos.side_to_move()};
for (IndexType p = 0; p < 2; ++p) {
- const IndexType offset = kHalfDimensions * p;
+ const IndexType offset = HalfDimensions * p;
#if defined(USE_AVX512)
auto out = reinterpret_cast<__m512i*>(&output[offset]);
- for (IndexType j = 0; j < kNumChunks; ++j) {
+ for (IndexType j = 0; j < NumChunks; ++j) {
__m512i sum0 = _mm512_load_si512(
&reinterpret_cast<const __m512i*>(accumulation[perspectives[p]][0])[j * 2 + 0]);
__m512i sum1 = _mm512_load_si512(
&reinterpret_cast<const __m512i*>(accumulation[perspectives[p]][0])[j * 2 + 1]);
- _mm512_store_si512(&out[j], _mm512_permutexvar_epi64(kControl,
- _mm512_max_epi8(_mm512_packs_epi16(sum0, sum1), kZero)));
+ _mm512_store_si512(&out[j], _mm512_permutexvar_epi64(Control,
+ _mm512_max_epi8(_mm512_packs_epi16(sum0, sum1), Zero)));
}
#elif defined(USE_AVX2)
auto out = reinterpret_cast<__m256i*>(&output[offset]);
- for (IndexType j = 0; j < kNumChunks; ++j) {
+ for (IndexType j = 0; j < NumChunks; ++j) {
__m256i sum0 = _mm256_load_si256(
&reinterpret_cast<const __m256i*>(accumulation[perspectives[p]][0])[j * 2 + 0]);
__m256i sum1 = _mm256_load_si256(
&reinterpret_cast<const __m256i*>(accumulation[perspectives[p]][0])[j * 2 + 1]);
_mm256_store_si256(&out[j], _mm256_permute4x64_epi64(_mm256_max_epi8(
- _mm256_packs_epi16(sum0, sum1), kZero), kControl));
+ _mm256_packs_epi16(sum0, sum1), Zero), Control));
}
#elif defined(USE_SSE2)
auto out = reinterpret_cast<__m128i*>(&output[offset]);
- for (IndexType j = 0; j < kNumChunks; ++j) {
+ for (IndexType j = 0; j < NumChunks; ++j) {
__m128i sum0 = _mm_load_si128(&reinterpret_cast<const __m128i*>(
accumulation[perspectives[p]][0])[j * 2 + 0]);
__m128i sum1 = _mm_load_si128(&reinterpret_cast<const __m128i*>(
_mm_store_si128(&out[j],
#ifdef USE_SSE41
- _mm_max_epi8(packedbytes, kZero)
+ _mm_max_epi8(packedbytes, Zero)
#else
_mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s)
#endif
#elif defined(USE_MMX)
auto out = reinterpret_cast<__m64*>(&output[offset]);
- for (IndexType j = 0; j < kNumChunks; ++j) {
+ for (IndexType j = 0; j < NumChunks; ++j) {
__m64 sum0 = *(&reinterpret_cast<const __m64*>(
accumulation[perspectives[p]][0])[j * 2 + 0]);
__m64 sum1 = *(&reinterpret_cast<const __m64*>(
#elif defined(USE_NEON)
const auto out = reinterpret_cast<int8x8_t*>(&output[offset]);
- for (IndexType j = 0; j < kNumChunks; ++j) {
+ for (IndexType j = 0; j < NumChunks; ++j) {
int16x8_t sum = reinterpret_cast<const int16x8_t*>(
accumulation[perspectives[p]][0])[j];
- out[j] = vmax_s8(vqmovn_s16(sum), kZero);
+ out[j] = vmax_s8(vqmovn_s16(sum), Zero);
}
#else
- for (IndexType j = 0; j < kHalfDimensions; ++j) {
+ for (IndexType j = 0; j < HalfDimensions; ++j) {
BiasType sum = accumulation[static_cast<int>(perspectives[p])][0][j];
output[offset + j] = static_cast<OutputType>(
std::max<int>(0, std::min<int>(127, sum)));
}
private:
- void UpdateAccumulator(const Position& pos, const Color c) const {
+ void update_accumulator(const Position& pos, const Color c) const {
#ifdef VECTOR
// Gcc-10.2 unnecessarily spills AVX2 registers if this array
// is defined in the VECTOR code below, once in each branch
- vec_t acc[kNumRegs];
+ vec_t acc[NumRegs];
#endif
// Look for a usable accumulator of an earlier position. We keep track
// The first condition tests whether an incremental update is
// possible at all: if this side's king has moved, it is not possible.
static_assert(std::is_same_v<RawFeatures::SortedTriggerSet,
- Features::CompileTimeList<Features::TriggerEvent, Features::TriggerEvent::kFriendKingMoved>>,
- "Current code assumes that only kFriendlyKingMoved refresh trigger is being used.");
+ Features::CompileTimeList<Features::TriggerEvent, Features::TriggerEvent::FriendKingMoved>>,
+ "Current code assumes that only FriendlyKingMoved refresh trigger is being used.");
if ( dp.piece[0] == make_piece(c, KING)
|| (gain -= dp.dirty_num + 1) < 0)
break;
// Gather all features to be updated. This code assumes HalfKP features
// only and doesn't support refresh triggers.
- static_assert(std::is_same_v<Features::FeatureSet<Features::HalfKP<Features::Side::kFriend>>,
+ static_assert(std::is_same_v<Features::FeatureSet<Features::HalfKP<Features::Side::Friend>>,
RawFeatures>);
Features::IndexList removed[2], added[2];
- Features::HalfKP<Features::Side::kFriend>::AppendChangedIndices(pos,
+ Features::HalfKP<Features::Side::Friend>::append_changed_indices(pos,
next->dirtyPiece, c, &removed[0], &added[0]);
for (StateInfo *st2 = pos.state(); st2 != next; st2 = st2->previous)
- Features::HalfKP<Features::Side::kFriend>::AppendChangedIndices(pos,
+ Features::HalfKP<Features::Side::Friend>::append_changed_indices(pos,
st2->dirtyPiece, c, &removed[1], &added[1]);
// Mark the accumulators as computed.
StateInfo *info[3] =
{ next, next == pos.state() ? nullptr : pos.state(), nullptr };
#ifdef VECTOR
- for (IndexType j = 0; j < kHalfDimensions / kTileHeight; ++j)
+ for (IndexType j = 0; j < HalfDimensions / TileHeight; ++j)
{
// Load accumulator
auto accTile = reinterpret_cast<vec_t*>(
- &st->accumulator.accumulation[c][0][j * kTileHeight]);
- for (IndexType k = 0; k < kNumRegs; ++k)
+ &st->accumulator.accumulation[c][0][j * TileHeight]);
+ for (IndexType k = 0; k < NumRegs; ++k)
acc[k] = vec_load(&accTile[k]);
for (IndexType i = 0; info[i]; ++i)
// Difference calculation for the deactivated features
for (const auto index : removed[i])
{
- const IndexType offset = kHalfDimensions * index + j * kTileHeight;
- auto column = reinterpret_cast<const vec_t*>(&weights_[offset]);
- for (IndexType k = 0; k < kNumRegs; ++k)
+ const IndexType offset = HalfDimensions * index + j * TileHeight;
+ auto column = reinterpret_cast<const vec_t*>(&weights[offset]);
+ for (IndexType k = 0; k < NumRegs; ++k)
acc[k] = vec_sub_16(acc[k], column[k]);
}
// Difference calculation for the activated features
for (const auto index : added[i])
{
- const IndexType offset = kHalfDimensions * index + j * kTileHeight;
- auto column = reinterpret_cast<const vec_t*>(&weights_[offset]);
- for (IndexType k = 0; k < kNumRegs; ++k)
+ const IndexType offset = HalfDimensions * index + j * TileHeight;
+ auto column = reinterpret_cast<const vec_t*>(&weights[offset]);
+ for (IndexType k = 0; k < NumRegs; ++k)
acc[k] = vec_add_16(acc[k], column[k]);
}
// Store accumulator
accTile = reinterpret_cast<vec_t*>(
- &info[i]->accumulator.accumulation[c][0][j * kTileHeight]);
- for (IndexType k = 0; k < kNumRegs; ++k)
+ &info[i]->accumulator.accumulation[c][0][j * TileHeight]);
+ for (IndexType k = 0; k < NumRegs; ++k)
vec_store(&accTile[k], acc[k]);
}
}
{
std::memcpy(info[i]->accumulator.accumulation[c][0],
st->accumulator.accumulation[c][0],
- kHalfDimensions * sizeof(BiasType));
+ HalfDimensions * sizeof(BiasType));
st = info[i];
// Difference calculation for the deactivated features
for (const auto index : removed[i])
{
- const IndexType offset = kHalfDimensions * index;
+ const IndexType offset = HalfDimensions * index;
- for (IndexType j = 0; j < kHalfDimensions; ++j)
- st->accumulator.accumulation[c][0][j] -= weights_[offset + j];
+ for (IndexType j = 0; j < HalfDimensions; ++j)
+ st->accumulator.accumulation[c][0][j] -= weights[offset + j];
}
// Difference calculation for the activated features
for (const auto index : added[i])
{
- const IndexType offset = kHalfDimensions * index;
+ const IndexType offset = HalfDimensions * index;
- for (IndexType j = 0; j < kHalfDimensions; ++j)
- st->accumulator.accumulation[c][0][j] += weights_[offset + j];
+ for (IndexType j = 0; j < HalfDimensions; ++j)
+ st->accumulator.accumulation[c][0][j] += weights[offset + j];
}
}
#endif
auto& accumulator = pos.state()->accumulator;
accumulator.state[c] = COMPUTED;
Features::IndexList active;
- Features::HalfKP<Features::Side::kFriend>::AppendActiveIndices(pos, c, &active);
+ Features::HalfKP<Features::Side::Friend>::append_active_indices(pos, c, &active);
#ifdef VECTOR
- for (IndexType j = 0; j < kHalfDimensions / kTileHeight; ++j)
+ for (IndexType j = 0; j < HalfDimensions / TileHeight; ++j)
{
auto biasesTile = reinterpret_cast<const vec_t*>(
- &biases_[j * kTileHeight]);
- for (IndexType k = 0; k < kNumRegs; ++k)
+ &biases[j * TileHeight]);
+ for (IndexType k = 0; k < NumRegs; ++k)
acc[k] = biasesTile[k];
for (const auto index : active)
{
- const IndexType offset = kHalfDimensions * index + j * kTileHeight;
- auto column = reinterpret_cast<const vec_t*>(&weights_[offset]);
+ const IndexType offset = HalfDimensions * index + j * TileHeight;
+ auto column = reinterpret_cast<const vec_t*>(&weights[offset]);
- for (unsigned k = 0; k < kNumRegs; ++k)
+ for (unsigned k = 0; k < NumRegs; ++k)
acc[k] = vec_add_16(acc[k], column[k]);
}
auto accTile = reinterpret_cast<vec_t*>(
- &accumulator.accumulation[c][0][j * kTileHeight]);
- for (unsigned k = 0; k < kNumRegs; k++)
+ &accumulator.accumulation[c][0][j * TileHeight]);
+ for (unsigned k = 0; k < NumRegs; k++)
vec_store(&accTile[k], acc[k]);
}
#else
- std::memcpy(accumulator.accumulation[c][0], biases_,
- kHalfDimensions * sizeof(BiasType));
+ std::memcpy(accumulator.accumulation[c][0], biases,
+ HalfDimensions * sizeof(BiasType));
for (const auto index : active)
{
- const IndexType offset = kHalfDimensions * index;
+ const IndexType offset = HalfDimensions * index;
- for (IndexType j = 0; j < kHalfDimensions; ++j)
- accumulator.accumulation[c][0][j] += weights_[offset + j];
+ for (IndexType j = 0; j < HalfDimensions; ++j)
+ accumulator.accumulation[c][0][j] += weights[offset + j];
}
#endif
}
using BiasType = std::int16_t;
using WeightType = std::int16_t;
- alignas(kCacheLineSize) BiasType biases_[kHalfDimensions];
- alignas(kCacheLineSize)
- WeightType weights_[kHalfDimensions * kInputDimensions];
+ alignas(CacheLineSize) BiasType biases[HalfDimensions];
+ alignas(CacheLineSize)
+ WeightType weights[HalfDimensions * InputDimensions];
};
} // namespace Stockfish::Eval::NNUE
&& !pos.can_castle(ANY_CASTLING))
{
StateInfo st;
- ASSERT_ALIGNED(&st, Eval::NNUE::kCacheLineSize);
+ ASSERT_ALIGNED(&st, Eval::NNUE::CacheLineSize);
Position p;
p.set(pos.fen(), pos.is_chess960(), &st, pos.this_thread());
assert(0 && "pos_is_ok: Bitboards");
StateInfo si = *st;
- ASSERT_ALIGNED(&si, Eval::NNUE::kCacheLineSize);
+ ASSERT_ALIGNED(&si, Eval::NNUE::CacheLineSize);
set_state(&si);
if (std::memcmp(&si, st, sizeof(StateInfo)))
uint64_t perft(Position& pos, Depth depth) {
StateInfo st;
- ASSERT_ALIGNED(&st, Eval::NNUE::kCacheLineSize);
+ ASSERT_ALIGNED(&st, Eval::NNUE::CacheLineSize);
uint64_t cnt, nodes = 0;
const bool leaf = (depth == 2);
Move pv[MAX_PLY+1], capturesSearched[32], quietsSearched[64];
StateInfo st;
- ASSERT_ALIGNED(&st, Eval::NNUE::kCacheLineSize);
+ ASSERT_ALIGNED(&st, Eval::NNUE::CacheLineSize);
TTEntry* tte;
Key posKey;
Move pv[MAX_PLY+1];
StateInfo st;
- ASSERT_ALIGNED(&st, Eval::NNUE::kCacheLineSize);
+ ASSERT_ALIGNED(&st, Eval::NNUE::CacheLineSize);
TTEntry* tte;
Key posKey;
bool RootMove::extract_ponder_from_tt(Position& pos) {
StateInfo st;
- ASSERT_ALIGNED(&st, Eval::NNUE::kCacheLineSize);
+ ASSERT_ALIGNED(&st, Eval::NNUE::CacheLineSize);
bool ttHit;