aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/tensor/tensor_attribute_loader.cpp
blob: 223c9d7d1f219a65c1a0d4d936e9b3e56e552a58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "tensor_attribute_loader.h"
#include "dense_tensor_store.h"
#include "nearest_neighbor_index.h"
#include "nearest_neighbor_index_loader.h"
#include "tensor_attribute_constants.h"
#include "tensor_attribute_saver.h"
#include <vespa/fastlib/io/bufferedfile.h>
#include <vespa/searchcommon/attribute/config.h>
#include <vespa/searchlib/attribute/attribute_header.h>
#include <vespa/searchlib/attribute/blob_sequence_reader.h>
#include <vespa/searchlib/attribute/load_utils.h>
#include <vespa/searchlib/attribute/readerbase.h>
#include <vespa/vespalib/util/arrayqueue.hpp>
#include <vespa/vespalib/util/cpu_usage.h>
#include <vespa/vespalib/util/lambdatask.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <mutex>
#include <condition_variable>

#include <vespa/log/log.h>
LOG_SETUP(".searchlib.tensor.tensor_attribute_loader");

using search::attribute::AttributeHeader;
using search::attribute::BlobSequenceReader;
using search::attribute::LoadUtils;
using vespalib::CpuUsage;
using vespalib::datastore::EntryRef;

namespace search::tensor {

inline namespace loader {

constexpr uint32_t LOAD_COMMIT_INTERVAL = 256;
const vespalib::string tensorTypeTag("tensortype");

bool
can_use_index_save_file(const search::attribute::Config &config, const AttributeHeader& header)
{
    if (!config.hnsw_index_params().has_value() || !header.get_hnsw_index_params().has_value()) {
        return false;
    }
    const auto &config_params = config.hnsw_index_params().value();
    const auto &header_params = header.get_hnsw_index_params().value();
    if ((config_params.max_links_per_node() != header_params.max_links_per_node()) ||
        (config_params.distance_metric() != header_params.distance_metric())) {
        return false;
    }
    return true;
}

bool
has_index_file(AttributeVector& attr)
{
    return LoadUtils::file_exists(attr, TensorAttributeSaver::index_file_suffix());
}

bool
is_present(uint8_t presence_flag) {
    if (presence_flag == tensorIsNotPresent) {
        return false;
    }
    if (presence_flag != tensorIsPresent) {
        LOG_ABORT("should not be reached");
    }
    return true;
}

class IndexBuilder {
public:
    virtual ~IndexBuilder() = default;
    virtual void add(uint32_t lid) = 0;
    virtual void wait_complete() = 0;
};

/**
 * Will build nearest neighbor index in parallel. Note that indexing order is not guaranteed,
 * but that is inline with the guarantees vespa already has.
 */
class ThreadedIndexBuilder : public IndexBuilder {
public:
    ThreadedIndexBuilder(TensorAttribute& attr, vespalib::GenerationHandler& generation_handler, TensorStore& store, NearestNeighborIndex& index, vespalib::Executor& shared_executor)
        : _attr(attr),
          _generation_handler(generation_handler),
          _index(index),
          _shared_executor(shared_executor),
          _queue(MAX_PENDING),
          _pending(0)
    {
        (void) store;
    }
    void add(uint32_t lid) override;
    void wait_complete() override {
        drainUntilPending(0);
    }
private:
    using Entry = std::pair<uint32_t, std::unique_ptr<PrepareResult>>;
    using Queue = vespalib::ArrayQueue<Entry>;

    bool pop(Entry & entry) {
        std::unique_lock guard(_mutex);
        if (_queue.empty()) return false;
        entry = std::move(_queue.front());
        _queue.pop();
        return true;
    }
    void drainQ() {
        Queue queue(MAX_PENDING);
        {
            std::unique_lock guard(_mutex);
            queue.swap(_queue);
        }
        while (!queue.empty()) {
            auto item = std::move(queue.front());
            queue.pop();
            complete(item.first, std::move(item.second));
        }
    }

    void complete(uint32_t lid, std::unique_ptr<PrepareResult> prepared) {
        _index.complete_add_document(lid, std::move(prepared));
        --_pending;
        if ((lid % LOAD_COMMIT_INTERVAL) == 0) {
            _attr.commit();
        };
    }
    void drainUntilPending(uint32_t maxPending) {
        while (_pending > maxPending) {
            {
                std::unique_lock guard(_mutex);
                while (_queue.empty()) {
                    _cond.wait(guard);
                }
            }
            drainQ();
        }
    }
    static constexpr uint32_t MAX_PENDING = 1000;
    TensorAttribute&        _attr;
    const vespalib::GenerationHandler& _generation_handler;
    NearestNeighborIndex&   _index;
    vespalib::Executor&     _shared_executor;
    std::mutex              _mutex;
    std::condition_variable _cond;
    Queue                   _queue;
    uint64_t                _pending; // _pending is only modified in forground thread
};

void
ThreadedIndexBuilder::add(uint32_t lid) {
    Entry item;
    while (pop(item)) {
        // First process items that are ready to complete
        complete(item.first, std::move(item.second));
    }
    // Then ensure that there no more than MAX_PENDING inflight
    drainUntilPending(MAX_PENDING);

    // Then we can issue a new one
    ++_pending;
    auto task = vespalib::makeLambdaTask([this, lid]() {
        auto prepared = _index.prepare_add_document(lid, _attr.get_vectors(lid),
                                                    _generation_handler.takeGuard());
        std::unique_lock guard(_mutex);
        _queue.push(std::make_pair(lid, std::move(prepared)));
        if (_queue.size() == 1) {
            _cond.notify_all();
        }
    });
    _shared_executor.execute(CpuUsage::wrap(std::move(task), CpuUsage::Category::SETUP));
}

class ForegroundIndexBuilder : public IndexBuilder {
public:
    ForegroundIndexBuilder(AttributeVector& attr, NearestNeighborIndex& index)
        : _attr(attr),
          _index(index)
    {
    }
    void add(uint32_t lid) override {
        _index.add_document(lid);
        if ((lid % LOAD_COMMIT_INTERVAL) == 0) {
            _attr.commit();
        }
    }
    void wait_complete() override {

    }
private:
    AttributeVector&      _attr;
    NearestNeighborIndex& _index;
};

}

TensorAttributeLoader::TensorAttributeLoader(TensorAttribute& attr, GenerationHandler& generation_handler, RefVector& ref_vector, TensorStore& store, NearestNeighborIndex* index)
    : _attr(attr),
      _generation_handler(generation_handler),
      _ref_vector(ref_vector),
      _store(store),
      _index(index)
{
}

TensorAttributeLoader::~TensorAttributeLoader() = default;

void
TensorAttributeLoader::load_dense_tensor_store(BlobSequenceReader& reader, uint32_t docid_limit, DenseTensorStore& dense_store)
{
    assert(reader.getVersion() == DENSE_TENSOR_ATTRIBUTE_VERSION);
    uint8_t presence_flag = 0;
    for (uint32_t lid = 0; lid < docid_limit; ++lid) {
        reader.readBlob(&presence_flag, sizeof(presence_flag));
        if (is_present(presence_flag)) {
            auto raw = dense_store.allocRawBuffer();
            reader.readBlob(raw.data, dense_store.getBufSize());
            _ref_vector.push_back(AtomicEntryRef(raw.ref));
        } else {
            _ref_vector.push_back(AtomicEntryRef());
        }
        if ((lid % LOAD_COMMIT_INTERVAL) == 0) {
            _attr.commit();
        }
    }
}

void
TensorAttributeLoader::load_tensor_store(BlobSequenceReader& reader, uint32_t docid_limit)
{
    assert(reader.getVersion() == TENSOR_ATTRIBUTE_VERSION);
    vespalib::Array<char> buffer(1024);
    for (uint32_t lid = 0; lid < docid_limit; ++lid) {
        uint32_t tensorSize = reader.getNextSize();
        if (tensorSize != 0) {
            if (tensorSize > buffer.size()) {
                buffer.resize(tensorSize + 1024);
            }
            reader.readBlob(&buffer[0], tensorSize);
            vespalib::nbostream source(&buffer[0], tensorSize);
            EntryRef ref = _store.store_encoded_tensor(source);
            _ref_vector.push_back(AtomicEntryRef(ref));
        } else {
            EntryRef invalid;
            _ref_vector.push_back(AtomicEntryRef(invalid));
        }
        if ((lid % LOAD_COMMIT_INTERVAL) == 0) {
            _attr.commit();
        }
    }
}

void
TensorAttributeLoader::build_index(vespalib::Executor* executor, uint32_t docid_limit)
{
    std::unique_ptr<IndexBuilder> builder;
    if (executor != nullptr) {
        builder = std::make_unique<ThreadedIndexBuilder>(_attr, _generation_handler, _store, *_index, *executor);
    } else {
        builder = std::make_unique<ForegroundIndexBuilder>(_attr, *_index);
    }
    for (uint32_t lid = 0; lid < docid_limit; ++lid) {
        auto ref = _ref_vector[lid].load_relaxed();
        if (ref.valid()) {
            builder->add(lid);
        }
    }
    builder->wait_complete();
    _attr.commit();
}

bool
TensorAttributeLoader::load_index()
{
    FileWithHeader index_file(LoadUtils::openFile(_attr, TensorAttributeSaver::index_file_suffix()));
    try {
        auto index_loader = _index->make_loader(index_file.file(), index_file.header());
        size_t cnt = 0;
        while (index_loader->load_next()) {
            if ((++cnt % LOAD_COMMIT_INTERVAL) == 0) {
                _attr.commit();
            }
        }
        _attr.commit();
    } catch (const std::runtime_error& ex) {
        LOG(error, "Exception while loading nearest neighbor index for tensor attribute '%s': %s",
            _attr.getName().c_str(), ex.what());
        return false;
    }
    return true;
}

bool
TensorAttributeLoader::on_load(vespalib::Executor* executor)
{
    BlobSequenceReader reader(_attr);
    if (!reader.hasData()) {
        return false;
    }
    _attr.setCreateSerialNum(reader.getCreateSerialNum());
    assert(_attr.getConfig().tensorType().to_spec() ==
           reader.getDatHeader().getTag(tensorTypeTag).asString());
    uint32_t docid_limit(reader.getDocIdLimit());
    _ref_vector.reset();
    _ref_vector.unsafe_reserve(docid_limit);
    auto dense_store = _store.as_dense();
    if (dense_store != nullptr) {
        load_dense_tensor_store(reader, docid_limit, *dense_store);
    } else {
        load_tensor_store(reader, docid_limit);
    }
    _attr.commit();
    _attr.getStatus().setNumDocs(docid_limit);
    _attr.setCommittedDocIdLimit(docid_limit);
    if (_index != nullptr) {
        bool use_index_file = false;
        if (has_index_file(_attr)) {
            auto header = AttributeHeader::extractTags(reader.getDatHeader(), _attr.getBaseFileName());
            use_index_file = can_use_index_save_file(_attr.getConfig(), header);
        }
        if (use_index_file) {
            if (!load_index()) {
                return false;
            }
            if (dense_store == nullptr) {
                check_consistency(docid_limit);
            }
        } else {
            build_index(executor, docid_limit);
        }
    }
    return true;
}

void
TensorAttributeLoader::check_consistency(uint32_t docid_limit)
{
    auto before = vespalib::steady_clock::now();
    uint32_t inconsistencies = _index->check_consistency(docid_limit);
    auto after = vespalib::steady_clock::now();
    double elapsed = vespalib::to_s(after - before);
    LOG(info, "%u inconsistencies detected after loading index for attribute %s, (check used %6.3fs)",
        inconsistencies, _attr.getName().c_str(), elapsed);
}

}