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

#pragma once

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

namespace search {
namespace fef {
class Property;
}

namespace 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);
    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:
    typedef std::pair<DimensionVType, ComponentType> Element; // <dimension, component>
    typedef std::vector<Element>                    Vector;
    typedef vespalib::hash_map<DimensionHType, ComponentType, vespalib::hash<DimensionHType>, HashMapComparator> HashMap;
protected:
    VectorBase();
    Vector _vector;
    HashMap _dimMap; // dimension -> component
public:
    ~VectorBase();
    const Vector & getVector() const { return _vector; }
    void syncMap() {
        Converter<DimensionVType, DimensionHType> conv;
        _dimMap.clear();
        _dimMap.resize(_vector.size()*2);
        for (size_t i = 0; i < _vector.size(); ++i) {
            _dimMap.insert(std::make_pair(conv.convert(_vector[i].first), _vector[i].second));
        }
    }
    const HashMap & getDimMap() const { return _dimMap; }
};

/**
 * Represents a vector where the dimensions are integers.
 **/
class IntegerVector : public VectorBase<int64_t, int64_t, feature_t> {
public:
    void insert(const vespalib::stringref & label, const vespalib::stringref & value) {
        _vector.push_back(std::make_pair(util::strToNum<int64_t>(label), util::strToNum<feature_t>(value)));
    }
};

/**
 * Represents a vector where the dimensions are string values.
 **/
class StringVector : public VectorBase<vespalib::string, const char *, feature_t, ConstCharComparator> {
public:
    ~StringVector();
    void insert(const vespalib::stringref & label, const vespalib::stringref & value) {
        _vector.push_back(std::make_pair(label, util::strToNum<feature_t>(value)));
    }
};

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


/**
 * Implements the executor for the dotproduct feature.
 */
template <typename Vector, typename Buffer>
class DotProductExecutor : public fef::FeatureExecutor {
private:
    const search::attribute::IAttributeVector * _attribute;
    Vector _queryVector;
    Buffer _buffer;

public:
    DotProductExecutor(const search::attribute::IAttributeVector * attribute, const Vector & queryVector);
    void execute(uint32_t docId) override;
};

}

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 AT = multivalue::Value<BaseType>;
    using V  = std::vector<BaseType>;
private:
    vespalib::hwaccelrated::IAccelrated::UP _multiplier;
    V                                       _queryVector;
    virtual size_t getAttributeValues(uint32_t docid, const AT * & count) = 0;
public:
    DotProductExecutorBase(const V & queryVector);
    ~DotProductExecutorBase();
    void execute(uint32_t docId) final override;
};

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

template <typename A>
class DotProductByCopyExecutor : public DotProductExecutor<A> {
public:
    typedef typename DotProductExecutor<A>::V V;
    DotProductByCopyExecutor(const A * attribute, const V & queryVector);
    ~DotProductByCopyExecutor();
private:
    typedef typename DotProductExecutor<A>::AT AT;
    size_t getAttributeValues(uint32_t docid, const AT * & count) final override;
    std::vector<typename A::BaseType> _copy;
};

/**
 * Dot product executor which uses AttributeContent for the specified base value type
 * to extract array elements from a given attribute vector. Used for "synthetic"
 * attribute vectors such as imported attributes, where we cannot directly access
 * the memory of the underlying attribute store.
 *
 * Some caveats:
 *   - 64 bit value type width is enforced, so 32-bit value types will not benefit
 *     from extra SIMD register capacity.
 *   - Additional overhead caused by call indirection and copy step.
 */
template <typename BaseType>
class DotProductByContentFillExecutor : public DotProductExecutorBase<BaseType> {
public:
    using V  = typename DotProductExecutorBase<BaseType>::V;
    using AT = typename DotProductExecutorBase<BaseType>::AT;
    using ValueFiller = attribute::AttributeContent<BaseType>;

    DotProductByContentFillExecutor(const attribute::IAttributeVector * attribute, const V & queryVector);
    ~DotProductByContentFillExecutor();
private:
    size_t getAttributeValues(uint32_t docid, const AT * & values) final override;

    const attribute::IAttributeVector* _attribute;
    ValueFiller _filler;
};

template <typename A>
class SparseDotProductExecutor : public DotProductExecutor<A> {
public:
    typedef std::vector<uint32_t> IV;
    typedef typename DotProductExecutor<A>::V V;
    SparseDotProductExecutor(const A * attribute, const V & queryVector, const IV & queryIndexes);
    ~SparseDotProductExecutor();
private:
    typedef typename DotProductExecutor<A>::AT AT;
    size_t getAttributeValues(uint32_t docid, const AT * & count) override;
protected:
    IV              _queryIndexes;
    std::vector<AT> _scratch;
};

template <typename A>
class SparseDotProductByCopyExecutor : public SparseDotProductExecutor<A> {
public:
    typedef std::vector<uint32_t> IV;
    typedef typename DotProductExecutor<A>::V V;
    SparseDotProductByCopyExecutor(const A * attribute, const V & queryVector, const IV & queryIndexes);
    ~SparseDotProductByCopyExecutor();
private:
    typedef typename DotProductExecutor<A>::AT AT;
    size_t getAttributeValues(uint32_t docid, const AT * & count) final override;
    std::vector<typename A::BaseType> _copy;
};

/**
 * Dot product executor which uses AttributeContent for fetching values. See
 * DotProductByContentFillExecutor for a more in-depth description and caveats.
 */
template <typename BaseType>
class SparseDotProductByContentFillExecutor : public DotProductExecutorBase<BaseType> {
public:
    using IV = std::vector<uint32_t>;
    using V  = typename DotProductExecutorBase<BaseType>::V;
    using AT = typename DotProductExecutorBase<BaseType>::AT;
    using ValueFiller = attribute::AttributeContent<BaseType>;

    SparseDotProductByContentFillExecutor(const attribute::IAttributeVector * attribute,
                                          const V & queryVector,
                                          const IV & queryIndexes);
    ~SparseDotProductByContentFillExecutor();
private:
    size_t getAttributeValues(uint32_t docid, const AT * & values) final override;

    const attribute::IAttributeVector* _attribute;
    IV          _queryIndexes;
    ValueFiller _filler;
};

}

}


/**
 * Implements the blueprint for the foreach executor.
 */
class DotProductBlueprint : public fef::Blueprint {
private:
    vespalib::string _defaultAttribute;
    vespalib::string _queryVector;

    vespalib::string getAttribute(const fef::IQueryEnvironment & env) const;

public:
    DotProductBlueprint();
    ~DotProductBlueprint();
    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;

};


} // namespace features
} // namespace search