summaryrefslogtreecommitdiffstats
path: root/eval/src/vespa/eval/eval/llvm/compiled_function.cpp
blob: b0bf1583c686e0d23d9f839e0dd7832c2a0af2cc (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "compiled_function.h"
#include <vespa/eval/eval/param_usage.h>
#include <vespa/eval/eval/gbdt.h>
#include <vespa/eval/eval/node_traverser.h>
#include <vespa/eval/eval/check_type.h>
#include <vespa/eval/eval/tensor_nodes.h>
#include <vespa/vespalib/util/classname.h>
#include <vespa/vespalib/util/benchmark_timer.h>
#include <vespa/vespalib/util/approx.h>

#include <vespa/log/log.h>
LOG_SETUP(".eval.eval.llvm.compiled_function");

namespace vespalib {
namespace eval {

namespace {

double empty_function_0() { return 0.0; }
double empty_function_1(double) { return 0.0; }
double empty_function_2(double, double) { return 0.0; }
double empty_function_3(double, double, double) { return 0.0; }
double empty_function_4(double, double, double, double) { return 0.0; }
double empty_function_5(double, double, double, double, double) { return 0.0; }
double empty_array_function(const double *) { return 0.0; }
double empty_lazy_function(CompiledFunction::resolve_function, void *) { return 0.0; }

double my_resolve(void *ctx, size_t idx) { return ((double *)ctx)[idx]; }

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

CompiledFunction::CompiledFunction(const nodes::Node &root_in, size_t num_params_in, PassParams pass_params_in,
                                   const gbdt::Optimize::Chain &forest_optimizers)
    : _llvm_wrapper(),
      _address(nullptr),
      _num_params(num_params_in),
      _pass_params(pass_params_in)
{
    size_t id = _llvm_wrapper.make_function(num_params_in,
                                            _pass_params,
                                            root_in,
                                            forest_optimizers);
    _llvm_wrapper.compile();
    _address = _llvm_wrapper.get_function_address(id);
}

CompiledFunction::CompiledFunction(CompiledFunction &&rhs)
    : _llvm_wrapper(std::move(rhs._llvm_wrapper)),
      _address(rhs._address),
      _num_params(rhs._num_params),
      _pass_params(rhs._pass_params)
{
    rhs._address = nullptr;
}

double
CompiledFunction::estimate_cost_us(const std::vector<double> &params, double budget) const
{
    assert(params.size() == _num_params);
    if (_pass_params == PassParams::ARRAY) {
        auto function = get_function();
        auto empty = empty_array_function;
        auto actual = [&](){function(&params[0]);};
        auto baseline = [&](){empty(&params[0]);};
        return BenchmarkTimer::benchmark(actual, baseline, budget) * 1000.0 * 1000.0;
    }
    if (_pass_params == PassParams::LAZY) {
        auto function = get_lazy_function();
        auto empty = empty_lazy_function;
        auto actual = [&](){function(my_resolve, const_cast<double*>(&params[0]));};
        auto baseline = [&](){empty(my_resolve, const_cast<double*>(&params[0]));};
        return BenchmarkTimer::benchmark(actual, baseline, budget) * 1000.0 * 1000.0;
    }
    assert(_pass_params == PassParams::SEPARATE);
    if (params.size() == 0) {
        auto function = get_function<0>();
        auto empty = empty_function_0;
        auto actual = [&](){function();};
        auto baseline = [&](){empty();};
        return BenchmarkTimer::benchmark(actual, baseline, budget) * 1000.0 * 1000.0;
    }
    if (params.size() == 1) {
        auto function = get_function<1>();
        auto empty = empty_function_1;
        auto actual = [&](){function(params[0]);};
        auto baseline = [&](){empty(params[0]);};
        return BenchmarkTimer::benchmark(actual, baseline, budget) * 1000.0 * 1000.0;
    }
    if (params.size() == 2) {
        auto function = get_function<2>();
        auto empty = empty_function_2;        
        auto actual = [&](){function(params[0], params[1]);};
        auto baseline = [&](){empty(params[0], params[1]);};
        return BenchmarkTimer::benchmark(actual, baseline, budget) * 1000.0 * 1000.0;
    }
    if (params.size() == 3) {
        auto function = get_function<3>();
        auto empty = empty_function_3;
        auto actual = [&](){function(params[0], params[1], params[2]);};
        auto baseline = [&](){empty(params[0], params[1], params[2]);};
        return BenchmarkTimer::benchmark(actual, baseline, budget) * 1000.0 * 1000.0;
    }
    if (params.size() == 4) {
        auto function = get_function<4>();
        auto empty = empty_function_4;
        auto actual = [&](){function(params[0], params[1], params[2], params[3]);};
        auto baseline = [&](){empty(params[0], params[1], params[2], params[3]);};
        return BenchmarkTimer::benchmark(actual, baseline, budget) * 1000.0 * 1000.0;
    }
    if (params.size() == 5) {
        auto function = get_function<5>();
        auto empty = empty_function_5;
        auto actual = [&](){function(params[0], params[1], params[2], params[3], params[4]);};
        auto baseline = [&](){empty(params[0], params[1], params[2], params[3], params[4]);};
        return BenchmarkTimer::benchmark(actual, baseline, budget) * 1000.0 * 1000.0;
    }
    LOG_ABORT("should not be reached");
}

Function::Issues
CompiledFunction::detect_issues(const nodes::Node &node)
{
    struct NotSupported : NodeTraverser {
        std::vector<vespalib::string> issues;
        bool open(const nodes::Node &) override { return true; }
        void close(const nodes::Node &node) override {
            if (nodes::check_type<nodes::TensorMap,
                                  nodes::TensorJoin,
                                  nodes::TensorMerge,
                                  nodes::TensorReduce,
                                  nodes::TensorRename,
                                  nodes::TensorConcat,
                                  nodes::TensorCellCast,
                                  nodes::TensorCreate,
                                  nodes::TensorLambda,
                                  nodes::TensorPeek>(node))
            {
                issues.push_back(make_string("unsupported node type: %s",
                                getClassName(node).c_str()));
            }
        }
    } checker;
    node.traverse(checker);
    return Function::Issues(std::move(checker.issues));
}

bool
CompiledFunction::should_use_lazy_params(const Function &function)
{
    if (gbdt::contains_gbdt(function.root(), 16)) {
        return false; // contains gbdt
    }
    auto usage = vespalib::eval::check_param_usage(function);
    for (double p_use: usage) {
        if (!approx_equal(p_use, 1.0)) {
            return true; // param not always used
        }
    }
    return false; // all params always used
}

} // namespace vespalib::eval
} // namespace vespalib