/* * Standalone two-stage binary Hamming scan + int8 cosine rescore benchmark kernel. * Companion artifact to SpecStream's engineering paper: * "Millisecond semantic retrieval over 47 million Wikipedia paragraphs in 6 GB of RAM" * https://specstream.ai/blog/millisecond-semantic-retrieval-47m-wikipedia-6gb-ram * * License: MIT */ #include #include #include #include #if defined(__AVX512F__) && defined(__AVX512VPOPCNTDQ__) #include #define HAVE_AVX512_POPCNT 1 #else #define HAVE_AVX512_POPCNT 0 #endif static inline uint32_t hamming_1024(const uint64_t *a, const uint64_t *b) { #if HAVE_AVX512_POPCNT __m512i x0 = _mm512_xor_si512(_mm512_loadu_si512((const void *)a), _mm512_loadu_si512((const void *)b)); __m512i x1 = _mm512_xor_si512(_mm512_loadu_si512((const void *)(a + 8)), _mm512_loadu_si512((const void *)(b + 8))); __m512i p = _mm512_add_epi64(_mm512_popcnt_epi64(x0), _mm512_popcnt_epi64(x1)); return (uint32_t)_mm512_reduce_add_epi64(p); #else uint32_t d = 0; for (int w = 0; w < 16; w++) { d += (uint32_t)__builtin_popcountll(a[w] ^ b[w]); } return d; #endif } static inline void heap_sift_down(uint64_t *h, int n, int i) { uint64_t v = h[i]; for (;;) { int l = (i << 1) + 1; if (l >= n) break; int r = l + 1; int big = (r < n && h[r] > h[l]) ? r : l; if (h[big] <= v) break; h[i] = h[big]; i = big; } h[i] = v; } static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a; uint64_t y = *(const uint64_t *)b; return (x > y) - (x < y); } /* * Exact top-cand binary Hamming scan across n 1024-bit vectors (16 uint64 words each). * Deterministic tie-breaking via (distance << 32) | row_id. */ int scan_hamming_top_k( const uint64_t *bin_db, uint32_t n, const uint64_t *query_bin, int cand, uint32_t *out_ids, uint32_t *out_dists ) { if (cand <= 0 || n == 0) return 0; if ((uint32_t)cand > n) cand = (int)n; int max_threads = omp_get_max_threads(); uint64_t *all_heaps = (uint64_t *)malloc((size_t)max_threads * (size_t)cand * sizeof(uint64_t)); int *counts = (int *)calloc((size_t)max_threads, sizeof(int)); if (!all_heaps || !counts) { free(all_heaps); free(counts); return -1; } #pragma omp parallel { int tid = omp_get_thread_num(); uint64_t *heap = all_heaps + (size_t)tid * (size_t)cand; int sz = 0; #pragma omp for schedule(static) for (uint32_t i = 0; i < n; i++) { uint32_t dist = hamming_1024(bin_db + (size_t)i * 16, query_bin); uint64_t key = ((uint64_t)dist << 32) | (uint64_t)i; if (sz < cand) { heap[sz] = key; int c = sz++; while (c > 0) { int p = (c - 1) >> 1; if (heap[p] >= heap[c]) break; uint64_t tmp = heap[p]; heap[p] = heap[c]; heap[c] = tmp; c = p; } } else if (key < heap[0]) { heap[0] = key; heap_sift_down(heap, cand, 0); } } counts[tid] = sz; } size_t total = 0; for (int t = 0; t < max_threads; t++) total += (size_t)counts[t]; uint64_t *merged = (uint64_t *)malloc(total * sizeof(uint64_t)); size_t pos = 0; for (int t = 0; t < max_threads; t++) { if (counts[t] > 0) { memcpy(merged + pos, all_heaps + (size_t)t * (size_t)cand, (size_t)counts[t] * sizeof(uint64_t)); pos += (size_t)counts[t]; } } qsort(merged, total, sizeof(uint64_t), cmp_u64); int out_n = (total < (size_t)cand) ? (int)total : cand; for (int i = 0; i < out_n; i++) { out_ids[i] = (uint32_t)(merged[i] & 0xFFFFFFFFULL); out_dists[i] = (uint32_t)(merged[i] >> 32); } free(merged); free(all_heaps); free(counts); return out_n; } /* * Stage 2: float32 query dot-product rescore against int8 quantized candidates. */ void rescore_int8( const int8_t *i8_db, int dim, const float *query_f32, const uint32_t *cand_ids, int cand_n, float *out_scores ) { const float inv_scale = 1.0f / 127.0f; for (int c = 0; c < cand_n; c++) { const int8_t *row = i8_db + (size_t)cand_ids[c] * (size_t)dim; float acc = 0.0f; for (int d = 0; d < dim; d++) { acc += query_f32[d] * (float)row[d]; } out_scores[c] = acc * inv_scale; } }