aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/matching/extract_features.cpp
blob: 4f9e1f6d1f427c6f7a474813154a5d347d560de0 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "extract_features.h"
#include "match_tools.h"
#include <vespa/vespalib/util/doom.h>
#include <vespa/eval/eval/value_codec.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/util/runnable.h>
#include <vespa/vespalib/util/thread_bundle.h>
#include <vespa/searchlib/fef/feature_resolver.h>
#include <vespa/searchlib/fef/rank_program.h>
#include <vespa/searchlib/fef/utils.h>
#include <vespa/searchlib/queryeval/searchiterator.h>

using vespalib::Doom;
using vespalib::FeatureSet;
using vespalib::FeatureValues;
using vespalib::Runnable;
using vespalib::ThreadBundle;
using search::fef::FeatureResolver;
using search::fef::RankProgram;
using search::queryeval::SearchIterator;

namespace proton::matching {

using OrderedDocs = ExtractFeatures::OrderedDocs;
using search::StringStringMap;

using FefUtils = search::fef::Utils;

namespace {

struct MyChunk : Runnable {
    const std::pair<uint32_t,uint32_t> *begin;
    const std::pair<uint32_t,uint32_t> *end;
    FeatureValues &result;
    const Doom &doom;
    MyChunk(const std::pair<uint32_t,uint32_t> *begin_in,
            const std::pair<uint32_t,uint32_t> *end_in,
            FeatureValues &result_in, const Doom &doom_in)
      : begin(begin_in), end(end_in), result(result_in), doom(doom_in) {}
    void calculate_features(SearchIterator &search, const FeatureResolver &resolver) {
        assert(end > begin);
        assert(resolver.num_features() == result.names.size());
        search.initRange(begin[0].first, end[-1].first + 1);
        for (auto pos = begin; pos != end; ++pos) {
            if (doom.hard_doom()) {
                return;
            }
            search.unpack(pos->first);
            auto *dst = &result.values[pos->second * resolver.num_features()];
            FefUtils::extract_feature_values(resolver, pos->first, dst);
        }
    }
};

struct FirstChunk : MyChunk {
    SearchIterator &search;
    const FeatureResolver &resolver;
    FirstChunk(const std::pair<uint32_t,uint32_t> *begin_in,
               const std::pair<uint32_t,uint32_t> *end_in,
               FeatureValues &result_in,
               const Doom &doom_in,
               SearchIterator &search_in,
               const FeatureResolver &resolver_in)
      : MyChunk(begin_in, end_in, result_in, doom_in),
        search(search_in),
        resolver(resolver_in) {}
    void run() override { calculate_features(search, resolver); }
};

struct LaterChunk : MyChunk {
    const MatchToolsFactory &mtf;
    LaterChunk(const std::pair<uint32_t,uint32_t> *begin_in,
               const std::pair<uint32_t,uint32_t> *end_in,
               FeatureValues &result_in,
               const Doom &doom_in,
               const MatchToolsFactory &mtf_in)
      : MyChunk(begin_in, end_in, result_in, doom_in),
        mtf(mtf_in) {}
    void run() override {
        auto tools = mtf.createMatchTools();
        tools->setup_match_features();
        FeatureResolver resolver(tools->rank_program().get_seeds(false));
        calculate_features(tools->search(), resolver);
    }
};

} // unnamed

FeatureSet::UP
ExtractFeatures::get_feature_set(SearchIterator &search, RankProgram &rank_program, const std::vector<uint32_t> &docs,
                                 const Doom &doom, const StringStringMap &renames)
{
    FeatureResolver resolver(rank_program.get_seeds(false));
    auto result = std::make_unique<FeatureSet>(FefUtils::extract_feature_names(resolver, renames), docs.size());
    if (!docs.empty()) {
        search.initRange(docs.front(), docs.back()+1);
        for (uint32_t docid: docs) {
            if (doom.hard_doom()) {
                return result;
            }
            search.unpack(docid);
            auto *dst = result->getFeaturesByIndex(result->addDocId(docid));
            FefUtils::extract_feature_values(resolver, docid, dst);
        }
    }
    return result;
}

FeatureValues
ExtractFeatures::get_match_features(const MatchToolsFactory &mtf, const OrderedDocs &docs, ThreadBundle &thread_bundle)
{
    FeatureValues result;
    auto tools = mtf.createMatchTools();
    tools->setup_match_features();
    FeatureResolver resolver(tools->rank_program().get_seeds(false));
    result.names = FefUtils::extract_feature_names(resolver, mtf.get_feature_rename_map());
    result.values.resize(result.names.size() * docs.size());
    size_t num_threads = thread_bundle.size();
    std::vector<Runnable::UP> chunks;
    chunks.reserve(num_threads);
    size_t per_thread = docs.size() / num_threads;
    size_t rest_docs = docs.size() % num_threads;
    size_t idx = 0;
    for (size_t i = 0; i < num_threads; ++i) {
        size_t chunk_size = per_thread + (i < rest_docs);
        if (chunk_size == 0) {
            break;
        }
        if (i == 0) {
            chunks.push_back(std::make_unique<FirstChunk>(&docs[idx], &docs[idx + chunk_size], result, tools->getDoom(), tools->search(), resolver));
        } else {
            chunks.push_back(std::make_unique<LaterChunk>(&docs[idx], &docs[idx + chunk_size], result, tools->getDoom(), mtf));
        }
        idx += chunk_size;
    }
    assert(idx == docs.size());
    thread_bundle.run(chunks);
    return result;
}

}