vec_impl.cpp 9.3 KB
Newer Older
X
MS-154  
xj.lin 已提交
1 2 3 4 5 6
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////

X
xj.lin 已提交
7
#include <src/utils/Log.h>
X
xj.lin 已提交
8
#include "knowhere/index/vector_index/idmap.h"
X
xj.lin 已提交
9
#include "knowhere/index/vector_index/gpu_ivf.h"
X
xj.lin 已提交
10
#include "knowhere/common/exception.h"
11
#include "knowhere/index/vector_index/cloner.h"
X
MS-154  
xj.lin 已提交
12 13 14

#include "vec_impl.h"
#include "data_transfer.h"
X
xj.lin 已提交
15
#include "wrapper_log.h"
X
MS-154  
xj.lin 已提交
16 17 18


namespace zilliz {
X
xj.lin 已提交
19
namespace milvus {
X
MS-154  
xj.lin 已提交
20 21 22 23
namespace engine {

using namespace zilliz::knowhere;

X
xj.lin 已提交
24 25 26 27 28 29 30 31 32 33 34 35
server::KnowhereError VecIndexImpl::BuildAll(const long &nb,
                                             const float *xb,
                                             const long *ids,
                                             const Config &cfg,
                                             const long &nt,
                                             const float *xt) {
    try {
        dim = cfg["dim"].as<int>();
        auto dataset = GenDatasetWithIds(nb, dim, xb, ids);

        auto preprocessor = index_->BuildPreprocessor(dataset, cfg);
        index_->set_preprocessor(preprocessor);
X
xj.lin 已提交
36
        auto model = index_->Train(dataset, cfg);
X
xj.lin 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        index_->set_index_model(model);
        index_->Add(dataset, cfg);
    } catch (KnowhereException &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_UNEXPECTED_ERROR;
    } catch (jsoncons::json_exception &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_INVALID_ARGUMENT;
    } catch (std::exception &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_ERROR;
    }
    return server::KNOWHERE_SUCCESS;
}

server::KnowhereError VecIndexImpl::Add(const long &nb, const float *xb, const long *ids, const Config &cfg) {
    try {
X
xj.lin 已提交
54
        auto dataset = GenDatasetWithIds(nb, dim, xb, ids);
X
xj.lin 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

        index_->Add(dataset, cfg);
    } catch (KnowhereException &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_UNEXPECTED_ERROR;
    } catch (jsoncons::json_exception &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_INVALID_ARGUMENT;
    } catch (std::exception &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_ERROR;
    }
    return server::KNOWHERE_SUCCESS;
}

server::KnowhereError VecIndexImpl::Search(const long &nq, const float *xq, float *dist, long *ids, const Config &cfg) {
    try {
        auto k = cfg["k"].as<int>();
X
xj.lin 已提交
73
        auto dataset = GenDataset(nq, dim, xq);
X
xj.lin 已提交
74

75 76
        Config search_cfg = cfg;

77
        ParameterValidation(type, search_cfg);
78 79

        auto res = index_->Search(dataset, search_cfg);
X
xj.lin 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        auto ids_array = res->array()[0];
        auto dis_array = res->array()[1];

        //{
        //    auto& ids = ids_array;
        //    auto& dists = dis_array;
        //    std::stringstream ss_id;
        //    std::stringstream ss_dist;
        //    for (auto i = 0; i < 10; i++) {
        //        for (auto j = 0; j < k; ++j) {
        //            ss_id << *(ids->data()->GetValues<int64_t>(1, i * k + j)) << " ";
        //            ss_dist << *(dists->data()->GetValues<float>(1, i * k + j)) << " ";
        //        }
        //        ss_id << std::endl;
        //        ss_dist << std::endl;
        //    }
        //    std::cout << "id\n" << ss_id.str() << std::endl;
        //    std::cout << "dist\n" << ss_dist.str() << std::endl;
        //}

        auto p_ids = ids_array->data()->GetValues<int64_t>(1, 0);
        auto p_dist = dis_array->data()->GetValues<float>(1, 0);

        // TODO(linxj): avoid copy here.
        memcpy(ids, p_ids, sizeof(int64_t) * nq * k);
        memcpy(dist, p_dist, sizeof(float) * nq * k);
    } catch (KnowhereException &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_UNEXPECTED_ERROR;
    } catch (jsoncons::json_exception &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_INVALID_ARGUMENT;
    } catch (std::exception &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_ERROR;
    }
    return server::KNOWHERE_SUCCESS;
X
MS-154  
xj.lin 已提交
117 118 119
}

zilliz::knowhere::BinarySet VecIndexImpl::Serialize() {
X
xj.lin 已提交
120
    type = ConvertToCpuIndexType(type);
X
MS-154  
xj.lin 已提交
121 122 123
    return index_->Serialize();
}

X
xj.lin 已提交
124
server::KnowhereError VecIndexImpl::Load(const zilliz::knowhere::BinarySet &index_binary) {
X
MS-154  
xj.lin 已提交
125
    index_->Load(index_binary);
X
xj.lin 已提交
126
    dim = Dimension();
X
xj.lin 已提交
127
    return server::KNOWHERE_SUCCESS;
X
MS-154  
xj.lin 已提交
128 129
}

X
xj.lin 已提交
130 131 132 133 134 135 136 137
int64_t VecIndexImpl::Dimension() {
    return index_->Dimension();
}

int64_t VecIndexImpl::Count() {
    return index_->Count();
}

X
xj.lin 已提交
138 139 140 141
IndexType VecIndexImpl::GetType() {
    return type;
}

142
VecIndexPtr VecIndexImpl::CopyToGpu(const int64_t &device_id, const Config &cfg) {
X
xj.lin 已提交
143
    // TODO(linxj): exception handle
144
    auto gpu_index = zilliz::knowhere::CopyCpuToGpu(index_, device_id, cfg);
X
xj.lin 已提交
145
    auto new_index = std::make_shared<VecIndexImpl>(gpu_index, ConvertToGpuIndexType(type));
W
wxyu 已提交
146 147
    new_index->dim = dim;
    return new_index;
148 149 150
}

VecIndexPtr VecIndexImpl::CopyToCpu(const Config &cfg) {
X
xj.lin 已提交
151
    // TODO(linxj): exception handle
152
    auto cpu_index = zilliz::knowhere::CopyGpuToCpu(index_, cfg);
X
xj.lin 已提交
153 154 155
    auto new_index = std::make_shared<VecIndexImpl>(cpu_index, ConvertToCpuIndexType(type));
    new_index->dim = dim;
    return new_index;
156 157
}

158
VecIndexPtr VecIndexImpl::Clone() {
X
xj.lin 已提交
159
    // TODO(linxj): exception handle
160 161 162 163 164 165 166 167 168
    auto clone_index = std::make_shared<VecIndexImpl>(index_->Clone(), type);
    clone_index->dim = dim;
    return clone_index;
}

int64_t VecIndexImpl::GetDeviceId() {
    if (auto device_idx = std::dynamic_pointer_cast<GPUIndex>(index_)){
        return device_idx->GetGpuDevice();
    }
X
xj.lin 已提交
169 170
    // else
    return -1; // -1 == cpu
171 172
}

X
xj.lin 已提交
173
float *BFIndex::GetRawVectors() {
X
xj.lin 已提交
174 175 176
    auto raw_index = std::dynamic_pointer_cast<IDMAP>(index_);
    if (raw_index) { return raw_index->GetRawVectors(); }
    return nullptr;
X
xj.lin 已提交
177 178 179 180 181 182
}

int64_t *BFIndex::GetRawIds() {
    return std::static_pointer_cast<IDMAP>(index_)->GetRawIds();
}

X
xj.lin 已提交
183
server::KnowhereError BFIndex::Build(const Config &cfg) {
X
xj.lin 已提交
184
    try {
X
xj.lin 已提交
185 186
        dim = cfg["dim"].as<int>();
        std::static_pointer_cast<IDMAP>(index_)->Train(cfg);
X
xj.lin 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    } catch (KnowhereException &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_UNEXPECTED_ERROR;
    } catch (jsoncons::json_exception &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_INVALID_ARGUMENT;
    } catch (std::exception &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_ERROR;
    }
    return server::KNOWHERE_SUCCESS;
}

server::KnowhereError BFIndex::BuildAll(const long &nb,
                                        const float *xb,
                                        const long *ids,
                                        const Config &cfg,
                                        const long &nt,
                                        const float *xt) {
    try {
        dim = cfg["dim"].as<int>();
        auto dataset = GenDatasetWithIds(nb, dim, xb, ids);

X
xj.lin 已提交
210
        std::static_pointer_cast<IDMAP>(index_)->Train(cfg);
X
xj.lin 已提交
211 212 213 214 215 216 217 218 219 220 221 222
        index_->Add(dataset, cfg);
    } catch (KnowhereException &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_UNEXPECTED_ERROR;
    } catch (jsoncons::json_exception &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_INVALID_ARGUMENT;
    } catch (std::exception &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_ERROR;
    }
    return server::KNOWHERE_SUCCESS;
X
xj.lin 已提交
223 224
}

X
xj.lin 已提交
225
// TODO(linxj): add lock here.
X
xj.lin 已提交
226 227 228 229 230 231 232 233 234 235 236 237
server::KnowhereError IVFMixIndex::BuildAll(const long &nb,
                                            const float *xb,
                                            const long *ids,
                                            const Config &cfg,
                                            const long &nt,
                                            const float *xt) {
    try {
        dim = cfg["dim"].as<int>();
        auto dataset = GenDatasetWithIds(nb, dim, xb, ids);

        auto preprocessor = index_->BuildPreprocessor(dataset, cfg);
        index_->set_preprocessor(preprocessor);
X
xj.lin 已提交
238
        auto model = index_->Train(dataset, cfg);
X
xj.lin 已提交
239 240 241 242
        index_->set_index_model(model);
        index_->Add(dataset, cfg);

        if (auto device_index = std::dynamic_pointer_cast<GPUIVF>(index_)) {
W
wxyu 已提交
243
            auto host_index = device_index->CopyGpuToCpu(Config());
X
xj.lin 已提交
244
            index_ = host_index;
X
xj.lin 已提交
245
            type = ConvertToCpuIndexType(type);
X
xj.lin 已提交
246 247
        } else {
            WRAPPER_LOG_ERROR << "Build IVFMIXIndex Failed";
X
xj.lin 已提交
248
            return server::KNOWHERE_ERROR;
X
xj.lin 已提交
249 250 251 252 253 254 255 256 257 258
        }
    } catch (KnowhereException &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_UNEXPECTED_ERROR;
    } catch (jsoncons::json_exception &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_INVALID_ARGUMENT;
    } catch (std::exception &e) {
        WRAPPER_LOG_ERROR << e.what();
        return server::KNOWHERE_ERROR;
X
xj.lin 已提交
259
    }
X
xj.lin 已提交
260
    return server::KNOWHERE_SUCCESS;
X
xj.lin 已提交
261 262
}

X
xj.lin 已提交
263
server::KnowhereError IVFMixIndex::Load(const zilliz::knowhere::BinarySet &index_binary) {
X
xj.lin 已提交
264
    //index_ = std::make_shared<IVF>();
X
xj.lin 已提交
265 266
    index_->Load(index_binary);
    dim = Dimension();
X
xj.lin 已提交
267
    return server::KNOWHERE_SUCCESS;
X
xj.lin 已提交
268 269
}

X
MS-154  
xj.lin 已提交
270 271 272
}
}
}