Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/sqlite-vec-cpp/distances/l2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ float l2_squared_distance(std::span<const T> a, std::span<const T> b) {
if (a.size() > 16) {
return simd::l2_squared_distance_float_neon(a, b);
}
#endif
#ifdef SQLITE_VEC_X86_RUNTIME_DISPATCH
if (a.size() >= 8 && x86::cpu_has_avx2_fma()) {
return x86::l2_squared_avx2(a.data(), b.data(), a.size());
}
#endif
return l2_squared_distance_float(a, b);
} else if constexpr (std::is_same_v<T, std::int8_t>) {
Expand Down
134 changes: 100 additions & 34 deletions include/sqlite-vec-cpp/index/hnsw.hpp

Large diffs are not rendered by default.

37 changes: 20 additions & 17 deletions include/sqlite-vec-cpp/index/hnsw_quantized.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,31 @@ template <concepts::VectorElement StorageT, typename MetricT> class HNSWQuantize
/// Delegates to HNSWIndex::search_quantized_rerank which uses the internal
/// beam search loop with visited pool, prefetching, and lock-free neighbors.
std::vector<std::pair<size_t, float>> search(std::span<const float> query, size_t k,
size_t ef_search = 50) const {
return search_with_filter(query, k, ef_search, nullptr);
size_t ef_search = 50,
SearchStats* stats = nullptr) const {
return search_with_filter(query, k, ef_search, nullptr, stats);
}

/// Two-stage search with pre-filtering
std::vector<std::pair<size_t, float>> search_with_filter(std::span<const float> query, size_t k,
size_t ef_search,
const FilterFn& filter) const {
const FilterFn& filter,
SearchStats* stats = nullptr) const {
// If no quantization built or index was mutated since build, fall back to base index
if (config_.quantization == QuantizationType::None || !has_quantization() || is_stale()) {
return filter ? base_index_.search_with_filter(query, k, ef_search, filter)
: base_index_.search(query, k, ef_search);
return filter ? base_index_.search_with_filter(query, k, ef_search, filter, stats)
: base_index_.search(query, k, ef_search, stats);
}

switch (config_.quantization) {
case QuantizationType::LVQ8:
return search_with_store(lvq8_store_, query, k, ef_search, filter);
return search_with_store(lvq8_store_, query, k, ef_search, filter, stats);
case QuantizationType::LVQ4:
return search_with_store(lvq4_store_, query, k, ef_search, filter);
return search_with_store(lvq4_store_, query, k, ef_search, filter, stats);
case QuantizationType::RaBitQ:
return search_rabitq(query, k, ef_search, filter);
return search_rabitq(query, k, ef_search, filter, stats);
default:
return base_index_.search(query, k, ef_search);
return base_index_.search(query, k, ef_search, stats);
}
}

Expand Down Expand Up @@ -212,34 +214,35 @@ template <concepts::VectorElement StorageT, typename MetricT> class HNSWQuantize
quantization::RaBitQStore rabitq_store_;

/// Search using an LVQ store (LVQ-8 or LVQ-4)
/// The store provides l2_distance(query, dense_id) and prefetch(dense_id)
/// Uses squared distances during beam search (monotonic with L2, avoids per-hop sqrt).
template <typename StoreT>
std::vector<std::pair<size_t, float>>
search_with_store(const StoreT& store, std::span<const float> query, size_t k, size_t ef_search,
const FilterFn& filter) const {
const FilterFn& filter, SearchStats* stats = nullptr) const {
auto approx_dist = [&store](std::span<const float> q, size_t dense_id) -> float {
return store.l2_distance(q, dense_id);
return store.l2_distance_squared(q, dense_id);
};
auto prefetch_fn = [&store](size_t dense_id) { store.prefetch(dense_id); };

return base_index_.search_quantized_rerank(query, k, ef_search, config_.rerank_factor,
approx_dist, prefetch_fn, filter);
approx_dist, prefetch_fn, filter, stats);
}

/// Search using RaBitQ (needs precomputed query state)
/// Search using RaBitQ (needs precomputed query state; uses squared distance in beam search)
std::vector<std::pair<size_t, float>> search_rabitq(std::span<const float> query, size_t k,
size_t ef_search,
const FilterFn& filter) const {
const FilterFn& filter,
SearchStats* stats = nullptr) const {
auto query_state = rabitq_store_.prepare_query(query);

auto approx_dist = [this, &query_state](std::span<const float> /*q*/,
size_t dense_id) -> float {
return rabitq_store_.l2_distance(query_state, dense_id);
return rabitq_store_.l2_distance_squared(query_state, dense_id);
};
auto prefetch_fn = [this](size_t dense_id) { rabitq_store_.prefetch(dense_id); };

return base_index_.search_quantized_rerank(query, k, ef_search, config_.rerank_factor,
approx_dist, prefetch_fn, filter);
approx_dist, prefetch_fn, filter, stats);
}
};

Expand Down
187 changes: 146 additions & 41 deletions include/sqlite-vec-cpp/quantization/rabitq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
/// FP32 rerank for final top-K

#include <algorithm>
#include <bit>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <numeric>
#include <random>
#include <span>
Expand All @@ -52,41 +54,89 @@ inline constexpr float kRaBitQEpsilon = 1e-12f;

namespace detail {

inline constexpr uint8_t kPopcountTable[256] = {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4,
5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5,
5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4,
5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6,
5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4,
5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5,
5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4,
5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7,
5, 6, 6, 7, 6, 7, 7, 8};

inline uint32_t popcount_scalar(const uint8_t* a, size_t num_bytes) {
inline uint32_t popcount_u64(const uint8_t* a, size_t num_bytes) noexcept {
uint32_t count = 0;
for (size_t i = 0; i < num_bytes; ++i) {
count += kPopcountTable[a[i]];
size_t i = 0;
for (; i + sizeof(uint64_t) <= num_bytes; i += sizeof(uint64_t)) {
uint64_t word = 0;
std::memcpy(&word, a + i, sizeof(word));
count += static_cast<uint32_t>(std::popcount(word));
}
for (; i < num_bytes; ++i) {
count += static_cast<uint32_t>(std::popcount(static_cast<unsigned int>(a[i])));
}
return count;
}

inline uint32_t and_popcount_scalar(const uint8_t* a, const uint8_t* b, size_t num_bytes) {
inline uint32_t and_popcount_u64(const uint8_t* a, const uint8_t* b, size_t num_bytes) noexcept {
uint32_t count = 0;
for (size_t i = 0; i < num_bytes; ++i) {
count += kPopcountTable[a[i] & b[i]];
size_t i = 0;
for (; i + sizeof(uint64_t) <= num_bytes; i += sizeof(uint64_t)) {
uint64_t wa = 0;
uint64_t wb = 0;
std::memcpy(&wa, a + i, sizeof(wa));
std::memcpy(&wb, b + i, sizeof(wb));
count += static_cast<uint32_t>(std::popcount(wa & wb));
}
for (; i < num_bytes; ++i) {
count += static_cast<uint32_t>(std::popcount(static_cast<unsigned int>(a[i] & b[i])));
}
return count;
}

inline uint32_t xor_popcount_scalar(const uint8_t* a, const uint8_t* b, size_t num_bytes) {
inline uint32_t xor_popcount_u64(const uint8_t* a, const uint8_t* b, size_t num_bytes) noexcept {
uint32_t count = 0;
for (size_t i = 0; i < num_bytes; ++i) {
count += kPopcountTable[a[i] ^ b[i]];
size_t i = 0;
for (; i + sizeof(uint64_t) <= num_bytes; i += sizeof(uint64_t)) {
uint64_t wa = 0;
uint64_t wb = 0;
std::memcpy(&wa, a + i, sizeof(wa));
std::memcpy(&wb, b + i, sizeof(wb));
count += static_cast<uint32_t>(std::popcount(wa ^ wb));
}
for (; i < num_bytes; ++i) {
count += static_cast<uint32_t>(std::popcount(static_cast<unsigned int>(a[i] ^ b[i])));
}
return count;
}

/// Fused single-pass weighted 4-plane AND-popcount:
/// sum_{j=0..3} (1 << j) * popcount(code & plane[j])
/// Loads each code word once instead of scanning `code` 4 separate times.
inline uint64_t and_popcount_4planes_u64(const uint8_t* code, const uint8_t* p0, const uint8_t* p1,
const uint8_t* p2, const uint8_t* p3,
size_t num_bytes) noexcept {
uint64_t s0 = 0;
uint64_t s1 = 0;
uint64_t s2 = 0;
uint64_t s3 = 0;
size_t i = 0;
for (; i + sizeof(uint64_t) <= num_bytes; i += sizeof(uint64_t)) {
uint64_t c = 0;
uint64_t q0 = 0;
uint64_t q1 = 0;
uint64_t q2 = 0;
uint64_t q3 = 0;
std::memcpy(&c, code + i, sizeof(c));
std::memcpy(&q0, p0 + i, sizeof(q0));
std::memcpy(&q1, p1 + i, sizeof(q1));
std::memcpy(&q2, p2 + i, sizeof(q2));
std::memcpy(&q3, p3 + i, sizeof(q3));
s0 += static_cast<uint64_t>(std::popcount(c & q0));
s1 += static_cast<uint64_t>(std::popcount(c & q1));
s2 += static_cast<uint64_t>(std::popcount(c & q2));
s3 += static_cast<uint64_t>(std::popcount(c & q3));
}
for (; i < num_bytes; ++i) {
const unsigned int c = code[i];
s0 += static_cast<uint64_t>(std::popcount(c & static_cast<unsigned int>(p0[i])));
s1 += static_cast<uint64_t>(std::popcount(c & static_cast<unsigned int>(p1[i])));
s2 += static_cast<uint64_t>(std::popcount(c & static_cast<unsigned int>(p2[i])));
s3 += static_cast<uint64_t>(std::popcount(c & static_cast<unsigned int>(p3[i])));
}
return s0 + (s1 << 1) + (s2 << 2) + (s3 << 3);
}

#ifdef SQLITE_VEC_ENABLE_NEON
inline uint32_t reduce_u8x16(uint8x16_t acc) {
uint16x8_t sum16 = vpaddlq_u8(acc);
Expand All @@ -111,9 +161,8 @@ inline uint32_t popcount_neon(const uint8_t* a, size_t num_bytes) {
}
}
total += reduce_u8x16(acc);
while (i < num_bytes) {
total += kPopcountTable[a[i]];
++i;
if (i < num_bytes) {
total += popcount_u64(a + i, num_bytes - i);
}
return total;
}
Expand All @@ -134,9 +183,8 @@ inline uint32_t and_popcount_neon(const uint8_t* a, const uint8_t* b, size_t num
}
}
total += reduce_u8x16(acc);
while (i < num_bytes) {
total += kPopcountTable[a[i] & b[i]];
++i;
if (i < num_bytes) {
total += and_popcount_u64(a + i, b + i, num_bytes - i);
}
return total;
}
Expand All @@ -157,39 +205,96 @@ inline uint32_t xor_popcount_neon(const uint8_t* a, const uint8_t* b, size_t num
}
}
total += reduce_u8x16(acc);
while (i < num_bytes) {
total += kPopcountTable[a[i] ^ b[i]];
++i;
if (i < num_bytes) {
total += xor_popcount_u64(a + i, b + i, num_bytes - i);
}
return total;
}

inline uint64_t and_popcount_4planes_neon(const uint8_t* code, const uint8_t* p0, const uint8_t* p1,
const uint8_t* p2, const uint8_t* p3,
size_t num_bytes) noexcept {
uint64_t s0 = 0;
uint64_t s1 = 0;
uint64_t s2 = 0;
uint64_t s3 = 0;
size_t i = 0;
const size_t end16 = (num_bytes / 16) * 16;
uint8x16_t acc0 = vdupq_n_u8(0);
uint8x16_t acc1 = vdupq_n_u8(0);
uint8x16_t acc2 = vdupq_n_u8(0);
uint8x16_t acc3 = vdupq_n_u8(0);
size_t iters = 0;
while (i < end16) {
const uint8x16_t c = vld1q_u8(code + i);
acc0 = vaddq_u8(acc0, vcntq_u8(vandq_u8(c, vld1q_u8(p0 + i))));
acc1 = vaddq_u8(acc1, vcntq_u8(vandq_u8(c, vld1q_u8(p1 + i))));
acc2 = vaddq_u8(acc2, vcntq_u8(vandq_u8(c, vld1q_u8(p2 + i))));
acc3 = vaddq_u8(acc3, vcntq_u8(vandq_u8(c, vld1q_u8(p3 + i))));
i += 16;
if (++iters == 31) {
s0 += reduce_u8x16(acc0);
s1 += reduce_u8x16(acc1);
s2 += reduce_u8x16(acc2);
s3 += reduce_u8x16(acc3);
acc0 = vdupq_n_u8(0);
acc1 = vdupq_n_u8(0);
acc2 = vdupq_n_u8(0);
acc3 = vdupq_n_u8(0);
iters = 0;
}
}
s0 += reduce_u8x16(acc0);
s1 += reduce_u8x16(acc1);
s2 += reduce_u8x16(acc2);
s3 += reduce_u8x16(acc3);
uint64_t total = s0 + (s1 << 1) + (s2 << 2) + (s3 << 3);
if (i < num_bytes) {
total += and_popcount_4planes_u64(code + i, p0 + i, p1 + i, p2 + i, p3 + i, num_bytes - i);
}
return total;
}
#endif

inline uint32_t popcount_bytes(const uint8_t* a, size_t num_bytes) {
inline uint32_t popcount_bytes(const uint8_t* a, size_t num_bytes) noexcept {
#ifdef SQLITE_VEC_ENABLE_NEON
if (num_bytes >= 16) {
return popcount_neon(a, num_bytes);
}
#endif
return popcount_scalar(a, num_bytes);
return popcount_u64(a, num_bytes);
}

inline uint32_t and_popcount_bytes(const uint8_t* a, const uint8_t* b, size_t num_bytes) {
inline uint32_t and_popcount_bytes(const uint8_t* a, const uint8_t* b, size_t num_bytes) noexcept {
#ifdef SQLITE_VEC_ENABLE_NEON
if (num_bytes >= 16) {
return and_popcount_neon(a, b, num_bytes);
}
#endif
return and_popcount_scalar(a, b, num_bytes);
return and_popcount_u64(a, b, num_bytes);
}

inline uint32_t xor_popcount_bytes(const uint8_t* a, const uint8_t* b, size_t num_bytes) {
inline uint32_t xor_popcount_bytes(const uint8_t* a, const uint8_t* b, size_t num_bytes) noexcept {
#ifdef SQLITE_VEC_ENABLE_NEON
if (num_bytes >= 16) {
return xor_popcount_neon(a, b, num_bytes);
}
#endif
return xor_popcount_scalar(a, b, num_bytes);
return xor_popcount_u64(a, b, num_bytes);
}

inline uint64_t and_popcount_4planes(const uint8_t* code, const uint8_t* planes,
size_t plane_bytes) noexcept {
const uint8_t* p0 = planes;
const uint8_t* p1 = planes + plane_bytes;
const uint8_t* p2 = planes + 2 * plane_bytes;
const uint8_t* p3 = planes + 3 * plane_bytes;
#ifdef SQLITE_VEC_ENABLE_NEON
if (plane_bytes >= 16) {
return and_popcount_4planes_neon(code, p0, p1, p2, p3, plane_bytes);
}
#endif
return and_popcount_4planes_u64(code, p0, p1, p2, p3, plane_bytes);
}

} // namespace detail
Expand Down Expand Up @@ -273,6 +378,7 @@ struct RaBitQCode {
float norm = 0.0f; ///< L2 norm of the original vector
float dist_to_centroid = 0.0f; ///< ||v - c||, used in the distance identity
float ip_quant = 1.0f; ///< <x_bar, o_rot>, the per-vector correction factor
uint16_t code_popcount = 0; ///< Precomputed popcount(bits)

/// Number of packed bytes
[[nodiscard]] size_t byte_size() const { return bits.size(); }
Expand Down Expand Up @@ -348,12 +454,15 @@ class RaBitQ {
rotation_.apply(std::span<const float>(residual), std::span<float>(rotated));

float abs_sum = 0.0f;
uint32_t pc = 0;
for (size_t i = 0; i < padded; ++i) {
abs_sum += std::abs(rotated[i]);
if (rotated[i] >= 0.0f) {
code.bits[i / 8] |= (1u << (i % 8));
++pc;
}
}
code.code_popcount = static_cast<uint16_t>(std::min<uint32_t>(pc, 65535u));
code.ip_quant = std::max(abs_sum * rotation_.inv_sqrt_padded(), kRaBitQEpsilon);

return code;
Expand Down Expand Up @@ -455,12 +564,8 @@ class RaBitQ {
const size_t nb = query.plane_bytes;
const uint8_t* bits = code.bits.data();

const uint32_t pc = detail::popcount_bytes(bits, nb);
uint64_t su = 0;
for (size_t j = 0; j < 4; ++j) {
su += static_cast<uint64_t>(1u << j) *
detail::and_popcount_bytes(bits, query.planes.data() + j * nb, nb);
}
const uint32_t pc = code.code_popcount;
const uint64_t su = detail::and_popcount_4planes(bits, query.planes.data(), nb);

const float sum_selected =
query.vl * static_cast<float>(pc) + query.delta * static_cast<float>(su);
Expand Down
Loading
Loading