VecIndex.h 3.9 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

X
xj.lin 已提交
36 37 38 39 40
enum class IndexType {
    INVALID = 0,
    FAISS_IDMAP = 1,
    FAISS_IVFFLAT_CPU,
    FAISS_IVFFLAT_GPU,
S
starlord 已提交
41
    FAISS_IVFFLAT_MIX,  // build on gpu and search on cpu
X
xj.lin 已提交
42 43 44
    FAISS_IVFPQ_CPU,
    FAISS_IVFPQ_GPU,
    SPTAG_KDT_RNT_CPU,
X
xj.lin 已提交
45
    FAISS_IVFSQ8_MIX,
X
xj.lin 已提交
46 47
    FAISS_IVFSQ8_CPU,
    FAISS_IVFSQ8_GPU,
48
    FAISS_IVFSQ8_HYBRID,  // only support build on gpu.
X
xj.lin 已提交
49
    NSG_MIX,
X
xj.lin 已提交
50 51
};

52
class VecIndex;
53

54 55
using VecIndexPtr = std::shared_ptr<VecIndex>;

56
class VecIndex : public cache::DataObj {
X
MS-154  
xj.lin 已提交
57
 public:
58
    virtual Status
S
starlord 已提交
59 60
    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;
61 62

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

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

    virtual VecIndexPtr
S
starlord 已提交
69
    CopyToGpu(const int64_t& device_id, const Config& cfg = Config()) = 0;
70 71

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

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

    virtual int64_t
    GetDeviceId() = 0;

    virtual IndexType
    GetType() = 0;

    virtual int64_t
    Dimension() = 0;

    virtual int64_t
    Count() = 0;

90 91 92
    int64_t
    Size() override;

93 94 95
    void
    set_size(int64_t size);

S
starlord 已提交
96
    virtual knowhere::BinarySet
97 98 99
    Serialize() = 0;

    virtual Status
S
starlord 已提交
100
    Load(const knowhere::BinarySet& index_binary) = 0;
X
xiaojun.lin 已提交
101 102

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

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

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

    virtual Status
121 122 123
    UnsetQuantizer() {
        return Status::OK();
    }
W
wxyu 已提交
124 125 126 127 128

    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 已提交
129
    ////////////////
130 131
 private:
    int64_t size_ = 0;
X
MS-154  
xj.lin 已提交
132 133
};

134
extern Status
S
starlord 已提交
135
write_index(VecIndexPtr index, const std::string& location);
136 137

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

140
extern VecIndexPtr
S
starlord 已提交
141
GetVecIndexFactory(const IndexType& type, const Config& cfg = Config());
X
xj.lin 已提交
142

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

146
extern IndexType
S
starlord 已提交
147
ConvertToCpuIndexType(const IndexType& type);
148

149
extern IndexType
S
starlord 已提交
150
ConvertToGpuIndexType(const IndexType& type);
W
wxyu 已提交
151

S
starlord 已提交
152 153
}  // namespace engine
}  // namespace milvus