FaissExecutionEngine.h 2.6 KB
Newer Older
X
Xu Peng 已提交
1 2 3 4 5
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/
X
Xu Peng 已提交
6 7
#pragma once

8
#include "ExecutionEngine.h"
Y
yu yunfeng 已提交
9
#include "faiss/Index.h"
10

X
Xu Peng 已提交
11 12 13
#include <memory>
#include <string>

Z
zhiru 已提交
14

X
Xu Peng 已提交
15
namespace zilliz {
J
jinhai 已提交
16
namespace milvus {
X
Xu Peng 已提交
17 18
namespace engine {

19 20 21
const static std::string BUILD_INDEX_TYPE_IDMAP = "IDMap";
const static std::string BUILD_INDEX_TYPE_IVF = "IVF";
const static std::string BUILD_INDEX_TYPE_IVFSQ8 = "IVFSQ8";
22

Z
zhiru 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
class IndexStatsHelper {

 public:
    using Ptr = std::shared_ptr<IndexStatsHelper>;
    virtual std::string ToString(const std::string &prefix = "") const;
    virtual void Reset() const;
    virtual ~IndexStatsHelper() {}
};

class FaissIndexIVFStatsHelper : public IndexStatsHelper {
 public:
    std::string ToString(const std::string &prefix = "") const override;

 private:
    const std::string identifier_ = BUILD_INDEX_TYPE_IVF;
};

G
groot 已提交
40
class FaissExecutionEngine : public ExecutionEngine {
Z
zhiru 已提交
41
 public:
X
Xu Peng 已提交
42

G
groot 已提交
43
    FaissExecutionEngine(uint16_t dimension,
Z
zhiru 已提交
44 45 46
                         const std::string &location,
                         const std::string &build_index_type,
                         const std::string &raw_index_type);
X
Xu Peng 已提交
47

G
groot 已提交
48
    FaissExecutionEngine(std::shared_ptr<faiss::Index> index,
Z
zhiru 已提交
49 50 51
                         const std::string &location,
                         const std::string &build_index_type,
                         const std::string &raw_index_type);
52

G
groot 已提交
53
    Status AddWithIds(long n, const float *xdata, const long *xids) override;
54

G
groot 已提交
55
    size_t Count() const override;
56

G
groot 已提交
57
    size_t Size() const override;
58

G
groot 已提交
59 60
    size_t Dimension() const override;

G
groot 已提交
61
    size_t PhysicalSize() const override;
62

G
groot 已提交
63
    Status Serialize() override;
64

S
starlord 已提交
65
    Status Load(bool to_cache) override;
66

Z
zhiru 已提交
67
    Status Merge(const std::string &location) override;
68 69 70 71 72

    Status Search(long n,
                  const float *data,
                  long k,
                  float *distances,
G
groot 已提交
73
                  long *labels) const override;
74

Z
zhiru 已提交
75
    ExecutionEnginePtr BuildIndex(const std::string &) override;
76

G
groot 已提交
77
    Status Cache() override;
78

Y
yu yunfeng 已提交
79 80
    Status Init() override;

Z
zhiru 已提交
81 82
 protected:
    FaissIndexIVFStatsHelper ivf_stats_helper_;
83 84
    std::shared_ptr<faiss::Index> pIndex_;
    std::string location_;
G
groot 已提交
85 86 87

    std::string build_index_type_;
    std::string raw_index_type_;
Y
yu yunfeng 已提交
88 89

    size_t nprobe_ = 0;
Y
yu yunfeng 已提交
90
    size_t nlist_ = 0;
91 92
};

X
Xu Peng 已提交
93 94

} // namespace engine
J
jinhai 已提交
95
} // namespace milvus
X
Xu Peng 已提交
96
} // namespace zilliz