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

32 33
#include <cuda.h>

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

X
xj.lin 已提交
38 39 40 41
struct FileIOReader {
    std::fstream fs;
    std::string name;

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

X
xj.lin 已提交
44
    ~FileIOReader();
45 46 47 48 49 50

    size_t
    operator()(void *ptr, size_t size);

    size_t
    operator()(void *ptr, size_t size, size_t pos);
X
xj.lin 已提交
51 52 53 54 55 56 57 58 59 60 61
};

FileIOReader::FileIOReader(const std::string &fname) {
    name = fname;
    fs = std::fstream(name, std::ios::in | std::ios::binary);
}

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

62 63
size_t
FileIOReader::operator()(void *ptr, size_t size) {
X
xj.lin 已提交
64 65 66
    fs.read(reinterpret_cast<char *>(ptr), size);
}

67 68
size_t
FileIOReader::operator()(void *ptr, size_t size, size_t pos) {
X
xj.lin 已提交
69 70 71
    return 0;
}

72 73 74 75
struct FileIOWriter {
    std::fstream fs;
    std::string name;

S
starlord 已提交
76
    explicit FileIOWriter(const std::string &fname);
77 78 79 80
    ~FileIOWriter();
    size_t operator()(void *ptr, size_t size);
};

X
xj.lin 已提交
81 82 83 84 85 86 87 88 89
FileIOWriter::FileIOWriter(const std::string &fname) {
    name = fname;
    fs = std::fstream(name, std::ios::out | std::ios::binary);
}

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

90 91
size_t
FileIOWriter::operator()(void *ptr, size_t size) {
X
xj.lin 已提交
92 93 94
    fs.write(reinterpret_cast<char *>(ptr), size);
}

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

151 152
VecIndexPtr
LoadVecIndex(const IndexType &index_type, const zilliz::knowhere::BinarySet &index_binary) {
X
MS-154  
xj.lin 已提交
153 154 155 156 157
    auto index = GetVecIndexFactory(index_type);
    index->Load(index_binary);
    return index;
}

158 159
VecIndexPtr
read_index(const std::string &location) {
X
xj.lin 已提交
160 161 162
    knowhere::BinarySet load_data_list;
    FileIOReader reader(location);
    reader.fs.seekg(0, reader.fs.end);
163
    int64_t length = reader.fs.tellg();
164
    if (length <= 0) {
165 166 167
        return nullptr;
    }

X
xj.lin 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    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);
197
        delete[] meta;
X
xj.lin 已提交
198 199 200 201 202
    }

    return LoadVecIndex(current_type, load_data_list);
}

203 204
Status
write_index(VecIndexPtr index, const std::string &location) {
X
xj.lin 已提交
205 206 207 208 209 210
    try {
        auto binaryset = index->Serialize();
        auto index_type = index->GetType();

        FileIOWriter writer(location);
        writer(&index_type, sizeof(IndexType));
S
starlord 已提交
211
        for (auto &iter : binaryset.binary_map_) {
X
xj.lin 已提交
212 213 214 215 216 217 218 219 220 221 222 223
            auto meta = iter.first.c_str();
            size_t meta_length = iter.first.length();
            writer(&meta_length, sizeof(meta_length));
            writer((void *) meta, meta_length);

            auto binary = iter.second;
            int64_t binary_length = binary->size;
            writer(&binary_length, sizeof(binary_length));
            writer((void *) binary->data.get(), binary_length);
        }
    } catch (knowhere::KnowhereException &e) {
        WRAPPER_LOG_ERROR << e.what();
224
        return Status(KNOWHERE_UNEXPECTED_ERROR, e.what());
X
xj.lin 已提交
225
    } catch (std::exception &e) {
X
xj.lin 已提交
226
        WRAPPER_LOG_ERROR << e.what();
227 228 229
        std::string estring(e.what());
        if (estring.find("No space left on device") != estring.npos) {
            WRAPPER_LOG_ERROR << "No space left on the device";
230
            return Status(KNOWHERE_NO_SPACE, "No space left on the device");
231
        } else {
232
            return Status(KNOWHERE_ERROR, e.what());
233
        }
X
xj.lin 已提交
234
    }
235
    return Status::OK();
X
xj.lin 已提交
236 237
}

238 239
IndexType
ConvertToCpuIndexType(const IndexType &type) {
X
xj.lin 已提交
240
    // TODO(linxj): add IDMAP
W
wxyu 已提交
241
    switch (type) {
X
xj.lin 已提交
242
        case IndexType::FAISS_IVFFLAT_GPU:
W
wxyu 已提交
243 244 245
        case IndexType::FAISS_IVFFLAT_MIX: {
            return IndexType::FAISS_IVFFLAT_CPU;
        }
X
xj.lin 已提交
246
        case IndexType::FAISS_IVFSQ8_GPU:
W
wxyu 已提交
247
        case IndexType::FAISS_IVFSQ8_MIX: {
X
xj.lin 已提交
248
            return IndexType::FAISS_IVFSQ8_CPU;
W
wxyu 已提交
249 250
        }
        default: {
X
xj.lin 已提交
251
            return type;
W
wxyu 已提交
252 253 254 255
        }
    }
}

256 257
IndexType
ConvertToGpuIndexType(const IndexType &type) {
X
xj.lin 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    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;
        }
        default: {
            return type;
        }
    }
}

S
starlord 已提交
273 274 275
} // namespace engine
} // namespace milvus
} // namespace zilliz