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
|
// Copyright Vespa.ai. 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/tensor_function.h>
#include <vespa/eval/instruction/replace_type_function.h>
#include <vespa/eval/instruction/fast_rename_optimizer.h>
#include <vespa/eval/eval/test/gen_spec.h>
#include <vespa/eval/eval/test/eval_fixture.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/util/stash.h>
using namespace vespalib;
using namespace vespalib::eval;
using namespace vespalib::eval::test;
using namespace vespalib::eval::tensor_function;
const ValueBuilderFactory &prod_factory = FastValueBuilderFactory::get();
EvalFixture::ParamRepo make_params() {
return EvalFixture::ParamRepo()
.add("x1y5z1", GenSpec().idx("x", 1).idx("y", 5).idx("z", 1))
.add("x1y5z1f", GenSpec().idx("x", 1).idx("y", 5).idx("z", 1).cells_float())
.add("x1y1z1", GenSpec().idx("x", 1).idx("y", 1).idx("z", 1))
.add("x1y5z_m", GenSpec().idx("x", 1).idx("y", 5).map("z", {"a"}));
}
EvalFixture::ParamRepo param_repo = make_params();
void verify_optimized(const vespalib::string &expr) {
EvalFixture fixture(prod_factory, expr, param_repo, true);
EXPECT_EQUAL(fixture.result(), EvalFixture::ref(expr, param_repo));
auto info = fixture.find_all<ReplaceTypeFunction>();
EXPECT_EQUAL(info.size(), 1u);
}
void verify_not_optimized(const vespalib::string &expr) {
EvalFixture fixture(prod_factory, expr, param_repo, true);
EXPECT_EQUAL(fixture.result(), EvalFixture::ref(expr, param_repo));
auto info = fixture.find_all<ReplaceTypeFunction>();
EXPECT_TRUE(info.empty());
}
TEST("require that dimension removal can be optimized for appropriate aggregators") {
TEST_DO(verify_optimized("reduce(x1y5z1,avg,x)"));
TEST_DO(verify_not_optimized("reduce(x1y5z1,count,x)")); // NB
TEST_DO(verify_optimized("reduce(x1y5z1,prod,x)"));
TEST_DO(verify_optimized("reduce(x1y5z1,sum,x)"));
TEST_DO(verify_optimized("reduce(x1y5z1,max,x)"));
TEST_DO(verify_optimized("reduce(x1y5z1,min,x)"));
}
TEST("require that multi-dimension removal can be optimized") {
TEST_DO(verify_optimized("reduce(x1y5z1,sum,x,z)"));
}
TEST("require that chained dimension removal can be optimized (and compacted)") {
TEST_DO(verify_optimized("reduce(reduce(x1y5z1,sum,x),sum,z)"));
}
TEST("require that reducing non-trivial dimension is not optimized") {
TEST_DO(verify_not_optimized("reduce(x1y5z1,sum,y)"));
TEST_DO(verify_not_optimized("reduce(x1y5z1,sum,x,y)"));
TEST_DO(verify_not_optimized("reduce(x1y5z1,sum,y,z)"));
}
TEST("require that full reduce is not optimized") {
TEST_DO(verify_not_optimized("reduce(x1y1z1,sum)"));
TEST_DO(verify_not_optimized("reduce(x1y1z1,sum,x,y,z)"));
}
TEST("require that mixed tensor types can be optimized") {
TEST_DO(verify_optimized("reduce(x1y5z_m,sum,x)"));
TEST_DO(verify_not_optimized("reduce(x1y5z_m,sum,y)"));
TEST_DO(verify_not_optimized("reduce(x1y5z_m,sum,z)"));
}
TEST("require that optimization works for float cells") {
TEST_DO(verify_optimized("reduce(x1y5z1f,avg,x)"));
}
TEST_MAIN() { TEST_RUN_ALL(); }
|