FaissExecutionEngine.cpp 3.7 KB
Newer Older
X
Xu Peng 已提交
1 2 3 4 5
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/
X
Xu Peng 已提交
6 7
#include <easylogging++.h>
#include <faiss/AutoTune.h>
8 9 10
#include <faiss/MetaIndexes.h>
#include <faiss/IndexFlat.h>
#include <faiss/index_io.h>
X
Xu Peng 已提交
11
#include <wrapper/Index.h>
X
Xu Peng 已提交
12
#include <wrapper/IndexBuilder.h>
X
Xu Peng 已提交
13
#include <cache/CpuCacheMgr.h>
X
Xu Peng 已提交
14

X
Xu Peng 已提交
15
#include "FaissExecutionEngine.h"
X
Xu Peng 已提交
16 17 18 19 20

namespace zilliz {
namespace vecwise {
namespace engine {

X
Xu Peng 已提交
21
const std::string RawIndexType = "IDMap,Flat";
X
xj.lin 已提交
22
const std::string BuildIndexType = "IVF"; // IDMap / IVF
X
Xu Peng 已提交
23

24

X
Xu Peng 已提交
25
FaissExecutionEngine::FaissExecutionEngine(uint16_t dimension, const std::string& location)
X
Xu Peng 已提交
26 27 28 29 30 31
    : pIndex_(faiss::index_factory(dimension, RawIndexType.c_str())),
      location_(location) {
}

FaissExecutionEngine::FaissExecutionEngine(std::shared_ptr<faiss::Index> index, const std::string& location)
    : pIndex_(index),
X
Xu Peng 已提交
32
      location_(location) {
X
Xu Peng 已提交
33 34
}

X
Xu Peng 已提交
35
Status FaissExecutionEngine::AddWithIds(long n, const float *xdata, const long *xids) {
X
Xu Peng 已提交
36
    pIndex_->add_with_ids(n, xdata, xids);
X
Xu Peng 已提交
37 38 39
    return Status::OK();
}

X
Xu Peng 已提交
40
size_t FaissExecutionEngine::Count() const {
X
Xu Peng 已提交
41 42 43
    return (size_t)(pIndex_->ntotal);
}

X
Xu Peng 已提交
44
size_t FaissExecutionEngine::Size() const {
X
Xu Peng 已提交
45 46 47
    return (size_t)(Count() * pIndex_->d);
}

48 49 50 51
size_t FaissExecutionEngine::PhysicalSize() const {
    return (size_t)(Size()*sizeof(float));
}

X
Xu Peng 已提交
52
Status FaissExecutionEngine::Serialize() {
X
Xu Peng 已提交
53 54
    write_index(pIndex_.get(), location_.c_str());
    return Status::OK();
X
Xu Peng 已提交
55 56
}

X
Xu Peng 已提交
57 58 59 60
Status FaissExecutionEngine::Load() {
    auto index  = zilliz::vecwise::cache::CpuCacheMgr::GetInstance()->GetIndex(location_);
    if (!index) {
        index = read_index(location_);
61 62
        Cache();
        LOG(DEBUG) << "Disk io from: " << location_;
X
Xu Peng 已提交
63 64 65 66 67 68
    }

    pIndex_ = index->data();
    return Status::OK();
}

69 70 71 72 73 74 75 76 77 78 79 80 81 82
Status FaissExecutionEngine::Merge(const std::string& location) {
    if (location == location_) {
        return Status::Error("Cannot Merge Self");
    }
    auto to_merge = zilliz::vecwise::cache::CpuCacheMgr::GetInstance()->GetIndex(location);
    if (!to_merge) {
        to_merge = read_index(location);
    }
    auto file_index = dynamic_cast<faiss::IndexIDMap*>(to_merge->data().get());
    pIndex_->add_with_ids(file_index->ntotal, dynamic_cast<faiss::IndexFlat*>(file_index->index)->xb.data(),
            file_index->id_map.data());
    return Status::OK();
}

X
Xu Peng 已提交
83
FaissExecutionEngine::Ptr FaissExecutionEngine::BuildIndex(const std::string& location) {
X
Xu Peng 已提交
84 85 86 87 88 89 90 91 92 93 94
    auto opd = std::make_shared<Operand>();
    opd->d = pIndex_->d;
    opd->index_type = BuildIndexType;
    IndexBuilderPtr pBuilder = GetIndexBuilder(opd);

    auto from_index = dynamic_cast<faiss::IndexIDMap*>(pIndex_.get());

    auto index = pBuilder->build_all(from_index->ntotal,
            dynamic_cast<faiss::IndexFlat*>(from_index->index)->xb.data(),
            from_index->id_map.data());

X
Xu Peng 已提交
95
    Ptr new_ee(new FaissExecutionEngine(index->data(), location));
X
Xu Peng 已提交
96 97 98 99
    new_ee->Serialize();
    return new_ee;
}

100 101 102 103 104 105 106 107 108 109
Status FaissExecutionEngine::Search(long n,
                                    const float *data,
                                    long k,
                                    float *distances,
                                    long *labels) const {

    pIndex_->search(n, data, k, distances, labels);
    return Status::OK();
}

X
Xu Peng 已提交
110
Status FaissExecutionEngine::Cache() {
X
Xu Peng 已提交
111 112 113 114 115
    zilliz::vecwise::cache::CpuCacheMgr::GetInstance(
            )->InsertItem(location_, std::make_shared<Index>(pIndex_));

    return Status::OK();
}
X
Xu Peng 已提交
116

117

X
Xu Peng 已提交
118 119 120
} // namespace engine
} // namespace vecwise
} // namespace zilliz