Skip to content

Commit

Permalink
Merge pull request facebookresearch#1 from yuhaijun999/wip-add-faiss-…
Browse files Browse the repository at this point in the history
…hook-dingodb

Added InnerProduct and L2Sqr hook interfaces.
  • Loading branch information
ketor authored May 16, 2024
2 parents ab2b7f5 + 532cf61 commit bf46c0f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
2 changes: 2 additions & 0 deletions faiss/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ set(FAISS_SRC
VectorTransform.cpp
clone_index.cpp
index_factory.cpp
FaissHook.cpp
impl/AuxIndexStructures.cpp
impl/CodePacker.cpp
impl/IDSelector.cpp
Expand Down Expand Up @@ -145,6 +146,7 @@ set(FAISS_HEADERS
clone_index.h
index_factory.h
index_io.h
FaissHook.h
impl/AdditiveQuantizer.h
impl/AuxIndexStructures.h
impl/CodePacker.h
Expand Down
40 changes: 40 additions & 0 deletions faiss/FaissHook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// -*- c++ -*-

#include "FaissHook.h"

namespace faiss {

extern float fvec_L2sqr_default(const float* x, const float* y, size_t d);

extern float fvec_inner_product_default(
const float* x,
const float* y,
size_t d);

FVEC_L2SQR_HOOK fvec_L2sqr_hook = fvec_L2sqr_default;
FVEC_INNER_PRODUCT_HOOK fvec_inner_product_hook = fvec_inner_product_default;

void set_fvec_L2sqr_hook(FVEC_L2SQR_HOOK_C hook) {
if (nullptr != hook)
fvec_L2sqr_hook = hook;
}
FVEC_L2SQR_HOOK_C get_fvec_L2sqr_hook() {
return fvec_L2sqr_hook;
}

void set_fvec_inner_product_hook(FVEC_INNER_PRODUCT_HOOK_C hook) {
if (nullptr != hook)
fvec_inner_product_hook = hook;
}
FVEC_INNER_PRODUCT_HOOK_C get_fvec_inner_product_hook() {
return fvec_inner_product_hook;
}

} // namespace faiss
41 changes: 41 additions & 0 deletions faiss/FaissHook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// -*- c++ -*-

#pragma once

#include <cstddef>
#include "faiss/impl/platform_macros.h"

namespace faiss {

using FVEC_L2SQR_HOOK = float (*)(const float*, const float*, size_t);

using FVEC_INNER_PRODUCT_HOOK = float (*)(const float*, const float*, size_t);

extern FVEC_L2SQR_HOOK fvec_L2sqr_hook;
extern FVEC_INNER_PRODUCT_HOOK fvec_inner_product_hook;

#ifdef __cplusplus
extern "C" {
#endif

typedef float (*FVEC_L2SQR_HOOK_C)(const float*, const float*, size_t);
typedef float (*FVEC_INNER_PRODUCT_HOOK_C)(const float*, const float*, size_t);

FAISS_API void set_fvec_L2sqr_hook(FVEC_L2SQR_HOOK_C hook);
FAISS_API FVEC_L2SQR_HOOK_C get_fvec_L2sqr_hook();

FAISS_API void set_fvec_inner_product_hook(FVEC_INNER_PRODUCT_HOOK_C hook);
FAISS_API FVEC_INNER_PRODUCT_HOOK_C get_fvec_inner_product_hook();

#ifdef __cplusplus
}
#endif

} // namespace faiss
13 changes: 11 additions & 2 deletions faiss/utils/distances_simd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <cstdio>
#include <cstring>

#include <faiss/FaissHook.h>
#include <faiss/impl/FaissAssert.h>
#include <faiss/impl/platform_macros.h>
#include <faiss/utils/simdlib.h>
Expand Down Expand Up @@ -186,7 +187,7 @@ void fvec_inner_products_ny_ref(
*/

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
float fvec_inner_product(const float* x, const float* y, size_t d) {
float fvec_inner_product_default(const float* x, const float* y, size_t d) {
float res = 0.F;
FAISS_PRAGMA_IMPRECISE_LOOP
for (size_t i = 0; i != d; ++i) {
Expand All @@ -196,6 +197,10 @@ float fvec_inner_product(const float* x, const float* y, size_t d) {
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

float fvec_inner_product(const float* x, const float* y, size_t d) {
return fvec_inner_product_hook(x, y, d);
}

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
float fvec_norm_L2sqr(const float* x, size_t d) {
// the double in the _ref is suspected to be a typo. Some of the manual
Expand All @@ -210,8 +215,12 @@ float fvec_norm_L2sqr(const float* x, size_t d) {
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
float fvec_L2sqr(const float* x, const float* y, size_t d) {
return fvec_L2sqr_hook(x, y, d);
}

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
float fvec_L2sqr_default(const float* x, const float* y, size_t d) {
size_t i;
float res = 0;
FAISS_PRAGMA_IMPRECISE_LOOP
Expand Down

0 comments on commit bf46c0f

Please sign in to comment.