Skip to content

Commit f77c24a

Browse files
committed
feat: ci update and avx experimental code
1 parent 0f347d7 commit f77c24a

9 files changed

Lines changed: 437 additions & 6 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,37 @@ jobs:
9090
name: coverage-linux
9191
path: coverage/
9292

93+
# =============================================================================
94+
# Linux Tests (AVX/AVX2 SIMD)
95+
# =============================================================================
96+
test-linux-avx:
97+
runs-on: ubuntu-latest
98+
99+
steps:
100+
- uses: actions/checkout@v4
101+
102+
- name: Install build dependencies
103+
run: |
104+
sudo apt-get update
105+
sudo apt-get install -y \
106+
meson \
107+
ninja-build \
108+
pkg-config \
109+
libsqlite3-dev
110+
111+
- name: Configure (AVX enabled)
112+
run: |
113+
meson setup build_avx \
114+
-Dbuildtype=debugoptimized \
115+
-Denable_simd_avx=true \
116+
-Denable_simd_neon=false
117+
118+
- name: Build
119+
run: meson compile -C build_avx
120+
121+
- name: Test
122+
run: meson test -C build_avx
123+
93124
# =============================================================================
94125
# Linux TSAN
95126
# =============================================================================
@@ -148,12 +179,12 @@ jobs:
148179
libsqlite3-dev \
149180
libbenchmark-dev
150181
151-
- name: Configure (release + benchmarks, no SIMD)
182+
- name: Configure (release + benchmarks, AVX enabled)
152183
run: |
153184
meson setup build_bench \
154185
-Dbuildtype=release \
155186
-Denable_benchmarks=true \
156-
-Denable_simd_avx=false \
187+
-Denable_simd_avx=true \
157188
-Denable_simd_neon=false
158189
159190
- name: Build

‎benchmarks/distance_benchmark.cpp‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,65 @@ BENCHMARK(BM_Int8_InnerProduct_Scalar_384);
333333

334334
#endif // SQLITE_VEC_ENABLE_NEON
335335

336+
// ============================================================================
337+
// AVX2 int8 Benchmarks
338+
// ============================================================================
339+
340+
#if defined(SQLITE_VEC_ENABLE_AVX2)
341+
#include "../include/sqlite-vec-cpp/simd/avx.hpp"
342+
343+
static void BM_Int8_L2_AVX2_384(benchmark::State& state) {
344+
auto a = generate_random_vector<int8_t>(384, int8_t{-127}, int8_t{127});
345+
auto b = generate_random_vector<int8_t>(384, int8_t{-127}, int8_t{127});
346+
347+
for (auto _ : state) {
348+
float result = simd::l2_distance_int8_avx2(std::span<const int8_t>(a),
349+
std::span<const int8_t>(b));
350+
benchmark::DoNotOptimize(result);
351+
}
352+
state.SetItemsProcessed(state.iterations() * 384);
353+
}
354+
BENCHMARK(BM_Int8_L2_AVX2_384);
355+
356+
static void BM_Int8_Cosine_AVX2_384(benchmark::State& state) {
357+
auto a = generate_random_vector<int8_t>(384, int8_t{-127}, int8_t{127});
358+
auto b = generate_random_vector<int8_t>(384, int8_t{-127}, int8_t{127});
359+
360+
for (auto _ : state) {
361+
float result = simd::cosine_distance_int8_avx2(std::span<const int8_t>(a),
362+
std::span<const int8_t>(b));
363+
benchmark::DoNotOptimize(result);
364+
}
365+
state.SetItemsProcessed(state.iterations() * 384);
366+
}
367+
BENCHMARK(BM_Int8_Cosine_AVX2_384);
368+
369+
static void BM_Int8_InnerProduct_AVX2_384(benchmark::State& state) {
370+
auto a = generate_random_vector<int8_t>(384, int8_t{-127}, int8_t{127});
371+
auto b = generate_random_vector<int8_t>(384, int8_t{-127}, int8_t{127});
372+
373+
for (auto _ : state) {
374+
float result = simd::inner_product_int8_avx2(std::span<const int8_t>(a),
375+
std::span<const int8_t>(b));
376+
benchmark::DoNotOptimize(result);
377+
}
378+
state.SetItemsProcessed(state.iterations() * 384);
379+
}
380+
BENCHMARK(BM_Int8_InnerProduct_AVX2_384);
381+
382+
static void BM_L1_Float_AVX_128(benchmark::State& state) {
383+
auto a = generate_random_vector<float>(128);
384+
auto b = generate_random_vector<float>(128);
385+
386+
for (auto _ : state) {
387+
float result = simd::l1_distance_float_avx(std::span<const float>(a),
388+
std::span<const float>(b));
389+
benchmark::DoNotOptimize(result);
390+
}
391+
state.SetItemsProcessed(state.iterations() * 128);
392+
}
393+
BENCHMARK(BM_L1_Float_AVX_128);
394+
395+
#endif // SQLITE_VEC_ENABLE_AVX2
396+
336397
BENCHMARK_MAIN();

‎include/sqlite-vec-cpp/distances/cosine.hpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ float cosine_distance(std::span<const T> a, std::span<const T> b) {
124124
if (a.size() >= 16) {
125125
return simd::cosine_distance_int8_neon(a, b);
126126
}
127+
#endif
128+
#ifdef SQLITE_VEC_ENABLE_AVX2
129+
if (a.size() >= 16) {
130+
return simd::cosine_distance_int8_avx2(a, b);
131+
}
127132
#endif
128133
return cosine_distance_int(a, b);
129134
} else if constexpr (concepts::IntegerElement<T>) {

‎include/sqlite-vec-cpp/distances/inner_product.hpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ float inner_product_distance(std::span<const T> a, std::span<const T> b) {
172172
if (a.size() >= 16) {
173173
return simd::inner_product_int8_neon(a, b);
174174
}
175+
#endif
176+
#ifdef SQLITE_VEC_ENABLE_AVX2
177+
if (a.size() >= 16) {
178+
return simd::inner_product_int8_avx2(a, b);
179+
}
175180
#endif
176181
return inner_product_distance_int(a, b);
177182
} else if constexpr (concepts::IntegerElement<T>) {

‎include/sqlite-vec-cpp/distances/l1.hpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include "../concepts/vector_element.hpp"
1010

1111
// Include SIMD implementations
12+
#ifdef SQLITE_VEC_ENABLE_AVX
13+
#include "../simd/avx.hpp"
14+
#endif
15+
1216
#ifdef SQLITE_VEC_ENABLE_NEON
1317
#include "../simd/neon.hpp"
1418
#endif
@@ -60,6 +64,11 @@ template <concepts::VectorElement T> float l1_distance(std::span<const T> a, std
6064
assert(a.size() == b.size() && "Vector dimensions must match");
6165

6266
if constexpr (std::is_same_v<T, float>) {
67+
#ifdef SQLITE_VEC_ENABLE_AVX
68+
if (a.size() >= 8) {
69+
return simd::l1_distance_float_avx(a, b);
70+
}
71+
#endif
6372
#ifdef SQLITE_VEC_ENABLE_NEON
6473
if (a.size() > 3) {
6574
return static_cast<float>(simd::l1_distance_float_neon(a, b));

‎include/sqlite-vec-cpp/distances/l2.hpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ template <concepts::VectorElement T> float l2_distance(std::span<const T> a, std
8080
#endif
8181
return l2_distance_float(a, b);
8282
} else if constexpr (std::is_same_v<T, std::int8_t>) {
83+
#ifdef SQLITE_VEC_ENABLE_AVX2
84+
if (a.size() >= 16) {
85+
return simd::l2_distance_int8_avx2(a, b);
86+
}
87+
#endif
8388
#ifdef SQLITE_VEC_ENABLE_NEON
8489
if (a.size() > 7) {
8590
return simd::l2_distance_int8_neon(a, b);

‎include/sqlite-vec-cpp/quantization/lvq.hpp‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ struct LVQ4 {
373373
sum_sq =
374374
l2_distance_neon(query.data(), code.codes.data(), original_dim, inv_scale, offset);
375375
} else
376+
#endif
377+
#ifdef SQLITE_VEC_ENABLE_AVX2
378+
if (original_dim >= 32) {
379+
sum_sq = l2_distance_avx2(query.data(), code.codes.data(), original_dim, inv_scale,
380+
offset);
381+
} else
376382
#endif
377383
{
378384
sum_sq = l2_distance_scalar(query.data(), code.codes.data(), original_dim, inv_scale,
@@ -490,6 +496,91 @@ struct LVQ4 {
490496
return sum;
491497
}
492498
#endif
499+
500+
#ifdef SQLITE_VEC_ENABLE_AVX2
501+
/// AVX2 LVQ-4 L2 distance: process 32 dims (16 packed bytes) per iteration
502+
static float l2_distance_avx2(const float* query, const uint8_t* codes, size_t dim,
503+
float inv_scale, float offset) {
504+
const size_t end32 = (dim / 32) * 32;
505+
506+
__m256 voffset = _mm256_set1_ps(offset);
507+
__m256 vinv_scale = _mm256_set1_ps(inv_scale);
508+
__m256 sum0 = _mm256_setzero_ps();
509+
__m256 sum1 = _mm256_setzero_ps();
510+
511+
// Nibble mask: 0x0F repeated
512+
__m128i mask_lo = _mm_set1_epi8(0x0F);
513+
514+
size_t i = 0;
515+
while (i < end32) {
516+
// Load 16 packed bytes = 32 nibbles = 32 dimensions
517+
__m128i packed = _mm_loadu_si128(reinterpret_cast<const __m128i*>(codes + i / 2));
518+
519+
// Extract low nibbles (even dims) and high nibbles (odd dims)
520+
__m128i lo_nibbles = _mm_and_si128(packed, mask_lo);
521+
__m128i hi_nibbles = _mm_and_si128(_mm_srli_epi16(packed, 4), mask_lo);
522+
523+
// Interleave to natural order: lo[0],hi[0],lo[1],hi[1],...
524+
__m128i unpacked_0_15 = _mm_unpacklo_epi8(lo_nibbles, hi_nibbles);
525+
__m128i unpacked_16_31 = _mm_unpackhi_epi8(lo_nibbles, hi_nibbles);
526+
527+
// === Process dims i..i+15 ===
528+
// Widen first 8 bytes to int32 then float
529+
__m256i codes32_a = _mm256_cvtepu8_epi32(unpacked_0_15);
530+
__m256 cf_a = _mm256_cvtepi32_ps(codes32_a);
531+
__m256 q_a = _mm256_loadu_ps(query + i);
532+
__m256 qn_a = _mm256_mul_ps(_mm256_sub_ps(q_a, voffset), vinv_scale);
533+
__m256 d_a = _mm256_sub_ps(qn_a, cf_a);
534+
sum0 = _mm256_fmadd_ps(d_a, d_a, sum0);
535+
536+
// Widen next 8 bytes
537+
__m128i upper_0_15 = _mm_srli_si128(unpacked_0_15, 8);
538+
__m256i codes32_b = _mm256_cvtepu8_epi32(upper_0_15);
539+
__m256 cf_b = _mm256_cvtepi32_ps(codes32_b);
540+
__m256 q_b = _mm256_loadu_ps(query + i + 8);
541+
__m256 qn_b = _mm256_mul_ps(_mm256_sub_ps(q_b, voffset), vinv_scale);
542+
__m256 d_b = _mm256_sub_ps(qn_b, cf_b);
543+
sum1 = _mm256_fmadd_ps(d_b, d_b, sum1);
544+
545+
// === Process dims i+16..i+31 ===
546+
__m256i codes32_c = _mm256_cvtepu8_epi32(unpacked_16_31);
547+
__m256 cf_c = _mm256_cvtepi32_ps(codes32_c);
548+
__m256 q_c = _mm256_loadu_ps(query + i + 16);
549+
__m256 qn_c = _mm256_mul_ps(_mm256_sub_ps(q_c, voffset), vinv_scale);
550+
__m256 d_c = _mm256_sub_ps(qn_c, cf_c);
551+
sum0 = _mm256_fmadd_ps(d_c, d_c, sum0);
552+
553+
__m128i upper_16_31 = _mm_srli_si128(unpacked_16_31, 8);
554+
__m256i codes32_d = _mm256_cvtepu8_epi32(upper_16_31);
555+
__m256 cf_d = _mm256_cvtepi32_ps(codes32_d);
556+
__m256 q_d = _mm256_loadu_ps(query + i + 24);
557+
__m256 qn_d = _mm256_mul_ps(_mm256_sub_ps(q_d, voffset), vinv_scale);
558+
__m256 d_d = _mm256_sub_ps(qn_d, cf_d);
559+
sum1 = _mm256_fmadd_ps(d_d, d_d, sum1);
560+
561+
i += 32;
562+
}
563+
564+
__m256 total = _mm256_add_ps(sum0, sum1);
565+
__m128 hi128 = _mm256_extractf128_ps(total, 1);
566+
__m128 lo128 = _mm256_castps256_ps128(total);
567+
__m128 sum128 = _mm_add_ps(lo128, hi128);
568+
sum128 = _mm_hadd_ps(sum128, sum128);
569+
sum128 = _mm_hadd_ps(sum128, sum128);
570+
float sum = _mm_cvtss_f32(sum128);
571+
572+
// Scalar tail
573+
while (i < dim) {
574+
uint8_t c = (i % 2 == 0) ? (codes[i / 2] & 0x0F) : ((codes[i / 2] >> 4) & 0x0F);
575+
float q_normalized = (query[i] - offset) * inv_scale;
576+
float diff = q_normalized - static_cast<float>(c);
577+
sum += diff * diff;
578+
++i;
579+
}
580+
581+
return sum;
582+
}
583+
#endif
493584
};
494585

495586
/// Batch quantizer: quantizes a collection of vectors

0 commit comments

Comments
 (0)