perf: hand-written SIMD for all network inference

The value and policy networks now run on hand-written vector kernels
(src/simd.h) instead of plain loops left to the auto-vectoriser. Paths for
AVX-512BW (VNNI dpwssd when present), AVX2 (AVX-VNNI when present), SSE2
(SSE4.1 sign extend when present) and NEON, chosen at compile time by
-march=native, plus a scalar fallback (-DRIGBY_NO_SIMD).

All arithmetic is exact integer maths, so every path gives the same results
as before and the bench is unchanged.

Value net:
- refresh sums the feature rows in registers, one tile at a time, and
  writes the accumulator once instead of once per piece
- update adds and subtracts the changed rows in one vector pass
- evaluate does SCReLU as v * (v * w) with mullo + madd; v * w fits
  16 bits because the output weights are at most 32767 / QA = 128 (now
  checked on load; the v1 net's largest is 127), and each half's sum fits
  32 bits (static_assert)

Policy net:
- the hidden layer sums the i8 rows (sign-extended on load) into
  register tiles of 8 vectors; row by row in memory was 1.8x slower
- CReLU and the pairwise product in one vector pass
- the output dot products run four moves at a time, sharing the loads of
  the activations (output stage about 20% faster than one at a time)

Tried and dropped: software prefetch of the weight rows (slower), storing
the policy's first layer as i16 to skip the sign extend (slower: the
accumulation is bound by memory, not by the extend).

Measured on a Ryzen 9 5900X (AVX2), bench 50000, four runs each:
main 4.43M nps, this 5.21M nps (+18%).

Checked: bench 615835 identical for AVX2, SSE2, SSE4.1 and scalar builds;
AVX-512 (skylake-avx512, icelake-server) and AVX-VNNI (alderlake) builds
compile; the NEON kernels compile for aarch64 (not run). UCI: uci,
isready, position, go nodes, setoption PolicyFile.

Bench: 615835

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rektdieandclaude committed 2026-11-16 15:30:56 +01:00
1 parent d7f97ee80b
commit 53553c351a
3 files changed
+329 -38

No files matched your search

+37 -19
View File
@@ -8,6 +8,8 @@
#include <iterator>
#include <vector>
#include "simd.h"
#ifdef POLICYFILE
#include "incbin.h"
INCBIN(RigbyPolicy, POLICYFILE);
@@ -160,12 +162,8 @@ void logits(const Position& pos, const MoveList& moves, float* out) {
const u64 defences = threats_by(pos, us);
// Hidden layer: bias plus one column per active input.
static thread_local std::vector<i16> acc;
static thread_local std::vector<i16> act;
acc.resize(size_t(h));
act.resize(size_t(half));
for (int i = 0; i < h; i++) acc[i] = net.l0b[i];
const i8* rows[32];
int count = 0;
u64 occ = pos.occupancy();
while (occ) {
const int sq = pop_lsb(occ);
@@ -173,24 +171,44 @@ void logits(const Position& pos, const MoveList& moves, float* out) {
int feature = 384 * (piece_color(piece) != us) + 64 * piece_type(piece) + (sq ^ flip);
if (threats & bb(sq)) feature += 768;
if (defences & bb(sq)) feature += 1536;
const i8* w = &net.l0w[size_t(feature) * h];
for (int i = 0; i < h; i++) acc[i] = i16(acc[i] + w[i]);
rows[count++] = &net.l0w[size_t(feature) * h];
}
// Output rows of the moves.
const i8* out_rows[256];
int out_idx[256];
for (int m = 0; m < moves.size(); m++) {
out_idx[m] = move_index(pos, moves[m]);
out_rows[m] = &net.l1w[size_t(out_idx[m]) * half];
}
static thread_local std::vector<i16> acc;
static thread_local std::vector<i16> act;
acc.resize(size_t(h));
act.resize(size_t(half));
simd::accumulate(acc.data(), net.l0b.data(), rows, count, h);
// CReLU on both halves, multiplied pairwise: values up to Q * Q.
for (int i = 0; i < half; i++) {
const int a = std::clamp<int>(acc[i], 0, Q);
const int b = std::clamp<int>(acc[i + half], 0, Q);
act[i] = i16(a * b);
{
using namespace simd;
const vi16 lo = set1(0), hi = set1(Q);
int i = 0;
for (; i + LANES <= half; i += LANES) {
const vi16 a = clamp(simd::load(&acc[i]), lo, hi);
const vi16 b = clamp(simd::load(&acc[i + half]), lo, hi);
store(&act[i], mullo(a, b));
}
for (; i < half; i++) act[i] = i16(std::clamp<int>(acc[i], 0, Q) * std::clamp<int>(acc[i + half], 0, Q));
}
for (int m = 0; m < moves.size(); m++) {
const int idx = move_index(pos, moves[m]);
const i8* w = &net.l1w[size_t(idx) * half];
int sum = 0;
for (int i = 0; i < half; i++) sum += int(act[i]) * int(w[i]);
out[m] = float(sum) / float(Q * Q * Q) + float(net.l1b[idx]) / float(Q);
}
// One dot product per move, at most half * Q^3 (fits 32 bits); four moves
// at a time share the activation loads.
int sums[256];
int m = 0;
for (; m + 4 <= moves.size(); m += 4) simd::dot4(act.data(), out_rows + m, half, sums + m);
for (; m < moves.size(); m++) sums[m] = simd::dot(act.data(), out_rows[m], half);
for (m = 0; m < moves.size(); m++)
out[m] = float(sums[m]) / float(Q * Q * Q) + float(net.l1b[out_idx[m]]) / float(Q);
}
} // namespace rigby::policy