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:
1 parent
d7f97ee80b
commit
53553c351a
3 files changed
+329
-38
No files matched your search
+44
-19
@@ -1,11 +1,13 @@
|
||||
#include "nnue.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <climits>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include "incbin.h"
|
||||
#include "simd.h"
|
||||
|
||||
#ifndef EVALFILE
|
||||
#error "EVALFILE must name the network file (the Makefile sets it)"
|
||||
@@ -35,10 +37,13 @@ inline int feature(int perspective, int piece, int sq) {
|
||||
return 384 * theirs + 64 * piece_type(piece) + rel_sq;
|
||||
}
|
||||
|
||||
inline i64 screlu(i16 x) {
|
||||
const i64 y = std::clamp<i64>(x, 0, QA);
|
||||
return y * y;
|
||||
}
|
||||
// The output layer squares the clamped hidden value with v * (v * w): v * w
|
||||
// must fit 16 bits, so the weights are checked on load.
|
||||
constexpr int MAX_L1W = 32767 / QA;
|
||||
|
||||
// Each half's dot product is summed in 32 bits.
|
||||
static_assert(i64(HIDDEN) * QA * QA * MAX_L1W <= INT_MAX);
|
||||
static_assert(HIDDEN % simd::LANES == 0);
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -49,22 +54,26 @@ void init() {
|
||||
std::exit(1);
|
||||
}
|
||||
std::memcpy(&net, gRigbyNetData, sizeof(Network));
|
||||
for (int i = 0; i < 2 * HIDDEN; i++) {
|
||||
if (std::abs(net.l1w[i]) > MAX_L1W) {
|
||||
std::fprintf(stderr, "network %s has an output weight of %d, the limit is %d\n", EVALFILE, net.l1w[i],
|
||||
MAX_L1W);
|
||||
std::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void refresh(const Position& pos, Accumulator& acc) {
|
||||
std::memcpy(acc.values[WHITE], net.l0b, sizeof(net.l0b));
|
||||
std::memcpy(acc.values[BLACK], net.l0b, sizeof(net.l0b));
|
||||
|
||||
const i16* rows[2][32];
|
||||
int count = 0;
|
||||
u64 occ = pos.occupancy();
|
||||
while (occ) {
|
||||
const int sq = pop_lsb(occ);
|
||||
const int piece = pos.piece_on(sq);
|
||||
for (int side : {WHITE, BLACK}) {
|
||||
const i16* w = net.l0w[feature(side, piece, sq)];
|
||||
i16* v = acc.values[side];
|
||||
for (int i = 0; i < HIDDEN; i++) v[i] = i16(v[i] + w[i]);
|
||||
}
|
||||
rows[WHITE][count] = net.l0w[feature(WHITE, piece, sq)];
|
||||
rows[BLACK][count++] = net.l0w[feature(BLACK, piece, sq)];
|
||||
}
|
||||
for (int side : {WHITE, BLACK}) simd::accumulate(acc.values[side], net.l0b, rows[side], count, HIDDEN);
|
||||
}
|
||||
|
||||
void update(const Accumulator& before, const Position& pos, Move m, Accumulator& after) {
|
||||
@@ -107,15 +116,23 @@ void update(const Accumulator& before, const Position& pos, Move m, Accumulator&
|
||||
i16* dst = after.values[side];
|
||||
const i16* a0 = net.l0w[add[side][0]];
|
||||
const i16* s0 = net.l0w[sub[side][0]];
|
||||
using simd::load, simd::store, simd::LANES;
|
||||
if (adds == 1 && subs == 1) {
|
||||
for (int i = 0; i < HIDDEN; i++) dst[i] = i16(src[i] + a0[i] - s0[i]);
|
||||
for (int i = 0; i < HIDDEN; i += LANES)
|
||||
store(dst + i, simd::sub(simd::add(load(src + i), load(a0 + i)), load(s0 + i)));
|
||||
} else if (adds == 1) { // capture or capture-promotion
|
||||
const i16* s1 = net.l0w[sub[side][1]];
|
||||
for (int i = 0; i < HIDDEN; i++) dst[i] = i16(src[i] + a0[i] - s0[i] - s1[i]);
|
||||
for (int i = 0; i < HIDDEN; i += LANES) {
|
||||
const simd::vi16 added = simd::add(load(src + i), load(a0 + i));
|
||||
store(dst + i, simd::sub(simd::sub(added, load(s0 + i)), load(s1 + i)));
|
||||
}
|
||||
} else { // castling
|
||||
const i16* a1 = net.l0w[add[side][1]];
|
||||
const i16* s1 = net.l0w[sub[side][1]];
|
||||
for (int i = 0; i < HIDDEN; i++) dst[i] = i16(src[i] + a0[i] + a1[i] - s0[i] - s1[i]);
|
||||
for (int i = 0; i < HIDDEN; i += LANES) {
|
||||
const simd::vi16 added = simd::add(simd::add(load(src + i), load(a0 + i)), load(a1 + i));
|
||||
store(dst + i, simd::sub(simd::sub(added, load(s0 + i)), load(s1 + i)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -124,10 +141,18 @@ int evaluate(const Accumulator& acc, int stm) {
|
||||
const i16* us = acc.values[stm];
|
||||
const i16* them = acc.values[stm ^ 1];
|
||||
|
||||
// 256 products of up to QA^2 * |w| overflow 32 bits in the worst case.
|
||||
i64 out = 0;
|
||||
for (int i = 0; i < HIDDEN; i++) out += screlu(us[i]) * net.l1w[i];
|
||||
for (int i = 0; i < HIDDEN; i++) out += screlu(them[i]) * net.l1w[HIDDEN + i];
|
||||
// SCReLU as v * (v * w) with v clamped to [0, QA]: exact, since v * w fits
|
||||
// 16 bits and each half's sum fits 32 (checked above).
|
||||
using namespace simd;
|
||||
const vi16 lo = set1(0), hi = set1(QA);
|
||||
vi32 sum_us = zero32(), sum_them = zero32();
|
||||
for (int i = 0; i < HIDDEN; i += LANES) {
|
||||
const vi16 u = clamp(load(us + i), lo, hi);
|
||||
const vi16 t = clamp(load(them + i), lo, hi);
|
||||
sum_us = dot(sum_us, mullo(u, load(net.l1w + i)), u);
|
||||
sum_them = dot(sum_them, mullo(t, load(net.l1w + HIDDEN + i)), t);
|
||||
}
|
||||
i64 out = i64(reduce(sum_us)) + reduce(sum_them);
|
||||
|
||||
out /= QA;
|
||||
out += net.l1b;
|
||||
|
||||
Reference in new issue
Block a user