Skip to content

Commit bf46c0f

Browse files
authored
Merge pull request #1 from yuhaijun999/wip-add-faiss-hook-dingodb
Added InnerProduct and L2Sqr hook interfaces.
2 parents ab2b7f5 + 532cf61 commit bf46c0f

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed

faiss/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ set(FAISS_SRC
5151
VectorTransform.cpp
5252
clone_index.cpp
5353
index_factory.cpp
54+
FaissHook.cpp
5455
impl/AuxIndexStructures.cpp
5556
impl/CodePacker.cpp
5657
impl/IDSelector.cpp
@@ -145,6 +146,7 @@ set(FAISS_HEADERS
145146
clone_index.h
146147
index_factory.h
147148
index_io.h
149+
FaissHook.h
148150
impl/AdditiveQuantizer.h
149151
impl/AuxIndexStructures.h
150152
impl/CodePacker.h

faiss/FaissHook.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
// -*- c++ -*-
9+
10+
#include "FaissHook.h"
11+
12+
namespace faiss {
13+
14+
extern float fvec_L2sqr_default(const float* x, const float* y, size_t d);
15+
16+
extern float fvec_inner_product_default(
17+
const float* x,
18+
const float* y,
19+
size_t d);
20+
21+
FVEC_L2SQR_HOOK fvec_L2sqr_hook = fvec_L2sqr_default;
22+
FVEC_INNER_PRODUCT_HOOK fvec_inner_product_hook = fvec_inner_product_default;
23+
24+
void set_fvec_L2sqr_hook(FVEC_L2SQR_HOOK_C hook) {
25+
if (nullptr != hook)
26+
fvec_L2sqr_hook = hook;
27+
}
28+
FVEC_L2SQR_HOOK_C get_fvec_L2sqr_hook() {
29+
return fvec_L2sqr_hook;
30+
}
31+
32+
void set_fvec_inner_product_hook(FVEC_INNER_PRODUCT_HOOK_C hook) {
33+
if (nullptr != hook)
34+
fvec_inner_product_hook = hook;
35+
}
36+
FVEC_INNER_PRODUCT_HOOK_C get_fvec_inner_product_hook() {
37+
return fvec_inner_product_hook;
38+
}
39+
40+
} // namespace faiss

faiss/FaissHook.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
// -*- c++ -*-
9+
10+
#pragma once
11+
12+
#include <cstddef>
13+
#include "faiss/impl/platform_macros.h"
14+
15+
namespace faiss {
16+
17+
using FVEC_L2SQR_HOOK = float (*)(const float*, const float*, size_t);
18+
19+
using FVEC_INNER_PRODUCT_HOOK = float (*)(const float*, const float*, size_t);
20+
21+
extern FVEC_L2SQR_HOOK fvec_L2sqr_hook;
22+
extern FVEC_INNER_PRODUCT_HOOK fvec_inner_product_hook;
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
28+
typedef float (*FVEC_L2SQR_HOOK_C)(const float*, const float*, size_t);
29+
typedef float (*FVEC_INNER_PRODUCT_HOOK_C)(const float*, const float*, size_t);
30+
31+
FAISS_API void set_fvec_L2sqr_hook(FVEC_L2SQR_HOOK_C hook);
32+
FAISS_API FVEC_L2SQR_HOOK_C get_fvec_L2sqr_hook();
33+
34+
FAISS_API void set_fvec_inner_product_hook(FVEC_INNER_PRODUCT_HOOK_C hook);
35+
FAISS_API FVEC_INNER_PRODUCT_HOOK_C get_fvec_inner_product_hook();
36+
37+
#ifdef __cplusplus
38+
}
39+
#endif
40+
41+
} // namespace faiss

faiss/utils/distances_simd.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <cstdio>
1616
#include <cstring>
1717

18+
#include <faiss/FaissHook.h>
1819
#include <faiss/impl/FaissAssert.h>
1920
#include <faiss/impl/platform_macros.h>
2021
#include <faiss/utils/simdlib.h>
@@ -186,7 +187,7 @@ void fvec_inner_products_ny_ref(
186187
*/
187188

188189
FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
189-
float fvec_inner_product(const float* x, const float* y, size_t d) {
190+
float fvec_inner_product_default(const float* x, const float* y, size_t d) {
190191
float res = 0.F;
191192
FAISS_PRAGMA_IMPRECISE_LOOP
192193
for (size_t i = 0; i != d; ++i) {
@@ -196,6 +197,10 @@ float fvec_inner_product(const float* x, const float* y, size_t d) {
196197
}
197198
FAISS_PRAGMA_IMPRECISE_FUNCTION_END
198199

200+
float fvec_inner_product(const float* x, const float* y, size_t d) {
201+
return fvec_inner_product_hook(x, y, d);
202+
}
203+
199204
FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
200205
float fvec_norm_L2sqr(const float* x, size_t d) {
201206
// the double in the _ref is suspected to be a typo. Some of the manual
@@ -210,8 +215,12 @@ float fvec_norm_L2sqr(const float* x, size_t d) {
210215
}
211216
FAISS_PRAGMA_IMPRECISE_FUNCTION_END
212217

213-
FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
214218
float fvec_L2sqr(const float* x, const float* y, size_t d) {
219+
return fvec_L2sqr_hook(x, y, d);
220+
}
221+
222+
FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
223+
float fvec_L2sqr_default(const float* x, const float* y, size_t d) {
215224
size_t i;
216225
float res = 0;
217226
FAISS_PRAGMA_IMPRECISE_LOOP

0 commit comments

Comments
 (0)