ExecutionEngine.h 3.9 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed 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
J
jinhai 已提交
5
//
6 7 8 9 10
// 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.
J
jinhai 已提交
11

X
Xu Peng 已提交
12 13
#pragma once

X
Xu Peng 已提交
14
#include <memory>
S
starlord 已提交
15
#include <string>
16
#include <unordered_map>
S
starlord 已提交
17
#include <vector>
X
Xu Peng 已提交
18

19 20 21
#include <faiss/utils/ConcurrentBitset.h>

#include "query/GeneralQuery.h"
22
#include "utils/Json.h"
T
Tinkerrr 已提交
23 24
#include "utils/Status.h"

J
jinhai 已提交
25
namespace milvus {
X
Xu Peng 已提交
26 27
namespace engine {

Z
zirui.chen 已提交
28
// TODO(linxj): replace with VecIndex::IndexType
G
groot 已提交
29
enum class EngineType {
G
groot 已提交
30
    INVALID = 0,
G
groot 已提交
31 32
    FAISS_IDMAP = 1,
    FAISS_IVFFLAT,
S
starlord 已提交
33
    FAISS_IVFSQ8,
X
xj.lin 已提交
34
    NSG_MIX,
W
wxyu 已提交
35
    FAISS_IVFSQ8H,
Z
zirui.chen 已提交
36
    FAISS_PQ,
37 38
    SPTAG_KDT,
    SPTAG_BKT,
G
groot 已提交
39 40
    FAISS_BIN_IDMAP,
    FAISS_BIN_IVFFLAT,
T
Tinkerrr 已提交
41
    HNSW,
O
op-hunter 已提交
42 43
    ANNOY,
    MAX_VALUE = ANNOY,
G
groot 已提交
44 45
};

46
enum class MetricType {
47 48 49 50 51 52 53 54
    L2 = 1,              // Euclidean Distance
    IP = 2,              // Cosine Similarity
    HAMMING = 3,         // Hamming Distance
    JACCARD = 4,         // Jaccard Distance
    TANIMOTO = 5,        // Tanimoto Distance
    SUBSTRUCTURE = 6,    // Substructure Distance
    SUPERSTRUCTURE = 7,  // Superstructure Distance
    MAX_VALUE = SUPERSTRUCTURE
55 56
};

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
enum class DataType {
    INT8 = 1,
    INT16 = 2,
    INT32 = 3,
    INT64 = 4,

    STRING = 20,

    BOOL = 30,

    FLOAT = 40,
    DOUBLE = 41,

    VECTOR = 100,
    UNKNOWN = 9999,
};

74
class ExecutionEngine {
S
starlord 已提交
75
 public:
S
starlord 已提交
76 77
    virtual Status
    AddWithIds(int64_t n, const float* xdata, const int64_t* xids) = 0;
78

G
groot 已提交
79 80 81
    virtual Status
    AddWithIds(int64_t n, const uint8_t* xdata, const int64_t* xids) = 0;

S
starlord 已提交
82 83
    virtual size_t
    Count() const = 0;
84

S
starlord 已提交
85 86
    virtual size_t
    Dimension() const = 0;
G
groot 已提交
87

S
starlord 已提交
88
    virtual size_t
89
    Size() const = 0;
90

S
starlord 已提交
91 92
    virtual Status
    Serialize() = 0;
93

S
starlord 已提交
94 95
    virtual Status
    Load(bool to_cache = true) = 0;
96

S
starlord 已提交
97
    virtual Status
W
wxyu 已提交
98
    CopyToGpu(uint64_t device_id, bool hybrid) = 0;
99

Y
Yu Kun 已提交
100 101 102
    virtual Status
    CopyToIndexFileToGpu(uint64_t device_id) = 0;

S
starlord 已提交
103 104
    virtual Status
    CopyToCpu() = 0;
105

106 107
    //    virtual std::shared_ptr<ExecutionEngine>
    //    Clone() = 0;
W
wxyu 已提交
108

109 110 111 112 113 114
    //    virtual Status
    //    Merge(const std::string& location) = 0;

    virtual Status
    GetVectorByID(const int64_t& id, float* vector, bool hybrid) = 0;

S
starlord 已提交
115
    virtual Status
116
    GetVectorByID(const int64_t& id, uint8_t* vector, bool hybrid) = 0;
117

118
    virtual Status
Y
yukun 已提交
119
    ExecBinaryQuery(query::GeneralQueryPtr general_query, faiss::ConcurrentBitsetPtr& bitset,
Y
yukun 已提交
120
                    std::unordered_map<std::string, DataType>& attr_type, std::string& vector_placeholder) = 0;
Y
yukun 已提交
121 122 123

    virtual Status
    HybridSearch(query::GeneralQueryPtr general_query, std::unordered_map<std::string, DataType>& attr_type,
Y
yukun 已提交
124
                 query::QueryPtr query_ptr, std::vector<float>& distances, std::vector<int64_t>& search_ids) = 0;
125

S
starlord 已提交
126
    virtual Status
127 128
    Search(int64_t n, const float* data, int64_t k, const milvus::json& extra_params, float* distances, int64_t* labels,
           bool hybrid) = 0;
129

G
groot 已提交
130
    virtual Status
131 132
    Search(int64_t n, const uint8_t* data, int64_t k, const milvus::json& extra_params, float* distances,
           int64_t* labels, bool hybrid) = 0;
G
groot 已提交
133

S
starlord 已提交
134 135
    virtual std::shared_ptr<ExecutionEngine>
    BuildIndex(const std::string& location, EngineType engine_type) = 0;
136

S
starlord 已提交
137 138
    virtual Status
    Cache() = 0;
Y
yu yunfeng 已提交
139

S
starlord 已提交
140 141
    virtual Status
    Init() = 0;
S
starlord 已提交
142

S
starlord 已提交
143 144
    virtual EngineType
    IndexEngineType() const = 0;
S
starlord 已提交
145

S
starlord 已提交
146 147
    virtual MetricType
    IndexMetricType() const = 0;
148

S
starlord 已提交
149 150
    virtual std::string
    GetLocation() const = 0;
151 152
};

G
groot 已提交
153 154
using ExecutionEnginePtr = std::shared_ptr<ExecutionEngine>;

S
starlord 已提交
155 156
}  // namespace engine
}  // namespace milvus