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

+248
View File
@@ -0,0 +1,248 @@
#pragma once
// Hand-written vector kernels for the networks, one set per instruction set,
// picked at compile time (the Makefile builds with -march=native;
// -DRIGBY_NO_SIMD forces the scalar fallback). Every op is
// exact integer arithmetic, so all paths give the same results as the scalar
// fallback, and the bench does not depend on the machine.
#include <cstdint>
#include "types.h"
#if defined(RIGBY_NO_SIMD)
#define RIGBY_SIMD_SCALAR
#elif defined(__AVX512F__) && defined(__AVX512BW__)
#define RIGBY_SIMD_AVX512
#include <immintrin.h>
#elif defined(__AVX2__)
#define RIGBY_SIMD_AVX2
#include <immintrin.h>
#elif defined(__SSE2__) || defined(_M_X64) || defined(_M_AMD64)
#define RIGBY_SIMD_SSE2
#include <immintrin.h>
#elif defined(__ARM_NEON) && defined(__aarch64__)
#define RIGBY_SIMD_NEON
#include <arm_neon.h>
#else
#define RIGBY_SIMD_SCALAR
#endif
namespace rigby::simd {
using i32 = std::int32_t;
// vi16: LANES 16-bit lanes. vi32: 32-bit sums of the same register width.
// ACC_REGS: registers a kernel may keep accumulating in without spilling.
#if defined(RIGBY_SIMD_AVX512)
using vi16 = __m512i;
using vi32 = __m512i;
constexpr int LANES = 32;
constexpr int ACC_REGS = 16;
constexpr const char* NAME = "avx512";
inline vi16 load(const i16* p) { return _mm512_loadu_si512(p); }
inline vi16 load(const i8* p) { return _mm512_cvtepi8_epi16(_mm256_loadu_si256(reinterpret_cast<const __m256i*>(p))); }
inline void store(i16* p, vi16 v) { _mm512_storeu_si512(p, v); }
inline vi16 set1(i16 x) { return _mm512_set1_epi16(x); }
inline vi16 add(vi16 a, vi16 b) { return _mm512_add_epi16(a, b); }
inline vi16 sub(vi16 a, vi16 b) { return _mm512_sub_epi16(a, b); }
inline vi16 min(vi16 a, vi16 b) { return _mm512_min_epi16(a, b); }
inline vi16 max(vi16 a, vi16 b) { return _mm512_max_epi16(a, b); }
inline vi16 mullo(vi16 a, vi16 b) { return _mm512_mullo_epi16(a, b); }
inline vi32 zero32() { return _mm512_setzero_si512(); }
inline vi32 add32(vi32 a, vi32 b) { return _mm512_add_epi32(a, b); }
// acc + a[2i] * b[2i] + a[2i+1] * b[2i+1] in each 32-bit lane.
inline vi32 dot(vi32 acc, vi16 a, vi16 b) {
#if defined(__AVX512VNNI__)
return _mm512_dpwssd_epi32(acc, a, b);
#else
return _mm512_add_epi32(acc, _mm512_madd_epi16(a, b));
#endif
}
inline i32 reduce(vi32 v) { return _mm512_reduce_add_epi32(v); }
#elif defined(RIGBY_SIMD_AVX2)
using vi16 = __m256i;
using vi32 = __m256i;
constexpr int LANES = 16;
constexpr int ACC_REGS = 8;
constexpr const char* NAME = "avx2";
inline vi16 load(const i16* p) { return _mm256_loadu_si256(reinterpret_cast<const __m256i*>(p)); }
inline vi16 load(const i8* p) { return _mm256_cvtepi8_epi16(_mm_loadu_si128(reinterpret_cast<const __m128i*>(p))); }
inline void store(i16* p, vi16 v) { _mm256_storeu_si256(reinterpret_cast<__m256i*>(p), v); }
inline vi16 set1(i16 x) { return _mm256_set1_epi16(x); }
inline vi16 add(vi16 a, vi16 b) { return _mm256_add_epi16(a, b); }
inline vi16 sub(vi16 a, vi16 b) { return _mm256_sub_epi16(a, b); }
inline vi16 min(vi16 a, vi16 b) { return _mm256_min_epi16(a, b); }
inline vi16 max(vi16 a, vi16 b) { return _mm256_max_epi16(a, b); }
inline vi16 mullo(vi16 a, vi16 b) { return _mm256_mullo_epi16(a, b); }
inline vi32 zero32() { return _mm256_setzero_si256(); }
inline vi32 add32(vi32 a, vi32 b) { return _mm256_add_epi32(a, b); }
inline vi32 dot(vi32 acc, vi16 a, vi16 b) {
#if defined(__AVXVNNI__)
return _mm256_dpwssd_avx_epi32(acc, a, b);
#else
return _mm256_add_epi32(acc, _mm256_madd_epi16(a, b));
#endif
}
inline i32 reduce(vi32 v) {
__m128i x = _mm_add_epi32(_mm256_castsi256_si128(v), _mm256_extracti128_si256(v, 1));
x = _mm_add_epi32(x, _mm_shuffle_epi32(x, _MM_SHUFFLE(1, 0, 3, 2)));
x = _mm_add_epi32(x, _mm_shuffle_epi32(x, _MM_SHUFFLE(2, 3, 0, 1)));
return _mm_cvtsi128_si32(x);
}
#elif defined(RIGBY_SIMD_SSE2)
using vi16 = __m128i;
using vi32 = __m128i;
constexpr int LANES = 8;
constexpr int ACC_REGS = 8;
constexpr const char* NAME = "sse2";
inline vi16 load(const i16* p) { return _mm_loadu_si128(reinterpret_cast<const __m128i*>(p)); }
inline vi16 load(const i8* p) {
const __m128i x = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(p));
#if defined(__SSE4_1__)
return _mm_cvtepi8_epi16(x);
#else
return _mm_srai_epi16(_mm_unpacklo_epi8(x, x), 8);
#endif
}
inline void store(i16* p, vi16 v) { _mm_storeu_si128(reinterpret_cast<__m128i*>(p), v); }
inline vi16 set1(i16 x) { return _mm_set1_epi16(x); }
inline vi16 add(vi16 a, vi16 b) { return _mm_add_epi16(a, b); }
inline vi16 sub(vi16 a, vi16 b) { return _mm_sub_epi16(a, b); }
inline vi16 min(vi16 a, vi16 b) { return _mm_min_epi16(a, b); }
inline vi16 max(vi16 a, vi16 b) { return _mm_max_epi16(a, b); }
inline vi16 mullo(vi16 a, vi16 b) { return _mm_mullo_epi16(a, b); }
inline vi32 zero32() { return _mm_setzero_si128(); }
inline vi32 add32(vi32 a, vi32 b) { return _mm_add_epi32(a, b); }
inline vi32 dot(vi32 acc, vi16 a, vi16 b) { return _mm_add_epi32(acc, _mm_madd_epi16(a, b)); }
inline i32 reduce(vi32 x) {
x = _mm_add_epi32(x, _mm_shuffle_epi32(x, _MM_SHUFFLE(1, 0, 3, 2)));
x = _mm_add_epi32(x, _mm_shuffle_epi32(x, _MM_SHUFFLE(2, 3, 0, 1)));
return _mm_cvtsi128_si32(x);
}
#elif defined(RIGBY_SIMD_NEON)
using vi16 = int16x8_t;
using vi32 = int32x4_t;
constexpr int LANES = 8;
constexpr int ACC_REGS = 16;
constexpr const char* NAME = "neon";
inline vi16 load(const i16* p) { return vld1q_s16(p); }
inline vi16 load(const i8* p) { return vmovl_s8(vld1_s8(p)); }
inline void store(i16* p, vi16 v) { vst1q_s16(p, v); }
inline vi16 set1(i16 x) { return vdupq_n_s16(x); }
inline vi16 add(vi16 a, vi16 b) { return vaddq_s16(a, b); }
inline vi16 sub(vi16 a, vi16 b) { return vsubq_s16(a, b); }
inline vi16 min(vi16 a, vi16 b) { return vminq_s16(a, b); }
inline vi16 max(vi16 a, vi16 b) { return vmaxq_s16(a, b); }
inline vi16 mullo(vi16 a, vi16 b) { return vmulq_s16(a, b); }
inline vi32 zero32() { return vdupq_n_s32(0); }
inline vi32 add32(vi32 a, vi32 b) { return vaddq_s32(a, b); }
// Pairs lanes differently from x86's madd, but every caller only needs the total.
inline vi32 dot(vi32 acc, vi16 a, vi16 b) {
acc = vmlal_s16(acc, vget_low_s16(a), vget_low_s16(b));
return vmlal_high_s16(acc, a, b);
}
inline i32 reduce(vi32 v) { return vaddvq_s32(v); }
#else
// One lane: the generic kernels below become the plain scalar loops.
using vi16 = i16;
using vi32 = i32;
constexpr int LANES = 1;
constexpr int ACC_REGS = 8;
constexpr const char* NAME = "scalar";
inline vi16 load(const i16* p) { return *p; }
inline vi16 load(const i8* p) { return *p; }
inline void store(i16* p, vi16 v) { *p = v; }
inline vi16 set1(i16 x) { return x; }
inline vi16 add(vi16 a, vi16 b) { return i16(a + b); }
inline vi16 sub(vi16 a, vi16 b) { return i16(a - b); }
inline vi16 min(vi16 a, vi16 b) { return a < b ? a : b; }
inline vi16 max(vi16 a, vi16 b) { return a > b ? a : b; }
inline vi16 mullo(vi16 a, vi16 b) { return i16(a * b); }
inline vi32 zero32() { return 0; }
inline vi32 add32(vi32 a, vi32 b) { return a + b; }
inline vi32 dot(vi32 acc, vi16 a, vi16 b) { return acc + i32(a) * i32(b); }
inline i32 reduce(vi32 v) { return v; }
#endif
inline vi16 clamp(vi16 x, vi16 lo, vi16 hi) { return min(max(x, lo), hi); }
namespace detail {
// R registers wide: dst = base + rows[0] + ... + rows[count - 1], from `offset`.
template <int R, typename B, typename W>
inline void accumulate_tile(i16* dst, const B* base, const W* const* rows, int count, int offset) {
vi16 r[R];
for (int k = 0; k < R; k++) r[k] = load(base + offset + k * LANES);
for (int f = 0; f < count; f++) {
const W* w = rows[f] + offset;
for (int k = 0; k < R; k++) r[k] = add(r[k], load(w + k * LANES));
}
for (int k = 0; k < R; k++) store(dst + offset + k * LANES, r[k]);
}
} // namespace detail
// dst[i] = base[i] + sum of rows[f][i] for i < n, wrapping in 16 bits like the
// scalar sum in any order. Rows are summed in registers one tile at a time,
// so dst is written once instead of once per row.
template <int TILE_REGS = ACC_REGS, typename B, typename W>
inline void accumulate(i16* dst, const B* base, const W* const* rows, int count, int n) {
constexpr int TILE = TILE_REGS * LANES;
int i = 0;
for (; i + TILE <= n; i += TILE) detail::accumulate_tile<TILE_REGS>(dst, base, rows, count, i);
for (; i + LANES <= n; i += LANES) detail::accumulate_tile<1>(dst, base, rows, count, i);
for (; i < n; i++) {
i16 v = i16(base[i]);
for (int f = 0; f < count; f++) v = i16(v + rows[f][i]);
dst[i] = v;
}
}
// Sum of a[i] * b[i] for i < n, in 32 bits (the caller makes sure it fits).
template <typename W>
inline i32 dot(const i16* a, const W* b, int n) {
vi32 s0 = zero32(), s1 = zero32();
int i = 0;
for (; i + 2 * LANES <= n; i += 2 * LANES) {
s0 = dot(s0, load(a + i), load(b + i));
s1 = dot(s1, load(a + i + LANES), load(b + i + LANES));
}
for (; i + LANES <= n; i += LANES) s0 = dot(s0, load(a + i), load(b + i));
i32 sum = reduce(add32(s0, s1));
for (; i < n; i++) sum += i32(a[i]) * i32(b[i]);
return sum;
}
// dot(a, b[k], n) for four rows at once, sharing the loads of `a`.
template <typename W>
inline void dot4(const i16* a, const W* const* b, int n, i32* out) {
vi32 s[4] = {zero32(), zero32(), zero32(), zero32()};
int i = 0;
for (; i + LANES <= n; i += LANES) {
const vi16 x = load(a + i);
for (int k = 0; k < 4; k++) s[k] = dot(s[k], x, load(b[k] + i));
}
for (int k = 0; k < 4; k++) {
out[k] = reduce(s[k]);
for (int j = i; j < n; j++) out[k] += i32(a[j]) * i32(b[k][j]);
}
}
} // namespace rigby::simd