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

#pragma once

#include <vespa/searchcommon/attribute/iattributevector.h>
#include <vespa/eval/eval/fast_value.h>
#include <vespa/eval/eval/value.h>
#include <vespa/vespalib/stllike/string.h>

using vespalib::eval::FastValueBuilderFactory;
using vespalib::eval::CellType;

namespace search::features {

/**
 * Feature executor that extracts the content from an attribute vector
 * and converts that into a tensor.
 */
template <typename WeightedBufferType>
class TensorFromAttributeExecutor : public fef::FeatureExecutor
{
private:
    const search::attribute::IAttributeVector *_attribute;
    vespalib::eval::ValueType _type;
    WeightedBufferType _attrBuffer;
    std::vector<vespalib::stringref> _addr_ref;
    std::unique_ptr<vespalib::eval::Value> _tensor;

public:
    TensorFromAttributeExecutor(const search::attribute::IAttributeVector *attribute,
                                const vespalib::string &dimension)
        : _attribute(attribute),
          _type(vespalib::eval::ValueType::make_type(CellType::DOUBLE, {{dimension}})),
          _attrBuffer(),
          _addr_ref(),
          _tensor()
    {
        _attrBuffer.allocate(_attribute->getMaxValueCount());
        _addr_ref.reserve(1);
    }
    void execute(uint32_t docId) override;
};

template <typename WeightedBufferType>
void
TensorFromAttributeExecutor<WeightedBufferType>::execute(uint32_t docId)
{
    _attrBuffer.fill(*_attribute, docId);
    auto factory = FastValueBuilderFactory::get();
    auto builder = factory.create_value_builder<double>(_type, 1, 1, _attrBuffer.size());
    for (size_t i = 0; i < _attrBuffer.size(); ++i) {
        vespalib::string label(_attrBuffer[i].value());
        _addr_ref.clear();
        _addr_ref.push_back(label);
        auto cell_array = builder->add_subspace(_addr_ref);
        cell_array[0] = _attrBuffer[i].weight();
    }
    _tensor = builder->build(std::move(builder));
    outputs().set_object(0, *_tensor);
}

}