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

#include "interpreted_function.h"
#include "node_visitor.h"
#include "node_traverser.h"
#include "tensor_nodes.h"
#include "make_tensor_function.h"
#include "optimize_tensor_function.h"
#include "compile_tensor_function.h"
#include <vespa/vespalib/util/classname.h>
#include <vespa/eval/eval/llvm/compile_cache.h>
#include <vespa/eval/eval/llvm/addr_to_symbol.h>
#include <vespa/vespalib/util/benchmark_timer.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <set>

namespace vespalib::eval {

namespace {

const Function *get_simple_lambda(const nodes::Node &node) {
    if (auto ptr = nodes::as<nodes::TensorMap>(node)) {
        return &ptr->lambda();
    }
    if (auto ptr = nodes::as<nodes::TensorJoin>(node)) {
        return &ptr->lambda();
    }
    if (auto ptr = nodes::as<nodes::TensorMerge>(node)) {
        return &ptr->lambda();
    }
    return nullptr;
}

const Function *get_complex_lambda(const nodes::Node &node) {
    if (auto ptr = nodes::as<nodes::TensorLambda>(node)) {
        return &ptr->lambda();
    }
    if (auto ptr = nodes::as<nodes::TensorMapSubspaces>(node)) {
        return &ptr->lambda();
    }
    return nullptr;
}

void my_nop(InterpretedFunction::State &, uint64_t) {}

} // namespace vespalib::<unnamed>


InterpretedFunction::State::State(const ValueBuilderFactory &factory_in)
    : factory(factory_in),
      params(nullptr),
      stash(),
      stack(),
      program_offset(0),
      if_cnt(0)
{
}

InterpretedFunction::State::~State() = default;

void
InterpretedFunction::State::init(const LazyParams &params_in) {
    params = &params_in;
    stash.clear();
    stack.clear();
    program_offset = 0;
    if_cnt = 0;
}

InterpretedFunction::Context::Context(const InterpretedFunction &ifun)
  : _state(ifun._factory)
{
}

InterpretedFunction::ProfiledContext::ProfiledContext(const InterpretedFunction &ifun)
  : context(ifun),
    cost(ifun.program_size(), std::make_pair(size_t(0), duration::zero()))
{
}

InterpretedFunction::ProfiledContext::~ProfiledContext() = default;

vespalib::string
InterpretedFunction::Instruction::resolve_symbol() const
{
    if (function == nullptr) {
        return "<inject_param>";
    }
    return addr_to_symbol((const void *)function);
}

InterpretedFunction::Instruction
InterpretedFunction::Instruction::nop()
{
    return Instruction(my_nop);
}

InterpretedFunction::Options::Options(const ValueBuilderFactory &factory_in)
  : _factory(factory_in),
    _optimize(optimize_tensor_function),
    _meta(nullptr)
{
}

InterpretedFunction::InterpretedFunction(const ValueBuilderFactory &factory, const TensorFunction &function)
  : _program(),
    _stash(),
    _factory(factory)
{
    _program = compile_tensor_function(function, CTFContext(factory, _stash, nullptr));
}

InterpretedFunction::InterpretedFunction(const Options &opts, const nodes::Node &root, const NodeTypes &types)
    : _program(),
      _stash(),
      _factory(opts.factory())
{
    const TensorFunction &plain_fun = make_tensor_function(opts.factory(), root, types, _stash);
    const TensorFunction &optimized = opts.optimize()(opts.factory(), plain_fun, _stash);
    _program = compile_tensor_function(optimized, CTFContext(opts.factory(), _stash, opts.meta()));
}

InterpretedFunction::~InterpretedFunction() = default;

const Value &
InterpretedFunction::eval(Context &ctx, const LazyParams &params) const
{
    State &state = ctx._state;
    state.init(params);
    while (state.program_offset < _program.size()) {
        _program[state.program_offset++].perform(state);
    }
    assert(state.stack.size() == 1);
    return state.stack.back();
}

const Value &
InterpretedFunction::eval(ProfiledContext &pctx, const LazyParams &params) const
{
    auto &ctx = pctx.context;                            // Profiling
    State &state = ctx._state;
    state.init(params);
    while (state.program_offset < _program.size()) {
        auto pos = state.program_offset;                 // Profiling
        auto before = steady_clock::now();               // Profiling
        _program[state.program_offset++].perform(state);
        auto after = steady_clock::now();                // Profiling
        ++pctx.cost[pos].first;                          // Profiling
        pctx.cost[pos].second += (after - before);       // Profiling
    }
    assert(state.stack.size() == 1);
    return state.stack.back();
}

double
InterpretedFunction::estimate_cost_us(const std::vector<double> &params, double budget) const
{
    Context ctx(*this);
    SimpleParams lazy_params(params);
    auto actual = [&](){eval(ctx, lazy_params);};
    return BenchmarkTimer::benchmark(actual, budget) * 1000.0 * 1000.0;
}

Function::Issues
InterpretedFunction::detect_issues(const Function &function)
{
    struct NotSupported : NodeTraverser {
        Function::Issues issues;
        bool open(const nodes::Node &) override { return true; }
        void close(const nodes::Node &node) override {
            // map/join/merge: simple scalar lambdas must be compilable with llvm
            if (auto lambda = get_simple_lambda(node)) {
                auto inner_issues = CompiledFunction::detect_issues(*lambda);
                if (inner_issues) {
                    auto ctx = make_string("within %s simple lambda", getClassName(node).c_str());
                    issues.add_nested_issues(ctx, inner_issues);
                }
            }
            // tensor lambda/map_subspaces: complex lambdas that may be interpreted and use tensor math
            if (auto lambda = get_complex_lambda(node)) {
                auto inner_issues = InterpretedFunction::detect_issues(*lambda);
                if (inner_issues) {
                    auto ctx = make_string("within %s complex lambda", getClassName(node).c_str());
                    issues.add_nested_issues(ctx, inner_issues);
                }
            }
        }
    } checker;
    function.root().traverse(checker);
    return std::move(checker.issues);
}

InterpretedFunction::EvalSingle::EvalSingle(const ValueBuilderFactory &factory, Instruction op, const LazyParams &params)
    : _state(factory),
      _op(op)
{
    _state.params = &params;
}

const Value &
InterpretedFunction::EvalSingle::eval(const std::vector<Value::CREF> &stack)
{
    _state.stash.clear();
    _state.stack = stack;
    _op.perform(_state);
    assert(_state.stack.size() == 1);
    return _state.stack.back();
}

}