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
|
// 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/tensor/dense/dense_dot_product_function.h>
#include <vespa/eval/tensor/dense/dense_tensor_function_compiler.h>
using namespace vespalib::eval;
using namespace vespalib::eval::operation;
using namespace vespalib::eval::tensor_function;
using namespace vespalib::tensor;
template <typename T>
const T *as(const TensorFunction &function) { return dynamic_cast<const T *>(&function); }
TensorFunction::UP
compileDotProduct(const vespalib::string &lhsType,
const vespalib::string &rhsType)
{
Node_UP reduceNode = reduce(apply(Mul(),
inject(ValueType::from_spec(lhsType), 1),
inject(ValueType::from_spec(rhsType), 3)),
Add(), {});
return DenseTensorFunctionCompiler::compile(std::move(reduceNode));
}
void
assertCompiledDotProduct(const vespalib::string &lhsType,
const vespalib::string &rhsType)
{
TensorFunction::UP func = compileDotProduct(lhsType, rhsType);
const DenseDotProductFunction *dotProduct = as<DenseDotProductFunction>(*func);
ASSERT_TRUE(dotProduct);
EXPECT_EQUAL(1u, dotProduct->lhsTensorId());
EXPECT_EQUAL(3u, dotProduct->rhsTensorId());
}
void
assertNotCompiledDotProduct(const vespalib::string &lhsType,
const vespalib::string &rhsType)
{
TensorFunction::UP func = compileDotProduct(lhsType, rhsType);
const Reduce *reduce = as<Reduce>(*func);
EXPECT_TRUE(reduce);
}
TEST("require that dot product with compatible dimensions is compiled")
{
TEST_DO(assertCompiledDotProduct("tensor(x[5])", "tensor(x[5])"));
TEST_DO(assertCompiledDotProduct("tensor(x[3])", "tensor(x[5])"));
TEST_DO(assertCompiledDotProduct("tensor(x[5])", "tensor(x[3])"));
TEST_DO(assertCompiledDotProduct("tensor(x[])", "tensor(x[5])"));
TEST_DO(assertCompiledDotProduct("tensor(x[5])", "tensor(x[])"));
TEST_DO(assertCompiledDotProduct("tensor(x[])", "tensor(x[])"));
}
TEST("require that dot product with incompatible dimensions is NOT compiled")
{
TEST_DO(assertNotCompiledDotProduct("tensor(x[5])", "tensor(y[5])"));
TEST_DO(assertNotCompiledDotProduct("tensor(y[5])", "tensor(x[5])"));
TEST_DO(assertNotCompiledDotProduct("tensor(y[])", "tensor(x[])"));
TEST_DO(assertNotCompiledDotProduct("tensor(x[5])", "tensor(x[5],y[7])"));
TEST_DO(assertNotCompiledDotProduct("tensor(x[5],y[7])", "tensor(x[5],y[7])"));
}
TEST_MAIN() { TEST_RUN_ALL(); }
|