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

#pragma once

#include <vespa/searchlib/fef/featureexecutor.h>
#include <vespa/eval/eval/tensor_spec.h>
#include <vespa/eval/eval/value.h>
#include <vespa/eval/eval/value_type.h>
#include <vespa/eval/eval/fast_value.h>
#include <vespa/eval/eval/value_codec.h>
#include <vespa/vespalib/util/stash.h>

namespace search::features {

/**
 * Feature executor that returns a constant tensor.
 */
class ConstantTensorExecutor : public fef::FeatureExecutor
{
private:
    vespalib::eval::Value::UP _tensor;

public:
    ConstantTensorExecutor(vespalib::eval::Value::UP tensor)
        : _tensor(std::move(tensor))
    {}
    bool isPure() override { return true; }
    void execute(uint32_t) override {
        outputs().set_object(0, *_tensor);
    }
    static fef::FeatureExecutor &create(std::unique_ptr<vespalib::eval::Value> tensor, vespalib::Stash &stash) {
        return stash.create<ConstantTensorExecutor>(std::move(tensor));
    }
    static fef::FeatureExecutor &createEmpty(const vespalib::eval::ValueType &valueType, vespalib::Stash &stash) {
        const auto &factory = vespalib::eval::FastValueBuilderFactory::get();
        auto spec = vespalib::eval::TensorSpec(valueType.to_spec());
        return stash.create<ConstantTensorExecutor>(vespalib::eval::value_from_spec(spec, factory));
    }
    static fef::FeatureExecutor &createEmpty(vespalib::Stash &stash) {
        return createEmpty(vespalib::eval::ValueType::double_type(), stash);
    }
};

/**
 * Feature executor that returns a constant tensor.
 */
class ConstantTensorRefExecutor : public fef::FeatureExecutor
{
private:
    const vespalib::eval::Value &_tensor_ref;
public:
    ConstantTensorRefExecutor(const vespalib::eval::Value &tensor_ref)
      : _tensor_ref(tensor_ref) {}
    bool isPure() final override { return true; }
    void execute(uint32_t) final override {
        outputs().set_object(0, _tensor_ref);
    }
};

}