summaryrefslogtreecommitdiff
path: root/src/nnuefeatures.c
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2026-10-15 15:21:26 -0700
committerScott Gasch <[email protected]>2026-10-15 15:21:26 -0700
commit2bf4eb27a473089b9b30726a678fbac27d0f8a69 (patch)
tree23e7271eab238cf2fa97128d902c0c828e7d1583 /src/nnuefeatures.c
parentac7572166bd24868fcaf5980a91bb82fe61ba677 (diff)
NNUE accumulator incremental maintenance: MakeMove/UnmakeMove hookup
Wires the live NNUE accumulator (SEARCHER_THREAD_CONTEXT.sNnueAccum[2], added in the previous commit) into real incremental maintenance, hooked directly into the piece-movement primitives rather than re-deriving move type from MOVE in a separate pass: - chess.h: shared HalfKAv2_hm feature-index formula/constants (NnueFeatureIndex and friends), moved here from nnuefeatures.c so the reference dump and the real hot-path code can never drift apart. Prototypes for NnueAccumAddFeature/RemoveFeature/Recompute and the (still placeholder, zero-initialized) embedding table. LiftPiece/PlacePiece/SlidePiece/SlidePawn/SlidePieceWithoutSigs signatures gain an NNUE_ACCUMULATOR sNnueAccum[2] parameter. - nnueaccum.c (new): the embedding table storage and the three accumulator operations. Written as straight-line 256-float loops, structured for -O3 -ffast-math auto-vectorization on this box's AVX2+FMA target; explicit SIMD intrinsics and real weight loading are separate, deliberately deferred follow-ups. - move.c: all 8 Lift/Place/Slide(WithoutSigs) primitives now keep sNnueAccum incrementally correct for both perspectives on every call -- except when the piece is a king, where each primitive skips its own-color perspective entirely (that perspective needs a full recompute, not a delta, since every HalfKAv2_hm feature in it is own-king-relative; computing an incremental update for it only to discard it unread would be wasted work). MakeMove backs up that perspective into the current ply's PLY_INFO.sNnueAccumBackup before a king moves (castling or a plain step) and overwrites it via NnueAccumRecompute once the board reflects the final position; UnmakeMove restores it from the same backup slot. DEBUG builds tag the backup with the color it belongs to and poison it after consumption, release builds carry no overhead for either. - nnuefeatures.c: now a thin board-walking wrapper around the shared chess.h formula instead of its own duplicated copy. - testmove.c: TestLiftPlaceSlidePiece passes a local (unread) dummy accumulator pair -- the only caller of these primitives outside move.c, and it never needed NNUE awareness, just a real argument. Verified via the full precommit_check.sh (all 4 gates, including the new combined DEBUG=1 TEST=1 self-test, which exercises MakeMove/ UnmakeMove heavily via TestMoveGenerator's perft counts and TestLegalMoveGenerator) -- confirms nothing crashes and no existing invariant breaks. Does NOT yet confirm the actual property that matters (incrementally-maintained accumulator == recompute-from- scratch) -- no test exercises that yet; next step. Co-Authored-By: Claude Sonnet 5 <[email protected]>
Diffstat (limited to 'src/nnuefeatures.c')
-rw-r--r--src/nnuefeatures.c124
1 files changed, 7 insertions, 117 deletions
diff --git a/src/nnuefeatures.c b/src/nnuefeatures.c
index c43e267..4035905 100644
--- a/src/nnuefeatures.c
+++ b/src/nnuefeatures.c
@@ -19,13 +19,12 @@ Abstract:
This is deliberately NOT wired into search/eval -- no accumulator,
no incremental maintenance, nothing performance-sensitive. It
exists purely so `dump nnuefeatures` can print the exact same
- active-feature-index set the eventual real C inference code must
- produce, so a Python/C cross-checker has a C-side ground truth to
- diff against before any of that real code exists. The *real*
- MakeMove/UnmakeMove accumulator plumbing (per NNUE.md's
- sequencing, gated behind a dedicated branch, not started) will
- reuse this same index math, not this file's dump-command
- plumbing.
+ active-feature-index set the real C inference code produces, so a
+ Python/C cross-checker has a C-side ground truth to diff against.
+ The actual feature-index formula (NnueFeatureIndex) now lives in
+ chess.h, shared verbatim with the real MakeMove/UnmakeMove
+ accumulator maintenance (move.c) -- this file only walks the board
+ and calls it, so the two can never silently drift apart.
Author:
@@ -35,102 +34,6 @@ Author:
#include "chess.h"
-//
-// HalfKAv2_hm constants, verbatim from Stockfish's half_ka_v2_hm.h.
-// PS_NB counts 5 non-king piece types x 2 "us/them" roles x 64
-// squares, plus one shared 64-square king plane (own king and enemy
-// king features both land in this one plane, distinguished only by
-// which square is set).
-//
-#define NNUE_SQUARE_NB (64)
-#define NNUE_PS_W_PAWN (0 * NNUE_SQUARE_NB)
-#define NNUE_PS_B_PAWN (1 * NNUE_SQUARE_NB)
-#define NNUE_PS_W_KNIGHT (2 * NNUE_SQUARE_NB)
-#define NNUE_PS_B_KNIGHT (3 * NNUE_SQUARE_NB)
-#define NNUE_PS_W_BISHOP (4 * NNUE_SQUARE_NB)
-#define NNUE_PS_B_BISHOP (5 * NNUE_SQUARE_NB)
-#define NNUE_PS_W_ROOK (6 * NNUE_SQUARE_NB)
-#define NNUE_PS_B_ROOK (7 * NNUE_SQUARE_NB)
-#define NNUE_PS_W_QUEEN (8 * NNUE_SQUARE_NB)
-#define NNUE_PS_B_QUEEN (9 * NNUE_SQUARE_NB)
-#define NNUE_PS_KING (10 * NNUE_SQUARE_NB)
-#define NNUE_PS_NB (11 * NNUE_SQUARE_NB) // 704
-
-#define NNUE_DIMENSIONS ((NNUE_SQUARE_NB * NNUE_PS_NB) / 2) // 22528
-
-//
-// Convert a typhoon 0x88 COOR to Stockfish's flat 0-63 square index
-// (file + 8*(rank-1), rank/file both derived from the engine's own
-// RANK()/FILE() macros -- see docs/NNUE.md's spec for why this
-// one-line conversion is the single highest-risk spot in the whole
-// scheme).
-//
-static int
-CoorToSq64(COOR c)
-{
- return (int)FILE(c) + 8 * ((int)RANK(c) - 1);
-}
-
-//
-// King bucket (0..31) for a king on Stockfish-square sq64, from
-// color uPerspective's (WHITE/BLACK) point of view. Verified against
-// Stockfish's literal KingBuckets[][] table -- see docs/NNUE.md.
-//
-static int
-KingBucket(ULONG uPerspective, int sq64)
-{
- int file = sq64 & 7;
- int rank0 = sq64 >> 3; // 0..7, rank-1
- int fileQuartet = (file < 4) ? file : (7 - file); // 0..3, mirrored
- int rankGroup = (uPerspective == WHITE) ? (7 - rank0) : rank0;
- return rankGroup * 4 + fileQuartet;
-}
-
-//
-// Orientation XOR mask for perspective uPerspective whose own king
-// sits on Stockfish-square ksq64. Only ksq64's file selects the
-// branch (queenside half a-d vs. kingside half e-h); any rank.
-//
-static int
-OrientMask(ULONG uPerspective, int ksq64)
-{
- int file = ksq64 & 7;
- if (uPerspective == WHITE)
- {
- return (file < 4) ? 7 : 0; // SQ_H1 : SQ_A1
- }
- return (file < 4) ? 63 : 56; // SQ_H8 : SQ_A8
-}
-
-//
-// Piece-square plane offset for a piece of color uPieceColor and
-// type uPieceType (chess.h's PAWN..KING values), as seen from
-// perspective uPerspective. Both kings (own and enemy) share
-// NNUE_PS_KING.
-//
-static int
-PiecePlane(ULONG uPerspective, ULONG uPieceColor, ULONG uPieceType)
-{
- FLAG fOwn;
-
- if (uPieceType == KING)
- {
- return NNUE_PS_KING;
- }
- fOwn = (uPieceColor == uPerspective);
- switch (uPieceType)
- {
- case PAWN: return fOwn ? NNUE_PS_W_PAWN : NNUE_PS_B_PAWN;
- case KNIGHT: return fOwn ? NNUE_PS_W_KNIGHT : NNUE_PS_B_KNIGHT;
- case BISHOP: return fOwn ? NNUE_PS_W_BISHOP : NNUE_PS_B_BISHOP;
- case ROOK: return fOwn ? NNUE_PS_W_ROOK : NNUE_PS_B_ROOK;
- case QUEEN: return fOwn ? NNUE_PS_W_QUEEN : NNUE_PS_B_QUEEN;
- default:
- ASSERT(FALSE);
- return 0;
- }
-}
-
static int
CompareInts(const void *a, const void *b)
{
@@ -149,9 +52,6 @@ ActiveFeatureIndices(POSITION *pos, ULONG uPerspective, int *pIndices)
{
int f, r;
COOR cKing = ILLEGAL_COOR;
- int ksq64;
- int orientMask;
- int kingBucketOffset;
int n = 0;
//
@@ -172,9 +72,6 @@ ActiveFeatureIndices(POSITION *pos, ULONG uPerspective, int *pIndices)
}
}
ASSERT(cKing != ILLEGAL_COOR);
- ksq64 = CoorToSq64(cKing);
- orientMask = OrientMask(uPerspective, ksq64);
- kingBucketOffset = KingBucket(uPerspective, ksq64) * NNUE_PS_NB;
for (f = 0; f < 8; f++)
{
@@ -182,20 +79,13 @@ ActiveFeatureIndices(POSITION *pos, ULONG uPerspective, int *pIndices)
{
COOR c = FILE_RANK_TO_COOR(f, r);
PIECE p = pos->rgSquare[c].pPiece;
- int sq64;
- int idx;
if (IS_EMPTY(p))
{
continue;
}
- sq64 = CoorToSq64(c);
- idx = (sq64 ^ orientMask) +
- PiecePlane(uPerspective, PIECE_COLOR(p), PIECE_TYPE(p)) +
- kingBucketOffset;
- ASSERT((idx >= 0) && (idx < NNUE_DIMENSIONS));
ASSERT(n < 32);
- pIndices[n++] = idx;
+ pIndices[n++] = NnueFeatureIndex(uPerspective, cKing, c, p);
}
}
qsort(pIndices, n, sizeof(int), CompareInts);