File: IndexScalarQuantizer_c.cpp

package info (click to toggle)
faiss 1.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,572 kB
  • sloc: cpp: 85,627; python: 27,889; sh: 905; ansic: 425; makefile: 41
file content (111 lines) | stat: -rw-r--r-- 3,526 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (c) Meta Platforms, Inc. and 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 "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
}