VecIndex.cpp 8.5 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.

S
starlord 已提交
18
#include "knowhere/common/Exception.h"
X
xiaojun.lin 已提交
19
#include "knowhere/index/vector_index/IndexGPUIVF.h"
X
xiaojun.lin 已提交
20
#include "knowhere/index/vector_index/IndexGPUIVFPQ.h"
S
starlord 已提交
21
#include "knowhere/index/vector_index/IndexGPUIVFSQ.h"
X
xiaojun.lin 已提交
22
#include "knowhere/index/vector_index/IndexIDMAP.h"
S
starlord 已提交
23 24 25
#include "knowhere/index/vector_index/IndexIVF.h"
#include "knowhere/index/vector_index/IndexIVFPQ.h"
#include "knowhere/index/vector_index/IndexIVFSQ.h"
26
#include "knowhere/index/vector_index/IndexIVFSQHybrid.h"
X
xiaojun.lin 已提交
27 28
#include "knowhere/index/vector_index/IndexKDT.h"
#include "knowhere/index/vector_index/IndexNSG.h"
X
xiaojun.lin 已提交
29 30
#include "wrapper/VecIndex.h"
#include "VecImpl.h"
S
starlord 已提交
31
#include "utils/Log.h"
X
MS-154  
xj.lin 已提交
32

33 34
#include <cuda.h>

X
xj.lin 已提交
35
namespace milvus {
X
MS-154  
xj.lin 已提交
36 37
namespace engine {

38 39 40 41 42
int64_t
VecIndex::Size() {
    return Count() * Dimension() * sizeof(float);
}

X
xj.lin 已提交
43 44 45 46
struct FileIOReader {
    std::fstream fs;
    std::string name;

S
starlord 已提交
47
    explicit FileIOReader(const std::string& fname);
48

X
xj.lin 已提交
49
    ~FileIOReader();
50 51

    size_t
S
starlord 已提交
52
    operator()(void* ptr, size_t size);
53 54

    size_t
S
starlord 已提交
55
    operator()(void* ptr, size_t size, size_t pos);
X
xj.lin 已提交
56 57
};

S
starlord 已提交
58
FileIOReader::FileIOReader(const std::string& fname) {
X
xj.lin 已提交
59 60 61 62 63 64 65 66
    name = fname;
    fs = std::fstream(name, std::ios::in | std::ios::binary);
}

FileIOReader::~FileIOReader() {
    fs.close();
}

67
size_t
S
starlord 已提交
68 69
FileIOReader::operator()(void* ptr, size_t size) {
    fs.read(reinterpret_cast<char*>(ptr), size);
X
xj.lin 已提交
70 71
}

72
size_t
S
starlord 已提交
73
FileIOReader::operator()(void* ptr, size_t size, size_t pos) {
X
xj.lin 已提交
74 75 76
    return 0;
}

77 78 79 80
struct FileIOWriter {
    std::fstream fs;
    std::string name;

S
starlord 已提交
81
    explicit FileIOWriter(const std::string& fname);
82
    ~FileIOWriter();
S
starlord 已提交
83 84
    size_t
    operator()(void* ptr, size_t size);
85 86
};

S
starlord 已提交
87
FileIOWriter::FileIOWriter(const std::string& fname) {
X
xj.lin 已提交
88 89 90 91 92 93 94 95
    name = fname;
    fs = std::fstream(name, std::ios::out | std::ios::binary);
}

FileIOWriter::~FileIOWriter() {
    fs.close();
}

96
size_t
S
starlord 已提交
97 98
FileIOWriter::operator()(void* ptr, size_t size) {
    fs.write(reinterpret_cast<char*>(ptr), size);
X
xj.lin 已提交
99 100
}

101
VecIndexPtr
S
starlord 已提交
102
GetVecIndexFactory(const IndexType& type, const Config& cfg) {
S
starlord 已提交
103
    std::shared_ptr<knowhere::VectorIndex> index;
S
starlord 已提交
104
    auto gpu_device = -1;  // TODO(linxj): remove hardcode here
X
xj.lin 已提交
105 106
    switch (type) {
        case IndexType::FAISS_IDMAP: {
S
starlord 已提交
107
            index = std::make_shared<knowhere::IDMAP>();
X
xj.lin 已提交
108 109 110
            return std::make_shared<BFIndex>(index);
        }
        case IndexType::FAISS_IVFFLAT_CPU: {
S
starlord 已提交
111
            index = std::make_shared<knowhere::IVF>();
X
xj.lin 已提交
112 113 114
            break;
        }
        case IndexType::FAISS_IVFFLAT_GPU: {
S
starlord 已提交
115
            index = std::make_shared<knowhere::GPUIVF>(gpu_device);
X
xj.lin 已提交
116 117
            break;
        }
X
xj.lin 已提交
118
        case IndexType::FAISS_IVFFLAT_MIX: {
S
starlord 已提交
119
            index = std::make_shared<knowhere::GPUIVF>(gpu_device);
X
xj.lin 已提交
120
            return std::make_shared<IVFMixIndex>(index, IndexType::FAISS_IVFFLAT_MIX);
X
xj.lin 已提交
121
        }
X
xj.lin 已提交
122
        case IndexType::FAISS_IVFPQ_CPU: {
S
starlord 已提交
123
            index = std::make_shared<knowhere::IVFPQ>();
X
xj.lin 已提交
124 125 126
            break;
        }
        case IndexType::FAISS_IVFPQ_GPU: {
S
starlord 已提交
127
            index = std::make_shared<knowhere::GPUIVFPQ>(gpu_device);
X
xj.lin 已提交
128 129
            break;
        }
X
xj.lin 已提交
130
        case IndexType::SPTAG_KDT_RNT_CPU: {
S
starlord 已提交
131
            index = std::make_shared<knowhere::CPUKDTRNG>();
X
xj.lin 已提交
132
            break;
X
xj.lin 已提交
133 134
        }
        case IndexType::FAISS_IVFSQ8_MIX: {
S
starlord 已提交
135
            index = std::make_shared<knowhere::GPUIVFSQ>(gpu_device);
X
xj.lin 已提交
136
            return std::make_shared<IVFMixIndex>(index, IndexType::FAISS_IVFSQ8_MIX);
X
xj.lin 已提交
137
        }
X
xj.lin 已提交
138
        case IndexType::FAISS_IVFSQ8_CPU: {
S
starlord 已提交
139
            index = std::make_shared<knowhere::IVFSQ>();
W
wxyu 已提交
140 141
            break;
        }
X
xj.lin 已提交
142
        case IndexType::FAISS_IVFSQ8_GPU: {
S
starlord 已提交
143
            index = std::make_shared<knowhere::GPUIVFSQ>(gpu_device);
X
xj.lin 已提交
144 145
            break;
        }
X
xiaojun.lin 已提交
146
#ifdef CUSTOMIZATION
X
xiaojun.lin 已提交
147
        case IndexType::FAISS_IVFSQ8_HYBRID: {
148
            index = std::make_shared<knowhere::IVFSQHybrid>(gpu_device);
W
wxyu 已提交
149
            return std::make_shared<IVFHybridIndex>(index, IndexType::FAISS_IVFSQ8_HYBRID);
X
xiaojun.lin 已提交
150
        }
X
xiaojun.lin 已提交
151
#endif
152
        case IndexType::NSG_MIX: {  // TODO(linxj): bug.
S
starlord 已提交
153
            index = std::make_shared<knowhere::NSG>(gpu_device);
X
xj.lin 已提交
154 155
            break;
        }
S
starlord 已提交
156
        default: { return nullptr; }
X
MS-154  
xj.lin 已提交
157
    }
X
xj.lin 已提交
158
    return std::make_shared<VecIndexImpl>(index, type);
X
MS-154  
xj.lin 已提交
159 160
}

161
VecIndexPtr
S
starlord 已提交
162
LoadVecIndex(const IndexType& index_type, const knowhere::BinarySet& index_binary) {
X
MS-154  
xj.lin 已提交
163
    auto index = GetVecIndexFactory(index_type);
X
xiaojun.lin 已提交
164 165
    if (index == nullptr) return nullptr;
    // else
X
MS-154  
xj.lin 已提交
166 167 168 169
    index->Load(index_binary);
    return index;
}

170
VecIndexPtr
S
starlord 已提交
171
read_index(const std::string& location) {
X
xj.lin 已提交
172 173 174
    knowhere::BinarySet load_data_list;
    FileIOReader reader(location);
    reader.fs.seekg(0, reader.fs.end);
175
    int64_t length = reader.fs.tellg();
176
    if (length <= 0) {
177 178 179
        return nullptr;
    }

X
xj.lin 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    reader.fs.seekg(0);

    size_t rp = 0;
    auto current_type = IndexType::INVALID;
    reader(&current_type, sizeof(current_type));
    rp += sizeof(current_type);
    while (rp < length) {
        size_t meta_length;
        reader(&meta_length, sizeof(meta_length));
        rp += sizeof(meta_length);
        reader.fs.seekg(rp);

        auto meta = new char[meta_length];
        reader(meta, meta_length);
        rp += meta_length;
        reader.fs.seekg(rp);

        size_t bin_length;
        reader(&bin_length, sizeof(bin_length));
        rp += sizeof(bin_length);
        reader.fs.seekg(rp);

        auto bin = new uint8_t[bin_length];
        reader(bin, bin_length);
        rp += bin_length;

        auto binptr = std::make_shared<uint8_t>();
        binptr.reset(bin);
        load_data_list.Append(std::string(meta, meta_length), binptr, bin_length);
209
        delete[] meta;
X
xj.lin 已提交
210 211 212 213 214
    }

    return LoadVecIndex(current_type, load_data_list);
}

215
Status
S
starlord 已提交
216
write_index(VecIndexPtr index, const std::string& location) {
X
xj.lin 已提交
217 218 219 220 221 222
    try {
        auto binaryset = index->Serialize();
        auto index_type = index->GetType();

        FileIOWriter writer(location);
        writer(&index_type, sizeof(IndexType));
S
starlord 已提交
223
        for (auto& iter : binaryset.binary_map_) {
X
xj.lin 已提交
224 225 226
            auto meta = iter.first.c_str();
            size_t meta_length = iter.first.length();
            writer(&meta_length, sizeof(meta_length));
S
starlord 已提交
227
            writer((void*)meta, meta_length);
X
xj.lin 已提交
228 229 230 231

            auto binary = iter.second;
            int64_t binary_length = binary->size;
            writer(&binary_length, sizeof(binary_length));
S
starlord 已提交
232
            writer((void*)binary->data.get(), binary_length);
X
xj.lin 已提交
233
        }
S
starlord 已提交
234
    } catch (knowhere::KnowhereException& e) {
X
xj.lin 已提交
235
        WRAPPER_LOG_ERROR << e.what();
236
        return Status(KNOWHERE_UNEXPECTED_ERROR, e.what());
S
starlord 已提交
237
    } catch (std::exception& e) {
X
xj.lin 已提交
238
        WRAPPER_LOG_ERROR << e.what();
239 240 241
        std::string estring(e.what());
        if (estring.find("No space left on device") != estring.npos) {
            WRAPPER_LOG_ERROR << "No space left on the device";
242
            return Status(KNOWHERE_NO_SPACE, "No space left on the device");
243
        } else {
244
            return Status(KNOWHERE_ERROR, e.what());
245
        }
X
xj.lin 已提交
246
    }
247
    return Status::OK();
X
xj.lin 已提交
248 249
}

250
IndexType
S
starlord 已提交
251
ConvertToCpuIndexType(const IndexType& type) {
X
xj.lin 已提交
252
    // TODO(linxj): add IDMAP
W
wxyu 已提交
253
    switch (type) {
X
xj.lin 已提交
254
        case IndexType::FAISS_IVFFLAT_GPU:
W
wxyu 已提交
255 256 257
        case IndexType::FAISS_IVFFLAT_MIX: {
            return IndexType::FAISS_IVFFLAT_CPU;
        }
X
xj.lin 已提交
258
        case IndexType::FAISS_IVFSQ8_GPU:
W
wxyu 已提交
259
        case IndexType::FAISS_IVFSQ8_MIX: {
X
xj.lin 已提交
260
            return IndexType::FAISS_IVFSQ8_CPU;
W
wxyu 已提交
261
        }
S
starlord 已提交
262
        default: { return type; }
W
wxyu 已提交
263 264 265
    }
}

266
IndexType
S
starlord 已提交
267
ConvertToGpuIndexType(const IndexType& type) {
X
xj.lin 已提交
268 269 270 271 272 273 274 275 276
    switch (type) {
        case IndexType::FAISS_IVFFLAT_MIX:
        case IndexType::FAISS_IVFFLAT_CPU: {
            return IndexType::FAISS_IVFFLAT_GPU;
        }
        case IndexType::FAISS_IVFSQ8_MIX:
        case IndexType::FAISS_IVFSQ8_CPU: {
            return IndexType::FAISS_IVFSQ8_GPU;
        }
S
starlord 已提交
277
        default: { return type; }
X
xj.lin 已提交
278 279 280
    }
}

S
starlord 已提交
281 282
}  // namespace engine
}  // namespace milvus