FaissExecutionEngine.cpp 3.4 KB
Newer Older
X
Xu Peng 已提交
1 2
#include <easylogging++.h>
#include <faiss/AutoTune.h>
3 4 5
#include <faiss/MetaIndexes.h>
#include <faiss/IndexFlat.h>
#include <faiss/index_io.h>
X
Xu Peng 已提交
6
#include <wrapper/Index.h>
X
Xu Peng 已提交
7
#include <wrapper/IndexBuilder.h>
X
Xu Peng 已提交
8
#include <cache/CpuCacheMgr.h>
X
Xu Peng 已提交
9

X
Xu Peng 已提交
10
#include "FaissExecutionEngine.h"
X
Xu Peng 已提交
11 12 13 14 15

namespace zilliz {
namespace vecwise {
namespace engine {

X
Xu Peng 已提交
16 17
const std::string RawIndexType = "IDMap,Flat";
const std::string BuildIndexType = "IDMap,Flat";
X
Xu Peng 已提交
18

X
Xu Peng 已提交
19
FaissExecutionEngine::FaissExecutionEngine(uint16_t dimension, const std::string& location)
X
Xu Peng 已提交
20 21 22 23 24 25
    : 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 已提交
26
      location_(location) {
X
Xu Peng 已提交
27 28
}

X
Xu Peng 已提交
29
Status FaissExecutionEngine::AddWithIds(long n, const float *xdata, const long *xids) {
X
Xu Peng 已提交
30
    pIndex_->add_with_ids(n, xdata, xids);
X
Xu Peng 已提交
31 32 33
    return Status::OK();
}

X
Xu Peng 已提交
34
size_t FaissExecutionEngine::Count() const {
X
Xu Peng 已提交
35 36 37
    return (size_t)(pIndex_->ntotal);
}

X
Xu Peng 已提交
38
size_t FaissExecutionEngine::Size() const {
X
Xu Peng 已提交
39 40 41
    return (size_t)(Count() * pIndex_->d);
}

42 43 44 45
size_t FaissExecutionEngine::PhysicalSize() const {
    return (size_t)(Size()*sizeof(float));
}

X
Xu Peng 已提交
46
Status FaissExecutionEngine::Serialize() {
X
Xu Peng 已提交
47 48
    write_index(pIndex_.get(), location_.c_str());
    return Status::OK();
X
Xu Peng 已提交
49 50
}

X
Xu Peng 已提交
51 52 53 54
Status FaissExecutionEngine::Load() {
    auto index  = zilliz::vecwise::cache::CpuCacheMgr::GetInstance()->GetIndex(location_);
    if (!index) {
        index = read_index(location_);
55 56
        Cache();
        LOG(DEBUG) << "Disk io from: " << location_;
X
Xu Peng 已提交
57 58 59 60 61 62
    }

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

63 64 65 66 67 68 69 70 71 72 73 74 75 76
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 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
std::shared_ptr<ExecutionEngine> FaissExecutionEngine::BuildIndex(const std::string& location) {
    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());

    std::shared_ptr<ExecutionEngine> new_ee(new FaissExecutionEngine(index->data(), location));
    new_ee->Serialize();
    return new_ee;
}

94 95 96 97 98 99 100 101 102 103
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 已提交
104
Status FaissExecutionEngine::Cache() {
X
Xu Peng 已提交
105 106 107 108 109
    zilliz::vecwise::cache::CpuCacheMgr::GetInstance(
            )->InsertItem(location_, std::make_shared<Index>(pIndex_));

    return Status::OK();
}
X
Xu Peng 已提交
110 111 112 113

} // namespace engine
} // namespace vecwise
} // namespace zilliz