Skip to content

Commit c47da8a

Browse files
xolottmdouze
authored andcommitted
[C_API] Pass compilation in current state (facebookresearch#989)
* Pass compilation in current state * Fix formatting and add missing parts * Define DistanceComputer * Add documentation to name changed on DistanceComputer::operator()() * Apply suggestions from code review Changes in docs Co-Authored-By: Eduardo Pinho <[email protected]> * Updated MetricType
1 parent 4c32fa4 commit c47da8a

14 files changed

Lines changed: 212 additions & 59 deletions

c_api/AutoTune_c.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ using faiss::Index;
1717
using faiss::ParameterRange;
1818
using faiss::ParameterSpace;
1919

20-
/** Build and index with the sequence of processing steps described in
21-
* the string.
22-
*/
23-
int faiss_index_factory(FaissIndex** p_index, int d, const char* description, FaissMetricType metric) {
24-
try {
25-
*p_index = reinterpret_cast<FaissIndex*>(faiss::index_factory(
26-
d, description, static_cast<faiss::MetricType>(metric)));
27-
} CATCH_AND_HANDLE
28-
}
29-
3020
const char* faiss_ParameterRange_name(const FaissParameterRange* range) {
3121
return reinterpret_cast<const ParameterRange*>(range)->name.c_str();
3222
}
@@ -90,4 +80,4 @@ int faiss_ParameterSpace_add_range(FaissParameterSpace* space, const char* name,
9080
*p_range = reinterpret_cast<FaissParameterRange*>(&range);
9181
}
9282
} CATCH_AND_HANDLE
93-
}
83+
}

c_api/AutoTune_c.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818
extern "C" {
1919
#endif
2020

21-
/** Build and index with the sequence of processing steps described in
22-
* the string.
23-
*/
24-
int faiss_index_factory(FaissIndex** p_index, int d, const char* description, FaissMetricType metric);
25-
2621
/// possible values of a parameter, sorted from least to most expensive/accurate
2722
FAISS_DECLARE_CLASS(ParameterRange)
2823

@@ -66,4 +61,4 @@ int faiss_ParameterSpace_add_range(FaissParameterSpace*, const char*, FaissParam
6661
}
6762
#endif
6863

69-
#endif
64+
#endif

c_api/Index_c.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,9 @@ int faiss_Index_compute_residual(const FaissIndex* index, const float* x, float*
9797
} CATCH_AND_HANDLE
9898
}
9999

100-
int faiss_Index_display(const FaissIndex* index) {
100+
int faiss_Index_compute_residual_n(const FaissIndex* index, idx_t n, const float* x, float* residuals, const idx_t* keys) {
101101
try {
102-
reinterpret_cast<const faiss::Index*>(index)->display();
102+
reinterpret_cast<const faiss::Index *>(index)->compute_residual_n(n, x, residuals, keys);
103103
} CATCH_AND_HANDLE
104104
}
105-
106-
}
105+
}

c_api/Index_c.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@ typedef struct FaissIDSelector_H FaissIDSelector;
2626

2727
/// Some algorithms support both an inner product version and a L2 search version.
2828
typedef enum FaissMetricType {
29-
METRIC_INNER_PRODUCT = 0,
30-
METRIC_L2 = 1,
29+
METRIC_INNER_PRODUCT = 0, ///< maximum inner product search
30+
METRIC_L2 = 1, ///< squared L2 search
31+
METRIC_L1, ///< L1 (aka cityblock)
32+
METRIC_Linf, ///< infinity distance
33+
METRIC_Lp, ///< L_p distance, p is given by metric_arg
34+
35+
/// some additional metrics defined in scipy.spatial.distance
36+
METRIC_Canberra = 20,
37+
METRIC_BrayCurtis,
38+
METRIC_JensenShannon,
3139
} FaissMetricType;
3240

3341
/// Opaque type for referencing to an index object
@@ -152,13 +160,24 @@ int faiss_Index_reconstruct_n (const FaissIndex* index, idx_t i0, idx_t ni, floa
152160
*/
153161
int faiss_Index_compute_residual(const FaissIndex* index, const float* x, float* residual, idx_t key);
154162

155-
/** Display the actual class name and some more info
163+
/** Computes a residual vector after indexing encoding.
164+
*
165+
* The residual vector is the difference between a vector and the
166+
* reconstruction that can be decoded from its representation in
167+
* the index. The residual can be used for multiple-stage indexing
168+
* methods, like IndexIVF's methods.
169+
*
156170
* @param index opaque pointer to index object
171+
* @param n number of vectors
172+
* @param x input vector, size (n x d)
173+
* @param residuals output residual vectors, size (n x d)
174+
* @param keys encoded index, as returned by search and assign
157175
*/
158-
int faiss_Index_display(const FaissIndex* index);
176+
int faiss_Index_compute_residual_n(const FaissIndex* index, idx_t n, const float* x, float* residuals, const idx_t* keys);
177+
159178

160179
#ifdef __cplusplus
161180
}
162181
#endif
163182

164-
#endif
183+
#endif

c_api/Makefile

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ DEBUGFLAG=-DNDEBUG # no debugging
1313
LIBNAME=libfaiss
1414
CLIBNAME=libfaiss_c
1515
LIBCOBJ=error_impl.o Index_c.o IndexFlat_c.o Clustering_c.o AutoTune_c.o \
16-
AuxIndexStructures_c.o IndexIVF_c.o IndexIVFFlat_c.o IndexLSH_c.o \
17-
index_io_c.o MetaIndexes_c.o IndexShards_c.o
16+
impl/AuxIndexStructures_c.o IndexIVF_c.o IndexIVFFlat_c.o IndexLSH_c.o \
17+
index_io_c.o MetaIndexes_c.o IndexShards_c.o index_factory_c.o \
18+
clone_index_c.o
1819
CFLAGS=-fPIC -m64 -Wno-sign-compare -g -O3 -Wall -Wextra
1920

2021
# Build static and shared object files by default
@@ -42,38 +43,44 @@ clean:
4243

4344
# Dependencies
4445

45-
error_impl.o: CXXFLAGS += -I.. $(DEBUGFLAG)
46+
error_impl.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
4647
error_impl.o: error_impl.cpp error_c.h error_impl.h macros_impl.h
4748

48-
index_io_c.o: CXXFLAGS += -I.. $(DEBUGFLAG)
49+
index_io_c.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
4950
index_io_c.o: index_io_c.cpp error_impl.cpp ../index_io.h macros_impl.h
5051

51-
Index_c.o: CXXFLAGS += -I.. $(DEBUGFLAG)
52+
index_factory_c.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
53+
index_factory_c.o: index_factory_c.cpp error_impl.cpp ../index_io.h macros_impl.h
54+
55+
clone_index_c.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
56+
clone_index_c.o: index_factory_c.cpp error_impl.cpp ../index_io.h macros_impl.h
57+
58+
Index_c.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
5259
Index_c.o: Index_c.cpp Index_c.h ../Index.h macros_impl.h
5360

54-
IndexFlat_c.o: CXXFLAGS += -I.. $(DEBUGFLAG)
61+
IndexFlat_c.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
5562
IndexFlat_c.o: IndexFlat_c.cpp IndexFlat_c.h ../IndexFlat.h macros_impl.h
5663

57-
IndexIVF_c.o: CXXFLAGS += -I.. $(DEBUGFLAG)
64+
IndexIVF_c.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
5865
IndexIVF_c.o: IndexIVF_c.cpp IndexIVF_c.h ../IndexIVF.h macros_impl.h
5966

60-
IndexIVFFlat_c.o: CXXFLAGS += -I.. $(DEBUGFLAG)
67+
IndexIVFFlat_c.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
6168
IndexIVFFlat_c.o: IndexIVFFlat_c.cpp IndexIVFFlat_c.h ../IndexIVFFlat.h macros_impl.h
6269

63-
IndexLSH_c.o: CXXFLAGS += -I.. $(DEBUGFLAG)
70+
IndexLSH_c.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
6471
IndexLSH_c.o: IndexLSH_c.cpp IndexLSH_c.h ../IndexLSH.h macros_impl.h
6572

66-
IndexShards_c.o: CXXFLAGS += -I.. $(DEBUGFLAG)
73+
IndexShards_c.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
6774
IndexShards_c.o: IndexShards_c.cpp IndexShards_c.h ../Index.h ../IndexShards.h macros_impl.h
6875

69-
Clustering_c.o: CXXFLAGS += -I.. $(DEBUGFLAG)
76+
Clustering_c.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
7077
Clustering_c.o: Clustering_c.cpp Clustering_c.h ../Clustering.h macros_impl.h
7178

72-
AutoTune_c.o: CXXFLAGS += -I.. $(DEBUGFLAG)
79+
AutoTune_c.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
7380
AutoTune_c.o: AutoTune_c.cpp AutoTune_c.h ../AutoTune.h macros_impl.h
7481

75-
AuxIndexStructures_c.o: CXXFLAGS += -I.. $(DEBUGFLAG)
76-
AuxIndexStructures_c.o: AuxIndexStructures_c.cpp AuxIndexStructures_c.h ../AuxIndexStructures.h macros_impl.h
82+
impl/AuxIndexStructures_c.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
83+
impl/AuxIndexStructures_c.o: impl/AuxIndexStructures_c.cpp impl/AuxIndexStructures_c.h ../impl/AuxIndexStructures.h macros_impl.h
7784

78-
MetaIndexes_c.o: CXXFLAGS += -I.. $(DEBUGFLAG)
85+
MetaIndexes_c.o: CXXFLAGS += -I.. -I ../impl $(DEBUGFLAG)
7986
MetaIndexes_c.o: MetaIndexes_c.cpp MetaIndexes_c.h ../MetaIndexes.h macros_impl.h

c_api/clone_index_c.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// Copyright 2004-present Facebook. All Rights Reserved
9+
// -*- c++ -*-
10+
// I/O code for indexes
11+
12+
#include "clone_index_c.h"
13+
#include "clone_index.h"
14+
#include "macros_impl.h"
15+
16+
using faiss::Index;
17+
18+
int faiss_clone_index (const FaissIndex *idx, FaissIndex **p_out) {
19+
try {
20+
auto out = faiss::clone_index(reinterpret_cast<const Index*>(idx));
21+
*p_out = reinterpret_cast<FaissIndex*>(out);
22+
} CATCH_AND_HANDLE
23+
}

c_api/clone_index_c.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// Copyright 2004-present Facebook. All Rights Reserved
9+
// -*- c++ -*-
10+
// I/O code for indexes
11+
12+
13+
#ifndef FAISS_CLONE_INDEX_C_H
14+
#define FAISS_CLONE_INDEX_C_H
15+
16+
#include <stdio.h>
17+
#include "faiss_c.h"
18+
#include "Index_c.h"
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
/* cloning functions */
25+
26+
/** Clone an index. This is equivalent to `faiss::clone_index` */
27+
int faiss_clone_index (const FaissIndex *, FaissIndex ** p_out);
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif
32+
#endif

c_api/example_c.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Index_c.h"
1818
#include "IndexFlat_c.h"
1919
#include "AutoTune_c.h"
20+
#include "clone_index_c.h"
2021

2122
#define FAISS_TRY(C) \
2223
{ \
Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
// -*- c++ -*-
1010

1111
#include "AuxIndexStructures_c.h"
12-
#include "AuxIndexStructures.h"
13-
#include "macros_impl.h"
12+
#include "../../impl/AuxIndexStructures.h"
13+
#include "../macros_impl.h"
1414
#include <iostream>
1515

1616
using faiss::BufferList;
@@ -20,6 +20,7 @@ using faiss::IDSelectorRange;
2020
using faiss::RangeSearchResult;
2121
using faiss::RangeSearchPartialResult;
2222
using faiss::RangeQueryResult;
23+
using faiss::DistanceComputer;
2324

2425
DEFINE_GETTER(RangeSearchResult, size_t, nq)
2526

@@ -191,3 +192,29 @@ int faiss_RangeSearchPartialResult_new_result(
191192
return 0;
192193
} CATCH_AND_HANDLE
193194
}
195+
196+
DEFINE_DESTRUCTOR(DistanceComputer)
197+
198+
int faiss_DistanceComputer_set_query(FaissDistanceComputer *dc, const float *x) {
199+
try {
200+
reinterpret_cast<DistanceComputer*>(dc)->set_query(x);
201+
return 0;
202+
}
203+
CATCH_AND_HANDLE
204+
}
205+
206+
int faiss_DistanceComputer_vector_to_query_dis(FaissDistanceComputer *dc, idx_t i, float *qd) {
207+
try {
208+
*qd = reinterpret_cast<DistanceComputer*>(dc)->operator()(i);
209+
return 0;
210+
}
211+
CATCH_AND_HANDLE
212+
}
213+
214+
int faiss_DistanceComputer_symmetric_dis(FaissDistanceComputer *dc, idx_t i, idx_t j, float *vd) {
215+
try {
216+
*vd = reinterpret_cast<DistanceComputer*>(dc)->symmetric_dis(i, j);
217+
return 0;
218+
}
219+
CATCH_AND_HANDLE
220+
}
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#ifndef FAISS_AUX_INDEX_STRUCTURES_C_H
1212
#define FAISS_AUX_INDEX_STRUCTURES_C_H
1313

14-
#include "Index_c.h"
15-
#include "faiss_c.h"
14+
#include "../Index_c.h"
15+
#include "../faiss_c.h"
1616

1717
#ifdef __cplusplus
1818
extern "C" {
@@ -126,6 +126,22 @@ int faiss_RangeSearchPartialResult_set_lims(
126126
int faiss_RangeSearchPartialResult_new_result(
127127
FaissRangeSearchPartialResult* res, idx_t qno, FaissRangeQueryResult** qr);
128128

129+
130+
FAISS_DECLARE_CLASS(DistanceComputer)
131+
/// called before computing distances
132+
int faiss_DistanceComputer_set_query(FaissDistanceComputer *dc, const float *x);
133+
134+
/**
135+
* Compute distance of vector i to current query.
136+
* This function corresponds to the function call operator: DistanceComputer::operator()
137+
*/
138+
int faiss_DistanceComputer_vector_to_query_dis( FaissDistanceComputer *dc, idx_t i, float *qd);
139+
/// compute distance between two stored vectors
140+
int faiss_DistanceComputer_symmetric_dis(FaissDistanceComputer *dc, idx_t i, idx_t j, float *vd);
141+
142+
FAISS_DECLARE_DESTRUCTOR(DistanceComputer)
143+
144+
129145
#ifdef __cplusplus
130146
}
131147
#endif

0 commit comments

Comments
 (0)