Commit 395d2ce6 authored by huchen's avatar huchen
Browse files

init the faiss for rocm

parent 5ded39f5
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c -*-
#ifndef FAISS_INDEX_IVF_FLAT_C_H
#define FAISS_INDEX_IVF_FLAT_C_H
#include "Clustering_c.h"
#include "Index_c.h"
#include "faiss_c.h"
#ifdef __cplusplus
extern "C" {
#endif
/** Inverted file with stored vectors. Here the inverted file
* pre-selects the vectors to be searched, but they are not otherwise
* encoded, the code array just contains the raw float entries.
*/
FAISS_DECLARE_CLASS_INHERITED(IndexIVFFlat, Index)
FAISS_DECLARE_DESTRUCTOR(IndexIVFFlat)
FAISS_DECLARE_INDEX_DOWNCAST(IndexIVFFlat)
/// number of possible key values
FAISS_DECLARE_GETTER(IndexIVFFlat, size_t, nlist)
/// number of probes at query time
FAISS_DECLARE_GETTER_SETTER(IndexIVFFlat, size_t, nprobe)
/// quantizer that maps vectors to inverted lists
FAISS_DECLARE_GETTER(IndexIVFFlat, FaissIndex*, quantizer)
/**
* = 0: use the quantizer as index in a kmeans training
* = 1: just pass on the training set to the train() of the quantizer
* = 2: kmeans training on a flat index + add the centroids to the quantizer
*/
FAISS_DECLARE_GETTER(IndexIVFFlat, char, quantizer_trains_alone)
/// whether object owns the quantizer
FAISS_DECLARE_GETTER_SETTER(IndexIVFFlat, int, own_fields)
int faiss_IndexIVFFlat_new(FaissIndexIVFFlat** p_index);
int faiss_IndexIVFFlat_new_with(
FaissIndexIVFFlat** p_index,
FaissIndex* quantizer,
size_t d,
size_t nlist);
int faiss_IndexIVFFlat_new_with_metric(
FaissIndexIVFFlat** p_index,
FaissIndex* quantizer,
size_t d,
size_t nlist,
FaissMetricType metric);
int faiss_IndexIVFFlat_add_core(
FaissIndexIVFFlat* index,
idx_t n,
const float* x,
const idx_t* xids,
const int64_t* precomputed_idx);
/** Update a subset of vectors.
*
* The index must have a direct_map
*
* @param nv nb of vectors to update
* @param idx vector indices to update, size nv
* @param v vectors of new values, size nv*d
*/
int faiss_IndexIVFFlat_update_vectors(
FaissIndexIVFFlat* index,
int nv,
idx_t* idx,
const float* v);
#ifdef __cplusplus
}
#endif
#endif
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-
#include "IndexIVF_c.h"
#include <faiss/IndexIVF.h>
#include "Clustering_c.h"
#include "Index_c.h"
#include "macros_impl.h"
using faiss::IndexIVF;
using faiss::IndexIVFStats;
DEFINE_DESTRUCTOR(IndexIVF)
DEFINE_INDEX_DOWNCAST(IndexIVF)
/// number of possible key values
DEFINE_GETTER(IndexIVF, size_t, nlist)
/// number of probes at query time
DEFINE_GETTER(IndexIVF, size_t, nprobe)
DEFINE_SETTER(IndexIVF, size_t, nprobe)
/// quantizer that maps vectors to inverted lists
DEFINE_GETTER_PERMISSIVE(IndexIVF, FaissIndex*, quantizer)
/**
* = 0: use the quantizer as index in a kmeans training
* = 1: just pass on the training set to the train() of the quantizer
* = 2: kmeans training on a flat index + add the centroids to the quantizer
*/
DEFINE_GETTER(IndexIVF, char, quantizer_trains_alone)
/// whether object owns the quantizer
DEFINE_GETTER(IndexIVF, int, own_fields)
DEFINE_SETTER(IndexIVF, int, own_fields)
using faiss::IndexIVF;
int faiss_IndexIVF_merge_from(
FaissIndexIVF* index,
FaissIndexIVF* other,
idx_t add_id) {
try {
reinterpret_cast<IndexIVF*>(index)->merge_from(
*reinterpret_cast<IndexIVF*>(other), add_id);
}
CATCH_AND_HANDLE
}
int faiss_IndexIVF_copy_subset_to(
const FaissIndexIVF* index,
FaissIndexIVF* other,
int subset_type,
idx_t a1,
idx_t a2) {
try {
reinterpret_cast<const IndexIVF*>(index)->copy_subset_to(
*reinterpret_cast<IndexIVF*>(other), subset_type, a1, a2);
}
CATCH_AND_HANDLE
}
int faiss_IndexIVF_search_preassigned(
const FaissIndexIVF* index,
idx_t n,
const float* x,
idx_t k,
const idx_t* assign,
const float* centroid_dis,
float* distances,
idx_t* labels,
int store_pairs) {
try {
reinterpret_cast<const IndexIVF*>(index)->search_preassigned(
n, x, k, assign, centroid_dis, distances, labels, store_pairs);
}
CATCH_AND_HANDLE
}
size_t faiss_IndexIVF_get_list_size(
const FaissIndexIVF* index,
size_t list_no) {
return reinterpret_cast<const IndexIVF*>(index)->get_list_size(list_no);
}
int faiss_IndexIVF_make_direct_map(
FaissIndexIVF* index,
int new_maintain_direct_map) {
try {
reinterpret_cast<IndexIVF*>(index)->make_direct_map(
static_cast<bool>(new_maintain_direct_map));
}
CATCH_AND_HANDLE
}
double faiss_IndexIVF_imbalance_factor(const FaissIndexIVF* index) {
return reinterpret_cast<const IndexIVF*>(index)
->invlists->imbalance_factor();
}
/// display some stats about the inverted lists
void faiss_IndexIVF_print_stats(const FaissIndexIVF* index) {
reinterpret_cast<const IndexIVF*>(index)->invlists->print_stats();
}
/// get inverted lists ids
void faiss_IndexIVF_invlists_get_ids(
const FaissIndexIVF* index,
size_t list_no,
idx_t* invlist) {
const idx_t* list =
reinterpret_cast<const IndexIVF*>(index)->invlists->get_ids(
list_no);
size_t list_size =
reinterpret_cast<const IndexIVF*>(index)->get_list_size(list_no);
memcpy(invlist, list, list_size * sizeof(idx_t));
}
void faiss_IndexIVFStats_reset(FaissIndexIVFStats* stats) {
reinterpret_cast<IndexIVFStats*>(stats)->reset();
}
FaissIndexIVFStats* faiss_get_indexIVF_stats() {
return reinterpret_cast<FaissIndexIVFStats*>(&faiss::indexIVF_stats);
}
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c -*-
#ifndef FAISS_INDEX_IVF_C_H
#define FAISS_INDEX_IVF_C_H
#include "Clustering_c.h"
#include "Index_c.h"
#include "faiss_c.h"
#ifdef __cplusplus
extern "C" {
#endif
/** Index based on a inverted file (IVF)
*
* In the inverted file, the quantizer (an Index instance) provides a
* quantization index for each vector to be added. The quantization
* index maps to a list (aka inverted list or posting list), where the
* id of the vector is then stored.
*
* At search time, the vector to be searched is also quantized, and
* only the list corresponding to the quantization index is
* searched. This speeds up the search by making it
* non-exhaustive. This can be relaxed using multi-probe search: a few
* (nprobe) quantization indices are selected and several inverted
* lists are visited.
*
* Sub-classes implement a post-filtering of the index that refines
* the distance estimation from the query to database vectors.
*/
FAISS_DECLARE_CLASS_INHERITED(IndexIVF, Index)
FAISS_DECLARE_DESTRUCTOR(IndexIVF)
FAISS_DECLARE_INDEX_DOWNCAST(IndexIVF)
/// number of possible key values
FAISS_DECLARE_GETTER(IndexIVF, size_t, nlist)
/// number of probes at query time
FAISS_DECLARE_GETTER_SETTER(IndexIVF, size_t, nprobe)
/// quantizer that maps vectors to inverted lists
FAISS_DECLARE_GETTER(IndexIVF, FaissIndex*, quantizer)
/**
* = 0: use the quantizer as index in a kmeans training
* = 1: just pass on the training set to the train() of the quantizer
* = 2: kmeans training on a flat index + add the centroids to the quantizer
*/
FAISS_DECLARE_GETTER(IndexIVF, char, quantizer_trains_alone)
/// whether object owns the quantizer
FAISS_DECLARE_GETTER_SETTER(IndexIVF, int, own_fields)
/** moves the entries from another dataset to self. On output,
* other is empty. add_id is added to all moved ids (for
* sequential ids, this would be this->ntotal */
int faiss_IndexIVF_merge_from(
FaissIndexIVF* index,
FaissIndexIVF* other,
idx_t add_id);
/** copy a subset of the entries index to the other index
*
* if subset_type == 0: copies ids in [a1, a2)
* if subset_type == 1: copies ids if id % a1 == a2
* if subset_type == 2: copies inverted lists such that a1
* elements are left before and a2 elements are after
*/
int faiss_IndexIVF_copy_subset_to(
const FaissIndexIVF* index,
FaissIndexIVF* other,
int subset_type,
idx_t a1,
idx_t a2);
/** search a set of vectors, that are pre-quantized by the IVF
* quantizer. Fill in the corresponding heaps with the query
* results. search() calls this.
*
* @param n nb of vectors to query
* @param x query vectors, size nx * d
* @param assign coarse quantization indices, size nx * nprobe
* @param centroid_dis
* distances to coarse centroids, size nx * nprobe
* @param distance
* output distances, size n * k
* @param labels output labels, size n * k
* @param store_pairs store inv list index + inv list offset
* instead in upper/lower 32 bit of result,
* instead of ids (used for reranking).
*/
int faiss_IndexIVF_search_preassigned(
const FaissIndexIVF* index,
idx_t n,
const float* x,
idx_t k,
const idx_t* assign,
const float* centroid_dis,
float* distances,
idx_t* labels,
int store_pairs);
size_t faiss_IndexIVF_get_list_size(const FaissIndexIVF* index, size_t list_no);
/** initialize a direct map
*
* @param new_maintain_direct_map if true, create a direct map,
* else clear it
*/
int faiss_IndexIVF_make_direct_map(
FaissIndexIVF* index,
int new_maintain_direct_map);
/** Check the inverted lists' imbalance factor.
*
* 1= perfectly balanced, >1: imbalanced
*/
double faiss_IndexIVF_imbalance_factor(const FaissIndexIVF* index);
/// display some stats about the inverted lists of the index
void faiss_IndexIVF_print_stats(const FaissIndexIVF* index);
/// Get the IDs in an inverted list. IDs are written to `invlist`, which must be
/// large enough
//// to accommodate the full list.
///
/// @param list_no the list ID
/// @param invlist output pointer to a slice of memory, at least as long as the
/// list's size
/// @see faiss_IndexIVF_get_list_size(size_t)
void faiss_IndexIVF_invlists_get_ids(
const FaissIndexIVF* index,
size_t list_no,
idx_t* invlist);
typedef struct FaissIndexIVFStats {
size_t nq; // nb of queries run
size_t nlist; // nb of inverted lists scanned
size_t ndis; // nb of distances computed
size_t nheap_updates; // nb of times the heap was updated
double quantization_time; // time spent quantizing vectors (in ms)
double search_time; // time spent searching lists (in ms)
} FaissIndexIVFStats;
void faiss_IndexIVFStats_reset(FaissIndexIVFStats* stats);
inline void faiss_IndexIVFStats_init(FaissIndexIVFStats* stats) {
faiss_IndexIVFStats_reset(stats);
}
/// global var that collects all statists
FaissIndexIVFStats* faiss_get_indexIVF_stats();
#ifdef __cplusplus
}
#endif
#endif
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-
#include "IndexLSH_c.h"
#include <faiss/IndexLSH.h>
#include "macros_impl.h"
using faiss::Index;
using faiss::IndexLSH;
DEFINE_DESTRUCTOR(IndexLSH)
DEFINE_INDEX_DOWNCAST(IndexLSH)
DEFINE_GETTER(IndexLSH, int, nbits)
DEFINE_GETTER(IndexLSH, int, code_size)
DEFINE_GETTER_PERMISSIVE(IndexLSH, int, rotate_data)
DEFINE_GETTER_PERMISSIVE(IndexLSH, int, train_thresholds)
int faiss_IndexLSH_new(FaissIndexLSH** p_index, idx_t d, int nbits) {
try {
*p_index = reinterpret_cast<FaissIndexLSH*>(new IndexLSH(d, nbits));
}
CATCH_AND_HANDLE
}
int faiss_IndexLSH_new_with_options(
FaissIndexLSH** p_index,
idx_t d,
int nbits,
int rotate_data,
int train_thresholds) {
try {
*p_index = reinterpret_cast<FaissIndexLSH*>(new IndexLSH(
d,
nbits,
static_cast<bool>(rotate_data),
static_cast<bool>(train_thresholds)));
}
CATCH_AND_HANDLE
}
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-
#ifndef INDEX_LSH_C_H
#define INDEX_LSH_C_H
#include "Clustering_c.h"
#include "Index_c.h"
#include "faiss_c.h"
#ifdef __cplusplus
extern "C" {
#endif
/** The sign of each vector component is put in a binary signature */
FAISS_DECLARE_CLASS_INHERITED(IndexLSH, Index)
FAISS_DECLARE_DESTRUCTOR(IndexLSH)
FAISS_DECLARE_INDEX_DOWNCAST(IndexLSH)
FAISS_DECLARE_GETTER(IndexLSH, int, nbits)
FAISS_DECLARE_GETTER(IndexLSH, int, code_size)
FAISS_DECLARE_GETTER(IndexLSH, int, rotate_data)
FAISS_DECLARE_GETTER(IndexLSH, int, train_thresholds)
int faiss_IndexLSH_new(FaissIndexLSH** p_index, idx_t d, int nbits);
int faiss_IndexLSH_new_with_options(
FaissIndexLSH** p_index,
idx_t d,
int nbits,
int rotate_data,
int train_thresholds);
#ifdef __cplusplus
}
#endif
#endif
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-
#include "IndexPreTransform_c.h"
#include <faiss/IndexPreTransform.h>
#include <faiss/VectorTransform.h>
#include "macros_impl.h"
using faiss::Index;
using faiss::IndexPreTransform;
using faiss::VectorTransform;
extern "C" {
DEFINE_DESTRUCTOR(IndexPreTransform)
DEFINE_INDEX_DOWNCAST(IndexPreTransform)
DEFINE_GETTER_PERMISSIVE(IndexPreTransform, FaissIndex*, index)
DEFINE_GETTER(IndexPreTransform, int, own_fields)
DEFINE_SETTER(IndexPreTransform, int, own_fields)
int faiss_IndexPreTransform_new(FaissIndexPreTransform** p_index) {
try {
*p_index = reinterpret_cast<FaissIndexPreTransform*>(
new IndexPreTransform());
}
CATCH_AND_HANDLE
}
int faiss_IndexPreTransform_new_with(
FaissIndexPreTransform** p_index,
FaissIndex* index) {
try {
auto ind = reinterpret_cast<Index*>(index);
*p_index = reinterpret_cast<FaissIndexPreTransform*>(
new IndexPreTransform(ind));
}
CATCH_AND_HANDLE
}
int faiss_IndexPreTransform_new_with_transform(
FaissIndexPreTransform** p_index,
FaissVectorTransform* ltrans,
FaissIndex* index) {
try {
auto lt = reinterpret_cast<VectorTransform*>(ltrans);
auto ind = reinterpret_cast<Index*>(index);
*p_index = reinterpret_cast<FaissIndexPreTransform*>(
new IndexPreTransform(lt, ind));
}
CATCH_AND_HANDLE
}
int faiss_IndexPreTransform_prepend_transform(
FaissIndexPreTransform* index,
FaissVectorTransform* ltrans) {
try {
auto lt = reinterpret_cast<VectorTransform*>(ltrans);
reinterpret_cast<IndexPreTransform*>(index)->prepend_transform(lt);
}
CATCH_AND_HANDLE
}
}
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c -*-
#ifndef FAISS_INDEX_PRETRANSFORM_C_H
#define FAISS_INDEX_PRETRANSFORM_C_H
#include "Index_c.h"
#include "VectorTransform_c.h"
#include "faiss_c.h"
#ifdef __cplusplus
extern "C" {
#endif
/** Index that applies a LinearTransform transform on vectors before
* handing them over to a sub-index */
FAISS_DECLARE_CLASS_INHERITED(IndexPreTransform, Index)
FAISS_DECLARE_DESTRUCTOR(IndexPreTransform)
FAISS_DECLARE_INDEX_DOWNCAST(IndexPreTransform)
FAISS_DECLARE_GETTER(IndexPreTransform, FaissIndex*, index)
FAISS_DECLARE_GETTER_SETTER(IndexPreTransform, int, own_fields)
int faiss_IndexPreTransform_new(FaissIndexPreTransform** p_index);
int faiss_IndexPreTransform_new_with(
FaissIndexPreTransform** p_index,
FaissIndex* index);
int faiss_IndexPreTransform_new_with_transform(
FaissIndexPreTransform** p_index,
FaissVectorTransform* ltrans,
FaissIndex* index);
int faiss_IndexPreTransform_prepend_transform(
FaissIndexPreTransform* index,
FaissVectorTransform* ltrans);
#ifdef __cplusplus
}
#endif
#endif
/**
* 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.
*/
#include "IndexReplicas_c.h"
#include <faiss/IndexReplicas.h>
#include "macros_impl.h"
using faiss::Index;
using faiss::IndexReplicas;
DEFINE_DESTRUCTOR(IndexReplicas)
DEFINE_GETTER(IndexReplicas, int, own_fields)
DEFINE_SETTER(IndexReplicas, int, own_fields)
int faiss_IndexReplicas_new(FaissIndexReplicas** p_index, idx_t d) {
try {
auto out = new IndexReplicas(d);
*p_index = reinterpret_cast<FaissIndexReplicas*>(out);
}
CATCH_AND_HANDLE
}
int faiss_IndexReplicas_new_with_options(
FaissIndexReplicas** p_index,
idx_t d,
int threaded) {
try {
auto out = new IndexReplicas(d, static_cast<bool>(threaded));
*p_index = reinterpret_cast<FaissIndexReplicas*>(out);
}
CATCH_AND_HANDLE
}
int faiss_IndexReplicas_add_replica(
FaissIndexReplicas* index,
FaissIndex* replica) {
try {
reinterpret_cast<IndexReplicas*>(index)->add_replica(
reinterpret_cast<Index*>(replica));
}
CATCH_AND_HANDLE
}
int faiss_IndexReplicas_remove_replica(
FaissIndexReplicas* index,
FaissIndex* replica) {
try {
reinterpret_cast<IndexReplicas*>(index)->remove_replica(
reinterpret_cast<Index*>(replica));
}
CATCH_AND_HANDLE
}
FaissIndex* faiss_IndexReplicas_at(FaissIndexReplicas* index, int i) {
auto replica = reinterpret_cast<IndexReplicas*>(index)->at(i);
return reinterpret_cast<FaissIndex*>(replica);
}
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-
#ifndef INDEXREPLICAS_C_H
#define INDEXREPLICAS_C_H
#include "Index_c.h"
#include "faiss_c.h"
#ifdef __cplusplus
extern "C" {
#endif
/** Index that concatenates the results from several sub-indexes
*/
FAISS_DECLARE_CLASS_INHERITED(IndexReplicas, Index)
FAISS_DECLARE_DESTRUCTOR(IndexReplicas)
FAISS_DECLARE_GETTER_SETTER(IndexReplicas, int, own_fields)
int faiss_IndexReplicas_new(FaissIndexReplicas** p_index, idx_t d);
int faiss_IndexReplicas_new_with_options(
FaissIndexReplicas** p_index,
idx_t d,
int threaded);
int faiss_IndexReplicas_add_replica(
FaissIndexReplicas* index,
FaissIndex* replica);
int faiss_IndexReplicas_remove_replica(
FaissIndexReplicas* index,
FaissIndex* replica);
FaissIndex* faiss_IndexReplicas_at(FaissIndexReplicas* index, int i);
#ifdef __cplusplus
}
#endif
#endif
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-
#include "IndexScalarQuantizer_c.h"
#include <faiss/IndexScalarQuantizer.h>
#include <faiss/impl/ScalarQuantizer.h>
#include "macros_impl.h"
using faiss::Index;
using faiss::IndexIVFScalarQuantizer;
using faiss::IndexScalarQuantizer;
DEFINE_DESTRUCTOR(IndexScalarQuantizer)
DEFINE_INDEX_DOWNCAST(IndexScalarQuantizer)
int faiss_IndexScalarQuantizer_new(FaissIndexScalarQuantizer** p_index) {
try {
*p_index = reinterpret_cast<FaissIndexScalarQuantizer*>(
new IndexScalarQuantizer());
}
CATCH_AND_HANDLE
}
int faiss_IndexScalarQuantizer_new_with(
FaissIndexScalarQuantizer** p_index,
idx_t d,
FaissQuantizerType qt,
FaissMetricType metric) {
try {
IndexScalarQuantizer* index = new IndexScalarQuantizer(
d,
static_cast<faiss::ScalarQuantizer::QuantizerType>(qt),
static_cast<faiss::MetricType>(metric));
*p_index = reinterpret_cast<FaissIndexScalarQuantizer*>(index);
return 0;
}
CATCH_AND_HANDLE
}
DEFINE_DESTRUCTOR(IndexIVFScalarQuantizer)
DEFINE_INDEX_DOWNCAST(IndexIVFScalarQuantizer)
/// quantizer that maps vectors to inverted lists
DEFINE_GETTER_PERMISSIVE(IndexIVFScalarQuantizer, FaissIndex*, quantizer)
/// number of possible key values
DEFINE_GETTER(IndexIVFScalarQuantizer, size_t, nlist)
/// number of probes at query time
DEFINE_GETTER(IndexIVFScalarQuantizer, size_t, nprobe)
DEFINE_SETTER(IndexIVFScalarQuantizer, size_t, nprobe)
/// whether object owns the quantizer
DEFINE_GETTER(IndexIVFScalarQuantizer, int, own_fields)
DEFINE_SETTER(IndexIVFScalarQuantizer, int, own_fields)
int faiss_IndexIVFScalarQuantizer_new_with(
FaissIndexIVFScalarQuantizer** p_index,
FaissIndex* quantizer,
size_t d,
size_t nlist,
FaissQuantizerType qt) {
try {
auto q = reinterpret_cast<Index*>(quantizer);
auto qt_ = static_cast<faiss::ScalarQuantizer::QuantizerType>(qt);
IndexIVFScalarQuantizer* index =
new IndexIVFScalarQuantizer(q, d, nlist, qt_);
*p_index = reinterpret_cast<FaissIndexIVFScalarQuantizer*>(index);
return 0;
}
CATCH_AND_HANDLE
}
int faiss_IndexIVFScalarQuantizer_new_with_metric(
FaissIndexIVFScalarQuantizer** p_index,
FaissIndex* quantizer,
size_t d,
size_t nlist,
FaissQuantizerType qt,
FaissMetricType metric,
int encode_residual) {
try {
auto q = reinterpret_cast<Index*>(quantizer);
auto mt = static_cast<faiss::MetricType>(metric);
auto er = static_cast<bool>(encode_residual);
auto qt_ = static_cast<faiss::ScalarQuantizer::QuantizerType>(qt);
IndexIVFScalarQuantizer* index =
new IndexIVFScalarQuantizer(q, d, nlist, qt_, mt, er);
*p_index = reinterpret_cast<FaissIndexIVFScalarQuantizer*>(index);
return 0;
}
CATCH_AND_HANDLE
}
int faiss_IndexIVFScalarQuantizer_add_core(
FaissIndexIVFScalarQuantizer* index,
idx_t n,
const float* x,
const idx_t* xids,
const idx_t* precomputed_idx) {
try {
reinterpret_cast<IndexIVFScalarQuantizer*>(index)->add_core(
n, x, xids, precomputed_idx);
}
CATCH_AND_HANDLE
}
int faiss_IndexIVFScalarQuantizer_train_residual(
FaissIndexIVFScalarQuantizer* index,
idx_t n,
const float* x) {
try {
reinterpret_cast<IndexIVFScalarQuantizer*>(index)->train_residual(n, x);
}
CATCH_AND_HANDLE
}
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c -*-
#ifndef FAISS_INDEX_SCALAR_QUANTIZER_C_H
#define FAISS_INDEX_SCALAR_QUANTIZER_C_H
#include "Index_c.h"
#include "faiss_c.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum FaissQuantizerType {
QT_8bit, ///< 8 bits per component
QT_4bit, ///< 4 bits per component
QT_8bit_uniform, ///< same, shared range for all dimensions
QT_4bit_uniform,
QT_fp16,
QT_8bit_direct, ///< fast indexing of uint8s
QT_6bit, ///< 6 bits per component
} FaissQuantizerType;
// forward declaration
typedef enum FaissMetricType FaissMetricType;
/** Opaque type for IndexScalarQuantizer */
FAISS_DECLARE_CLASS_INHERITED(IndexScalarQuantizer, Index)
int faiss_IndexScalarQuantizer_new(FaissIndexScalarQuantizer** p_index);
int faiss_IndexScalarQuantizer_new_with(
FaissIndexScalarQuantizer** p_index,
idx_t d,
FaissQuantizerType qt,
FaissMetricType metric);
FAISS_DECLARE_INDEX_DOWNCAST(IndexScalarQuantizer)
FAISS_DECLARE_DESTRUCTOR(IndexScalarQuantizer)
/** Opaque type for IndexIVFScalarQuantizer */
FAISS_DECLARE_CLASS_INHERITED(IndexIVFScalarQuantizer, Index)
FAISS_DECLARE_INDEX_DOWNCAST(IndexIVFScalarQuantizer)
FAISS_DECLARE_DESTRUCTOR(IndexIVFScalarQuantizer)
int faiss_IndexIVFScalarQuantizer_new(FaissIndexIVFScalarQuantizer** p_index);
int faiss_IndexIVFScalarQuantizer_new_with(
FaissIndexIVFScalarQuantizer** p_index,
FaissIndex* quantizer,
idx_t d,
size_t nlist,
FaissQuantizerType qt);
int faiss_IndexIVFScalarQuantizer_new_with_metric(
FaissIndexIVFScalarQuantizer** p_index,
FaissIndex* quantizer,
size_t d,
size_t nlist,
FaissQuantizerType qt,
FaissMetricType metric,
int encode_residual);
/// number of possible key values
FAISS_DECLARE_GETTER(IndexIVFScalarQuantizer, size_t, nlist)
/// number of probes at query time
FAISS_DECLARE_GETTER_SETTER(IndexIVFScalarQuantizer, size_t, nprobe)
/// quantizer that maps vectors to inverted lists
FAISS_DECLARE_GETTER(IndexIVFScalarQuantizer, FaissIndex*, quantizer)
/// whether object owns the quantizer
FAISS_DECLARE_GETTER_SETTER(IndexIVFScalarQuantizer, int, own_fields)
int faiss_IndexIVFScalarQuantizer_add_core(
FaissIndexIVFScalarQuantizer* index,
idx_t n,
const float* x,
const idx_t* xids,
const idx_t* precomputed_idx);
int faiss_IndexIVFScalarQuantizer_train_residual(
FaissIndexIVFScalarQuantizer* index,
idx_t n,
const float* x);
#ifdef __cplusplus
}
#endif
#endif
/**
* 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.
*/
#include "IndexShards_c.h"
#include <faiss/IndexShards.h>
#include "macros_impl.h"
using faiss::Index;
using faiss::IndexShards;
DEFINE_DESTRUCTOR(IndexShards)
DEFINE_GETTER(IndexShards, int, own_fields)
DEFINE_SETTER(IndexShards, int, own_fields)
DEFINE_GETTER(IndexShards, int, successive_ids)
DEFINE_SETTER(IndexShards, int, successive_ids)
int faiss_IndexShards_new(FaissIndexShards** p_index, idx_t d) {
try {
auto out = new IndexShards(d);
*p_index = reinterpret_cast<FaissIndexShards*>(out);
}
CATCH_AND_HANDLE
}
int faiss_IndexShards_new_with_options(
FaissIndexShards** p_index,
idx_t d,
int threaded,
int successive_ids) {
try {
auto out = new IndexShards(
d,
static_cast<bool>(threaded),
static_cast<bool>(successive_ids));
*p_index = reinterpret_cast<FaissIndexShards*>(out);
}
CATCH_AND_HANDLE
}
int faiss_IndexShards_add_shard(FaissIndexShards* index, FaissIndex* shard) {
try {
reinterpret_cast<IndexShards*>(index)->add_shard(
reinterpret_cast<Index*>(shard));
}
CATCH_AND_HANDLE
}
int faiss_IndexShards_remove_shard(FaissIndexShards* index, FaissIndex* shard) {
try {
reinterpret_cast<IndexShards*>(index)->remove_shard(
reinterpret_cast<Index*>(shard));
}
CATCH_AND_HANDLE
}
FaissIndex* faiss_IndexShards_at(FaissIndexShards* index, int i) {
auto shard = reinterpret_cast<IndexShards*>(index)->at(i);
return reinterpret_cast<FaissIndex*>(shard);
}
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-
#ifndef INDEXSHARDS_C_H
#define INDEXSHARDS_C_H
#include "Index_c.h"
#include "faiss_c.h"
#ifdef __cplusplus
extern "C" {
#endif
/** Index that concatenates the results from several sub-indexes
*/
FAISS_DECLARE_CLASS_INHERITED(IndexShards, Index)
FAISS_DECLARE_DESTRUCTOR(IndexShards)
FAISS_DECLARE_GETTER_SETTER(IndexShards, int, own_fields)
FAISS_DECLARE_GETTER_SETTER(IndexShards, int, successive_ids)
int faiss_IndexShards_new(FaissIndexShards** p_index, idx_t d);
int faiss_IndexShards_new_with_options(
FaissIndexShards** p_index,
idx_t d,
int threaded,
int successive_ids);
int faiss_IndexShards_add_shard(FaissIndexShards* index, FaissIndex* shard);
int faiss_IndexShards_remove_shard(FaissIndexShards* index, FaissIndex* shard);
FaissIndex* faiss_IndexShards_at(FaissIndexShards* index, int i);
#ifdef __cplusplus
}
#endif
#endif
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-
#include "Index_c.h"
#include <faiss/Index.h>
#include "macros_impl.h"
extern "C" {
DEFINE_DESTRUCTOR(Index)
DEFINE_GETTER(Index, int, d)
DEFINE_GETTER(Index, int, is_trained)
DEFINE_GETTER(Index, idx_t, ntotal)
DEFINE_GETTER(Index, FaissMetricType, metric_type)
DEFINE_GETTER(Index, int, verbose);
DEFINE_SETTER(Index, int, verbose);
int faiss_Index_train(FaissIndex* index, idx_t n, const float* x) {
try {
reinterpret_cast<faiss::Index*>(index)->train(n, x);
}
CATCH_AND_HANDLE
}
int faiss_Index_add(FaissIndex* index, idx_t n, const float* x) {
try {
reinterpret_cast<faiss::Index*>(index)->add(n, x);
}
CATCH_AND_HANDLE
}
int faiss_Index_add_with_ids(
FaissIndex* index,
idx_t n,
const float* x,
const idx_t* xids) {
try {
reinterpret_cast<faiss::Index*>(index)->add_with_ids(n, x, xids);
}
CATCH_AND_HANDLE
}
int faiss_Index_search(
const FaissIndex* index,
idx_t n,
const float* x,
idx_t k,
float* distances,
idx_t* labels) {
try {
reinterpret_cast<const faiss::Index*>(index)->search(
n, x, k, distances, labels);
}
CATCH_AND_HANDLE
}
int faiss_Index_range_search(
const FaissIndex* index,
idx_t n,
const float* x,
float radius,
FaissRangeSearchResult* result) {
try {
reinterpret_cast<const faiss::Index*>(index)->range_search(
n,
x,
radius,
reinterpret_cast<faiss::RangeSearchResult*>(result));
}
CATCH_AND_HANDLE
}
int faiss_Index_assign(
FaissIndex* index,
idx_t n,
const float* x,
idx_t* labels,
idx_t k) {
try {
reinterpret_cast<faiss::Index*>(index)->assign(n, x, labels, k);
}
CATCH_AND_HANDLE
}
int faiss_Index_reset(FaissIndex* index) {
try {
reinterpret_cast<faiss::Index*>(index)->reset();
}
CATCH_AND_HANDLE
}
int faiss_Index_remove_ids(
FaissIndex* index,
const FaissIDSelector* sel,
size_t* n_removed) {
try {
size_t n{reinterpret_cast<faiss::Index*>(index)->remove_ids(
*reinterpret_cast<const faiss::IDSelector*>(sel))};
if (n_removed) {
*n_removed = n;
}
}
CATCH_AND_HANDLE
}
int faiss_Index_reconstruct(const FaissIndex* index, idx_t key, float* recons) {
try {
reinterpret_cast<const faiss::Index*>(index)->reconstruct(key, recons);
}
CATCH_AND_HANDLE
}
int faiss_Index_reconstruct_n(
const FaissIndex* index,
idx_t i0,
idx_t ni,
float* recons) {
try {
reinterpret_cast<const faiss::Index*>(index)->reconstruct_n(
i0, ni, recons);
}
CATCH_AND_HANDLE
}
int faiss_Index_compute_residual(
const FaissIndex* index,
const float* x,
float* residual,
idx_t key) {
try {
reinterpret_cast<const faiss::Index*>(index)->compute_residual(
x, residual, key);
}
CATCH_AND_HANDLE
}
int faiss_Index_compute_residual_n(
const FaissIndex* index,
idx_t n,
const float* x,
float* residuals,
const idx_t* keys) {
try {
reinterpret_cast<const faiss::Index*>(index)->compute_residual_n(
n, x, residuals, keys);
}
CATCH_AND_HANDLE
}
}
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved
// -*- c -*-
#ifndef FAISS_INDEX_C_H
#define FAISS_INDEX_C_H
#include <stddef.h>
#include "faiss_c.h"
#ifdef __cplusplus
extern "C" {
#endif
// forward declaration required here
FAISS_DECLARE_CLASS(RangeSearchResult)
// typedef struct FaissRangeSearchResult_H FaissRangeSearchResult;
typedef struct FaissIDSelector_H FaissIDSelector;
/// Some algorithms support both an inner product version and a L2 search
/// version.
typedef enum FaissMetricType {
METRIC_INNER_PRODUCT = 0, ///< maximum inner product search
METRIC_L2 = 1, ///< squared L2 search
METRIC_L1, ///< L1 (aka cityblock)
METRIC_Linf, ///< infinity distance
METRIC_Lp, ///< L_p distance, p is given by metric_arg
/// some additional metrics defined in scipy.spatial.distance
METRIC_Canberra = 20,
METRIC_BrayCurtis,
METRIC_JensenShannon,
} FaissMetricType;
/// Opaque type for referencing to an index object
FAISS_DECLARE_CLASS(Index)
FAISS_DECLARE_DESTRUCTOR(Index)
/// Getter for d
FAISS_DECLARE_GETTER(Index, int, d)
/// Getter for is_trained
FAISS_DECLARE_GETTER(Index, int, is_trained)
/// Getter for ntotal
FAISS_DECLARE_GETTER(Index, idx_t, ntotal)
/// Getter for metric_type
FAISS_DECLARE_GETTER(Index, FaissMetricType, metric_type)
FAISS_DECLARE_GETTER_SETTER(Index, int, verbose)
/** Perform training on a representative set of vectors
*
* @param index opaque pointer to index object
* @param n nb of training vectors
* @param x training vectors, size n * d
*/
int faiss_Index_train(FaissIndex* index, idx_t n, const float* x);
/** Add n vectors of dimension d to the index.
*
* Vectors are implicitly assigned labels ntotal .. ntotal + n - 1
* This function slices the input vectors in chunks smaller than
* blocksize_add and calls add_core.
* @param index opaque pointer to index object
* @param x input matrix, size n * d
*/
int faiss_Index_add(FaissIndex* index, idx_t n, const float* x);
/** Same as add, but stores xids instead of sequential ids.
*
* The default implementation fails with an assertion, as it is
* not supported by all indexes.
*
* @param index opaque pointer to index object
* @param xids if non-null, ids to store for the vectors (size n)
*/
int faiss_Index_add_with_ids(
FaissIndex* index,
idx_t n,
const float* x,
const idx_t* xids);
/** query n vectors of dimension d to the index.
*
* return at most k vectors. If there are not enough results for a
* query, the result array is padded with -1s.
*
* @param index opaque pointer to index object
* @param x input vectors to search, size n * d
* @param labels output labels of the NNs, size n*k
* @param distances output pairwise distances, size n*k
*/
int faiss_Index_search(
const FaissIndex* index,
idx_t n,
const float* x,
idx_t k,
float* distances,
idx_t* labels);
/** query n vectors of dimension d to the index.
*
* return all vectors with distance < radius. Note that many
* indexes do not implement the range_search (only the k-NN search
* is mandatory).
*
* @param index opaque pointer to index object
* @param x input vectors to search, size n * d
* @param radius search radius
* @param result result table
*/
int faiss_Index_range_search(
const FaissIndex* index,
idx_t n,
const float* x,
float radius,
FaissRangeSearchResult* result);
/** return the indexes of the k vectors closest to the query x.
*
* This function is identical as search but only return labels of neighbors.
* @param index opaque pointer to index object
* @param x input vectors to search, size n * d
* @param labels output labels of the NNs, size n*k
*/
int faiss_Index_assign(
FaissIndex* index,
idx_t n,
const float* x,
idx_t* labels,
idx_t k);
/** removes all elements from the database.
* @param index opaque pointer to index object
*/
int faiss_Index_reset(FaissIndex* index);
/** removes IDs from the index. Not supported by all indexes
* @param index opaque pointer to index object
* @param nremove output for the number of IDs removed
*/
int faiss_Index_remove_ids(
FaissIndex* index,
const FaissIDSelector* sel,
size_t* n_removed);
/** Reconstruct a stored vector (or an approximation if lossy coding)
*
* this function may not be defined for some indexes
* @param index opaque pointer to index object
* @param key id of the vector to reconstruct
* @param recons reconstructed vector (size d)
*/
int faiss_Index_reconstruct(const FaissIndex* index, idx_t key, float* recons);
/** Reconstruct vectors i0 to i0 + ni - 1
*
* this function may not be defined for some indexes
* @param index opaque pointer to index object
* @param recons reconstructed vector (size ni * d)
*/
int faiss_Index_reconstruct_n(
const FaissIndex* index,
idx_t i0,
idx_t ni,
float* recons);
/** Computes a residual vector after indexing encoding.
*
* The residual vector is the difference between a vector and the
* reconstruction that can be decoded from its representation in
* the index. The residual can be used for multiple-stage indexing
* methods, like IndexIVF's methods.
*
* @param index opaque pointer to index object
* @param x input vector, size d
* @param residual output residual vector, size d
* @param key encoded index, as returned by search and assign
*/
int faiss_Index_compute_residual(
const FaissIndex* index,
const float* x,
float* residual,
idx_t key);
/** Computes a residual vector after indexing encoding.
*
* The residual vector is the difference between a vector and the
* reconstruction that can be decoded from its representation in
* the index. The residual can be used for multiple-stage indexing
* methods, like IndexIVF's methods.
*
* @param index opaque pointer to index object
* @param n number of vectors
* @param x input vector, size (n x d)
* @param residuals output residual vectors, size (n x d)
* @param keys encoded index, as returned by search and assign
*/
int faiss_Index_compute_residual_n(
const FaissIndex* index,
idx_t n,
const float* x,
float* residuals,
const idx_t* keys);
#ifdef __cplusplus
}
#endif
#endif
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-
#include "MetaIndexes_c.h"
#include <faiss/MetaIndexes.h>
#include "macros_impl.h"
using faiss::Index;
using faiss::IndexIDMap;
using faiss::IndexIDMap2;
DEFINE_GETTER(IndexIDMap, int, own_fields)
DEFINE_SETTER(IndexIDMap, int, own_fields)
DEFINE_INDEX_DOWNCAST(IndexIDMap)
DEFINE_GETTER(IndexIDMap2, int, own_fields)
DEFINE_SETTER(IndexIDMap2, int, own_fields)
DEFINE_INDEX_DOWNCAST(IndexIDMap2)
int faiss_IndexIDMap_new(FaissIndexIDMap** p_index, FaissIndex* index) {
try {
auto out = new IndexIDMap(reinterpret_cast<Index*>(index));
*p_index = reinterpret_cast<FaissIndexIDMap*>(out);
}
CATCH_AND_HANDLE
}
void faiss_IndexIDMap_id_map(
FaissIndexIDMap* index,
idx_t** p_id_map,
size_t* p_size) {
auto idx = reinterpret_cast<IndexIDMap*>(index);
if (p_id_map)
*p_id_map = idx->id_map.data();
if (p_size)
*p_size = idx->id_map.size();
}
FaissIndex* faiss_IndexIDMap_sub_index(FaissIndexIDMap* index) {
auto idx = reinterpret_cast<IndexIDMap*>(index);
return (FaissIndex*)reinterpret_cast<Index*>(idx->index);
}
int faiss_IndexIDMap2_new(FaissIndexIDMap2** p_index, FaissIndex* index) {
try {
auto out = new IndexIDMap2(reinterpret_cast<Index*>(index));
*p_index = reinterpret_cast<FaissIndexIDMap2*>(out);
}
CATCH_AND_HANDLE
}
int faiss_IndexIDMap2_construct_rev_map(FaissIndexIDMap2* index) {
try {
reinterpret_cast<IndexIDMap2*>(index)->construct_rev_map();
}
CATCH_AND_HANDLE
}
void faiss_IndexIDMap2_id_map(
FaissIndexIDMap2* index,
idx_t** p_id_map,
size_t* p_size) {
auto idx = reinterpret_cast<IndexIDMap2*>(index);
if (p_id_map)
*p_id_map = idx->id_map.data();
if (p_size)
*p_size = idx->id_map.size();
}
FaissIndex* faiss_IndexIDMap2_sub_index(FaissIndexIDMap2* index) {
auto idx = reinterpret_cast<IndexIDMap2*>(index);
return (FaissIndex*)reinterpret_cast<Index*>(idx->index);
}
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-
#ifndef METAINDEXES_C_H
#define METAINDEXES_C_H
#include "Index_c.h"
#include "faiss_c.h"
#ifdef __cplusplus
extern "C" {
#endif
/** Index that translates search results to ids */
FAISS_DECLARE_CLASS_INHERITED(IndexIDMap, Index)
FAISS_DECLARE_GETTER_SETTER(IndexIDMap, int, own_fields)
int faiss_IndexIDMap_new(FaissIndexIDMap** p_index, FaissIndex* index);
/** attempt a dynamic cast to a IDMap, thus checking
* check whether the underlying index type is `IndexIDMap`.
*
* @param index opaque pointer to index object
* @return the same pointer if the index is a IDMap index, NULL otherwise
*/
FAISS_DECLARE_INDEX_DOWNCAST(IndexIDMap)
/** get a pointer to the index map's internal ID vector (the `id_map` field).
* The outputs of this function become invalid after any operation that can
* modify the index.
*
* @param index opaque pointer to index object
* @param p_id_map output, the pointer to the beginning of `id_map`.
* @param p_size output, the current length of `id_map`.
*/
void faiss_IndexIDMap_id_map(
FaissIndexIDMap* index,
idx_t** p_id_map,
size_t* p_size);
/** get a pointer to the sub-index (the `index` field).
* The outputs of this function become invalid after any operation that can
* modify the index.
*
* @param index opaque pointer to index object
*/
FaissIndex* faiss_IndexIDMap_sub_index(FaissIndexIDMap* index);
/** same as IndexIDMap but also provides an efficient reconstruction
implementation via a 2-way index */
FAISS_DECLARE_CLASS_INHERITED(IndexIDMap2, Index)
FAISS_DECLARE_GETTER_SETTER(IndexIDMap2, int, own_fields)
int faiss_IndexIDMap2_new(FaissIndexIDMap2** p_index, FaissIndex* index);
/// make the rev_map from scratch
int faiss_IndexIDMap2_construct_rev_map(FaissIndexIDMap2* index);
/** attempt a dynamic cast to a IDMap2, thus checking
* check whether the underlying index type is `IndexIDMap`.
*
* @param index opaque pointer to index object
* @return the same pointer if the index is a IDMap2 index, NULL otherwise
*/
FAISS_DECLARE_INDEX_DOWNCAST(IndexIDMap2)
/** get a pointer to the index map's internal ID vector (the `id_map` field).
* The outputs of this function become invalid after any operation that can
* modify the index.
*
* @param index opaque pointer to index object
* @param p_id_map output, the pointer to the beginning of `id_map`.
* @param p_size output, the current length of `id_map`.
*/
void faiss_IndexIDMap2_id_map(
FaissIndexIDMap2* index,
idx_t** p_id_map,
size_t* p_size);
/** get a pointer to the sub-index (the `index` field).
* The outputs of this function become invalid after any operation that can
* modify the index.
*
* @param index opaque pointer to index object
*/
FaissIndex* faiss_IndexIDMap2_sub_index(FaissIndexIDMap2* index);
#ifdef __cplusplus
}
#endif
#endif
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-
#include "VectorTransform_c.h"
#include <faiss/VectorTransform.h>
#include "macros_impl.h"
extern "C" {
DEFINE_DESTRUCTOR(VectorTransform)
DEFINE_GETTER(VectorTransform, int, is_trained)
DEFINE_GETTER(VectorTransform, int, d_in)
DEFINE_GETTER(VectorTransform, int, d_out)
int faiss_VectorTransform_train(
FaissVectorTransform* vt,
idx_t n,
const float* x) {
try {
reinterpret_cast<faiss::VectorTransform*>(vt)->train(n, x);
}
CATCH_AND_HANDLE
}
float* faiss_VectorTransform_apply(
const FaissVectorTransform* vt,
idx_t n,
const float* x) {
return reinterpret_cast<const faiss::VectorTransform*>(vt)->apply(n, x);
}
void faiss_VectorTransform_apply_noalloc(
const FaissVectorTransform* vt,
idx_t n,
const float* x,
float* xt) {
return reinterpret_cast<const faiss::VectorTransform*>(vt)->apply_noalloc(
n, x, xt);
}
void faiss_VectorTransform_reverse_transform(
const FaissVectorTransform* vt,
idx_t n,
const float* xt,
float* x) {
return reinterpret_cast<const faiss::VectorTransform*>(vt)
->reverse_transform(n, xt, x);
}
/*********************************************
* LinearTransform
*********************************************/
DEFINE_DESTRUCTOR(LinearTransform)
DEFINE_GETTER(LinearTransform, int, have_bias)
DEFINE_GETTER(LinearTransform, int, is_orthonormal)
void faiss_LinearTransform_transform_transpose(
const FaissLinearTransform* vt,
idx_t n,
const float* y,
float* x) {
return reinterpret_cast<const faiss::LinearTransform*>(vt)
->transform_transpose(n, y, x);
}
void faiss_LinearTransform_set_is_orthonormal(FaissLinearTransform* vt) {
return reinterpret_cast<faiss::LinearTransform*>(vt)->set_is_orthonormal();
}
/*********************************************
* RandomRotationMatrix
*********************************************/
DEFINE_DESTRUCTOR(RandomRotationMatrix)
int faiss_RandomRotationMatrix_new_with(
FaissRandomRotationMatrix** p_vt,
int d_in,
int d_out) {
try {
*p_vt = reinterpret_cast<FaissRandomRotationMatrix*>(
new faiss::RandomRotationMatrix(d_in, d_out));
}
CATCH_AND_HANDLE
}
/*********************************************
* PCAMatrix
*********************************************/
DEFINE_DESTRUCTOR(PCAMatrix)
int faiss_PCAMatrix_new_with(
FaissPCAMatrix** p_vt,
int d_in,
int d_out,
float eigen_power,
int random_rotation) {
try {
bool random_rotation_ = static_cast<bool>(random_rotation);
*p_vt = reinterpret_cast<FaissPCAMatrix*>(new faiss::PCAMatrix(
d_in, d_out, eigen_power, random_rotation_));
}
CATCH_AND_HANDLE
}
DEFINE_GETTER(PCAMatrix, float, eigen_power)
DEFINE_GETTER(PCAMatrix, int, random_rotation)
/*********************************************
* ITQMatrix
*********************************************/
DEFINE_DESTRUCTOR(ITQMatrix)
int faiss_ITQMatrix_new_with(FaissITQMatrix** p_vt, int d) {
try {
*p_vt = reinterpret_cast<FaissITQMatrix*>(new faiss::ITQMatrix(d));
}
CATCH_AND_HANDLE
}
DEFINE_DESTRUCTOR(ITQTransform)
int faiss_ITQTransform_new_with(
FaissITQTransform** p_vt,
int d_in,
int d_out,
int do_pca) {
try {
bool do_pca_ = static_cast<bool>(do_pca);
*p_vt = reinterpret_cast<FaissITQTransform*>(
new faiss::ITQTransform(d_in, d_out, do_pca_));
}
CATCH_AND_HANDLE
}
DEFINE_GETTER(ITQTransform, int, do_pca)
/*********************************************
* OPQMatrix
*********************************************/
DEFINE_DESTRUCTOR(OPQMatrix)
int faiss_OPQMatrix_new_with(FaissOPQMatrix** p_vt, int d, int M, int d2) {
try {
*p_vt = reinterpret_cast<FaissOPQMatrix*>(
new faiss::OPQMatrix(d, M, d2));
}
CATCH_AND_HANDLE
}
DEFINE_GETTER(OPQMatrix, int, verbose)
DEFINE_SETTER(OPQMatrix, int, verbose)
DEFINE_GETTER(OPQMatrix, int, niter)
DEFINE_SETTER(OPQMatrix, int, niter)
DEFINE_GETTER(OPQMatrix, int, niter_pq)
DEFINE_SETTER(OPQMatrix, int, niter_pq)
/*********************************************
* RemapDimensionsTransform
*********************************************/
DEFINE_DESTRUCTOR(RemapDimensionsTransform)
int faiss_RemapDimensionsTransform_new_with(
FaissRemapDimensionsTransform** p_vt,
int d_in,
int d_out,
int uniform) {
try {
bool uniform_ = static_cast<bool>(uniform);
*p_vt = reinterpret_cast<FaissRemapDimensionsTransform*>(
new faiss::RemapDimensionsTransform(d_in, d_out, uniform_));
}
CATCH_AND_HANDLE
}
/*********************************************
* NormalizationTransform
*********************************************/
DEFINE_DESTRUCTOR(NormalizationTransform)
int faiss_NormalizationTransform_new_with(
FaissNormalizationTransform** p_vt,
int d,
float norm) {
try {
*p_vt = reinterpret_cast<FaissNormalizationTransform*>(
new faiss::NormalizationTransform(d, norm));
}
CATCH_AND_HANDLE
}
DEFINE_GETTER(NormalizationTransform, float, norm)
/*********************************************
* CenteringTransform
*********************************************/
DEFINE_DESTRUCTOR(CenteringTransform)
int faiss_CenteringTransform_new_with(FaissCenteringTransform** p_vt, int d) {
try {
*p_vt = reinterpret_cast<FaissCenteringTransform*>(
new faiss::CenteringTransform(d));
}
CATCH_AND_HANDLE
}
}
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c -*-
#ifndef FAISS_VECTOR_TRANSFORM_C_H
#define FAISS_VECTOR_TRANSFORM_C_H
/** Defines a few objects that apply transformations to a set of
* vectors Often these are pre-processing steps.
*/
#include "faiss_c.h"
#ifdef __cplusplus
extern "C" {
#endif
/// Opaque type for referencing to a VectorTransform object
FAISS_DECLARE_CLASS(VectorTransform)
FAISS_DECLARE_DESTRUCTOR(VectorTransform)
/// Getter for is_trained
FAISS_DECLARE_GETTER(VectorTransform, int, is_trained)
/// Getter for input dimension
FAISS_DECLARE_GETTER(VectorTransform, int, d_in)
/// Getter for output dimension
FAISS_DECLARE_GETTER(VectorTransform, int, d_out)
/** Perform training on a representative set of vectors
*
* @param vt opaque pointer to VectorTransform object
* @param n nb of training vectors
* @param x training vectors, size n * d
*/
int faiss_VectorTransform_train(
FaissVectorTransform* vt,
idx_t n,
const float* x);
/** apply the random rotation, return new allocated matrix
* @param x size n * d_in
* @return size n * d_out
*/
float* faiss_VectorTransform_apply(
const FaissVectorTransform* vt,
idx_t n,
const float* x);
/** apply transformation and result is pre-allocated
* @param x size n * d_in
* @param xt size n * d_out
*/
void faiss_VectorTransform_apply_noalloc(
const FaissVectorTransform* vt,
idx_t n,
const float* x,
float* xt);
/// reverse transformation. May not be implemented or may return
/// approximate result
void faiss_VectorTransform_reverse_transform(
const FaissVectorTransform* vt,
idx_t n,
const float* xt,
float* x);
/// Opaque type for referencing to a LinearTransform object
FAISS_DECLARE_CLASS_INHERITED(LinearTransform, VectorTransform)
FAISS_DECLARE_DESTRUCTOR(LinearTransform)
/// compute x = A^T * (x - b)
/// is reverse transform if A has orthonormal lines
void faiss_LinearTransform_transform_transpose(
const FaissLinearTransform* vt,
idx_t n,
const float* y,
float* x);
/// compute A^T * A to set the is_orthonormal flag
void faiss_LinearTransform_set_is_orthonormal(FaissLinearTransform* vt);
/// Getter for have_bias
FAISS_DECLARE_GETTER(LinearTransform, int, have_bias)
/// Getter for is_orthonormal
FAISS_DECLARE_GETTER(LinearTransform, int, is_orthonormal)
FAISS_DECLARE_CLASS_INHERITED(RandomRotationMatrix, VectorTransform)
FAISS_DECLARE_DESTRUCTOR(RandomRotationMatrix)
int faiss_RandomRotationMatrix_new_with(
FaissRandomRotationMatrix** p_vt,
int d_in,
int d_out);
FAISS_DECLARE_CLASS_INHERITED(PCAMatrix, VectorTransform)
FAISS_DECLARE_DESTRUCTOR(PCAMatrix)
int faiss_PCAMatrix_new_with(
FaissPCAMatrix** p_vt,
int d_in,
int d_out,
float eigen_power,
int random_rotation);
/// Getter for eigen_power
FAISS_DECLARE_GETTER(PCAMatrix, float, eigen_power)
/// Getter for random_rotation
FAISS_DECLARE_GETTER(PCAMatrix, int, random_rotation)
FAISS_DECLARE_CLASS_INHERITED(ITQMatrix, VectorTransform)
FAISS_DECLARE_DESTRUCTOR(ITQMatrix)
int faiss_ITQMatrix_new_with(FaissITQMatrix** p_vt, int d);
FAISS_DECLARE_CLASS_INHERITED(ITQTransform, VectorTransform)
FAISS_DECLARE_DESTRUCTOR(ITQTransform)
int faiss_ITQTransform_new_with(
FaissITQTransform** p_vt,
int d_in,
int d_out,
int do_pca);
/// Getter for do_pca
FAISS_DECLARE_GETTER(ITQTransform, int, do_pca)
FAISS_DECLARE_CLASS_INHERITED(OPQMatrix, VectorTransform)
FAISS_DECLARE_DESTRUCTOR(OPQMatrix)
int faiss_OPQMatrix_new_with(FaissOPQMatrix** p_vt, int d, int M, int d2);
FAISS_DECLARE_GETTER_SETTER(OPQMatrix, int, verbose)
FAISS_DECLARE_GETTER_SETTER(OPQMatrix, int, niter)
FAISS_DECLARE_GETTER_SETTER(OPQMatrix, int, niter_pq)
FAISS_DECLARE_CLASS_INHERITED(RemapDimensionsTransform, VectorTransform)
FAISS_DECLARE_DESTRUCTOR(RemapDimensionsTransform)
int faiss_RemapDimensionsTransform_new_with(
FaissRemapDimensionsTransform** p_vt,
int d_in,
int d_out,
int uniform);
FAISS_DECLARE_CLASS_INHERITED(NormalizationTransform, VectorTransform)
FAISS_DECLARE_DESTRUCTOR(NormalizationTransform)
int faiss_NormalizationTransform_new_with(
FaissNormalizationTransform** p_vt,
int d,
float norm);
FAISS_DECLARE_GETTER(NormalizationTransform, float, norm)
FAISS_DECLARE_CLASS_INHERITED(CenteringTransform, VectorTransform)
FAISS_DECLARE_DESTRUCTOR(CenteringTransform)
int faiss_CenteringTransform_new_with(FaissCenteringTransform** p_vt, int d);
#ifdef __cplusplus
}
#endif
#endif
\ No newline at end of file
/**
* 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.
*/
// Copyright 2004-present Facebook. All Rights Reserved
// -*- c++ -*-
// I/O code for indexes
#include "clone_index_c.h"
#include <faiss/clone_index.h>
#include "macros_impl.h"
using faiss::Index;
int faiss_clone_index(const FaissIndex* idx, FaissIndex** p_out) {
try {
auto out = faiss::clone_index(reinterpret_cast<const Index*>(idx));
*p_out = reinterpret_cast<FaissIndex*>(out);
}
CATCH_AND_HANDLE
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment