aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/vespa/eval/instruction/vector_from_doubles_function.cpp
blob: d8e1d7cb52f8aad3220ea323ace59c5ea41eff4b (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "vector_from_doubles_function.h"
#include <vespa/eval/eval/value.h>

namespace vespalib::eval {

using Child = TensorFunction::Child;
using namespace tensor_function;

namespace {

struct CallVectorFromDoubles {
    template <typename CT>
    static TypedCells
    invoke(InterpretedFunction::State &state, size_t numCells) {
        ArrayRef<CT> outputCells = state.stash.create_uninitialized_array<CT>(numCells);
        for (size_t i = numCells; i-- > 0; ) {
            outputCells[i] = (CT) state.peek(0).as_double();
            state.stack.pop_back();
        }
        return TypedCells(outputCells);
    }
};

void my_vector_from_doubles_op(InterpretedFunction::State &state, uint64_t param) {
    const auto &self = unwrap_param<VectorFromDoublesFunction::Self>(param);
    CellType ct = self.resultType.cell_type();
    size_t numCells = self.resultSize;
    using MyTypify = TypifyCellType;
    TypedCells cells = typify_invoke<1,MyTypify,CallVectorFromDoubles>(ct, state, numCells);
    const Value &result = state.stash.create<DenseValueView>(self.resultType, cells);
    state.stack.emplace_back(result);
}

size_t vector_size(const TensorFunction &child, const vespalib::string &dimension) {
    if (child.result_type().is_double()) {
        return 1;
    }
    if (auto vfd = as<VectorFromDoublesFunction>(child)) {
        if (vfd->dimension() == dimension) {
            return vfd->size();
        }
    }
    return 0;
}

void flatten_into(const TensorFunction &child, std::vector<Child> &vec) {
    if (child.result_type().is_double()) {
        vec.emplace_back(child);
    } else {
        std::vector<Child::CREF> tmp;
        child.push_children(tmp);
        for (const Child &c : tmp) {
            assert(c.get().result_type().is_double());
            vec.push_back(c);
        }
    }
}

std::vector<Child> flatten(const TensorFunction &lhs, const TensorFunction &rhs) {
    std::vector<Child> vec;
    flatten_into(lhs, vec);
    flatten_into(rhs, vec);
    return vec;
}

} // namespace vespalib::eval::<unnamed>


VectorFromDoublesFunction::VectorFromDoublesFunction(std::vector<Child> children, const ValueType &res_type)
    : TensorFunction(),
      _self(res_type, children.size()),
      _children(std::move(children))
{
}

VectorFromDoublesFunction::~VectorFromDoublesFunction() = default;

void
VectorFromDoublesFunction::push_children(std::vector<Child::CREF> &target) const
{
    for (const Child &c : _children) {
        target.emplace_back(c);
    }
}

InterpretedFunction::Instruction
VectorFromDoublesFunction::compile_self(const CTFContext &) const
{
    return InterpretedFunction::Instruction(my_vector_from_doubles_op, wrap_param<VectorFromDoublesFunction::Self>(_self));
}

const TensorFunction &
VectorFromDoublesFunction::optimize(const TensorFunction &expr, Stash &stash)
{
    if (auto concat = as<Concat>(expr)) {
        const vespalib::string &dimension = concat->dimension();
        size_t a_size = vector_size(concat->lhs(), dimension);
        size_t b_size = vector_size(concat->rhs(), dimension);
        if ((a_size > 0) && (b_size > 0)) {
            auto children = flatten(concat->lhs(), concat->rhs());
            assert(children.size() == (a_size + b_size));
            return stash.create<VectorFromDoublesFunction>(std::move(children), expr.result_type());
        }
    }
    return expr;
}

} // namespace vespalib::eval