vec_impl.cpp 9.0 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;

S
starlord 已提交
24
ErrorCode VecIndexImpl::BuildAll(const long &nb,
X
xj.lin 已提交
25 26 27 28 29 30 31 32 33 34 35
                                             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
        index_->set_index_model(model);
        index_->Add(dataset, cfg);
    } catch (KnowhereException &e) {
        WRAPPER_LOG_ERROR << e.what();
S
starlord 已提交
41
        return KNOWHERE_UNEXPECTED_ERROR;
X
xj.lin 已提交
42 43
    } catch (jsoncons::json_exception &e) {
        WRAPPER_LOG_ERROR << e.what();
S
starlord 已提交
44
        return KNOWHERE_INVALID_ARGUMENT;
X
xj.lin 已提交
45 46
    } catch (std::exception &e) {
        WRAPPER_LOG_ERROR << e.what();
S
starlord 已提交
47
        return KNOWHERE_ERROR;
X
xj.lin 已提交
48
    }
S
starlord 已提交
49
    return KNOWHERE_SUCCESS;
X
xj.lin 已提交
50 51
}

S
starlord 已提交
52
ErrorCode VecIndexImpl::Add(const long &nb, const float *xb, const long *ids, const Config &cfg) {
X
xj.lin 已提交
53
    try {
X
xj.lin 已提交
54
        auto dataset = GenDatasetWithIds(nb, dim, xb, ids);
X
xj.lin 已提交
55 56 57 58

        index_->Add(dataset, cfg);
    } catch (KnowhereException &e) {
        WRAPPER_LOG_ERROR << e.what();
S
starlord 已提交
59
        return KNOWHERE_UNEXPECTED_ERROR;
X
xj.lin 已提交
60 61
    } catch (jsoncons::json_exception &e) {
        WRAPPER_LOG_ERROR << e.what();
S
starlord 已提交
62
        return KNOWHERE_INVALID_ARGUMENT;
X
xj.lin 已提交
63 64
    } catch (std::exception &e) {
        WRAPPER_LOG_ERROR << e.what();
S
starlord 已提交
65
        return KNOWHERE_ERROR;
X
xj.lin 已提交
66
    }
S
starlord 已提交
67
    return KNOWHERE_SUCCESS;
X
xj.lin 已提交
68 69
}

S
starlord 已提交
70
ErrorCode VecIndexImpl::Search(const long &nq, const float *xq, float *dist, long *ids, const Config &cfg) {
X
xj.lin 已提交
71 72
    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
        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);
J
jinhai 已提交
106

X
xj.lin 已提交
107 108
    } catch (KnowhereException &e) {
        WRAPPER_LOG_ERROR << e.what();
S
starlord 已提交
109
        return KNOWHERE_UNEXPECTED_ERROR;
X
xj.lin 已提交
110 111
    } catch (jsoncons::json_exception &e) {
        WRAPPER_LOG_ERROR << e.what();
S
starlord 已提交
112
        return KNOWHERE_INVALID_ARGUMENT;
X
xj.lin 已提交
113 114
    } catch (std::exception &e) {
        WRAPPER_LOG_ERROR << e.what();
S
starlord 已提交
115
        return KNOWHERE_ERROR;
X
xj.lin 已提交
116
    }
S
starlord 已提交
117
    return KNOWHERE_SUCCESS;
X
MS-154  
xj.lin 已提交
118 119 120
}

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

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

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

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

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

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

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

159
VecIndexPtr VecIndexImpl::Clone() {
X
xj.lin 已提交
160
    // TODO(linxj): exception handle
161 162 163 164 165 166 167 168 169
    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 已提交
170 171
    // else
    return -1; // -1 == cpu
172 173
}

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

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

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

S
starlord 已提交
201
ErrorCode BFIndex::BuildAll(const long &nb,
X
xj.lin 已提交
202 203 204 205 206 207 208 209 210
                                        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 已提交
211
        std::static_pointer_cast<IDMAP>(index_)->Train(cfg);
X
xj.lin 已提交
212 213 214
        index_->Add(dataset, cfg);
    } catch (KnowhereException &e) {
        WRAPPER_LOG_ERROR << e.what();
S
starlord 已提交
215
        return KNOWHERE_UNEXPECTED_ERROR;
X
xj.lin 已提交
216 217
    } catch (jsoncons::json_exception &e) {
        WRAPPER_LOG_ERROR << e.what();
S
starlord 已提交
218
        return KNOWHERE_INVALID_ARGUMENT;
X
xj.lin 已提交
219 220
    } catch (std::exception &e) {
        WRAPPER_LOG_ERROR << e.what();
S
starlord 已提交
221
        return KNOWHERE_ERROR;
X
xj.lin 已提交
222
    }
S
starlord 已提交
223
    return KNOWHERE_SUCCESS;
X
xj.lin 已提交
224 225
}

X
xj.lin 已提交
226
// TODO(linxj): add lock here.
S
starlord 已提交
227
ErrorCode IVFMixIndex::BuildAll(const long &nb,
X
xj.lin 已提交
228 229 230 231 232 233 234 235 236 237 238
                                            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 已提交
239
        auto model = index_->Train(dataset, cfg);
X
xj.lin 已提交
240 241 242 243
        index_->set_index_model(model);
        index_->Add(dataset, cfg);

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

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

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