aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/dotproductfeature.h
blob: 229f57b7526391b506f9ee5bfa0848bafba3d4ff (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "utils.h"
#include <vespa/searchcommon/attribute/attributecontent.h>
#include <vespa/searchcommon/attribute/i_multi_value_read_view.h>
#include <vespa/searchcommon/attribute/multivalue.h>
#include <vespa/searchlib/fef/blueprint.h>
#include <vespa/vespalib/hwaccelrated/iaccelrated.h>
#include <vespa/vespalib/stllike/hash_map.hpp>

namespace search::fef { class Property; }
namespace vespalib { class nbostream; }

namespace search::features {

namespace dotproduct {

struct ConstCharComparator {
    bool operator()(const char * lhs, const char * rhs) const {
        return strcmp(lhs, rhs) == 0;
    }
};

template <typename Src, typename Dst>
struct Converter {
    Dst convert(const Src & value) const { return value; }
};

template <>
struct Converter<vespalib::string, const char *> {
    const char * convert(const vespalib::string & value) const { return value.c_str(); }
};

template <typename T>
struct ArrayParam : public fef::Anything {
    ArrayParam(const fef::Property & prop);
    ArrayParam(vespalib::nbostream & stream);
    ArrayParam(std::vector<T> v) : values(std::move(v)) {}
    ~ArrayParam() override;
    std::vector<T>        values;
    std::vector<uint32_t> indexes;
};

namespace wset {

template <typename DimensionVType, typename DimensionHType, typename ComponentType, typename HashMapComparator = std::equal_to<DimensionHType> >
class VectorBase : public fef::Anything {
public:
    using Element = std::pair<DimensionVType, ComponentType>; // <dimension, component>
    using Vector = std::vector<Element>;
    using HashMap = vespalib::hash_map<DimensionHType, ComponentType, vespalib::hash<DimensionHType>, HashMapComparator, vespalib::hashtable_base::and_modulator>;
protected:
    VectorBase();
    Vector _vector;
    HashMap _dimMap; // dimension -> component
public:
    VectorBase(VectorBase && rhs) = default;
    VectorBase & operator = (VectorBase && rhs) = default;
    ~VectorBase();
    const Vector & getVector() const { return _vector; }
    VectorBase & syncMap();
    const HashMap & getDimMap() const { return _dimMap; }
    bool empty() const { return _vector.empty(); }
};

template <typename T>
using NumericVectorBaseT = VectorBase<T, T, feature_t>;

/**
 * Represents a vector where the dimensions are integers.
 **/
template<typename T>
class IntegerVectorT : public NumericVectorBaseT<T> {
public:
    void insert(vespalib::stringref label, vespalib::stringref value) {
        this->_vector.emplace_back(util::strToNum<T>(label), util::strToNum<feature_t>(value));
    }
};

extern template class VectorBase<int64_t, int64_t, double>;
extern template class VectorBase<uint32_t, uint32_t, double>;
extern template class IntegerVectorT<int64_t>;

using IntegerVector = IntegerVectorT<int64_t>;

using StringVectorBase = VectorBase<vespalib::string, const char*, feature_t, ConstCharComparator>;

/**
 * Represents a vector where the dimensions are string values.
 **/
class StringVector : public StringVectorBase {
public:
    StringVector();
    StringVector(StringVector &&) = default;
    StringVector & operator = (StringVector &&) = default;
    ~StringVector();
    void insert(vespalib::stringref label, vespalib::stringref value) {
        _vector.emplace_back(label, util::strToNum<feature_t>(value));
    }
};

/**
 * Represents a vector where the dimensions are enum values for strings.
 **/
class EnumVector : public VectorBase<attribute::EnumHandle, attribute::EnumHandle, feature_t> {
private:
    const attribute::IAttributeVector * _attribute;
public:
    EnumVector(const attribute::IAttributeVector * attribute) : _attribute(attribute) {}
    void insert(vespalib::stringref label, vespalib::stringref value) {
        attribute::EnumHandle e;
        if (_attribute->findEnum(label.data(), e)) {
            _vector.emplace_back(e, util::strToNum<feature_t>(value));
        }
    }
};

/**
 * Common base for handling execution for all wset dot product executors.
 * Only cares about the underlying value type, not the concrete type of the
 * attribute vector itself.
 */
template <typename BaseType>
class DotProductExecutorBase : public fef::FeatureExecutor {
public:
    using AT = multivalue::WeightedValue<BaseType>;
    using V  = std::conditional_t<std::is_same_v<BaseType,const char*>,StringVectorBase,NumericVectorBaseT<BaseType>>;
private:
    const V                                    & _queryVector;
    const typename V::HashMap::const_iterator    _end;
    virtual vespalib::ConstArrayRef<AT> getAttributeValues(uint32_t docid) = 0;
public:
    DotProductExecutorBase(const V & queryVector);
    ~DotProductExecutorBase() override;
    void execute(uint32_t docId) override;
};

template <typename BaseType>
class DotProductByWeightedSetReadViewExecutor final : public DotProductExecutorBase<BaseType> {
public:
    using WeightedSetReadView = attribute::IWeightedSetReadView<BaseType>;
    using AT = typename DotProductExecutorBase<BaseType>::AT;
    using V  = typename DotProductExecutorBase<BaseType>::V;
protected:
    const WeightedSetReadView * _weighted_set_read_view;
private:
    std::unique_ptr<V> _backing;
    vespalib::ConstArrayRef<AT> getAttributeValues(uint32_t docid) override;
public:
    DotProductByWeightedSetReadViewExecutor(const WeightedSetReadView* weighted_set_read_view, const V & queryVector);
    DotProductByWeightedSetReadViewExecutor(const WeightedSetReadView * weighted_set_read_view, std::unique_ptr<V> queryVector);
    ~DotProductByWeightedSetReadViewExecutor();
};

}

namespace array {

/**
 * Common base for handling execution for all array dot product executors.
 * Only cares about the underlying value type, not the concrete type of the
 * attribute vector itself.
 */
template <typename BaseType>
class DotProductExecutorBase : public fef::FeatureExecutor {
public:
    using V  = std::vector<BaseType>;
private:
    const vespalib::hwaccelrated::IAccelrated   & _multiplier;
    V                                             _queryVector;
    virtual vespalib::ConstArrayRef<BaseType> getAttributeValues(uint32_t docid) = 0;
public:
    DotProductExecutorBase(const V & queryVector);
    ~DotProductExecutorBase() override;
    void execute(uint32_t docId) final override;
};

/**
 * Implements the executor for the dotproduct feature using array read view.
 */
template <typename BaseType>
class DotProductByArrayReadViewExecutor : public DotProductExecutorBase<BaseType> {
public:
    using V  = typename DotProductExecutorBase<BaseType>::V;
    using ArrayReadView = attribute::IArrayReadView<BaseType>;
protected:
    const ArrayReadView* _array_read_view;
private:
    vespalib::ConstArrayRef<BaseType> getAttributeValues(uint32_t docid) override;
public:
    DotProductByArrayReadViewExecutor(const ArrayReadView* array_read_view, const V & queryVector);
    ~DotProductByArrayReadViewExecutor();
};

/**
 * Implements the executor for the dotproduct feature.
 */
template <typename A>
class DotProductExecutor : public DotProductExecutorBase<typename A::BaseType> {
public:
    using V  = typename DotProductExecutorBase<typename A::BaseType>::V;
protected:
    const A * _attribute;
public:
    DotProductExecutor(const A * attribute, const V & queryVector);
    ~DotProductExecutor();
};

template <typename BaseType>
class SparseDotProductExecutorBase : public DotProductExecutorBase<BaseType> {
public:
    using IV = std::vector<uint32_t>;
    using V = typename DotProductExecutorBase<BaseType>::V;
    SparseDotProductExecutorBase(const V & queryVector, const IV & queryIndexes);
    ~SparseDotProductExecutorBase();
protected:
    IV              _queryIndexes;
    std::vector<BaseType> _scratch;
};

template <typename BaseType>
class SparseDotProductByArrayReadViewExecutor : public SparseDotProductExecutorBase<BaseType> {
public:
    using SparseDotProductExecutorBase<BaseType>::_queryIndexes;
    using SparseDotProductExecutorBase<BaseType>::_scratch;
    using IV = std::vector<uint32_t>;
    using V = typename SparseDotProductExecutorBase<BaseType>::V;
    using ArrayReadView = attribute::IArrayReadView<BaseType>;
    SparseDotProductByArrayReadViewExecutor(const ArrayReadView* array_read_view, const V & queryVector, const IV & queryIndexes);
    ~SparseDotProductByArrayReadViewExecutor();
private:
    vespalib::ConstArrayRef<BaseType> getAttributeValues(uint32_t docid) override;
    const ArrayReadView* _array_read_view;
};

}

}

/**
 * Implements the blueprint for the foreach executor.
 */
class DotProductBlueprint : public fef::Blueprint {
private:
    using IAttributeVector = attribute::IAttributeVector;
    vespalib::string _defaultAttribute;
    vespalib::string _attributeOverride;
    vespalib::string _queryVector;
    vespalib::string _attrKey;
    vespalib::string _queryVectorKey;

    const vespalib::string & getAttribute(const fef::IQueryEnvironment & env) const;
    const IAttributeVector * upgradeIfNecessary(const IAttributeVector * attribute, const fef::IQueryEnvironment & env) const;

public:
    DotProductBlueprint();
    ~DotProductBlueprint() override;
    void visitDumpFeatures(const fef::IIndexEnvironment & env, fef::IDumpFeatureVisitor & visitor) const override;
    fef::Blueprint::UP createInstance() const override;

    fef::ParameterDescriptions getDescriptions() const override;

    bool setup(const fef::IIndexEnvironment & env, const fef::ParameterList & params) override;
    void prepareSharedState(const fef::IQueryEnvironment & queryEnv, fef::IObjectStore & objectStore) const override;
    fef::FeatureExecutor &createExecutor(const fef::IQueryEnvironment &env, vespalib::Stash &stash) const override;
};

}