@@ -7,13 +7,9 @@ use nalgebra::{DMatrix, DVector, DVectorView};
7
7
use num_traits:: ToPrimitive ;
8
8
use serde:: { Deserialize , Serialize } ;
9
9
10
+ use crate :: consts:: { DEFAULT_X_DOT_PRODUCT , EPSILON , THETA_LOG_DIM , WINDOWS_SIZE } ;
10
11
use crate :: metrics:: METRICS ;
11
- use crate :: utils:: { gen_random_bias, gen_random_orthogonal, matrix_from_fvecs} ;
12
-
13
- const DEFAULT_X_DOT_PRODUCT : f32 = 0.8 ;
14
- const EPSILON : f32 = 1.9 ;
15
- const THETA_LOG_DIM : u32 = 4 ;
16
- const WINDOWS_SIZE : usize = 16 ;
12
+ use crate :: utils:: { gen_random_bias, gen_random_qr_orthogonal, matrix_from_fvecs} ;
17
13
18
14
/// Convert the vector to binary format and store in a u64 vector.
19
15
fn vector_binarize_u64 ( vec : & DVector < f32 > ) -> Vec < u64 > {
@@ -32,8 +28,25 @@ fn vector_binarize_one(vec: &DVector<f32>) -> DVector<f32> {
32
28
DVector :: from_fn ( vec. len ( ) , |i, _| if vec[ i] > 0.0 { 1.0 } else { -1.0 } )
33
29
}
34
30
31
+ /// Interface of `vector_binarize_query`
32
+ fn vector_binarize_query ( vec : & DVector < u8 > ) -> Vec < u64 > {
33
+ #[ cfg( any( target_arch = "x86_64" , target_arch = "x86" ) ) ]
34
+ {
35
+ if is_x86_feature_detected ! ( "avx2" ) {
36
+ unsafe { crate :: simd:: vector_binarize_query_avx2 ( & vec. as_view ( ) ) }
37
+ } else {
38
+ vector_binarize_query_raw ( vec)
39
+ }
40
+ }
41
+ #[ cfg( not( any( target_arch = "x86_64" , target_arch = "x86" ) ) ) ]
42
+ {
43
+ vector_binarize_query_raw ( vec)
44
+ }
45
+ }
46
+
35
47
/// Convert the vector to binary format (one value to multiple bits) and store in a u64 vector.
36
- fn query_vector_binarize ( vec : & DVector < u8 > ) -> Vec < u64 > {
48
+ #[ inline]
49
+ fn vector_binarize_query_raw ( vec : & DVector < u8 > ) -> Vec < u64 > {
37
50
let length = vec. len ( ) ;
38
51
let mut binary = vec ! [ 0u64 ; length * THETA_LOG_DIM as usize / 64 ] ;
39
52
for j in 0 ..THETA_LOG_DIM as usize {
@@ -78,7 +91,7 @@ fn kmeans_nearest_cluster(centroids: &DMatrix<f32>, vec: &DVectorView<f32>) -> u
78
91
#[ cfg( any( target_arch = "x86_64" , target_arch = "x86" ) ) ]
79
92
{
80
93
if is_x86_feature_detected ! ( "avx2" ) {
81
- unsafe { crate :: distance :: l2_squared_distance_avx2 ( & centroid, vec) }
94
+ unsafe { crate :: simd :: l2_squared_distance_avx2 ( & centroid, vec) }
82
95
} else {
83
96
vec. sub_to ( & centroid, & mut residual) ;
84
97
residual. norm_squared ( )
@@ -123,7 +136,7 @@ impl RaBitQ {
123
136
let centroids = matrix_from_fvecs ( centroid_path) ;
124
137
let k = centroids. shape ( ) . 0 ;
125
138
debug ! ( "n: {}, dim: {}, k: {}" , n, dim, k) ;
126
- let orthogonal = gen_random_orthogonal ( dim) ;
139
+ let orthogonal = gen_random_qr_orthogonal ( dim) ;
127
140
let rand_bias = gen_random_bias ( dim) ;
128
141
129
142
// projection
@@ -226,10 +239,7 @@ impl RaBitQ {
226
239
{
227
240
if is_x86_feature_detected ! ( "avx2" ) {
228
241
unsafe {
229
- crate :: distance:: l2_squared_distance_avx2 (
230
- & centroid,
231
- & y_projected. as_view ( ) ,
232
- )
242
+ crate :: simd:: l2_squared_distance_avx2 ( & centroid, & y_projected. as_view ( ) )
233
243
}
234
244
} else {
235
245
y_projected. sub_to ( & centroid, & mut residual) ;
@@ -257,7 +267,7 @@ impl RaBitQ {
257
267
let y_scaled = residual. add_scalar ( -lower_bound) * one_over_delta + & self . rand_bias ;
258
268
let y_quantized = y_scaled. map ( |v| v. to_u8 ( ) . expect ( "convert to u8 error" ) ) ;
259
269
let scalar_sum = y_quantized. iter ( ) . fold ( 0u32 , |acc, & v| acc + v as u32 ) ;
260
- let y_binary_vec = query_vector_binarize ( & y_quantized) ;
270
+ let y_binary_vec = vector_binarize_query ( & y_quantized) ;
261
271
let dist_sqrt = dist. sqrt ( ) ;
262
272
for j in self . offsets [ i] ..self . offsets [ i + 1 ] {
263
273
let ju = j as usize ;
@@ -300,7 +310,7 @@ impl RaBitQ {
300
310
{
301
311
if is_x86_feature_detected ! ( "avx2" ) {
302
312
unsafe {
303
- crate :: distance :: l2_squared_distance_avx2 (
313
+ crate :: simd :: l2_squared_distance_avx2 (
304
314
& self . base . column ( u as usize ) ,
305
315
& query. as_view ( ) ,
306
316
)
0 commit comments