Skip to content

Commit 8e5392d

Browse files
sscg13xu-shawnanematodeViren6Yoshie2000
committed
Update NNUE architecture to SFNNv10 with Threat Inputs and net nn-49c1193b131c.nnue
This commit introduces Full Threat Input features, which are a subset of Piece(Square)-Piece(Square) pairs. In any given position, the active features consist of pairs where the second piece’s square lies in the attack set of the first piece. This is an extremely simplified explanation that leaves out many details. The already-used HalfKAv2_hm feature set completes the input features. Minor quantization changes have also been made. The net nn-49c1193b131c.nnue was trained by vondele using the following setup: https://github.com/vondele/nettest/blob/7de71238e9b295e3f88ed7c9c5936af632c9b981/threats.yaml A graphical version of an earlier scheme (with less refinement) that illustrates the core concepts can be found attached. [NewInputs.pdf](https://github.com/user-attachments/files/23478441/NewInputs.pdf) Further information, as well as a brief description of the history of development, can be found attached. [Stockfish threat inputs PR summary.pdf](https://github.com/user-attachments/files/23478634/Stockfish.threat.inputs.PR.summary.pdf) This has been a huge effort spanning over half a year, with the original [discussion thread](https://discord.com/channels/435943710472011776/1336647760388034610) reaching over 11k messages. Thanks to everyone who has contributed. Monty PRs: official-monty/Monty#87 (Initial threat input PR) official-monty/Monty#114 (Fixed threat indexing to take into account colour correctly) official-monty/Monty#116 (i8 quantisation of weights whilst keeping calculations in i16) Yukari commit: yukarichess/yukari@2d482c6 (Threat inputs merged) Plentychess PRs: Yoshie2000/PlentyChess#400 (Threat inputs merged) Yoshie2000/PlentyChess#411 (Threat input weights quantised to i8) Passed STC: LLR: 2.93 (-2.94,2.94) <0.00,2.00> Total: 63424 W: 16956 L: 16591 D: 29877 Ptnml(0-2): 276, 7522, 15797, 7795, 322 https://tests.stockfishchess.org/tests/view/69105b3dec1d00d2c195c569 Passed LTC: LLR: 2.95 (-2.94,2.94) <0.50,2.50> Total: 27876 W: 7417 L: 7110 D: 13349 Ptnml(0-2): 23, 3033, 7530, 3318, 34 https://tests.stockfishchess.org/tests/view/6910d817ec1d00d2c195c66e Passed VVLTC (Hash accidentally set to 1/2 normal value for both sides): LLR: 2.94 (-2.94,2.94) <0.50,2.50> Total: 12458 W: 3353 L: 3102 D: 6003 Ptnml(0-2): 0, 1106, 3767, 1355, 1 https://tests.stockfishchess.org/tests/view/69115a26ec1d00d2c195c7cd This version has also passed non-regression LTC against the originally passed version: LLR: 2.94 (-2.94,2.94) <-1.75,0.25> Total: 51144 W: 13086 L: 12903 D: 25155 Ptnml(0-2): 22, 5167, 15018, 5336, 29 https://tests.stockfishchess.org/tests/view/69138a317ca87818523314bf LTC elo estimate on ARM: 1 patch : 13.9 1.9 38296.5 73728 52 2 master : 0.0 ---- 35431.5 73728 48 closes official-stockfish#6411 bench: 2626086 Co-authored-by: Shawn Xu <xu107288696@gmail.com> Co-authored-by: Timothy Herchen <timothy.herchen@gmail.com> Co-authored-by: Viren6 <94880762+Viren6@users.noreply.github.com> Co-authored-by: Yoshie2000 <patrick.leonhardt@gmx.net> Co-authored-by: Joost Vandevondele <Joost.VandeVondele@gmail.com> Co-authored-by: rn5f107s2 <clemens.lerchl@gmail.com> Co-authored-by: cj5716 <125858804+cj5716@users.noreply.github.com> Co-authored-by: AliceRoselia <63040919+AliceRoselia@users.noreply.github.com> Co-authored-by: Linmiao Xu <linmiao.xu@gmail.com> Co-authored-by: Disservin <disservin.social@gmail.com>
1 parent 69a01b8 commit 8e5392d

23 files changed

+1474
-285
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ Panthee
200200
Pascal Romaret
201201
Pasquale Pigazzini (ppigazzini)
202202
Patrick Jansen (mibere)
203+
Patrick Leonhardt (Yoshie2000)
203204
Peter Schneider (pschneider1968)
204205
Peter Zsifkovits (CoffeeOne)
205206
PikaCat

src/Makefile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,16 @@ PGOBENCH = $(WINE_PATH) ./$(EXE) bench
5555
SRCS = benchmark.cpp bitboard.cpp evaluate.cpp main.cpp \
5656
misc.cpp movegen.cpp movepick.cpp position.cpp \
5757
search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp \
58-
nnue/nnue_accumulator.cpp nnue/nnue_misc.cpp nnue/features/half_ka_v2_hm.cpp nnue/network.cpp \
58+
nnue/nnue_accumulator.cpp nnue/nnue_misc.cpp nnue/network.cpp \
59+
nnue/features/half_ka_v2_hm.cpp nnue/features/full_threats.cpp \
5960
engine.cpp score.cpp memory.cpp
6061

6162
HEADERS = benchmark.h bitboard.h evaluate.h misc.h movegen.h movepick.h history.h \
62-
nnue/nnue_misc.h nnue/features/half_ka_v2_hm.h nnue/layers/affine_transform.h \
63-
nnue/layers/affine_transform_sparse_input.h nnue/layers/clipped_relu.h \
64-
nnue/layers/sqr_clipped_relu.h nnue/nnue_accumulator.h nnue/nnue_architecture.h \
65-
nnue/nnue_common.h nnue/nnue_feature_transformer.h nnue/simd.h position.h \
66-
search.h syzygy/tbprobe.h thread.h thread_win32_osx.h timeman.h \
63+
nnue/nnue_misc.h nnue/features/half_ka_v2_hm.h nnue/features/full_threats.h \
64+
nnue/layers/affine_transform.h nnue/layers/affine_transform_sparse_input.h \
65+
nnue/layers/clipped_relu.h nnue/layers/sqr_clipped_relu.h nnue/nnue_accumulator.h \
66+
nnue/nnue_architecture.h nnue/nnue_common.h nnue/nnue_feature_transformer.h nnue/simd.h \
67+
position.h search.h syzygy/tbprobe.h thread.h thread_win32_osx.h timeman.h \
6768
tt.h tune.h types.h uci.h ucioption.h perft.h nnue/network.h engine.h score.h numa.h memory.h
6869

6970
OBJS = $(notdir $(SRCS:.cpp=.o))

src/benchmark.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ const std::vector<std::string> Defaults = {
6565
"3Qb1k1/1r2ppb1/pN1n2q1/Pp1Pp1Pr/4P2p/4BP2/4B1R1/1R5K b - - 11 40",
6666
"4k3/3q1r2/1N2r1b1/3ppN2/2nPP3/1B1R2n1/2R1Q3/3K4 w - - 5 1",
6767

68+
// Positions with high numbers of changed threats
69+
"k7/2n1n3/1nbNbn2/2NbRBn1/1nbRQR2/2NBRBN1/3N1N2/7K w - - 0 1",
70+
"K7/8/8/BNQNQNB1/N5N1/R1Q1q2r/n5n1/bnqnqnbk w - - 0 1",
71+
6872
// 5-man positions
6973
"8/8/8/8/5kp1/P7/8/1K1N4 w - - 0 1", // Kc2 - mate
7074
"8/8/8/5N2/8/p7/8/2NK3k w - - 0 1", // Na2 - mate
@@ -509,4 +513,4 @@ BenchmarkSetup setup_benchmark(std::istream& is) {
509513
return setup;
510514
}
511515

512-
} // namespace Stockfish
516+
} // namespace Stockfish

src/bitboard.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
3131

3232
Bitboard LineBB[SQUARE_NB][SQUARE_NB];
3333
Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
34+
Bitboard RayPassBB[SQUARE_NB][SQUARE_NB];
3435
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
3536

3637
alignas(64) Magic Magics[SQUARE_NB][2];
@@ -105,6 +106,8 @@ void Bitboards::init() {
105106
LineBB[s1][s2] = (attacks_bb(pt, s1, 0) & attacks_bb(pt, s2, 0)) | s1 | s2;
106107
BetweenBB[s1][s2] =
107108
(attacks_bb(pt, s1, square_bb(s2)) & attacks_bb(pt, s2, square_bb(s1)));
109+
RayPassBB[s1][s2] =
110+
attacks_bb(pt, s1, 0) & (attacks_bb(pt, s2, square_bb(s1)) | s2);
108111
}
109112
BetweenBB[s1][s2] |= s2;
110113
}

src/bitboard.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
6161

6262
extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
6363
extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
64+
extern Bitboard RayPassBB[SQUARE_NB][SQUARE_NB];
6465
extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
6566

6667

@@ -252,6 +253,20 @@ inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {
252253
}
253254
}
254255

256+
inline Bitboard attacks_bb(Piece pc, Square s) {
257+
if (type_of(pc) == PAWN)
258+
return PseudoAttacks[color_of(pc)][s];
259+
260+
return PseudoAttacks[type_of(pc)][s];
261+
}
262+
263+
264+
inline Bitboard attacks_bb(Piece pc, Square s, Bitboard occupied) {
265+
if (type_of(pc) == PAWN)
266+
return PseudoAttacks[color_of(pc)][s];
267+
268+
return attacks_bb(type_of(pc), s, occupied);
269+
}
255270

256271
// Counts the number of non-zero bits in a bitboard.
257272
inline int popcount(Bitboard b) {

src/evaluate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace Eval {
3333
// for the build process (profile-build and fishtest) to work. Do not change the
3434
// name of the macro or the location where this macro is defined, as it is used
3535
// in the Makefile/Fishtest.
36-
#define EvalFileDefaultNameBig "nn-1c0000000000.nnue"
36+
#define EvalFileDefaultNameBig "nn-49c1193b131c.nnue"
3737
#define EvalFileDefaultNameSmall "nn-37f18f62d772.nnue"
3838

3939
namespace NNUE {

src/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
#include "bitboard.h"
2323
#include "misc.h"
24+
#include "nnue/features/full_threats.h"
2425
#include "position.h"
2526
#include "tune.h"
26-
#include "types.h"
2727
#include "uci.h"
2828

2929
using namespace Stockfish;
@@ -33,6 +33,7 @@ int main(int argc, char* argv[]) {
3333

3434
Bitboards::init();
3535
Position::init();
36+
Eval::NNUE::Features::init_threat_offsets();
3637

3738
auto uci = std::make_unique<UCIEngine>(argc, argv);
3839

src/misc.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,13 @@ class ValueList {
134134

135135
public:
136136
std::size_t size() const { return size_; }
137-
void push_back(const T& value) { values_[size_++] = value; }
138-
const T* begin() const { return values_; }
139-
const T* end() const { return values_ + size_; }
140-
const T& operator[](int index) const { return values_[index]; }
137+
void push_back(const T& value) {
138+
assert(size_ < MaxSize);
139+
values_[size_++] = value;
140+
}
141+
const T* begin() const { return values_; }
142+
const T* end() const { return values_ + size_; }
143+
const T& operator[](int index) const { return values_[index]; }
141144

142145
private:
143146
T values_[MaxSize];

0 commit comments

Comments
 (0)