VecIndex.h 4.1 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

X
MS-154  
xj.lin 已提交
18 19 20
#pragma once

#include <memory>
S
starlord 已提交
21
#include <string>
W
wxyu 已提交
22
#include <utility>
X
MS-154  
xj.lin 已提交
23

24
#include "cache/DataObj.h"
X
xiaojun.lin 已提交
25
#include "knowhere/common/BinarySet.h"
S
starlord 已提交
26
#include "knowhere/common/Config.h"
X
xiaojun.lin 已提交
27
#include "knowhere/index/vector_index/Quantizer.h"
W
add log  
wxyu 已提交
28
#include "utils/Log.h"
S
starlord 已提交
29
#include "utils/Status.h"
X
MS-154  
xj.lin 已提交
30

X
xj.lin 已提交
31
namespace milvus {
X
MS-154  
xj.lin 已提交
32 33
namespace engine {

S
starlord 已提交
34
using Config = knowhere::Config;
X
MS-154  
xj.lin 已提交
35

Z
zirui.chen 已提交
36
// TODO(linxj): replace with string, Do refactor serialization
X
xj.lin 已提交
37 38 39 40 41
enum class IndexType {
    INVALID = 0,
    FAISS_IDMAP = 1,
    FAISS_IVFFLAT_CPU,
    FAISS_IVFFLAT_GPU,
S
starlord 已提交
42
    FAISS_IVFFLAT_MIX,  // build on gpu and search on cpu
X
xj.lin 已提交
43 44 45
    FAISS_IVFPQ_CPU,
    FAISS_IVFPQ_GPU,
    SPTAG_KDT_RNT_CPU,
X
xj.lin 已提交
46
    FAISS_IVFSQ8_MIX,
X
xj.lin 已提交
47 48
    FAISS_IVFSQ8_CPU,
    FAISS_IVFSQ8_GPU,
49
    FAISS_IVFSQ8_HYBRID,  // only support build on gpu.
X
xj.lin 已提交
50
    NSG_MIX,
Z
zirui.chen 已提交
51
    FAISS_IVFPQ_MIX,
52
    SPTAG_BKT_RNT_CPU,
X
xj.lin 已提交
53 54
};

55
class VecIndex;
56

57 58
using VecIndexPtr = std::shared_ptr<VecIndex>;

59
class VecIndex : public cache::DataObj {
X
MS-154  
xj.lin 已提交
60
 public:
61
    virtual Status
S
starlord 已提交
62 63
    BuildAll(const int64_t& nb, const float* xb, const int64_t* ids, const Config& cfg, const int64_t& nt = 0,
             const float* xt = nullptr) = 0;
64 65

    virtual Status
S
starlord 已提交
66
    Add(const int64_t& nb, const float* xb, const int64_t* ids, const Config& cfg = Config()) = 0;
67 68

    virtual Status
S
starlord 已提交
69
    Search(const int64_t& nq, const float* xq, float* dist, int64_t* ids, const Config& cfg = Config()) = 0;
70 71

    virtual VecIndexPtr
S
starlord 已提交
72
    CopyToGpu(const int64_t& device_id, const Config& cfg = Config()) = 0;
73 74

    virtual VecIndexPtr
S
starlord 已提交
75
    CopyToCpu(const Config& cfg = Config()) = 0;
76

X
xiaojun.lin 已提交
77
    // TODO(linxj): Deprecated
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    virtual VecIndexPtr
    Clone() = 0;

    virtual int64_t
    GetDeviceId() = 0;

    virtual IndexType
    GetType() = 0;

    virtual int64_t
    Dimension() = 0;

    virtual int64_t
    Count() = 0;

93 94 95
    int64_t
    Size() override;

96 97 98
    void
    set_size(int64_t size);

S
starlord 已提交
99
    virtual knowhere::BinarySet
100 101 102
    Serialize() = 0;

    virtual Status
S
starlord 已提交
103
    Load(const knowhere::BinarySet& index_binary) = 0;
X
xiaojun.lin 已提交
104 105

    // TODO(linxj): refactor later
X
xiaojun.lin 已提交
106
    ////////////////
X
xiaojun.lin 已提交
107
    virtual knowhere::QuantizerPtr
108
    LoadQuantizer(const Config& conf) {
W
add log  
wxyu 已提交
109
        ENGINE_LOG_ERROR << "LoadQuantizer virtual funciton called.";
110 111
        return nullptr;
    }
X
xiaojun.lin 已提交
112

W
wxyu 已提交
113
    virtual VecIndexPtr
114
    LoadData(const knowhere::QuantizerPtr& q, const Config& conf) {
W
wxyu 已提交
115
        return nullptr;
116
    }
X
xiaojun.lin 已提交
117 118

    virtual Status
119 120 121
    SetQuantizer(const knowhere::QuantizerPtr& q) {
        return Status::OK();
    }
J
JinHai-CN 已提交
122 123

    virtual Status
124 125 126
    UnsetQuantizer() {
        return Status::OK();
    }
W
wxyu 已提交
127 128 129 130 131

    virtual std::pair<VecIndexPtr, knowhere::QuantizerPtr>
    CopyToGpuWithQuantizer(const int64_t& device_id, const Config& cfg = Config()) {
        return std::make_pair(nullptr, nullptr);
    }
X
xiaojun.lin 已提交
132
    ////////////////
133 134
 private:
    int64_t size_ = 0;
X
MS-154  
xj.lin 已提交
135 136
};

137
extern Status
S
starlord 已提交
138
write_index(VecIndexPtr index, const std::string& location);
139 140

extern VecIndexPtr
S
starlord 已提交
141
read_index(const std::string& location);
X
xj.lin 已提交
142

143 144 145
VecIndexPtr
read_index(const std::string& location, knowhere::BinarySet& index_binary);

146
extern VecIndexPtr
S
starlord 已提交
147
GetVecIndexFactory(const IndexType& type, const Config& cfg = Config());
X
xj.lin 已提交
148

149
extern VecIndexPtr
150
LoadVecIndex(const IndexType& index_type, const knowhere::BinarySet& index_binary, int64_t size);
X
MS-154  
xj.lin 已提交
151

152
extern IndexType
S
starlord 已提交
153
ConvertToCpuIndexType(const IndexType& type);
154

155
extern IndexType
S
starlord 已提交
156
ConvertToGpuIndexType(const IndexType& type);
W
wxyu 已提交
157

S
starlord 已提交
158 159
}  // namespace engine
}  // namespace milvus