faiss 0.13.0

High-level bindings for Faiss, the vector similarity search engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Interface and implementation to IVFFlat index type.

use super::*;

use std::os::raw::{c_char, c_int};

/// Alias for the native implementation of a flat index.
pub type IVFFlatIndex = IVFFlatIndexImpl;

/// Native implementation of a flat index.
#[derive(Debug)]
pub struct IVFFlatIndexImpl {
    inner: *mut FaissIndexIVFFlat,
}

unsafe impl Send for IVFFlatIndexImpl {}
unsafe impl Sync for IVFFlatIndexImpl {}

impl CpuIndex for IVFFlatIndexImpl {}

impl Drop for IVFFlatIndexImpl {
    fn drop(&mut self) {
        unsafe {
            faiss_IndexIVFFlat_free(self.inner);
        }
    }
}

impl IVFFlatIndexImpl {
    fn new_helper(
        quantizer: &flat::FlatIndex,
        d: u32,
        nlist: u32,
        metric: MetricType,
        own_fields: bool,
    ) -> Result<Self> {
        unsafe {
            let metric = metric as c_uint;
            let mut inner = ptr::null_mut();
            faiss_try(faiss_IndexIVFFlat_new_with_metric(
                &mut inner,
                quantizer.inner_ptr(),
                d as usize,
                nlist as usize,
                metric,
            ))?;
            faiss_IndexIVFFlat_set_own_fields(inner, c_int::from(own_fields));
            Ok(IVFFlatIndexImpl { inner })
        }
    }

    /// Create a new IVF flat index.
    // The index owns the quantizer.
    pub fn new(quantizer: flat::FlatIndex, d: u32, nlist: u32, metric: MetricType) -> Result<Self> {
        let index = IVFFlatIndexImpl::new_helper(&quantizer, d, nlist, metric, true)?;
        std::mem::forget(quantizer);

        Ok(index)
    }

    /// Create a new IVF flat index with L2 as the metric type.
    // The index owns the quantizer.
    pub fn new_l2(quantizer: flat::FlatIndex, d: u32, nlist: u32) -> Result<Self> {
        IVFFlatIndexImpl::new(quantizer, d, nlist, MetricType::L2)
    }

    /// Create a new IVF flat index with IP (inner product) as the metric type.
    // The index owns the quantizer.
    pub fn new_ip(quantizer: flat::FlatIndex, d: u32, nlist: u32) -> Result<Self> {
        IVFFlatIndexImpl::new(quantizer, d, nlist, MetricType::InnerProduct)
    }

    /// Get number of probes at query time
    pub fn nprobe(&self) -> u32 {
        unsafe { faiss_IndexIVFFlat_nprobe(self.inner_ptr()) as u32 }
    }

    /// Set number of probes at query time
    pub fn set_nprobe(&mut self, value: u32) {
        unsafe {
            faiss_IndexIVFFlat_set_nprobe(self.inner_ptr(), value as usize);
        }
    }

    /// Get number of possible key values
    pub fn nlist(&self) -> u32 {
        unsafe { faiss_IndexIVFFlat_nlist(self.inner_ptr()) as u32 }
    }

    /// Get train type
    pub fn train_type(&self) -> Option<TrainType> {
        unsafe {
            let code = faiss_IndexIVFFlat_quantizer_trains_alone(self.inner_ptr());
            TrainType::from_code(code)
        }
    }
}

/**
 * = 0: use the quantizer as index in a kmeans training
 * = 1: just pass on the training set to the train() of the quantizer
 * = 2: kmeans training on a flat index + add the centroids to the quantizer
 */
#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
pub enum TrainType {
    /// use the quantizer as index in a kmeans training
    QuantizerAsIndex,
    /// just pass on the training set to the train() of the quantizer
    QuantizerTrainsAlone,
    /// kmeans training on a flat index + add the centroids to the quantizer
    FlatIndexAndQuantizer,
}

impl TrainType {
    pub(crate) fn from_code(code: c_char) -> Option<Self> {
        match code {
            0 => Some(TrainType::QuantizerAsIndex),
            1 => Some(TrainType::QuantizerTrainsAlone),
            2 => Some(TrainType::FlatIndexAndQuantizer),
            _ => None,
        }
    }
}

impl NativeIndex for IVFFlatIndexImpl {
    type Inner = FaissIndex;
    fn inner_ptr(&self) -> *mut FaissIndex {
        self.inner
    }
}

impl FromInnerPtr for IVFFlatIndexImpl {
    unsafe fn from_inner_ptr(inner_ptr: *mut FaissIndex) -> Self {
        IVFFlatIndexImpl {
            inner: inner_ptr as *mut FaissIndexIVFFlat,
        }
    }
}

impl_native_index!(IVFFlatIndex);

impl TryClone for IVFFlatIndexImpl {
    fn try_clone(&self) -> Result<Self>
    where
        Self: Sized,
    {
        try_clone_from_inner_ptr(self)
    }
}

impl_concurrent_index!(IVFFlatIndexImpl);

impl IndexImpl {
    /// Attempt a dynamic cast of an index to the IVF flat index type.
    pub fn into_ivf_flat(self) -> Result<IVFFlatIndexImpl> {
        unsafe {
            let new_inner = faiss_IndexIVFFlat_cast(self.inner_ptr());
            if new_inner.is_null() {
                Err(Error::BadCast)
            } else {
                mem::forget(self);
                Ok(IVFFlatIndexImpl { inner: new_inner })
            }
        }
    }
}


mod binary {
    use super::*;
    use crate::index::BinaryIndexImpl;

    /// Alias for the native implementation of a binary IVF index.
    pub type BinaryIVFIndex = BinaryIVFIndexImpl;

    /// Native implementation of a binary IVF index.
    #[derive(Debug)]
    pub struct BinaryIVFIndexImpl {
        inner: *mut FaissIndexBinaryIVF,
    }

    unsafe impl Send for BinaryIVFIndexImpl {}
    unsafe impl Sync for BinaryIVFIndexImpl {}

    impl CpuIndex<u8, i32> for BinaryIVFIndexImpl {}

    impl Drop for BinaryIVFIndexImpl {
        fn drop(&mut self) {
            unsafe {
                faiss_IndexBinaryIVF_free(self.inner);
            }
        }
    }

    impl NativeIndex<u8, i32> for BinaryIVFIndexImpl {
        type Inner = FaissIndexBinary;
        fn inner_ptr(&self) -> *mut FaissIndexBinary {
            self.inner
        }
    }

    impl FromInnerPtr<u8, i32> for BinaryIVFIndexImpl {
        unsafe fn from_inner_ptr(inner_ptr: *mut FaissIndexBinary) -> Self {
            BinaryIVFIndexImpl {
                inner: inner_ptr as *mut FaissIndexBinaryIVF,
            }
        }
    }

    impl BinaryIVFIndexImpl {

        /// Get number of probes at query time
        pub fn nprobe(&self) -> u32 {
            unsafe { faiss_IndexBinaryIVF_nprobe(self.inner_ptr()) as u32 }
        }

        /// Set number of probes at query time
        pub fn set_nprobe(&mut self, value: u32) {
            unsafe {
                faiss_IndexBinaryIVF_set_nprobe(self.inner_ptr(), value as usize);
            }
        }

        /// Get number of possible key values
        pub fn nlist(&self) -> u32 {
            unsafe { faiss_IndexBinaryIVF_nlist(self.inner_ptr()) as u32 }
        }

        /// Get max number of codes to visit to do a query
        pub fn max_codes(&self) -> u32 {
            unsafe { faiss_IndexBinaryIVF_max_codes(self.inner_ptr()) as u32 }
        }

        /// Set max number of codes to visit to do a query
        pub fn set_max_codes(&self, value: u32) {
            unsafe {
                faiss_IndexBinaryIVF_set_max_codes(self.inner_ptr(), value as usize);
            }
        }

        /// Check the inverted lists' imbalance factor.
        /// 
        /// 1 = perfectly balanced, > 1: imbalanced
        pub fn imbalance_factor(&self) -> f64 {
            unsafe { faiss_IndexBinaryIVF_imbalance_factor(self.inner_ptr()) }
        }
    }

    impl_native_index_binary!(BinaryIVFIndexImpl);

    impl TryClone for BinaryIVFIndexImpl {
        fn try_clone(&self) -> Result<Self>
        where
            Self: Sized,
        {
            try_clone_binary_from_inner_ptr(self)
        }
    }

    impl_concurrent_index_binary!(BinaryIVFIndexImpl);

    impl BinaryIndexImpl {
        /// 
        /// Attempt a dynamic cast of a binary index to the Binary IVF index type.
        pub fn into_binary_ivf(self) -> Result<BinaryIVFIndexImpl> {
            unsafe {
                let new_inner = faiss_IndexBinaryIVF_cast(self.inner_ptr());
                if new_inner.is_null() {
                    Err(Error::BadCast)
                } else {
                    mem::forget(self);
                    Ok(BinaryIVFIndexImpl { inner: new_inner })
                }
            }
        }
    }

}
pub use binary::*;


#[cfg(test)]
mod tests {

    use super::IVFFlatIndexImpl;
    use crate::index::flat::FlatIndexImpl;
    use crate::index::ivf_flat::BinaryIVFIndexImpl;
    use crate::index::{index_factory, ConcurrentIndex, Idx, Index, SearchWithParams, UpcastIndex};
    use crate::search_params::SearchParametersIVFImpl;
    use crate::selector::IdSelector;
    use crate::{index_binary_factory, MetricType};

    const D: u32 = 8;

    #[test]
    // #[ignore]
    fn index_search() {
        let q = FlatIndexImpl::new_l2(D).unwrap();
        let mut index = IVFFlatIndexImpl::new_l2(q, D, 1).unwrap();
        assert_eq!(index.d(), D);
        assert_eq!(index.ntotal(), 0);
        let some_data = &[
            7.5_f32, -7.5, 7.5, -7.5, 7.5, 7.5, 7.5, 7.5, -1., 1., 1., 1., 1., 1., 1., -1., 4.,
            -4., -8., 1., 1., 2., 4., -1., 8., 8., 10., -10., -10., 10., -10., 10., 16., 16., 32.,
            25., 20., 20., 40., 15.,
        ];
        index.train(some_data).unwrap();
        index.add(some_data).unwrap();
        assert_eq!(index.ntotal(), 5);

        let my_query = [0.; D as usize];
        let result = index.search(&my_query, 3).unwrap();
        assert_eq!(result.labels.len(), 3);
        assert!(result.labels.into_iter().all(Idx::is_some));
        assert_eq!(result.distances.len(), 3);
        assert!(result.distances.iter().all(|x| *x > 0.));

        let my_query = [100.; D as usize];
        // flat index can be used behind an immutable ref
        let result = (&index).search(&my_query, 3).unwrap();
        assert_eq!(result.labels.len(), 3);
        assert!(result.labels.into_iter().all(Idx::is_some));
        assert_eq!(result.distances.len(), 3);
        assert!(result.distances.iter().all(|x| *x > 0.));

        index.reset().unwrap();
        assert_eq!(index.ntotal(), 0);
    }

    #[test]
    fn index_search_with_params_ivf() {
        let q = FlatIndexImpl::new_l2(D).unwrap();
        let mut index = IVFFlatIndexImpl::new_l2(q, D, 1).unwrap();
        assert_eq!(index.d(), D);
        assert_eq!(index.ntotal(), 0);
        let some_data = &[
            7.5_f32, -7.5, 7.5, -7.5, 7.5, 7.5, 7.5, 7.5, -1., 1., 1., 1., 1., 1., 1., -1., 4.,
            -4., -8., 1., 1., 2., 4., -1., 8., 8., 10., -10., -10., 10., -10., 10., 16., 16., 32.,
            25., 20., 20., 40., 15.,
        ];
        index.train(some_data).unwrap();
        index.add(some_data).unwrap();
        assert_eq!(index.ntotal(), 5);

        let my_query = [0.; D as usize];

        // set the selector to mask out the entire dataset 
        // so any search would result in an empty queryset even though there 
        // exists other vectors in the dataset.
        let selector = IdSelector::range(Idx::new(6), Idx::new(10)).unwrap();
        let params = SearchParametersIVFImpl::new_with(selector, 10, 100).unwrap();
        let hits = index.search_with_params(&my_query, 5, &params.upcast()).unwrap();
        assert!(hits.labels.into_iter().all(Idx::is_none));

        // now filter the search to include only vectors with id in [0, 2).
        let selector = IdSelector::range(Idx::new(0), Idx::new(2)).unwrap();
        let params = SearchParametersIVFImpl::new_with(selector, 10, 1000).unwrap();
        // we're asking for 5 neighbors but 3 of them have been masked out
        // so we expect to get only 2 hits (i.e. ids 0 and 1).
        let hits = index.search_with_params(&my_query, 5, &params.upcast()).unwrap();

        let mut observed_labels = 
            hits.labels
            .into_iter()
            .filter_map(|v| v.get())
            .collect::<Vec<_>>();

        observed_labels.sort();

        assert_eq!(
            observed_labels,
            vec![0, 1]
        );

    }

    #[test]
    fn index_search_own() {
        let q = FlatIndexImpl::new_l2(D).unwrap();
        let mut index = IVFFlatIndexImpl::new_l2(q, D, 1).unwrap();
        assert_eq!(index.d(), D);
        assert_eq!(index.ntotal(), 0);
        let some_data = &[
            7.5_f32, -7.5, 7.5, -7.5, 7.5, 7.5, 7.5, 7.5, -1., 1., 1., 1., 1., 1., 1., -1., 4.,
            -4., -8., 1., 1., 2., 4., -1., 8., 8., 10., -10., -10., 10., -10., 10., 16., 16., 32.,
            25., 20., 20., 40., 15.,
        ];
        index.train(some_data).unwrap();
        index.add(some_data).unwrap();
        assert_eq!(index.ntotal(), 5);

        let my_query = [0.; D as usize];
        let result = index.search(&my_query, 3).unwrap();
        assert_eq!(result.labels.len(), 3);
        assert!(result.labels.into_iter().all(Idx::is_some));
        assert_eq!(result.distances.len(), 3);
        assert!(result.distances.iter().all(|x| *x > 0.));

        let my_query = [100.; D as usize];
        // flat index can be used behind an immutable ref
        let result = (&index).search(&my_query, 3).unwrap();
        assert_eq!(result.labels.len(), 3);
        assert!(result.labels.into_iter().all(Idx::is_some));
        assert_eq!(result.distances.len(), 3);
        assert!(result.distances.iter().all(|x| *x > 0.));

        index.reset().unwrap();
        assert_eq!(index.ntotal(), 0);
    }

    #[test]
    fn index_assign() {
        let q = FlatIndexImpl::new_l2(D).unwrap();
        let mut index = IVFFlatIndexImpl::new_l2(q, D, 1).unwrap();
        assert_eq!(index.d(), D);
        assert_eq!(index.ntotal(), 0);
        let some_data = &[
            7.5_f32, -7.5, 7.5, -7.5, 7.5, 7.5, 7.5, 7.5, -1., 1., 1., 1., 1., 1., 1., -1., 4.,
            -4., -8., 1., 1., 2., 4., -1., 8., 8., 10., -10., -10., 10., -10., 10., 16., 16., 32.,
            25., 20., 20., 40., 15.,
        ];
        index.train(some_data).unwrap();
        index.add(some_data).unwrap();
        assert_eq!(index.ntotal(), 5);

        let my_query = [0.; D as usize];
        let result = index.assign(&my_query, 3).unwrap();
        assert_eq!(result.labels.len(), 3);
        assert!(result.labels.into_iter().all(Idx::is_some));

        let my_query = [100.; D as usize];
        // flat index can be used behind an immutable ref
        let result = (&index).assign(&my_query, 3).unwrap();
        assert_eq!(result.labels.len(), 3);
        assert!(result.labels.into_iter().all(Idx::is_some));

        index.reset().unwrap();
        assert_eq!(index.ntotal(), 0);
    }

    #[test]
    fn ivf_flat_index_from_cast() {
        let mut index = index_factory(8, "IVF1,Flat", MetricType::L2).unwrap();
        let some_data = &[
            7.5_f32, -7.5, 7.5, -7.5, 7.5, 7.5, 7.5, 7.5, -1., 1., 1., 1., 1., 1., 1., -1., 0., 0.,
            0., 1., 1., 0., 0., -1., 100., 100., 100., 100., -100., 100., 100., 100., 120., 100.,
            100., 105., -100., 100., 100., 105.,
        ];
        index.train(some_data).unwrap();
        index.add(some_data).unwrap();
        assert_eq!(index.ntotal(), 5);

        let index: IVFFlatIndexImpl = index.into_ivf_flat().unwrap();
        assert_eq!(index.is_trained(), true);
        assert_eq!(index.ntotal(), 5);
    }

    #[test]
    fn index_upcast() {
        let q = FlatIndexImpl::new_l2(D).unwrap();
        let index = IVFFlatIndexImpl::new_l2(q, D, 1).unwrap();
        assert_eq!(index.d(), D);
        assert_eq!(index.ntotal(), 0);

        let index_impl = index.upcast();
        assert_eq!(index_impl.d(), D);
    }

    #[test]
    fn binary_ivf_index_from_cast() {
        let mut index = index_binary_factory(16, "BIVF2").unwrap();
        let some_data = &[
            255u8,127,
            1,1,
            2,2,
            10,10
        ];
        index.train(some_data).unwrap();
        index.add(some_data).unwrap();
        assert_eq!(index.ntotal(), 4);

        let mut index: BinaryIVFIndexImpl = index.into_binary_ivf().unwrap();
        assert_eq!(index.is_trained(), true);
        assert_eq!(index.ntotal(), 4);
        index.set_nprobe(3);
        assert_eq!(index.nprobe(), 3);
        index.set_max_codes(1);
        assert_eq!(index.max_codes(), 1);

        let hits = index.search(&[1, 1], 1).unwrap();
        assert_eq!(hits.distances.len(), 1);
        assert_eq!(hits.distances[0], 0);
        assert_eq!(hits.labels.len(), 1);
        assert_eq!(hits.labels[0].get().unwrap(), 1);
    }

    #[test]
    fn binary_ivf_range_search() {
        let mut index = index_binary_factory(16, "BIVF2").unwrap();
        let some_data = &[
            255u8,127,
            1,1,
            2,2,
            10,10
        ];
        index.train(some_data).unwrap();
        index.add(some_data).unwrap();
        assert_eq!(index.ntotal(), 4);

        let mut index: BinaryIVFIndexImpl = index.into_binary_ivf().unwrap();
        assert_eq!(index.is_trained(), true);
        assert_eq!(index.ntotal(), 4);
        index.set_nprobe(3);
        assert_eq!(index.nprobe(), 3);
        index.set_max_codes(1);
        assert_eq!(index.max_codes(), 1);

        let hits = index.range_search(&[1, 1], 5).unwrap();

        let (distances, labels) = hits.distance_and_labels();
        assert_eq!(distances.len(), 2);

        for (distance, label) in distances.iter().zip(labels) {
            assert!(*distance < 5.);
            assert!(label.is_some());
        }
    }
}