summaryrefslogtreecommitdiffstats
path: root/eval/src/tests/tensor/tensor_performance/tensor_performance_test.cpp
blob: c7d4006b749f4d8640daba9965e24e9cdea0e51f (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/eval/eval/function.h>
#include <vespa/eval/eval/interpreted_function.h>
#include <vespa/eval/eval/tensor_nodes.h>
#include <vespa/eval/eval/tensor_spec.h>
#include <vespa/eval/tensor/sparse/sparse_tensor.h>
#include <vespa/eval/tensor/sparse/sparse_tensor_builder.h>
#include <vespa/eval/tensor/dense/dense_tensor_builder.h>
#include <vespa/eval/tensor/tensor.h>
#include <vespa/eval/tensor/tensor_builder.h>
#include <vespa/vespalib/util/benchmark_timer.h>
#include <vespa/eval/tensor/default_tensor_engine.h>

using namespace vespalib;
using namespace vespalib::eval;
using namespace vespalib::tensor;

//-----------------------------------------------------------------------------

const vespalib::string dot_product_match_expr    = "sum(query*document)";
const vespalib::string dot_product_multiply_expr = "sum(query*document)";
const vespalib::string model_match_expr          = "sum((query*document)*model)";
const vespalib::string matrix_product_expr       = "sum(sum((query+document)*model,x))";

//-----------------------------------------------------------------------------

Value::UP wrap(std::unique_ptr<eval::Tensor> tensor) {
    return Value::UP(new TensorValue(std::move(tensor)));
}

//-----------------------------------------------------------------------------

struct Params {
    std::map<vespalib::string, Value::UP> map;
    Params &add(const vespalib::string &name, Value::UP value) {
        map.emplace(name, std::move(value));
        return *this;
    }
    Params &add(const vespalib::string &name, std::unique_ptr<eval::Tensor> value) {
        return add(name, wrap(std::move(value)));
    }
};

InterpretedFunction::SimpleObjectParams make_params(const Function &function, const Params &params)
{
    InterpretedFunction::SimpleObjectParams fun_params({});
    EXPECT_EQUAL(params.map.size(), function.num_params());
    for (size_t i = 0; i < function.num_params(); ++i) {
        auto param = params.map.find(function.param_name(i));
        ASSERT_TRUE(param != params.map.end());
        fun_params.params.push_back(*(param->second));
    }
    return fun_params;
}

std::vector<ValueType> extract_param_types(const Function &function, const Params &params) {
    std::vector<ValueType> result;
    EXPECT_EQUAL(params.map.size(), function.num_params());
    for (size_t i = 0; i < function.num_params(); ++i) {
        auto param = params.map.find(function.param_name(i));
        ASSERT_TRUE(param != params.map.end());
        result.push_back(param->second->type());
    }
    return result;
}

double calculate_expression(const vespalib::string &expression, const Params &params) {
    const Function function = Function::parse(expression);
    const NodeTypes types(function, extract_param_types(function, params));
    const InterpretedFunction interpreted(tensor::DefaultTensorEngine::ref(), function, types);
    InterpretedFunction::Context context(interpreted);
    auto fun_params = make_params(function, params);
    const Value &result = interpreted.eval(context, fun_params);
    EXPECT_TRUE(result.is_double());
    return result.as_double();
}

DoubleValue dummy_result(0.0);
const Value &dummy_ranking(InterpretedFunction::Context &, InterpretedFunction::LazyParams &) { return dummy_result; }

double benchmark_expression_us(const vespalib::string &expression, const Params &params) {
    const Function function = Function::parse(expression);
    const NodeTypes types(function, extract_param_types(function, params));
    const InterpretedFunction interpreted(tensor::DefaultTensorEngine::ref(), function, types);
    InterpretedFunction::Context context(interpreted);
    auto fun_params = make_params(function, params);
    auto ranking = [&](){ interpreted.eval(context, fun_params); };
    auto baseline = [&](){ dummy_ranking(context, fun_params); };
    return BenchmarkTimer::benchmark(ranking, baseline, 5.0) * 1000.0 * 1000.0;
}

//-----------------------------------------------------------------------------

tensor::Tensor::UP make_tensor(const TensorSpec &spec) {
    auto tensor = DefaultTensorEngine::ref().create(spec);
    return tensor::Tensor::UP(dynamic_cast<tensor::Tensor*>(tensor.release()));
}

//-----------------------------------------------------------------------------

TEST("SMOKETEST - require that dot product benchmark expressions produce expected results") {
    Params params;
    params.add("query",    make_tensor(TensorSpec("tensor(x{})")
                                       .add({{"x","0"}}, 1.0)
                                       .add({{"x","1"}}, 2.0)
                                       .add({{"x","2"}}, 3.0)));
    params.add("document", make_tensor(TensorSpec("tensor(x{})")
                                       .add({{"x","0"}}, 2.0)
                                       .add({{"x","1"}}, 2.0)
                                       .add({{"x","2"}}, 2.0)));
    EXPECT_EQUAL(calculate_expression(dot_product_match_expr, params), 12.0);
    EXPECT_EQUAL(calculate_expression(dot_product_multiply_expr, params), 12.0);
}

TEST("SMOKETEST - require that model match benchmark expression produces expected result") {
    Params params;
    params.add("query",    make_tensor(TensorSpec("tensor(x{})")
                                       .add({{"x","0"}}, 1.0)
                                       .add({{"x","1"}}, 2.0)));
    params.add("document", make_tensor(TensorSpec("tensor(y{})")
                                       .add({{"y","0"}}, 3.0)
                                       .add({{"y","1"}}, 4.0)));
    params.add("model",    make_tensor(TensorSpec("tensor(x{},y{})")
                                       .add({{"x","0"},{"y","0"}}, 2.0)
                                       .add({{"x","0"},{"y","1"}}, 2.0)
                                       .add({{"x","1"},{"y","0"}}, 2.0)
                                       .add({{"x","1"},{"y","1"}}, 2.0)));
    EXPECT_EQUAL(calculate_expression(model_match_expr, params), 42.0);
}

TEST("SMOKETEST - require that matrix product benchmark expression produces expected result") {
    Params params;
    params.add("query",    make_tensor(TensorSpec("tensor(x{})")
                                       .add({{"x","0"}}, 1.0)
                                       .add({{"x","1"}}, 0.0)));
    params.add("document", make_tensor(TensorSpec("tensor(x{})")
                                       .add({{"x","0"}}, 0.0)
                                       .add({{"x","1"}}, 2.0)));
    params.add("model",    make_tensor(TensorSpec("tensor(x{},y{})")
                                       .add({{"x","0"},{"y","0"}}, 1.0)
                                       .add({{"x","0"},{"y","1"}}, 2.0)
                                       .add({{"x","1"},{"y","0"}}, 3.0)
                                       .add({{"x","1"},{"y","1"}}, 4.0)));
    EXPECT_EQUAL(calculate_expression(matrix_product_expr, params), 17.0);
}

//-----------------------------------------------------------------------------

struct DummyBuilder : TensorBuilder {
    Dimension define_dimension(const vespalib::string &) override { return 0; }
    TensorBuilder &add_label(Dimension, const vespalib::string &) override { return *this; }
    TensorBuilder &add_cell(double) override { return *this; }
    tensor::Tensor::UP build() override { return tensor::Tensor::UP(); }
};


struct DummyDenseTensorBuilder
{
    using Dimension = TensorBuilder::Dimension;
    Dimension defineDimension(const vespalib::string &, size_t) { return 0; }
    DummyDenseTensorBuilder &addLabel(Dimension, size_t) { return *this; }
    DummyDenseTensorBuilder &addCell(double) { return *this; }
    tensor::Tensor::UP build() { return tensor::Tensor::UP(); }
};

struct DimensionSpec {
    vespalib::string name;
    size_t count;
    size_t offset;
    DimensionSpec(const vespalib::string &name_in, size_t count_in, size_t offset_in = 0)
        : name(name_in), count(count_in), offset(offset_in) {}
};

struct StringBinding {
    TensorBuilder::Dimension dimension;
    vespalib::string label;
    StringBinding(TensorBuilder &builder, const DimensionSpec &dimension_in)
        : dimension(builder.define_dimension(dimension_in.name)),
          label()
    {
    }
    void set_label(size_t id) {
        label = vespalib::make_string("%zu", id);
    }
    static void add_cell(TensorBuilder &builder, double value) {
        builder.add_cell(value);
    }
    void add_label(TensorBuilder &builder) const {
        builder.add_label(dimension, label);
    }
};

struct NumberBinding {
    TensorBuilder::Dimension dimension;
    size_t label;
    template <typename Builder>
    NumberBinding(Builder &builder, const DimensionSpec &dimension_in)
        : dimension(builder.defineDimension(dimension_in.name,
                                            dimension_in.offset +
                                            dimension_in.count)),
          label()
    {
    }
    void set_label(size_t id) {
        label = id;
    }
    template <typename Builder>
    static void add_cell(Builder &builder, double value) {
        builder.addCell(value);
    }
    template <typename Builder>
    void add_label(Builder &builder) const {
        builder.addLabel(dimension, label);
    }
};


template <typename Builder, typename Binding>
void build_tensor(Builder &builder, const std::vector<DimensionSpec> &dimensions,
                  std::vector<Binding> &bindings)
{
    if (bindings.size() == dimensions.size()) {
        for (const auto &bound: bindings) {
            bound.add_label(builder);
        }
        Binding::add_cell(builder, 42);
    } else {
        const auto &spec = dimensions[bindings.size()];
        bindings.emplace_back(builder, spec);
        for (size_t i = 0; i < spec.count; ++i) {
            bindings.back().set_label(spec.offset + i);
            build_tensor(builder, dimensions, bindings);
        }
        bindings.pop_back();
    }
}

template <typename Builder, typename IBuilder, typename Binding>
tensor::Tensor::UP make_tensor_impl(const std::vector<DimensionSpec> &dimensions) {
    Builder builder;
    std::vector<Binding> bindings;
    bindings.reserve(dimensions.size());
    build_tensor<IBuilder, Binding>(builder, dimensions, bindings);
    return builder.build();
}

//-----------------------------------------------------------------------------

enum class BuilderType { DUMMY, SPARSE, NUMBERDUMMY,
        DENSE };

const BuilderType DUMMY = BuilderType::DUMMY;
const BuilderType SPARSE = BuilderType::SPARSE;
const BuilderType NUMBERDUMMY = BuilderType::NUMBERDUMMY;
const BuilderType DENSE = BuilderType::DENSE;

const char *name(BuilderType type) {
    switch (type) {
    case BuilderType::DUMMY:   return "  dummy";
    case BuilderType::SPARSE: return "sparse";
    case BuilderType::NUMBERDUMMY: return "numberdummy";
    case BuilderType::DENSE: return "dense";
    }
    abort();
}

tensor::Tensor::UP make_tensor(BuilderType type, const std::vector<DimensionSpec> &dimensions) {
    switch (type) {
    case BuilderType::DUMMY:
        return make_tensor_impl<DummyBuilder, TensorBuilder, StringBinding>
            (dimensions);
    case BuilderType::SPARSE:
        return make_tensor_impl<SparseTensorBuilder, TensorBuilder,
            StringBinding>(dimensions);
    case BuilderType::NUMBERDUMMY:
        return make_tensor_impl<DummyDenseTensorBuilder,
            DummyDenseTensorBuilder, NumberBinding>(dimensions);
    case BuilderType::DENSE:
        return make_tensor_impl<DenseTensorBuilder, DenseTensorBuilder,
            NumberBinding>(dimensions);
    }
    abort();
}

//-----------------------------------------------------------------------------

struct BuildTask {
    BuilderType type;
    std::vector<DimensionSpec> spec;
    BuildTask(BuilderType type_in, const std::vector<DimensionSpec> &spec_in) : type(type_in), spec(spec_in) {}
    void operator()() { tensor::Tensor::UP tensor = make_tensor(type, spec); }
};

double benchmark_build_us(BuilderType type, const std::vector<DimensionSpec> &spec) {
    BuildTask build_task(type, spec);
    BuildTask dummy_task((type == DENSE) ? NUMBERDUMMY : DUMMY, spec);
    return BenchmarkTimer::benchmark(build_task, dummy_task, 5.0) * 1000.0 * 1000.0;
}

TEST("benchmark create/destroy time for 1d tensors") {
    for (size_t size: {5, 10, 25, 50, 100, 250, 500}) {
        for (auto type: {SPARSE, DENSE}) {
            double time_us = benchmark_build_us(type, {DimensionSpec("x", size)});
            fprintf(stderr, "-- 1d tensor create/destroy (%s) with size %zu: %g us\n", name(type), size, time_us);
        }
    }
}

TEST("benchmark create/destroy time for 2d tensors") {
    for (size_t size: {5, 10, 25, 50, 100}) {
        for (auto type: {SPARSE, DENSE}) {
            double time_us = benchmark_build_us(type, {DimensionSpec("x", size), DimensionSpec("y", size)});
            fprintf(stderr, "-- 2d tensor create/destroy (%s) with size %zux%zu: %g us\n", name(type), size, size, time_us);
        }
    }
}

//-----------------------------------------------------------------------------

TEST("benchmark dot product using match") {
    for (size_t size: {10, 25, 50, 100, 250}) {
        for (auto type: {SPARSE, DENSE}) {
            Params params;
            params.add("query",    make_tensor(type, {DimensionSpec("x", size)}));
            params.add("document", make_tensor(type, {DimensionSpec("x", size)}));
            double time_us = benchmark_expression_us(dot_product_match_expr, params);
            fprintf(stderr, "-- dot product (%s) using match %zu vs %zu: %g us\n", name(type), size, size, time_us);
        }
    }
}

TEST("benchmark dot product using multiply") {
    for (size_t size: {10, 25, 50, 100, 250}) {
        for (auto type: {SPARSE, DENSE}) {
            Params params;
            params.add("query",    make_tensor(type, {DimensionSpec("x", size)}));
            params.add("document", make_tensor(type, {DimensionSpec("x", size)}));
            double time_us = benchmark_expression_us(dot_product_multiply_expr, params);
            fprintf(stderr, "-- dot product (%s) using multiply %zu vs %zu: %g us\n", name(type), size, size, time_us);
        }
    }
}

TEST("benchmark model match") {
    for (size_t model_size: {25, 50, 100}) {
        for (size_t vector_size: {5, 10, 25, 50, 100}) {
            if (vector_size <= model_size) {
                for (auto type: {SPARSE}) {
                    Params params;
                    params.add("query",    make_tensor(type, {DimensionSpec("x", vector_size)}));
                    params.add("document", make_tensor(type, {DimensionSpec("y", vector_size)}));
                    params.add("model",    make_tensor(type, {DimensionSpec("x", model_size), DimensionSpec("y", model_size)}));
                    double time_us = benchmark_expression_us(model_match_expr, params);
                    fprintf(stderr, "-- model match (%s) %zu * %zu vs %zux%zu: %g us\n", name(type), vector_size, vector_size, model_size, model_size, time_us);
                }
            }
        }
    }
}

TEST("benchmark matrix product") {
    for (size_t vector_size: {5, 10, 25, 50}) {
        size_t matrix_size = vector_size * 2;
        for (auto type: {SPARSE, DENSE}) {
            Params params;
            params.add("query",    make_tensor(type, {DimensionSpec("x", matrix_size)}));
            params.add("document", make_tensor(type, {DimensionSpec("x", matrix_size)}));
            params.add("model",    make_tensor(type, {DimensionSpec("x", matrix_size), DimensionSpec("y", matrix_size)}));
            double time_us = benchmark_expression_us(matrix_product_expr, params);
            fprintf(stderr, "-- matrix product (%s) %zu + %zu vs %zux%zu: %g us\n", name(type), vector_size, vector_size, matrix_size, matrix_size, time_us);
        }
    }
}

//-----------------------------------------------------------------------------

TEST_MAIN() { TEST_RUN_ALL(); }