faiss-next 0.6.0

Rust bindings for Faiss (Facebook AI Similarity Search)
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
use std::ptr;

use faiss_next_sys::{
    self, FaissCenteringTransform, FaissITQMatrix, FaissITQTransform, FaissLinearTransform,
    FaissNormalizationTransform, FaissOPQMatrix, FaissPCAMatrix, FaissRandomRotationMatrix,
    FaissRemapDimensionsTransform, FaissVectorTransform,
};

use crate::error::{check_return_code, Result};

pub trait VectorTransform {
    fn inner_ptr(&self) -> *mut FaissVectorTransform;

    fn is_trained(&self) -> bool {
        unsafe { faiss_next_sys::faiss_VectorTransform_is_trained(self.inner_ptr()) != 0 }
    }

    fn d_in(&self) -> u32 {
        unsafe { faiss_next_sys::faiss_VectorTransform_d_in(self.inner_ptr()) as u32 }
    }

    fn d_out(&self) -> u32 {
        unsafe { faiss_next_sys::faiss_VectorTransform_d_out(self.inner_ptr()) as u32 }
    }

    fn train(&mut self, n: usize, x: &[f32]) -> Result<()> {
        check_return_code(unsafe {
            faiss_next_sys::faiss_VectorTransform_train(self.inner_ptr(), n as i64, x.as_ptr())
        })
    }

    fn apply(&self, n: usize, x: &[f32]) -> Result<Vec<f32>> {
        let d_out = self.d_out() as usize;
        let mut xt = vec![0.0f32; n * d_out];
        unsafe {
            faiss_next_sys::faiss_VectorTransform_apply_noalloc(
                self.inner_ptr(),
                n as i64,
                x.as_ptr(),
                xt.as_mut_ptr(),
            )
        }
        Ok(xt)
    }

    fn apply_noalloc(&self, n: usize, x: &[f32], xt: &mut [f32]) {
        unsafe {
            faiss_next_sys::faiss_VectorTransform_apply_noalloc(
                self.inner_ptr(),
                n as i64,
                x.as_ptr(),
                xt.as_mut_ptr(),
            )
        }
    }

    fn reverse_transform(&self, n: usize, xt: &[f32], x: &mut [f32]) {
        unsafe {
            faiss_next_sys::faiss_VectorTransform_reverse_transform(
                self.inner_ptr(),
                n as i64,
                xt.as_ptr(),
                x.as_mut_ptr(),
            )
        }
    }
}

pub trait LinearTransform: VectorTransform {
    fn inner_linear_ptr(&self) -> *mut FaissLinearTransform;

    fn transform_transpose(&self, n: usize, y: &[f32], x: &mut [f32]) {
        unsafe {
            faiss_next_sys::faiss_LinearTransform_transform_transpose(
                self.inner_linear_ptr(),
                n as i64,
                y.as_ptr(),
                x.as_mut_ptr(),
            )
        }
    }

    fn set_is_orthonormal(&mut self) {
        unsafe { faiss_next_sys::faiss_LinearTransform_set_is_orthonormal(self.inner_linear_ptr()) }
    }

    fn have_bias(&self) -> bool {
        unsafe { faiss_next_sys::faiss_LinearTransform_have_bias(self.inner_linear_ptr()) != 0 }
    }

    fn is_orthonormal(&self) -> bool {
        unsafe {
            faiss_next_sys::faiss_LinearTransform_is_orthonormal(self.inner_linear_ptr()) != 0
        }
    }
}

pub struct PcaMatrix {
    ptr: *mut FaissPCAMatrix,
}

impl PcaMatrix {
    pub fn new(d_in: u32, d_out: u32, eigen_power: f32, random_rotation: bool) -> Result<Self> {
        unsafe {
            let mut ptr: *mut FaissPCAMatrix = ptr::null_mut();
            check_return_code(faiss_next_sys::faiss_PCAMatrix_new_with(
                &mut ptr,
                d_in as i32,
                d_out as i32,
                eigen_power,
                random_rotation as i32,
            ))?;
            Ok(Self { ptr })
        }
    }

    pub fn eigen_power(&self) -> f32 {
        unsafe { faiss_next_sys::faiss_PCAMatrix_eigen_power(self.ptr) }
    }

    pub fn random_rotation(&self) -> bool {
        unsafe { faiss_next_sys::faiss_PCAMatrix_random_rotation(self.ptr) != 0 }
    }
}

impl VectorTransform for PcaMatrix {
    fn inner_ptr(&self) -> *mut FaissVectorTransform {
        self.ptr as *mut FaissVectorTransform
    }
}

impl LinearTransform for PcaMatrix {
    fn inner_linear_ptr(&self) -> *mut FaissLinearTransform {
        self.ptr as *mut FaissLinearTransform
    }
}

impl Drop for PcaMatrix {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                faiss_next_sys::faiss_PCAMatrix_free(self.ptr);
            }
        }
    }
}

pub struct RandomRotationMatrix {
    ptr: *mut FaissRandomRotationMatrix,
}

impl RandomRotationMatrix {
    pub fn new(d_in: u32, d_out: u32) -> Result<Self> {
        unsafe {
            let mut ptr: *mut FaissRandomRotationMatrix = ptr::null_mut();
            check_return_code(faiss_next_sys::faiss_RandomRotationMatrix_new_with(
                &mut ptr,
                d_in as i32,
                d_out as i32,
            ))?;
            Ok(Self { ptr })
        }
    }
}

impl VectorTransform for RandomRotationMatrix {
    fn inner_ptr(&self) -> *mut FaissVectorTransform {
        self.ptr as *mut FaissVectorTransform
    }
}

impl LinearTransform for RandomRotationMatrix {
    fn inner_linear_ptr(&self) -> *mut FaissLinearTransform {
        self.ptr as *mut FaissLinearTransform
    }
}

impl Drop for RandomRotationMatrix {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                faiss_next_sys::faiss_RandomRotationMatrix_free(self.ptr);
            }
        }
    }
}

pub struct ItqMatrix {
    ptr: *mut FaissITQMatrix,
}

impl ItqMatrix {
    pub fn new(d: u32) -> Result<Self> {
        unsafe {
            let mut ptr: *mut FaissITQMatrix = ptr::null_mut();
            check_return_code(faiss_next_sys::faiss_ITQMatrix_new_with(&mut ptr, d as i32))?;
            Ok(Self { ptr })
        }
    }
}

impl VectorTransform for ItqMatrix {
    fn inner_ptr(&self) -> *mut FaissVectorTransform {
        self.ptr as *mut FaissVectorTransform
    }
}

impl LinearTransform for ItqMatrix {
    fn inner_linear_ptr(&self) -> *mut FaissLinearTransform {
        self.ptr as *mut FaissLinearTransform
    }
}

impl Drop for ItqMatrix {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                faiss_next_sys::faiss_ITQMatrix_free(self.ptr);
            }
        }
    }
}

pub struct ItqTransform {
    ptr: *mut FaissITQTransform,
}

impl ItqTransform {
    pub fn new(d_in: u32, d_out: u32, do_pca: bool) -> Result<Self> {
        unsafe {
            let mut ptr: *mut FaissITQTransform = ptr::null_mut();
            check_return_code(faiss_next_sys::faiss_ITQTransform_new_with(
                &mut ptr,
                d_in as i32,
                d_out as i32,
                do_pca as i32,
            ))?;
            Ok(Self { ptr })
        }
    }

    pub fn do_pca(&self) -> bool {
        unsafe { faiss_next_sys::faiss_ITQTransform_do_pca(self.ptr) != 0 }
    }
}

impl VectorTransform for ItqTransform {
    fn inner_ptr(&self) -> *mut FaissVectorTransform {
        self.ptr as *mut FaissVectorTransform
    }
}

impl Drop for ItqTransform {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                faiss_next_sys::faiss_ITQTransform_free(self.ptr);
            }
        }
    }
}

pub struct OpqMatrix {
    ptr: *mut FaissOPQMatrix,
}

impl OpqMatrix {
    pub fn new(d_in: u32, d_out: u32, m: u32) -> Result<Self> {
        unsafe {
            let mut ptr: *mut FaissOPQMatrix = ptr::null_mut();
            check_return_code(faiss_next_sys::faiss_OPQMatrix_new_with(
                &mut ptr,
                d_in as i32,
                d_out as i32,
                m as i32,
            ))?;
            Ok(Self { ptr })
        }
    }

    pub fn verbose(&self) -> bool {
        unsafe { faiss_next_sys::faiss_OPQMatrix_verbose(self.ptr) != 0 }
    }

    pub fn set_verbose(&mut self, verbose: bool) {
        unsafe { faiss_next_sys::faiss_OPQMatrix_set_verbose(self.ptr, verbose as i32) }
    }

    pub fn niter(&self) -> i32 {
        unsafe { faiss_next_sys::faiss_OPQMatrix_niter(self.ptr) }
    }

    pub fn set_niter(&mut self, niter: i32) {
        unsafe { faiss_next_sys::faiss_OPQMatrix_set_niter(self.ptr, niter) }
    }

    pub fn niter_pq(&self) -> i32 {
        unsafe { faiss_next_sys::faiss_OPQMatrix_niter_pq(self.ptr) }
    }

    pub fn set_niter_pq(&mut self, niter: i32) {
        unsafe { faiss_next_sys::faiss_OPQMatrix_set_niter_pq(self.ptr, niter) }
    }
}

impl VectorTransform for OpqMatrix {
    fn inner_ptr(&self) -> *mut FaissVectorTransform {
        self.ptr as *mut FaissVectorTransform
    }
}

impl LinearTransform for OpqMatrix {
    fn inner_linear_ptr(&self) -> *mut FaissLinearTransform {
        self.ptr as *mut FaissLinearTransform
    }
}

impl Drop for OpqMatrix {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                faiss_next_sys::faiss_OPQMatrix_free(self.ptr);
            }
        }
    }
}

pub struct NormalizationTransform {
    ptr: *mut FaissNormalizationTransform,
}

impl NormalizationTransform {
    pub fn new(d: u32, norm: f32) -> Result<Self> {
        unsafe {
            let mut ptr: *mut FaissNormalizationTransform = ptr::null_mut();
            check_return_code(faiss_next_sys::faiss_NormalizationTransform_new_with(
                &mut ptr, d as i32, norm,
            ))?;
            Ok(Self { ptr })
        }
    }

    pub fn norm(&self) -> f32 {
        unsafe { faiss_next_sys::faiss_NormalizationTransform_norm(self.ptr) }
    }
}

impl VectorTransform for NormalizationTransform {
    fn inner_ptr(&self) -> *mut FaissVectorTransform {
        self.ptr as *mut FaissVectorTransform
    }
}

impl Drop for NormalizationTransform {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                faiss_next_sys::faiss_NormalizationTransform_free(self.ptr);
            }
        }
    }
}

pub struct CenteringTransform {
    ptr: *mut FaissCenteringTransform,
}

impl CenteringTransform {
    pub fn new(d: u32) -> Result<Self> {
        unsafe {
            let mut ptr: *mut FaissCenteringTransform = ptr::null_mut();
            check_return_code(faiss_next_sys::faiss_CenteringTransform_new_with(
                &mut ptr, d as i32,
            ))?;
            Ok(Self { ptr })
        }
    }
}

impl VectorTransform for CenteringTransform {
    fn inner_ptr(&self) -> *mut FaissVectorTransform {
        self.ptr as *mut FaissVectorTransform
    }
}

impl Drop for CenteringTransform {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                faiss_next_sys::faiss_CenteringTransform_free(self.ptr);
            }
        }
    }
}

pub struct RemapDimensionsTransform {
    ptr: *mut FaissRemapDimensionsTransform,
}

impl RemapDimensionsTransform {
    pub fn new(d_in: u32, d_out: u32, uniform: bool) -> Result<Self> {
        unsafe {
            let mut ptr: *mut FaissRemapDimensionsTransform = ptr::null_mut();
            check_return_code(faiss_next_sys::faiss_RemapDimensionsTransform_new_with(
                &mut ptr,
                d_in as i32,
                d_out as i32,
                uniform as i32,
            ))?;
            Ok(Self { ptr })
        }
    }
}

impl VectorTransform for RemapDimensionsTransform {
    fn inner_ptr(&self) -> *mut FaissVectorTransform {
        self.ptr as *mut FaissVectorTransform
    }
}

impl Drop for RemapDimensionsTransform {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                faiss_next_sys::faiss_RemapDimensionsTransform_free(self.ptr);
            }
        }
    }
}