FaissExecutionEngine.cpp 5.2 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.
 ******************************************************************************/
6
#include "FaissExecutionEngine.h"
G
groot 已提交
7
#include "Log.h"
X
Xu Peng 已提交
8

X
Xu Peng 已提交
9
#include <faiss/AutoTune.h>
10 11 12
#include <faiss/MetaIndexes.h>
#include <faiss/IndexFlat.h>
#include <faiss/index_io.h>
X
Xu Peng 已提交
13
#include <wrapper/Index.h>
X
Xu Peng 已提交
14
#include <wrapper/IndexBuilder.h>
X
Xu Peng 已提交
15
#include <cache/CpuCacheMgr.h>
Y
yu yunfeng 已提交
16
#include "metrics/Metrics.h"
X
Xu Peng 已提交
17 18 19


namespace zilliz {
J
jinhai 已提交
20
namespace milvus {
X
Xu Peng 已提交
21 22
namespace engine {

23

G
groot 已提交
24 25 26 27 28 29 30 31
FaissExecutionEngine::FaissExecutionEngine(uint16_t dimension,
        const std::string& location,
        const std::string& build_index_type,
        const std::string& raw_index_type)
    : pIndex_(faiss::index_factory(dimension, raw_index_type.c_str())),
      location_(location),
      build_index_type_(build_index_type),
      raw_index_type_(raw_index_type) {
X
Xu Peng 已提交
32 33
}

G
groot 已提交
34 35 36 37
FaissExecutionEngine::FaissExecutionEngine(std::shared_ptr<faiss::Index> index,
        const std::string& location,
        const std::string& build_index_type,
        const std::string& raw_index_type)
X
Xu Peng 已提交
38
    : pIndex_(index),
G
groot 已提交
39 40 41
      location_(location),
      build_index_type_(build_index_type),
      raw_index_type_(raw_index_type) {
X
Xu Peng 已提交
42 43
}

G
groot 已提交
44
Status FaissExecutionEngine::AddWithIds(long n, const float *xdata, const long *xids) {
X
Xu Peng 已提交
45
    pIndex_->add_with_ids(n, xdata, xids);
X
Xu Peng 已提交
46 47 48
    return Status::OK();
}

G
groot 已提交
49
size_t FaissExecutionEngine::Count() const {
X
Xu Peng 已提交
50 51 52
    return (size_t)(pIndex_->ntotal);
}

G
groot 已提交
53
size_t FaissExecutionEngine::Size() const {
54
    return (size_t)(Count() * pIndex_->d)*sizeof(float);
X
Xu Peng 已提交
55 56
}

G
groot 已提交
57 58 59 60
size_t FaissExecutionEngine::Dimension() const {
    return pIndex_->d;
}

G
groot 已提交
61
size_t FaissExecutionEngine::PhysicalSize() const {
62
    return (size_t)(Count() * pIndex_->d)*sizeof(float);
63 64
}

G
groot 已提交
65
Status FaissExecutionEngine::Serialize() {
X
Xu Peng 已提交
66 67
    write_index(pIndex_.get(), location_.c_str());
    return Status::OK();
X
Xu Peng 已提交
68 69
}

G
groot 已提交
70
Status FaissExecutionEngine::Load() {
J
jinhai 已提交
71
    auto index  = zilliz::milvus::cache::CpuCacheMgr::GetInstance()->GetIndex(location_);
72
    bool to_cache = false;
Y
yu yunfeng 已提交
73
    auto start_time = METRICS_NOW_TIME;
X
Xu Peng 已提交
74 75
    if (!index) {
        index = read_index(location_);
76
        to_cache = true;
G
groot 已提交
77
        ENGINE_LOG_DEBUG << "Disk io from: " << location_;
X
Xu Peng 已提交
78 79 80
    }

    pIndex_ = index->data();
81 82
    if (to_cache) {
        Cache();
Y
yu yunfeng 已提交
83 84 85
        auto end_time = METRICS_NOW_TIME;
        auto total_time = METRICS_MICROSECONDS(start_time, end_time);

Y
yu yunfeng 已提交
86
        server::Metrics::GetInstance().FaissDiskLoadDurationSecondsHistogramObserve(total_time);
Y
yu yunfeng 已提交
87 88 89
        double total_size = (pIndex_->d) * (pIndex_->ntotal) * 4;


Y
yu yunfeng 已提交
90
        server::Metrics::GetInstance().FaissDiskLoadSizeBytesHistogramObserve(total_size);
Y
yu yunfeng 已提交
91 92
//        server::Metrics::GetInstance().FaissDiskLoadIOSpeedHistogramObserve(total_size/double(total_time));
        server::Metrics::GetInstance().FaissDiskLoadIOSpeedGaugeSet(total_size/double(total_time));
93
    }
X
Xu Peng 已提交
94 95 96
    return Status::OK();
}

G
groot 已提交
97
Status FaissExecutionEngine::Merge(const std::string& location) {
98 99 100
    if (location == location_) {
        return Status::Error("Cannot Merge Self");
    }
G
groot 已提交
101 102
    ENGINE_LOG_DEBUG << "Merge index file: " << location << " to: " << location_;

J
jinhai 已提交
103
    auto to_merge = zilliz::milvus::cache::CpuCacheMgr::GetInstance()->GetIndex(location);
104 105 106 107 108 109 110 111 112
    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();
}

G
groot 已提交
113 114
ExecutionEnginePtr
FaissExecutionEngine::BuildIndex(const std::string& location) {
G
groot 已提交
115 116
    ENGINE_LOG_DEBUG << "Build index file: " << location << " from: " << location_;

X
Xu Peng 已提交
117 118
    auto opd = std::make_shared<Operand>();
    opd->d = pIndex_->d;
G
groot 已提交
119
    opd->index_type = build_index_type_;
X
Xu Peng 已提交
120 121 122 123 124 125 126 127
    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());

G
groot 已提交
128
    ExecutionEnginePtr new_ee(new FaissExecutionEngine(index->data(), location, build_index_type_, raw_index_type_));
X
Xu Peng 已提交
129 130 131
    return new_ee;
}

G
groot 已提交
132
Status FaissExecutionEngine::Search(long n,
133 134 135 136
                                    const float *data,
                                    long k,
                                    float *distances,
                                    long *labels) const {
Y
yu yunfeng 已提交
137
    auto start_time = METRICS_NOW_TIME;
138
    pIndex_->search(n, data, k, distances, labels);
Y
yu yunfeng 已提交
139 140
    auto end_time = METRICS_NOW_TIME;
    auto total_time = METRICS_MICROSECONDS(start_time,end_time);
Y
yu yunfeng 已提交
141
    server::Metrics::GetInstance().QueryIndexTypePerSecondSet(build_index_type_, double(n)/double(total_time));
142 143 144
    return Status::OK();
}

G
groot 已提交
145
Status FaissExecutionEngine::Cache() {
J
jinhai 已提交
146
    zilliz::milvus::cache::CpuCacheMgr::GetInstance(
X
Xu Peng 已提交
147 148 149 150
            )->InsertItem(location_, std::make_shared<Index>(pIndex_));

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

152

X
Xu Peng 已提交
153
} // namespace engine
J
jinhai 已提交
154
} // namespace milvus
X
Xu Peng 已提交
155
} // namespace zilliz