Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add faiss debug option #2113

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile.d/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ cmd/agent/core/faiss/faiss: \
CFLAGS="$(CFLAGS)" \
CXXFLAGS="$(CXXFLAGS)" \
CGO_ENABLED=1 \
CGO_CXXFLAGS="-g -Ofast -march=native" \
CGO_FFLAGS="-g -Ofast -march=native" \
CGO_LDFLAGS="-g -Ofast -march=native" \
CGO_CXXFLAGS="-g -Ofast -march=native -DDEBUG=0" \
CGO_FFLAGS="-g -Ofast -march=native -DDEBUG=0" \
CGO_LDFLAGS="-g -Ofast -march=native -DDEBUG=0" \
ykadowak marked this conversation as resolved.
Show resolved Hide resolved
GO111MODULE=on \
GOPRIVATE=$(GOPRIVATE) \
go build \
Expand Down
167 changes: 111 additions & 56 deletions internal/core/algorithm/faiss/Capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,23 @@
#include <faiss/MetricType.h>
#include "Capi.h"

#if DEBUG
constexpr bool isDebug = true;
#else
constexpr bool isDebug = false;
#endif

FaissStruct* faiss_create_index(
const int d,
const int nlist,
const int m,
const int nbits_per_idx,
const int metric_type) {
//printf(__FUNCTION__);
//printf("\n");
//fflush(stdout);
if (isDebug) {
printf(__FUNCTION__);
printf("\n");
fflush(stdout);
}

FaissStruct *st = NULL;
try {
Expand All @@ -52,7 +60,9 @@ FaissStruct* faiss_create_index(
return NULL;
}
faiss::IndexIVFPQ *index = new faiss::IndexIVFPQ(quantizer, d, nlist, m, nbits_per_idx);
//index->verbose = true;
if (isDebug) {
index->verbose = true;
}
st = new FaissStruct{
static_cast<FaissQuantizer>(quantizer),
static_cast<FaissIndex>(index)
Expand All @@ -67,9 +77,11 @@ FaissStruct* faiss_create_index(
}

FaissStruct* faiss_read_index(const char* fname) {
//printf(__FUNCTION__);
//printf("\n");
//fflush(stdout);
if (isDebug) {
printf(__FUNCTION__);
printf("\n");
fflush(stdout);
}

FaissStruct *st = NULL;
try {
Expand All @@ -83,15 +95,20 @@ FaissStruct* faiss_read_index(const char* fname) {
std::cerr << ss.str() << std::endl;
}

if (isDebug) {
fflush(stdout);
}
return st;
}

bool faiss_write_index(
const FaissStruct* st,
const char* fname) {
//printf(__FUNCTION__);
//printf("\n");
//fflush(stdout);
if (isDebug) {
printf(__FUNCTION__);
printf("\n");
fflush(stdout);
}

try {
faiss::write_index(static_cast<faiss::IndexIVFPQ*>(st->faiss_index), fname);
Expand All @@ -102,32 +119,42 @@ bool faiss_write_index(
return false;
}

fflush(stdout);
if (isDebug) {
fflush(stdout);
}
return true;
}

bool faiss_train(
const FaissStruct* st,
const int nb,
const float* xb) {
//printf(__FUNCTION__);
//printf("\n");
//fflush(stdout);
if (isDebug) {
printf(__FUNCTION__);
printf("\n");
fflush(stdout);
}

try {
//printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
//printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
if (isDebug) {
printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
}
(static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->train(nb, xb);
//printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
//printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
if (isDebug) {
printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
}
} catch(std::exception &err) {
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : Error: " << err.what();
std::cerr << ss.str() << std::endl;
return false;
}

fflush(stdout);
if (isDebug) {
fflush(stdout);
}
return true;
}

Expand All @@ -136,24 +163,32 @@ int faiss_add(
const int nb,
const float* xb,
const long int* xids ) {
//printf(__FUNCTION__);
//printf("\n");
//fflush(stdout);
if (isDebug) {
printf(__FUNCTION__);
printf("\n");
fflush(stdout);
}

try {
//printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
//printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
if (isDebug) {
printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
}
(static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->add_with_ids(nb, xb, xids);
//printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
//printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
if (isDebug) {
printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
}
} catch(std::exception &err) {
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : Error: " << err.what();
std::cerr << ss.str() << std::endl;
return -1;
}

fflush(stdout);
if (isDebug) {
fflush(stdout);
}
return (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal;
}

Expand All @@ -164,67 +199,87 @@ bool faiss_search(
const float* xq,
long* I,
float* D) {
//printf(__FUNCTION__);
//printf("\n");
//fflush(stdout);
if (isDebug) {
printf(__FUNCTION__);
printf("\n");
fflush(stdout);
}

try {
//printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
//printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
if (isDebug) {
printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
}
(static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->search(nq, xq, k, D, I);
//printf("I=\n");
//for(int i = 0; i < nq; i++) {
// for(int j = 0; j < k; j++) {
// printf("%5ld ", I[i * k + j]);
// }
// printf("\n");
//}
//printf("D=\n");
//for(int i = 0; i < nq; i++) {
// for(int j = 0; j < k; j++) {
// printf("%7g ", D[i * k + j]);
// }
// printf("\n");
//}
if (isDebug) {
printf("I=\n");
for(int i = 0; i < nq; i++) {
for(int j = 0; j < k; j++) {
printf("%5ld ", I[i * k + j]);
}
printf("\n");
}
printf("D=\n");
for(int i = 0; i < nq; i++) {
for(int j = 0; j < k; j++) {
printf("%7g ", D[i * k + j]);
}
printf("\n");
}
}
} catch(std::exception &err) {
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : Error: " << err.what();
std::cerr << ss.str() << std::endl;
return false;
}

if (isDebug) {
fflush(stdout);
}
return true;
}

int faiss_remove(
const FaissStruct* st,
const int size,
const long int* ids) {
//printf(__FUNCTION__);
//printf("\n");
//fflush(stdout);
if (isDebug) {
printf(__FUNCTION__);
printf("\n");
fflush(stdout);
}

try {
//printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
//printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
if (isDebug) {
printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
}
faiss::IDSelectorArray sel(size, ids);
(static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->remove_ids(sel);
//printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
//printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
if (isDebug) {
printf("is_trained: %d\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->is_trained);
printf("ntotal: %ld\n", (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal);
}
} catch(std::exception &err) {
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : Error: " << err.what();
std::cerr << ss.str() << std::endl;
return -1;
}

if (isDebug) {
fflush(stdout);
}
return (static_cast<faiss::IndexIVFPQ*>(st->faiss_index))->ntotal;
}

void faiss_free(FaissStruct* st) {
//printf(__FUNCTION__);
//printf("\n");
//fflush(stdout);
if (isDebug) {
printf(__FUNCTION__);
printf("\n");
fflush(stdout);
}

free(st);
return;
Expand Down
4 changes: 4 additions & 0 deletions internal/core/algorithm/faiss/Capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
// limitations under the License.
//

#ifndef DEBUG
#define DEBUG
#endif

ykadowak marked this conversation as resolved.
Show resolved Hide resolved
#ifdef __cplusplus
extern "C" {
#endif
Expand Down