feat(accel): runtime-dispatched int8 dot product
The int8-quantised HNSW index compared vectors with a scalar loop that the compiler vectorised for the x86-64 baseline (SSE2), while the f32 path it was measured against goes through `clawhdf5-accel` and runs AVX2. So the ~13% throughput cost recorded for `quantized_index` was a missing kernel rather than a property of int8. `clawhdf5_accel::dot_i8` adds a scalar fallback and an AVX2 path: sign-extend each 16-byte half to i16, then `madd_epi16`, which multiplies and sums adjacent pairs straight into i32 lanes. It is dispatched through the same detected backend as the f32 kernels, and the index now calls it. Integer arithmetic, so the SIMD path must agree with scalar bit for bit — tested at lengths that are and are not multiples of the block, and at the -128 extreme for overflow. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
@@ -25,6 +25,55 @@ unsafe fn hsum_256(v: __m256) -> f32 {
|
||||
_mm_cvtss_f32(result)
|
||||
}
|
||||
|
||||
/// AVX2 dot product of two `i8` slices, widened to `i32`.
|
||||
///
|
||||
/// Each 16-byte half is sign-extended to sixteen `i16` lanes and multiplied
|
||||
/// pairwise with `madd_epi16`, which sums adjacent products straight into
|
||||
/// eight `i32` lanes — the widening that an autovectorised scalar loop does
|
||||
/// in several shuffles is one instruction here. A pair sum is at most
|
||||
/// `2 * 127 * 127`, far inside `i32`.
|
||||
///
|
||||
/// # Safety
|
||||
/// Caller must verify is_x86_feature_detected!("avx2").
|
||||
// SAFETY: Caller must have verified AVX2 via is_x86_feature_detected!.
|
||||
#[target_feature(enable = "avx2")]
|
||||
pub unsafe fn dot_i8(a: &[i8], b: &[i8]) -> i32 {
|
||||
// SAFETY: Caller guarantees AVX2 is available per the # Safety contract;
|
||||
// every load reads 32 bytes at an index checked against `len` first.
|
||||
unsafe {
|
||||
assert_eq!(a.len(), b.len());
|
||||
let len = a.len();
|
||||
let mut i = 0;
|
||||
let mut acc0 = _mm256_setzero_si256();
|
||||
let mut acc1 = _mm256_setzero_si256();
|
||||
|
||||
while i + 32 <= len {
|
||||
let va = _mm256_loadu_si256(a.as_ptr().add(i).cast());
|
||||
let vb = _mm256_loadu_si256(b.as_ptr().add(i).cast());
|
||||
let a_lo = _mm256_cvtepi8_epi16(_mm256_castsi256_si128(va));
|
||||
let b_lo = _mm256_cvtepi8_epi16(_mm256_castsi256_si128(vb));
|
||||
let a_hi = _mm256_cvtepi8_epi16(_mm256_extracti128_si256(va, 1));
|
||||
let b_hi = _mm256_cvtepi8_epi16(_mm256_extracti128_si256(vb, 1));
|
||||
acc0 = _mm256_add_epi32(acc0, _mm256_madd_epi16(a_lo, b_lo));
|
||||
acc1 = _mm256_add_epi32(acc1, _mm256_madd_epi16(a_hi, b_hi));
|
||||
i += 32;
|
||||
}
|
||||
|
||||
// Horizontal sum of the eight i32 lanes.
|
||||
let v = _mm256_add_epi32(acc0, acc1);
|
||||
let s128 = _mm_add_epi32(_mm256_castsi256_si128(v), _mm256_extracti128_si256(v, 1));
|
||||
let s64 = _mm_add_epi32(s128, _mm_unpackhi_epi64(s128, s128));
|
||||
let s32 = _mm_add_epi32(s64, _mm_shuffle_epi32(s64, 0b01));
|
||||
let mut sum = _mm_cvtsi128_si32(s32);
|
||||
|
||||
while i < len {
|
||||
sum += i32::from(a[i]) * i32::from(b[i]);
|
||||
i += 1;
|
||||
}
|
||||
sum
|
||||
}
|
||||
}
|
||||
|
||||
/// AVX2 dot product for f32 slices.
|
||||
///
|
||||
/// # Safety
|
||||
|
||||
Reference in New Issue
Block a user