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:
https://github.com/official-monty/Monty/pull/87 (Initial threat input PR)
https://github.com/official-monty/Monty/pull/114 (Fixed threat indexing to take into account colour correctly)
https://github.com/official-monty/Monty/pull/116 (i8 quantisation of weights whilst keeping calculations in i16)

Yukari commit:
https://github.com/yukarichess/yukari/commit/2d482c64a79cec03cf4987d5289334b9cdc737bc (Threat inputs merged)

Plentychess PRs:
https://github.com/Yoshie2000/PlentyChess/pull/400 (Threat inputs merged)
https://github.com/Yoshie2000/PlentyChess/pull/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 https://github.com/official-stockfish/Stockfish/pull/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>
This commit is contained in:
sscg13
2025-11-12 10:49:39 +01:00
committed by Joost VandeVondele
co-authored by Shawn Xu Timothy Herchen Viren6 Yoshie2000 Joost Vandevondele rn5f107s2 cj5716 AliceRoselia Linmiao Xu Disservin
parent 69a01b88f3
commit 8e5392d79a
23 changed files with 1468 additions and 279 deletions
+137 -20
View File
@@ -48,6 +48,7 @@ Key psq[PIECE_NB][SQUARE_NB];
Key enpassant[FILE_NB];
Key castling[CASTLING_RIGHT_NB];
Key side, noPawns;
}
namespace {
@@ -687,10 +688,10 @@ bool Position::gives_check(Move m) const {
// moves should be filtered out before this function is called.
// If a pointer to the TT table is passed, the entry for the new position
// will be prefetched
DirtyPiece Position::do_move(Move m,
StateInfo& newSt,
bool givesCheck,
const TranspositionTable* tt = nullptr) {
DirtyBoardData Position::do_move(Move m,
StateInfo& newSt,
bool givesCheck,
const TranspositionTable* tt = nullptr) {
assert(m.is_ok());
assert(&newSt != st);
@@ -724,6 +725,10 @@ DirtyPiece Position::do_move(Move m,
dp.from = from;
dp.to = to;
dp.add_sq = SQ_NONE;
DirtyThreats dts;
dts.us = us;
dts.prevKsq = square<KING>(us);
dts.threatenedSqs = dts.threateningSqs = 0;
assert(color_of(pc) == us);
assert(captured == NO_PIECE || color_of(captured) == (m.type_of() != CASTLING ? them : us));
@@ -735,7 +740,7 @@ DirtyPiece Position::do_move(Move m,
assert(captured == make_piece(us, ROOK));
Square rfrom, rto;
do_castling<true>(us, from, to, rfrom, rto, &dp);
do_castling<true>(us, from, to, rfrom, rto, &dts, &dp);
k ^= Zobrist::psq[captured][rfrom] ^ Zobrist::psq[captured][rto];
st->nonPawnKey[us] ^= Zobrist::psq[captured][rfrom] ^ Zobrist::psq[captured][rto];
@@ -758,6 +763,9 @@ DirtyPiece Position::do_move(Move m,
assert(relative_rank(us, to) == RANK_6);
assert(piece_on(to) == NO_PIECE);
assert(piece_on(capsq) == make_piece(them, PAWN));
// Update board and piece lists in ep case, normal captures are updated later
remove_piece(capsq, &dts);
}
st->pawnKey ^= Zobrist::psq[captured][capsq];
@@ -774,11 +782,9 @@ DirtyPiece Position::do_move(Move m,
dp.remove_pc = captured;
dp.remove_sq = capsq;
// Update board and piece lists
remove_piece(capsq);
k ^= Zobrist::psq[captured][capsq];
st->materialKey ^= Zobrist::psq[captured][8 + pieceCount[captured]];
st->materialKey ^=
Zobrist::psq[captured][8 + pieceCount[captured] - (m.type_of() != EN_PASSANT)];
// Reset rule 50 counter
st->rule50 = 0;
@@ -806,7 +812,15 @@ DirtyPiece Position::do_move(Move m,
// Move the piece. The tricky Chess960 castling is handled earlier
if (m.type_of() != CASTLING)
move_piece(from, to);
{
if (captured && m.type_of() != EN_PASSANT)
{
remove_piece(from, &dts);
swap_piece(to, pc, &dts);
}
else
move_piece(from, to, &dts);
}
// If the moving piece is a pawn do some special extra work
if (type_of(pc) == PAWN)
@@ -823,8 +837,7 @@ DirtyPiece Position::do_move(Move m,
assert(relative_rank(us, to) == RANK_8);
assert(type_of(promotion) >= KNIGHT && type_of(promotion) <= QUEEN);
remove_piece(to);
put_piece(promotion, to);
swap_piece(to, promotion, &dts);
dp.add_pc = promotion;
dp.add_sq = to;
@@ -949,13 +962,16 @@ DirtyPiece Position::do_move(Move m,
}
}
dts.ksq = square<KING>(us);
assert(pos_is_ok());
assert(dp.pc != NO_PIECE);
assert(!(bool(captured) || m.type_of() == CASTLING) ^ (dp.remove_sq != SQ_NONE));
assert(dp.from != SQ_NONE);
assert(!(dp.add_sq != SQ_NONE) ^ (m.type_of() == PROMOTION || m.type_of() == CASTLING));
return dp;
return {dp, dts};
}
@@ -1021,12 +1037,113 @@ void Position::undo_move(Move m) {
assert(pos_is_ok());
}
template<bool PutPiece>
inline void add_dirty_threat(
DirtyThreats* const dts, Piece pc, Piece threatened, Square s, Square threatenedSq) {
if (PutPiece)
{
dts->threatenedSqs |= square_bb(threatenedSq);
dts->threateningSqs |= square_bb(s);
}
dts->list.push_back({pc, threatened, s, threatenedSq, PutPiece});
}
template<bool PutPiece, bool ComputeRay>
void Position::update_piece_threats(Piece pc, Square s, DirtyThreats* const dts) {
// Add newly threatened pieces
Bitboard occupied = pieces();
Bitboard rAttacks = attacks_bb<ROOK>(s, occupied);
Bitboard bAttacks = attacks_bb<BISHOP>(s, occupied);
Bitboard qAttacks = rAttacks | bAttacks;
Bitboard threatened;
switch (type_of(pc))
{
case PAWN :
threatened = PseudoAttacks[color_of(pc)][s];
break;
case BISHOP :
threatened = bAttacks;
break;
case ROOK :
threatened = rAttacks;
break;
case QUEEN :
threatened = qAttacks;
break;
default :
threatened = PseudoAttacks[type_of(pc)][s];
}
threatened &= occupied;
while (threatened)
{
Square threatened_sq = pop_lsb(threatened);
Piece threatened_pc = piece_on(threatened_sq);
assert(threatened_sq != s);
assert(threatened_pc);
add_dirty_threat<PutPiece>(dts, pc, threatened_pc, s, threatened_sq);
}
Bitboard sliders = (pieces(ROOK, QUEEN) & rAttacks) | (pieces(BISHOP, QUEEN) & bAttacks);
Bitboard incoming_threats = (attacks_bb<KNIGHT>(s, occupied) & pieces(KNIGHT))
| (attacks_bb<PAWN>(s, WHITE) & pieces(BLACK, PAWN))
| (attacks_bb<PAWN>(s, BLACK) & pieces(WHITE, PAWN))
| (attacks_bb<KING>(s, occupied) & pieces(KING));
while (sliders)
{
Square slider_sq = pop_lsb(sliders);
Piece slider = piece_on(slider_sq);
Bitboard ray = RayPassBB[slider_sq][s] & ~BetweenBB[slider_sq][s];
threatened = ray & qAttacks & occupied;
assert(!more_than_one(threatened));
if (ComputeRay && threatened)
{
Square threatened_sq = lsb(threatened);
Piece threatened_pc = piece_on(threatened_sq);
add_dirty_threat<!PutPiece>(dts, slider, threatened_pc, slider_sq, threatened_sq);
}
add_dirty_threat<PutPiece>(dts, slider, pc, slider_sq, s);
}
// Add threats of sliders that were already threatening s,
// sliders are already handled in the loop above
while (incoming_threats)
{
Square src_sq = pop_lsb(incoming_threats);
Piece src_pc = piece_on(src_sq);
assert(src_sq != s);
assert(src_pc != NO_PIECE);
add_dirty_threat<PutPiece>(dts, src_pc, pc, src_sq, s);
}
}
// Helper used to do/undo a castling move. This is a bit
// tricky in Chess960 where from/to squares can overlap.
template<bool Do>
void Position::do_castling(
Color us, Square from, Square& to, Square& rfrom, Square& rto, DirtyPiece* const dp) {
void Position::do_castling(Color us,
Square from,
Square& to,
Square& rfrom,
Square& rto,
DirtyThreats* const dts,
DirtyPiece* const dp) {
bool kingSide = to > from;
rfrom = to; // Castling is encoded as "king captures friendly rook"
@@ -1044,12 +1161,12 @@ void Position::do_castling(
}
// Remove both pieces first since squares could overlap in Chess960
remove_piece(Do ? from : to);
remove_piece(Do ? rfrom : rto);
remove_piece(Do ? from : to, dts);
remove_piece(Do ? rfrom : rto, dts);
board[Do ? from : to] = board[Do ? rfrom : rto] =
NO_PIECE; // remove_piece does not do this for us
put_piece(make_piece(us, KING), Do ? to : from);
put_piece(make_piece(us, ROOK), Do ? rto : rfrom);
put_piece(make_piece(us, KING), Do ? to : from, dts);
put_piece(make_piece(us, ROOK), Do ? rto : rfrom, dts);
}
@@ -1349,7 +1466,7 @@ bool Position::pos_is_ok() const {
for (Piece pc : Pieces)
if (pieceCount[pc] != popcount(pieces(color_of(pc), type_of(pc)))
|| pieceCount[pc] != std::count(board, board + SQUARE_NB, pc))
|| pieceCount[pc] != std::count(board.begin(), board.end(), pc))
assert(0 && "pos_is_ok: Pieces");
for (Color c : {WHITE, BLACK})