diff --git a/src/Makefile b/src/Makefile index 8f30fff6..33a2434d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -49,14 +49,6 @@ SRCS = benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp main.cpp search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp \ nnue/evaluate_nnue.cpp \ nnue/features/half_kp.cpp \ - nnue/features/half_ka.cpp \ - nnue/features/half_relative_kp.cpp \ - nnue/features/half_relative_ka.cpp \ - nnue/features/k.cpp \ - nnue/features/p.cpp \ - nnue/features/a.cpp \ - nnue/features/castling_right.cpp \ - nnue/features/enpassant.cpp \ tools/sfen_packer.cpp \ tools/gensfen.cpp \ tools/gensfen_nonpv.cpp \ diff --git a/src/nnue/architectures/halfka_256x2-32-32.h b/src/nnue/architectures/halfka_256x2-32-32.h deleted file mode 100644 index c108ef5d..00000000 --- a/src/nnue/architectures/halfka_256x2-32-32.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - Stockfish, a UCI chess playing engine derived from Glaurung 2.1 - Copyright (C) 2004-2020 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 . -*/ - -// Definition of input features and network structure used in NNUE evaluation function - -#ifndef NNUE_HALFKA_256X2_32_32_H_INCLUDED -#define NNUE_HALFKA_256X2_32_32_H_INCLUDED - -#include "nnue/features/feature_set.h" -#include "nnue/features/half_ka.h" - -#include "nnue/layers/input_slice.h" -#include "nnue/layers/affine_transform.h" -#include "nnue/layers/clipped_relu.h" - -namespace Eval::NNUE { - - // Input features used in evaluation function - using RawFeatures = Features::FeatureSet< - Features::HalfKA>; - - // Number of input feature dimensions after conversion - constexpr IndexType kTransformedFeatureDimensions = 256; - - namespace Layers { - - // Define network structure - using InputLayer = InputSlice; - using HiddenLayer1 = ClippedReLU>; - using HiddenLayer2 = ClippedReLU>; - using OutputLayer = AffineTransform; - - } // namespace Layers - - using Network = Layers::OutputLayer; - -} // namespace Eval::NNUE - -#endif // #ifndef NNUE_HALFA_256X2_32_32_H_INCLUDED diff --git a/src/nnue/architectures/halfkp-cr-ep_256x2-32-32.h b/src/nnue/architectures/halfkp-cr-ep_256x2-32-32.h deleted file mode 100644 index 6327b78a..00000000 --- a/src/nnue/architectures/halfkp-cr-ep_256x2-32-32.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - Stockfish, a UCI chess playing engine derived from Glaurung 2.1 - Copyright (C) 2004-2020 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 . -*/ - -// Definition of input features and network structure used in NNUE evaluation function - -#ifndef NNUE_HALFKP_CR_EP_256X2_32_32_H_INCLUDED -#define NNUE_HALFKP_CR_EP_256X2_32_32_H_INCLUDED - -#include "nnue/features/feature_set.h" -#include "nnue/features/half_kp.h" -#include "nnue/features/castling_right.h" -#include "nnue/features/enpassant.h" - -#include "nnue/layers/input_slice.h" -#include "nnue/layers/affine_transform.h" -#include "nnue/layers/clipped_relu.h" - -namespace Eval::NNUE { - - // Input features used in evaluation function - using RawFeatures = Features::FeatureSet< - Features::HalfKP, Features::CastlingRight, - Features::EnPassant>; - - // Number of input feature dimensions after conversion - constexpr IndexType kTransformedFeatureDimensions = 256; - - namespace Layers { - - // Define network structure - using InputLayer = InputSlice; - using HiddenLayer1 = ClippedReLU>; - using HiddenLayer2 = ClippedReLU>; - using OutputLayer = AffineTransform; - - } // namespace Layers - - using Network = Layers::OutputLayer; - -} // namespace Eval::NNUE - -#endif // #ifndef NNUE_HALFKP_CR_EP_256X2_32_32_H_INCLUDED diff --git a/src/nnue/architectures/halfkp-cr_256x2-32-32.h b/src/nnue/architectures/halfkp-cr_256x2-32-32.h deleted file mode 100644 index dd587d1d..00000000 --- a/src/nnue/architectures/halfkp-cr_256x2-32-32.h +++ /dev/null @@ -1,37 +0,0 @@ -// Definition of input features and network structure used in NNUE evaluation function - -#ifndef NNUE_HALFKP_CR_256X2_32_32_H_INCLUDED -#define NNUE_HALFKP_CR_256X2_32_32_H_INCLUDED - -#include "nnue/features/feature_set.h" -#include "nnue/features/half_kp.h" -#include "nnue/features/castling_right.h" - -#include "nnue/layers/input_slice.h" -#include "nnue/layers/affine_transform.h" -#include "nnue/layers/clipped_relu.h" - -namespace Eval::NNUE { - - // Input features used in evaluation function - using RawFeatures = Features::FeatureSet< - Features::HalfKP, Features::CastlingRight>; - - // Number of input feature dimensions after conversion - constexpr IndexType kTransformedFeatureDimensions = 256; - - namespace Layers { - - // Define network structure - using InputLayer = InputSlice; - using HiddenLayer1 = ClippedReLU>; - using HiddenLayer2 = ClippedReLU>; - using OutputLayer = AffineTransform; - - } // namespace Layers - - using Network = Layers::OutputLayer; - -} // namespace Eval::NNUE - -#endif // #ifndef NNUE_HALFKP_CR_256X2_32_32_H_INCLUDED diff --git a/src/nnue/architectures/halfkp_384x2-32-32.h b/src/nnue/architectures/halfkp_384x2-32-32.h deleted file mode 100644 index 96913295..00000000 --- a/src/nnue/architectures/halfkp_384x2-32-32.h +++ /dev/null @@ -1,35 +0,0 @@ -// Definition of input features and network structure used in NNUE evaluation function - -#ifndef HALFKP_384X2_32_32_H -#define HALFKP_384X2_32_32_H - -#include "nnue/features/feature_set.h" -#include "nnue/features/half_kp.h" - -#include "nnue/layers/input_slice.h" -#include "nnue/layers/affine_transform.h" -#include "nnue/layers/clipped_relu.h" - -namespace Eval::NNUE { - - // Input features used in evaluation function - using RawFeatures = Features::FeatureSet< - Features::HalfKP>; - - // Number of input feature dimensions after conversion - constexpr IndexType kTransformedFeatureDimensions = 384; - - namespace Layers { - - // define network structure - using InputLayer = InputSlice; - using HiddenLayer1 = ClippedReLU>; - using HiddenLayer2 = ClippedReLU>; - using OutputLayer = AffineTransform; - - } // namespace Layers - - using Network = Layers::OutputLayer; - -} // namespace Eval::NNUE -#endif // HALFKP_384X2_32_32_H diff --git a/src/nnue/features/a.cpp b/src/nnue/features/a.cpp deleted file mode 100644 index 1bfb583f..00000000 --- a/src/nnue/features/a.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "a.h" -#include "index_list.h" - -// Definition of input feature A of NNUE evaluation function -namespace Eval::NNUE::Features { - - // Orient a square according to perspective (rotate the board 180° for black) - // Important note for "halfka": this arch was designed with "flip" in mind - // although it still is untested which approach is better. - // this has to stay until we find a better arch that works with "flip". - // allows us to use current master net for gensfen (primarily needed for higher quality data) - inline Square orient(Color perspective, Square s) { - return Square(int(s) ^ (bool(perspective) * 63)); - } - - // Find the index of the feature quantity from the king position and PieceSquare - inline IndexType A::make_index( - Color perspective, Square s, Piece pc) { - return IndexType(orient(perspective, s) + kpp_board_index[pc][perspective]); - } - - // Get a list of indices with a value of 1 among the features - void A::append_active_indices( - const Position& pos, - Color perspective, - IndexList* active) { - - Bitboard bb = pos.pieces(); - while (bb) { - Square s = pop_lsb(&bb); - active->push_back(make_index(perspective, s, pos.piece_on(s))); - } - } - - // Get a list of indices whose values ​​have changed from the previous one in the feature quantity - void A::append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added) { - - const auto& dp = pos.state()->dirtyPiece; - for (int i = 0; i < dp.dirty_num; ++i) { - Piece pc = dp.piece[i]; - - if (dp.from[i] != SQ_NONE) - removed->push_back(make_index(perspective, dp.from[i], pc)); - - if (dp.to[i] != SQ_NONE) - added->push_back(make_index(perspective, dp.to[i], pc)); - } - } - -} // namespace Eval::NNUE::Features diff --git a/src/nnue/features/a.h b/src/nnue/features/a.h deleted file mode 100644 index 50a0d8be..00000000 --- a/src/nnue/features/a.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef _NNUE_FEATURES_A_H_ -#define _NNUE_FEATURES_A_H_ - -#include "features_common.h" - -#include "evaluate.h" - -// Definition of input feature A of NNUE evaluation function -// A is a union of P features and K features, so technically the -// same effect can be achieved by including both P and K features -// but it would result in slower index appending because -// P would conditionally exclude K features and vice versa, -// where A doesn't have any conditionals. -namespace Eval::NNUE::Features { - - // Feature P: PieceSquare of pieces other than balls - class A { - public: - // feature quantity name - static constexpr const char* kName = "A"; - - // Hash value embedded in the evaluation function file - static constexpr std::uint32_t kHashValue = 0x7A4C414Cu; - - // number of feature dimensions - static constexpr IndexType kDimensions = PS_END2; - - // The maximum value of the number of indexes whose value is 1 at the same time among the feature values - static constexpr IndexType kMaxActiveDimensions = 32; - - // Timing of full calculation instead of difference calculation - static constexpr TriggerEvent kRefreshTrigger = TriggerEvent::kNone; - - // Get a list of indices with a value of 1 among the features - static void append_active_indices( - const Position& pos, - Color perspective, - IndexList* active); - - // Get a list of indices whose values ​​have changed from the previous one in the feature quantity - static void append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added); - - private: - // Index of a feature for a given piece on some square - static IndexType make_index(Color perspective, Square s, Piece pc); - }; - -} // namespace Eval::NNUE::Features - -#endif // #ifndef _NNUE_FEATURES_UNION_P_K_H_ diff --git a/src/nnue/features/castling_right.cpp b/src/nnue/features/castling_right.cpp deleted file mode 100644 index cbac0851..00000000 --- a/src/nnue/features/castling_right.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "castling_right.h" -#include "index_list.h" - -//Definition of input feature quantity CastlingRight of NNUE evaluation function -namespace Eval::NNUE::Features { - - // Get a list of indices with a value of 1 among the features - void CastlingRight::append_active_indices( - const Position& pos, - Color perspective, - IndexList* active) { - - // do nothing if array size is small to avoid compiler warning - if (RawFeatures::kMaxActiveDimensions < kMaxActiveDimensions) return; - - int castling_rights = pos.state()->castlingRights; - int relative_castling_rights; - if (perspective == WHITE) { - relative_castling_rights = castling_rights; - } - else { - // Invert the perspective. - relative_castling_rights = ((castling_rights & 3) << 2) - & ((castling_rights >> 2) & 3); - } - - for (Eval::NNUE::IndexType i = 0; i < kDimensions; ++i) { - if (relative_castling_rights & (1 << i)) { - active->push_back(i); - } - } - } - - // Get a list of indices whose values ​​have changed from the previous one in the feature quantity - void CastlingRight::append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* /* added */) { - - int previous_castling_rights = pos.state()->previous->castlingRights; - int current_castling_rights = pos.state()->castlingRights; - int relative_previous_castling_rights; - int relative_current_castling_rights; - if (perspective == WHITE) { - relative_previous_castling_rights = previous_castling_rights; - relative_current_castling_rights = current_castling_rights; - } - else { - // Invert the perspective. - relative_previous_castling_rights = ((previous_castling_rights & 3) << 2) - & ((previous_castling_rights >> 2) & 3); - relative_current_castling_rights = ((current_castling_rights & 3) << 2) - & ((current_castling_rights >> 2) & 3); - } - - for (Eval::NNUE::IndexType i = 0; i < kDimensions; ++i) { - if ((relative_previous_castling_rights & (1 << i)) && - (relative_current_castling_rights & (1 << i)) == 0) { - removed->push_back(i); - } - } - } - -} // namespace Eval::NNUE::Features diff --git a/src/nnue/features/castling_right.h b/src/nnue/features/castling_right.h deleted file mode 100644 index cada24b6..00000000 --- a/src/nnue/features/castling_right.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef _NNUE_FEATURES_CASTLING_RIGHT_H_ -#define _NNUE_FEATURES_CASTLING_RIGHT_H_ - -#include "features_common.h" - -#include "evaluate.h" - -//Definition of input feature quantity CastlingRight of NNUE evaluation function -namespace Eval::NNUE::Features { - - class CastlingRight { - public: - // feature quantity name - static constexpr const char* kName = "CastlingRight"; - - // Hash value embedded in the evaluation function file - static constexpr std::uint32_t kHashValue = 0x913968AAu; - - // number of feature dimensions - static constexpr IndexType kDimensions = 4; - - // The maximum value of the number of indexes whose value is 1 at the same time among the feature values - static constexpr IndexType kMaxActiveDimensions = 4; - - // Timing of full calculation instead of difference calculation - static constexpr TriggerEvent kRefreshTrigger = TriggerEvent::kNone; - - // Get a list of indices with a value of 1 among the features - static void append_active_indices( - const Position& pos, - Color perspective, - IndexList* active); - - // Get a list of indices whose values ​​have changed from the previous one in the feature quantity - static void append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added); - }; - -} // namespace Eval::NNUE::Features - -#endif diff --git a/src/nnue/features/enpassant.cpp b/src/nnue/features/enpassant.cpp deleted file mode 100644 index 06ba2d49..00000000 --- a/src/nnue/features/enpassant.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include "enpassant.h" -#include "index_list.h" - -//Definition of input feature quantity EnPassant of NNUE evaluation function -namespace Eval::NNUE::Features { - - // Get a list of indices with a value of 1 among the features - void EnPassant::append_active_indices( - const Position& pos, - Color /* perspective */, - IndexList* active) { - - // do nothing if array size is small to avoid compiler warning - if (RawFeatures::kMaxActiveDimensions < kMaxActiveDimensions) - return; - - auto epSquare = pos.state()->epSquare; - if (epSquare == SQ_NONE) - return; - - auto file = file_of(epSquare); - active->push_back(file); - } - - // Get a list of indices whose values ​​have changed from the previous one in the feature quantity - void EnPassant::append_changed_indices( - const Position& pos, - Color /* perspective */, - IndexList* removed, - IndexList* added) { - - auto previous_epSquare = pos.state()->previous->epSquare; - auto epSquare = pos.state()->epSquare; - - if (previous_epSquare != SQ_NONE) { - if (epSquare != SQ_NONE && file_of(epSquare) == file_of(previous_epSquare)) - return; - - auto file = file_of(previous_epSquare); - removed->push_back(file); - } - - if (epSquare != SQ_NONE) { - auto file = file_of(epSquare); - added->push_back(file); - } - } - -} // namespace Eval::NNUE::Features diff --git a/src/nnue/features/enpassant.h b/src/nnue/features/enpassant.h deleted file mode 100644 index 6ccb6046..00000000 --- a/src/nnue/features/enpassant.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef _NNUE_FEATURES_ENPASSANT_H_ -#define _NNUE_FEATURES_ENPASSANT_H_ - -#include "features_common.h" - -#include "evaluate.h" - -//Definition of input feature quantity EnPassant of NNUE evaluation function -namespace Eval::NNUE::Features { - - class EnPassant { - public: - // feature quantity name - static constexpr const char* kName = "EnPassant"; - // Hash value embedded in the evaluation function file - static constexpr std::uint32_t kHashValue = 0x02924F91u; - // number of feature dimensions - static constexpr IndexType kDimensions = 8; - // The maximum value of the number of indexes whose value is 1 at the same time among the feature values - static constexpr IndexType kMaxActiveDimensions = 1; - // Timing of full calculation instead of difference calculation - static constexpr TriggerEvent kRefreshTrigger = TriggerEvent::kNone; - - // Get a list of indices with a value of 1 among the features - static void append_active_indices( - const Position& pos, - Color perspective, - IndexList* active); - - // Get a list of indices whose values have changed from the previous one in the feature quantity - static void append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added); - }; - -} // namespace Eval::NNUE::Features - -#endif diff --git a/src/nnue/features/half_ka.cpp b/src/nnue/features/half_ka.cpp deleted file mode 100644 index 08124b96..00000000 --- a/src/nnue/features/half_ka.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/* - Stockfish, a UCI chess playing engine derived from Glaurung 2.1 - Copyright (C) 2004-2020 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 . -*/ - -//Definition of input features HalfKA of NNUE evaluation function - -#include "half_ka.h" -#include "index_list.h" - -namespace Eval::NNUE::Features { - - // Orient a square according to perspective (rotate the board 180° for black) - // Important note for "halfka": this arch was designed with "flip" in mind - // although it still is untested which approach is better. - // this has to stay until we find a better arch that works with "flip". - // allows us to use current master net for gensfen (primarily needed for higher quality data) - inline Square orient(Color perspective, Square s) { - return Square(int(s) ^ (bool(perspective) * 63)); - } - - // Find the index of the feature quantity from the king position and PieceSquare - template - inline IndexType HalfKA::make_index( - Color perspective, - Square s, - Piece pc, - Square ksq) { - - return IndexType(orient(perspective, s) + kpp_board_index[pc][perspective] + PS_END2 * ksq); - } - - // Get a list of indices for active features - template - void HalfKA::append_active_indices( - const Position& pos, - Color perspective, - IndexList* active) { - - Square ksq = orient( - perspective, - pos.square( - AssociatedKing == Side::kFriend ? perspective : ~perspective)); - - Bitboard bb = pos.pieces(); - while (bb) { - Square s = pop_lsb(&bb); - active->push_back(make_index(perspective, s, pos.piece_on(s), ksq)); - } - } - - // Get a list of indices for recently changed features - template - void HalfKA::append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added) { - - Square ksq = orient( - perspective, - pos.square( - AssociatedKing == Side::kFriend ? perspective : ~perspective)); - - const auto& dp = pos.state()->dirtyPiece; - for (int i = 0; i < dp.dirty_num; ++i) { - Piece pc = dp.piece[i]; - - if (dp.from[i] != SQ_NONE) - removed->push_back(make_index(perspective, dp.from[i], pc, ksq)); - - if (dp.to[i] != SQ_NONE) - added->push_back(make_index(perspective, dp.to[i], pc, ksq)); - } - } - - template class HalfKA; - template class HalfKA; - -} // namespace Eval::NNUE::Features diff --git a/src/nnue/features/half_ka.h b/src/nnue/features/half_ka.h deleted file mode 100644 index 2839357e..00000000 --- a/src/nnue/features/half_ka.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - Stockfish, a UCI chess playing engine derived from Glaurung 2.1 - Copyright (C) 2004-2020 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 . -*/ - -#ifndef NNUE_FEATURES_HALF_KA_H_INCLUDED -#define NNUE_FEATURES_HALF_KA_H_INCLUDED - -#include "features_common.h" - -#include "evaluate.h" - -//Definition of input features HalfKPK of NNUE evaluation function -namespace Eval::NNUE::Features { - - // Feature HalfKPK: Combination of the position of own king - // and the position of pieces other than kings - template - class HalfKA { - - public: - // Feature name - static constexpr const char* kName = (AssociatedKing == Side::kFriend) ? - "HalfKA(Friend)" : "HalfKA(Enemy)"; - - // Hash value embedded in the evaluation file - static constexpr std::uint32_t kHashValue = - 0x5F134CB9u ^ (AssociatedKing == Side::kFriend); - - // Number of feature dimensions - static constexpr IndexType kDimensions = - static_cast(SQUARE_NB) * static_cast(PS_END2); - - // Maximum number of simultaneously active features - static constexpr IndexType kMaxActiveDimensions = 32; - - // Trigger for full calculation instead of difference calculation - static constexpr TriggerEvent kRefreshTrigger = - (AssociatedKing == Side::kFriend) ? - TriggerEvent::kFriendKingMoved : TriggerEvent::kEnemyKingMoved; - - // Get a list of indices for active features - static void append_active_indices( - const Position& pos, - Color perspective, - IndexList* active); - - // Get a list of indices for recently changed features - static void append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added); - - private: - // Index of a feature for a given king position and another piece on some square - static IndexType make_index(Color perspective, Square s, Piece pc, Square sq_k); - }; - -} // namespace Eval::NNUE::Features - -#endif // #ifndef NNUE_FEATURES_HALF_KA_H_INCLUDED diff --git a/src/nnue/features/half_relative_ka.cpp b/src/nnue/features/half_relative_ka.cpp deleted file mode 100644 index d2ad31e6..00000000 --- a/src/nnue/features/half_relative_ka.cpp +++ /dev/null @@ -1,90 +0,0 @@ -#include "half_relative_ka.h" -#include "index_list.h" - -//Definition of input features HalfRelativeKA of NNUE evaluation function -namespace Eval::NNUE::Features { - - // Orient a square according to perspective (rotate the board 180° for black) - // Important note for "halfka": this arch was designed with "flip" in mind - // although it still is untested which approach is better. - // this has to stay until we find a better arch that works with "flip". - // allows us to use current master net for gensfen (primarily needed for higher quality data) - inline Square orient(Color perspective, Square s) { - return Square(int(s) ^ (bool(perspective) * 63)); - } - - // Find the index of the feature quantity from the ball position and PieceSquare - template - inline IndexType HalfRelativeKA::make_index( - Color perspective, - Square s, - Piece pc, - Square sq_k) { - - const IndexType p = IndexType(orient(perspective, s) + kpp_board_index[pc][perspective]); - return make_index(sq_k, p); - } - - // Find the index of the feature quantity from the ball position and PieceSquare - template - inline IndexType HalfRelativeKA::make_index( - Square sq_k, - IndexType p) { - - constexpr IndexType W = kBoardWidth; - constexpr IndexType H = kBoardHeight; - const IndexType piece_index = (p - PS_W_PAWN) / SQUARE_NB; - const Square sq_p = static_cast((p - PS_W_PAWN) % SQUARE_NB); - const IndexType relative_file = file_of(sq_p) - file_of(sq_k) + (W / 2); - const IndexType relative_rank = rank_of(sq_p) - rank_of(sq_k) + (H / 2); - return H * W * piece_index + H * relative_file + relative_rank; - } - - // Get a list of indices with a value of 1 among the features - template - void HalfRelativeKA::append_active_indices( - const Position& pos, - Color perspective, - IndexList* active) { - - Square ksq = orient( - perspective, - pos.square( - AssociatedKing == Side::kFriend ? perspective : ~perspective)); - - Bitboard bb = pos.pieces(); - while (bb) { - Square s = pop_lsb(&bb); - active->push_back(make_index(perspective, s, pos.piece_on(s), ksq)); - } - } - - // Get a list of indices whose values ​​have changed from the previous one in the feature quantity - template - void HalfRelativeKA::append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added) { - - Square ksq = orient( - perspective, - pos.square( - AssociatedKing == Side::kFriend ? perspective : ~perspective)); - - const auto& dp = pos.state()->dirtyPiece; - for (int i = 0; i < dp.dirty_num; ++i) { - Piece pc = dp.piece[i]; - - if (dp.from[i] != SQ_NONE) - removed->push_back(make_index(perspective, dp.from[i], pc, ksq)); - - if (dp.to[i] != SQ_NONE) - added->push_back(make_index(perspective, dp.to[i], pc, ksq)); - } - } - - template class HalfRelativeKA; - template class HalfRelativeKA; - -} // namespace Eval::NNUE::Features diff --git a/src/nnue/features/half_relative_ka.h b/src/nnue/features/half_relative_ka.h deleted file mode 100644 index f42661e9..00000000 --- a/src/nnue/features/half_relative_ka.h +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef _NNUE_FEATURES_HALF_RELATIVE_KA_H_ -#define _NNUE_FEATURES_HALF_RELATIVE_KA_H_ - -#include "features_common.h" - -#include "evaluate.h" - -// Definition of input features HalfRelativeKA of NNUE evaluation function -// K - King -// A - Any piece -// KA - product of K and A -namespace Eval::NNUE::Features { - - // Feature HalfRelativeKA: Relative position of each piece other than the ball based on own ball or enemy ball - template - class HalfRelativeKA { - public: - // feature quantity name - static constexpr const char* kName = (AssociatedKing == Side::kFriend) ? - "HalfRelativeKA(Friend)" : "HalfRelativeKA(Enemy)"; - - // Hash value embedded in the evaluation function file - static constexpr std::uint32_t kHashValue = - 0xA123051Fu ^ (AssociatedKing == Side::kFriend); - - static constexpr IndexType kNumPieceKinds = 6 * 2; - - // width of the virtual board with the ball in the center - static constexpr IndexType kBoardWidth = FILE_NB * 2 - 1; - - // height of a virtual board with balls in the center - static constexpr IndexType kBoardHeight = RANK_NB * 2 - 1; - - // number of feature dimensions - static constexpr IndexType kDimensions = - kNumPieceKinds * kBoardHeight * kBoardWidth; - - // The maximum value of the number of indexes whose value is 1 at the same time among the feature values - static constexpr IndexType kMaxActiveDimensions = 32; - - // Timing of full calculation instead of difference calculation - static constexpr TriggerEvent kRefreshTrigger = - (AssociatedKing == Side::kFriend) ? - TriggerEvent::kFriendKingMoved : TriggerEvent::kEnemyKingMoved; - - // Get a list of indices with a value of 1 among the features - static void append_active_indices( - const Position& pos, - Color perspective, - IndexList* active); - - // Get a list of indices whose values ​​have changed from the previous one in the feature quantity - static void append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added); - - // Find the index of the feature quantity from the ball position and PieceSquare - static IndexType make_index(Square s, IndexType p); - - // Find the index of the feature quantity from the ball position and PieceSquare - static IndexType make_index(Color perspective, Square s, Piece pc, Square sq_k); - }; - -} // namespace Eval::NNUE::Features - -#endif // #ifndef _NNUE_FEATURES_HALF_RELATIVE_KA_H_ diff --git a/src/nnue/features/half_relative_kp.cpp b/src/nnue/features/half_relative_kp.cpp deleted file mode 100644 index 2ebccd59..00000000 --- a/src/nnue/features/half_relative_kp.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "half_relative_kp.h" -#include "index_list.h" - -//Definition of input features HalfRelativeKP of NNUE evaluation function -namespace Eval::NNUE::Features { - - // Orient a square according to perspective (rotate the board 180° for black) - // this has to stay until we find a better arch that works with "flip". - // allows us to use current master net for gensfen (primarily needed for higher quality data) - inline Square orient(Color perspective, Square s) { - return Square(int(s) ^ (bool(perspective) * 63)); - } - - // Find the index of the feature quantity from the ball position and PieceSquare - template - inline IndexType HalfRelativeKP::make_index( - Color perspective, - Square s, - Piece pc, - Square sq_k) { - - const IndexType p = IndexType(orient(perspective, s) + kpp_board_index[pc][perspective]); - return make_index(sq_k, p); - } - - // Find the index of the feature quantity from the ball position and PieceSquare - template - inline IndexType HalfRelativeKP::make_index( - Square sq_k, - IndexType p) { - - constexpr IndexType W = kBoardWidth; - constexpr IndexType H = kBoardHeight; - const IndexType piece_index = (p - PS_W_PAWN) / SQUARE_NB; - const Square sq_p = static_cast((p - PS_W_PAWN) % SQUARE_NB); - const IndexType relative_file = file_of(sq_p) - file_of(sq_k) + (W / 2); - const IndexType relative_rank = rank_of(sq_p) - rank_of(sq_k) + (H / 2); - return H * W * piece_index + H * relative_file + relative_rank; - } - - // Get a list of indices with a value of 1 among the features - template - void HalfRelativeKP::append_active_indices( - const Position& pos, - Color perspective, - IndexList* active) { - - Square ksq = orient( - perspective, - pos.square( - AssociatedKing == Side::kFriend ? perspective : ~perspective)); - - Bitboard bb = pos.pieces() & ~pos.pieces(KING); - while (bb) { - Square s = pop_lsb(&bb); - active->push_back(make_index(perspective, s, pos.piece_on(s), ksq)); - } - } - - // Get a list of indices whose values ​​have changed from the previous one in the feature quantity - template - void HalfRelativeKP::append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added) { - - Square ksq = orient( - perspective, - pos.square( - AssociatedKing == Side::kFriend ? perspective : ~perspective)); - - const auto& dp = pos.state()->dirtyPiece; - for (int i = 0; i < dp.dirty_num; ++i) { - Piece pc = dp.piece[i]; - - if (type_of(pc) == KING) - continue; - - if (dp.from[i] != SQ_NONE) - removed->push_back(make_index(perspective, dp.from[i], pc, ksq)); - - if (dp.to[i] != SQ_NONE) - added->push_back(make_index(perspective, dp.to[i], pc, ksq)); - } - } - - template class HalfRelativeKP; - template class HalfRelativeKP; - -} // namespace Eval::NNUE::Features diff --git a/src/nnue/features/half_relative_kp.h b/src/nnue/features/half_relative_kp.h deleted file mode 100644 index 590a01a3..00000000 --- a/src/nnue/features/half_relative_kp.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef _NNUE_FEATURES_HALF_RELATIVE_KP_H_ -#define _NNUE_FEATURES_HALF_RELATIVE_KP_H_ - -#include "features_common.h" - -#include "evaluate.h" - -//Definition of input features HalfRelativeKP of NNUE evaluation function -namespace Eval::NNUE::Features { - - // Feature HalfRelativeKP: Relative position of each piece other than the ball based on own ball or enemy ball - template - class HalfRelativeKP { - public: - // feature quantity name - static constexpr const char* kName = (AssociatedKing == Side::kFriend) ? - "HalfRelativeKP(Friend)" : "HalfRelativeKP(Enemy)"; - - // Hash value embedded in the evaluation function file - static constexpr std::uint32_t kHashValue = - 0xF9180919u ^ (AssociatedKing == Side::kFriend); - - // Piece type excluding balls - static constexpr IndexType kNumPieceKinds = 5 * 2; - - // width of the virtual board with the ball in the center - static constexpr IndexType kBoardWidth = FILE_NB * 2 - 1; - - // height of a virtual board with balls in the center - static constexpr IndexType kBoardHeight = RANK_NB * 2 - 1; - - // number of feature dimensions - static constexpr IndexType kDimensions = - kNumPieceKinds * kBoardHeight * kBoardWidth; - - // The maximum value of the number of indexes whose value is 1 at the same time among the feature values - static constexpr IndexType kMaxActiveDimensions = 30; // Kings don't count - - // Timing of full calculation instead of difference calculation - static constexpr TriggerEvent kRefreshTrigger = - (AssociatedKing == Side::kFriend) ? - TriggerEvent::kFriendKingMoved : TriggerEvent::kEnemyKingMoved; - - // Get a list of indices with a value of 1 among the features - static void append_active_indices( - const Position& pos, - Color perspective, - IndexList* active); - - // Get a list of indices whose values ​​have changed from the previous one in the feature quantity - static void append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added); - - // Find the index of the feature quantity from the ball position and PieceSquare - static IndexType make_index(Square s, IndexType p); - - // Find the index of the feature quantity from the ball position and PieceSquare - static IndexType make_index(Color perspective, Square s, Piece pc, Square sq_k); - }; - -} // namespace Eval::NNUE::Features - -#endif diff --git a/src/nnue/features/k.cpp b/src/nnue/features/k.cpp deleted file mode 100644 index 7b62a75a..00000000 --- a/src/nnue/features/k.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "k.h" -#include "index_list.h" - -//Definition of input feature quantity K of NNUE evaluation function -namespace Eval::NNUE::Features { - - // Orient a square according to perspective (rotate the board 180° for black) - // this has to stay until we find a better arch that works with "flip". - // allows us to use current master net for gensfen (primarily needed for higher quality data) - inline Square orient(Color perspective, Square s) { - return Square(int(s) ^ (bool(perspective) * 63)); - } - - // Index of a feature for a given king position. - IndexType K::make_index(Color perspective, Square s, Color king_color) { - return IndexType(orient(perspective, s) + bool(perspective ^ king_color) * 64); - } - - // Get a list of indices with a value of 1 among the features - void K::append_active_indices( - const Position& pos, - Color perspective, - IndexList* active) { - - for (auto color : Colors) { - active->push_back(make_index(perspective, pos.square(color), color)); - } - } - - // Get a list of indices whose values ​​have changed from the previous one in the feature quantity - void K::append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added) { - - const auto& dp = pos.state()->dirtyPiece; - if (type_of(dp.piece[0]) == KING) - { - removed->push_back(make_index(perspective, dp.from[0], color_of(dp.piece[0]))); - added->push_back(make_index(perspective, dp.to[0], color_of(dp.piece[0]))); - } - } - -} // namespace Eval::NNUE::Features diff --git a/src/nnue/features/k.h b/src/nnue/features/k.h deleted file mode 100644 index 928d77de..00000000 --- a/src/nnue/features/k.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef _NNUE_FEATURES_K_H_ -#define _NNUE_FEATURES_K_H_ - -#include "features_common.h" - -#include "evaluate.h" - -//Definition of input feature quantity K of NNUE evaluation function -namespace Eval::NNUE::Features { - - // Feature K: Ball position - class K { - public: - // feature quantity name - static constexpr const char* kName = "K"; - - // Hash value embedded in the evaluation function file - static constexpr std::uint32_t kHashValue = 0xD3CEE169u; - - // number of feature dimensions - static constexpr IndexType kDimensions = SQUARE_NB * 2; - - // The maximum value of the number of indexes whose value is 1 at the same time among the feature values - static constexpr IndexType kMaxActiveDimensions = 2; - - // Timing of full calculation instead of difference calculation - static constexpr TriggerEvent kRefreshTrigger = TriggerEvent::kNone; - - // Get a list of indices with a value of 1 among the features - static void append_active_indices( - const Position& pos, - Color perspective, - IndexList* active); - - // Get a list of indices whose values ​​have changed from the previous one in the feature quantity - static void append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added); - - private: - // Index of a feature for a given king position. - static IndexType make_index(Color perspective, Square s, Color king_color); - }; - -} // namespace Eval::NNUE::Features - -#endif diff --git a/src/nnue/features/p.cpp b/src/nnue/features/p.cpp deleted file mode 100644 index a17e304f..00000000 --- a/src/nnue/features/p.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include "p.h" -#include "index_list.h" - -//Definition of input feature P of NNUE evaluation function -namespace Eval::NNUE::Features { - - // Orient a square according to perspective (rotate the board 180° for black) - // this has to stay until we find a better arch that works with "flip". - // allows us to use current master net for gensfen (primarily needed for higher quality data) - inline Square orient(Color perspective, Square s) { - return Square(int(s) ^ (bool(perspective) * 63)); - } - - // Find the index of the feature quantity from the king position and PieceSquare - inline IndexType P::make_index( - Color perspective, Square s, Piece pc) { - return IndexType(orient(perspective, s) + kpp_board_index[pc][perspective]); - } - - // Get a list of indices with a value of 1 among the features - void P::append_active_indices( - const Position& pos, - Color perspective, - IndexList* active) { - - Bitboard bb = pos.pieces() & ~pos.pieces(KING); - while (bb) { - Square s = pop_lsb(&bb); - active->push_back(make_index(perspective, s, pos.piece_on(s))); - } - } - - // Get a list of indices whose values ​​have changed from the previous one in the feature quantity - void P::append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added) { - - const auto& dp = pos.state()->dirtyPiece; - for (int i = 0; i < dp.dirty_num; ++i) { - Piece pc = dp.piece[i]; - - if (type_of(pc) == KING) - continue; - - if (dp.from[i] != SQ_NONE) - removed->push_back(make_index(perspective, dp.from[i], pc)); - - if (dp.to[i] != SQ_NONE) - added->push_back(make_index(perspective, dp.to[i], pc)); - } - } - -} // namespace Eval::NNUE::Features diff --git a/src/nnue/features/p.h b/src/nnue/features/p.h deleted file mode 100644 index d461086b..00000000 --- a/src/nnue/features/p.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef _NNUE_FEATURES_P_H_ -#define _NNUE_FEATURES_P_H_ - -#include "features_common.h" - -#include "evaluate.h" - -//Definition of input feature P of NNUE evaluation function -namespace Eval::NNUE::Features { - - // Feature P: PieceSquare of pieces other than balls - class P { - public: - // feature quantity name - static constexpr const char* kName = "P"; - - // Hash value embedded in the evaluation function file - static constexpr std::uint32_t kHashValue = 0x764CFB4Bu; - - // number of feature dimensions - static constexpr IndexType kDimensions = PS_END; - - // The maximum value of the number of indexes whose value is 1 at the same time among the feature values - static constexpr IndexType kMaxActiveDimensions = 30; // Kings don't count - - // Timing of full calculation instead of difference calculation - static constexpr TriggerEvent kRefreshTrigger = TriggerEvent::kNone; - - // Get a list of indices with a value of 1 among the features - static void append_active_indices( - const Position& pos, - Color perspective, - IndexList* active); - - // Get a list of indices whose values ​​have changed from the previous one in the feature quantity - static void append_changed_indices( - const Position& pos, - Color perspective, - IndexList* removed, - IndexList* added); - - private: - // Index of a feature for a given piece on some square - static IndexType make_index(Color perspective, Square s, Piece pc); - }; - -} // namespace Eval::NNUE::Features - -#endif diff --git a/src/nnue/layers/sum.h b/src/nnue/layers/sum.h deleted file mode 100644 index 01ae251c..00000000 --- a/src/nnue/layers/sum.h +++ /dev/null @@ -1,196 +0,0 @@ -#ifndef _NNUE_LAYERS_SUM_H_ -#define _NNUE_LAYERS_SUM_H_ - -#include "nnue/nnue_common.h" - -// Definition of layer Sum of NNUE evaluation function -namespace Eval::NNUE::Layers { - - // Layer that sums the output of multiple layers - template - class Sum : public Sum { - private: - using Head = FirstPreviousLayer; - using Tail = Sum; - - public: - // Input/output type - using InputType = typename Head::OutputType; - - using OutputType = InputType; - - static_assert(std::is_same::value, ""); - - // number of input/output dimensions - static constexpr IndexType kInputDimensions = Head::kOutputDimensions; - - static constexpr IndexType kOutputDimensions = kInputDimensions; - - static_assert(kInputDimensions == Tail::kInputDimensions ,""); - - // Size of forward propagation buffer used in this layer - static constexpr std::size_t kSelfBufferSize = - CeilToMultiple(kOutputDimensions * sizeof(OutputType), kCacheLineSize); - - // Size of the forward propagation buffer used from the input layer to this layer - static constexpr std::size_t kBufferSize = - std::max(Head::kBufferSize + kSelfBufferSize, Tail::kBufferSize); - - static constexpr int kLayerIndex = Tail::kLayerIndex + 1; - - // Hash value embedded in the evaluation function file - static constexpr std::uint32_t GetHashValue() { - std::uint32_t hash_value = 0xBCE400B4u; - hash_value ^= Head::GetHashValue() >> 1; - hash_value ^= Head::GetHashValue() << 31; - hash_value ^= Tail::GetHashValue() >> 2; - hash_value ^= Tail::GetHashValue() << 30; - return hash_value; - } - - static std::string get_name() { - return "Sum[" + - std::to_string(kOutputDimensions) + "]"; - } - - // A string that represents the structure from the input layer to this layer - static std::string get_structure_string() { - return get_name() + "(" + get_summands_string() + ")"; - } - - static std::string get_layers_info() { - std::string info = Tail::get_layers_info(); - info += "\n - "; - info += std::to_string(kLayerIndex); - info += " - "; - info += get_name(); - return info; - } - - // read parameters - bool ReadParameters(std::istream& stream) { - if (!Tail::ReadParameters(stream)) - return false; - - return previous_layer_.ReadParameters(stream); - } - - // write parameters - bool WriteParameters(std::ostream& stream) const { - if (!Tail::WriteParameters(stream)) - return false; - - return previous_layer_.WriteParameters(stream); - } - - // forward propagation - const OutputType* propagate( - const TransformedFeatureType* transformed_features, char* buffer) const { - - Tail::propagate(transformed_features, buffer); - - const auto head_output = previous_layer_.Propagate( - transformed_features, buffer + kSelfBufferSize); - - const auto output = reinterpret_cast(buffer); - - for (IndexType i = 0; i ; - - // the layer immediately before this layer - FirstPreviousLayer previous_layer_; - }; - - // Layer that sums the output of multiple layers (when there is one template argument) - template - class Sum { - public: - // Input/output type - using InputType = typename PreviousLayer::OutputType; - - using OutputType = InputType; - - // number of input/output dimensions - static constexpr IndexType kInputDimensions = - PreviousLayer::kOutputDimensions; - - static constexpr IndexType kOutputDimensions = kInputDimensions; - - // Size of the forward propagation buffer used from the input layer to this layer - static constexpr std::size_t kBufferSize = PreviousLayer::kBufferSize; - - static constexpr int kLayerIndex = PreviousLayer::kLayerIndex + 1; - - // Hash value embedded in the evaluation function file - static constexpr std::uint32_t GetHashValue() { - std::uint32_t hash_value = 0xBCE400B4u; - hash_value ^= PreviousLayer::GetHashValue() >> 1; - hash_value ^= PreviousLayer::GetHashValue() << 31; - return hash_value; - } - - static std::string get_name() { - return "Sum[" + - std::to_string(kOutputDimensions) + "]"; - } - - // A string that represents the structure from the input layer to this layer - static std::string get_structure_string() { - return get_name() + "(" + get_summands_string() + ")"; - } - - static std::string get_layers_info() { - std::string info = PreviousLayer::get_layers_info(); - info += '\n'; - info += std::to_string(kLayerIndex); - info += ": "; - info += get_name(); - return info; - } - - // read parameters - bool ReadParameters(std::istream& stream) { - return previous_layer_.ReadParameters(stream); - } - - // write parameters - bool WriteParameters(std::ostream& stream) const { - return previous_layer_.WriteParameters(stream); - } - - // forward propagation - const OutputType* Propagate( - const TransformedFeatureType* transformed_features, char* buffer) const { - - return previous_layer_.Propagate(transformed_features, buffer); - } - - protected: - // A string that represents the list of layers to be summed - static std::string get_summands_string() { - return PreviousLayer::get_structure_string(); - } - - // Make the learning class a friend - friend class Trainer; - - // the layer immediately before this layer - PreviousLayer previous_layer_; - }; - -} // namespace Eval::NNUE::Layers - -#endif