Files
leleka/crates/cpu-sys/csrc/dot_avx2.c
T
Pavlo RatushnyiandClaude Sonnet 5.5 37620f1b60 feat: add CPU runtime kernel layer with AVX2 and AVX-VNNI dot products (STRA-202)
leleka-cpu-sys holds the C kernels (one translation unit per ISA level, built
by build.rs with cc) behind a checked FFI; it is the only unsafe code of the CPU
runtime and opts out of the workspace unsafe_code lint with a recorded exception.
leleka-cpu-kernels does safe run-time dispatch with a LELEKA_CPU_ISA override
that fails rather than silently changing level.

Ports the int8xint8 and int4xint8 dot products from Colibri c/idot.h at scalar,
AVX2 and AVX-VNNI. Tests compare every level with the C scalar kernel and an
independent i64 oracle (13 tests; Windows MSVC and Linux GCC 14.2). Activations
must be in [-127, 127]: -128 is outside the contract and a test pins that. No
performance claim: nothing was timed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 15:00:03 +02:00

60 lines
2.6 KiB
C

/* dot_avx2.c - AVX2 kernels. Built with /arch:AVX2 (MSVC) or -mavx2 (GNU) by build.rs.
*
* Adapted from Colibri c/idot.h (commit bf2442915d6e3dd4cdfd2eb9c2a3d2aa44a25850),
* Apache-2.0, copyright 2026 Vincenzo Fornaro. Local changes: the AVX2 branch of
* dot_i8i8 and dot_i4i8 is split into its own translation unit so that a run-time
* dispatcher can choose it; size_t lengths; the tail is shared with the scalar file's
* semantics. Exactness: x must be in [-127, 127] (see leleka_cpu.h). */
#include "leleka_cpu.h"
#include <immintrin.h>
static inline int32_t hsum256_i32(__m256i v) {
__m128i lo = _mm256_castsi256_si128(v), hi = _mm256_extracti128_si256(v, 1);
lo = _mm_add_epi32(lo, hi);
lo = _mm_hadd_epi32(lo, lo);
lo = _mm_hadd_epi32(lo, lo);
return _mm_cvtsi128_si32(lo);
}
int32_t leleka_dot_i8i8_avx2(const int8_t *w, const int8_t *x, size_t n) {
size_t i = 0;
__m256i acc = _mm256_setzero_si256();
const __m256i ones = _mm256_set1_epi16(1);
for (; i + 32 <= n; i += 32) {
__m256i wv = _mm256_loadu_si256((const __m256i *)(w + i));
__m256i xv = _mm256_loadu_si256((const __m256i *)(x + i));
__m256i p = _mm256_maddubs_epi16(_mm256_sign_epi8(wv, wv), _mm256_sign_epi8(xv, wv));
acc = _mm256_add_epi32(acc, _mm256_madd_epi16(p, ones));
}
int32_t sum = hsum256_i32(acc);
for (; i < n; i++) sum += (int32_t)w[i] * (int32_t)x[i];
return sum;
}
int32_t leleka_dot_i4i8_avx2(const uint8_t *w4, const int8_t *x, size_t n) {
size_t i = 0;
const __m128i m4 = _mm_set1_epi8(0x0F);
const __m256i b8 = _mm256_set1_epi8(8);
const __m256i ones = _mm256_set1_epi16(1);
__m256i acc = _mm256_setzero_si256();
for (; i + 32 <= n; i += 32) {
__m128i by = _mm_loadu_si128((const __m128i *)(w4 + (i >> 1)));
__m128i lo = _mm_and_si128(by, m4), hi = _mm_and_si128(_mm_srli_epi16(by, 4), m4);
__m128i n0 = _mm_unpacklo_epi8(lo, hi), n1 = _mm_unpackhi_epi8(lo, hi);
__m256i wv = _mm256_sub_epi8(_mm256_set_m128i(n1, n0), b8);
__m256i xv = _mm256_loadu_si256((const __m256i *)(x + i));
__m256i p = _mm256_maddubs_epi16(_mm256_sign_epi8(wv, wv), _mm256_sign_epi8(xv, wv));
acc = _mm256_add_epi32(acc, _mm256_madd_epi16(p, ones));
}
int32_t sum = hsum256_i32(acc);
for (; i + 1 < n; i += 2) {
uint8_t b = w4[i >> 1];
sum += ((int32_t)(b & 0xF) - 8) * (int32_t)x[i] + ((int32_t)(b >> 4) - 8) * (int32_t)x[i + 1];
}
if (i < n) {
uint8_t b = w4[i >> 1];
sum += ((int32_t)(b & 0xF) - 8) * (int32_t)x[i];
}
return sum;
}