diff options
Diffstat (limited to 'eval/src/tests')
-rw-r--r-- | eval/src/tests/eval/function/function_test.cpp | 11 | ||||
-rw-r--r-- | eval/src/tests/eval/tensor_lambda/CMakeLists.txt | 8 | ||||
-rw-r--r-- | eval/src/tests/eval/tensor_lambda/tensor_lambda_test.cpp | 100 |
3 files changed, 112 insertions, 7 deletions
diff --git a/eval/src/tests/eval/function/function_test.cpp b/eval/src/tests/eval/function/function_test.cpp index 5316217d549..11fd8abf549 100644 --- a/eval/src/tests/eval/function/function_test.cpp +++ b/eval/src/tests/eval/function/function_test.cpp @@ -813,8 +813,8 @@ TEST("require that tensor rename dimension lists must have equal size") { //----------------------------------------------------------------------------- TEST("require that tensor lambda can be parsed") { - EXPECT_EQUAL("tensor(x[3]):{{x:0}:0,{x:1}:1,{x:2}:2}", Function::parse({}, "tensor(x[3])(x)")->dump()); - EXPECT_EQUAL("tensor(x[2],y[2]):{{x:0,y:0}:(0==0),{x:0,y:1}:(0==1),{x:1,y:0}:(1==0),{x:1,y:1}:(1==1)}", + EXPECT_EQUAL("tensor(x[3])(x)", Function::parse({}, "tensor(x[3])(x)")->dump()); + EXPECT_EQUAL("tensor(x[2],y[2])(x==y)", Function::parse({}, " tensor ( x [ 2 ] , y [ 2 ] ) ( x == y ) ")->dump()); } @@ -825,7 +825,7 @@ TEST("require that tensor lambda requires appropriate tensor type") { } TEST("require that tensor lambda can use non-dimension symbols") { - EXPECT_EQUAL("tensor(x[2]):{{x:0}:(0==a),{x:1}:(1==a)}", + EXPECT_EQUAL("tensor(x[2])(x==a)", Function::parse({"a"}, "tensor(x[2])(x==a)")->dump()); } @@ -1002,11 +1002,8 @@ TEST("require that tensor peek empty label is not allowed") { //----------------------------------------------------------------------------- TEST("require that nested tensor lambda using tensor peek can be parsed") { - vespalib::string expect("tensor(x[2]):{{x:0}:tensor(y[2]):{{y:0}:((0+0)+a),{y:1}:((0+1)+a)}{y:\"0\"}," - "{x:1}:tensor(y[2]):{{y:0}:((1+0)+a),{y:1}:((1+1)+a)}{y:\"1\"}}"); + vespalib::string expect("tensor(x[2])(tensor(y[2])((x+y)+a){y:(x)})"); EXPECT_EQUAL(Function::parse(expect)->dump(), expect); - auto fun = Function::parse("tensor(x[2])(tensor(y[2])(x+y+a){y:(x)})"); - EXPECT_EQUAL(fun->dump(), expect); } //----------------------------------------------------------------------------- diff --git a/eval/src/tests/eval/tensor_lambda/CMakeLists.txt b/eval/src/tests/eval/tensor_lambda/CMakeLists.txt new file mode 100644 index 00000000000..29cbbd936aa --- /dev/null +++ b/eval/src/tests/eval/tensor_lambda/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(eval_tensor_lambda_test_app TEST + SOURCES + tensor_lambda_test.cpp + DEPENDS + vespaeval +) +vespa_add_test(NAME eval_tensor_lambda_test_app COMMAND eval_tensor_lambda_test_app) diff --git a/eval/src/tests/eval/tensor_lambda/tensor_lambda_test.cpp b/eval/src/tests/eval/tensor_lambda/tensor_lambda_test.cpp new file mode 100644 index 00000000000..5b0f2cf0a7e --- /dev/null +++ b/eval/src/tests/eval/tensor_lambda/tensor_lambda_test.cpp @@ -0,0 +1,100 @@ +// Copyright Verizon Media. 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/eval/simple_tensor.h> +#include <vespa/eval/eval/simple_tensor_engine.h> +#include <vespa/eval/tensor/default_tensor_engine.h> +#include <vespa/eval/tensor/dense/dense_replace_type_function.h> +#include <vespa/eval/tensor/dense/dense_fast_rename_optimizer.h> +#include <vespa/eval/tensor/dense/dense_tensor.h> +#include <vespa/eval/eval/test/tensor_model.hpp> +#include <vespa/eval/eval/test/eval_fixture.h> +#include <vespa/eval/eval/tensor_nodes.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::tensor; +using namespace vespalib::eval::tensor_function; + +const TensorEngine &prod_engine = DefaultTensorEngine::ref(); + +EvalFixture::ParamRepo make_params() { + return EvalFixture::ParamRepo() + .add("a", spec(1)) + .add("x3", spec({x(3)}, N())) + .add("x3f", spec(float_cells({x(3)}), N())); +} +EvalFixture::ParamRepo param_repo = make_params(); + +void verify_dynamic(const vespalib::string &expr, const vespalib::string &expect) { + EvalFixture fixture(prod_engine, expr, param_repo, true); + EXPECT_EQUAL(fixture.result(), EvalFixture::ref(expr, param_repo)); + EXPECT_EQUAL(fixture.result(), EvalFixture::ref(expect, param_repo)); + auto info = fixture.find_all<Lambda>(); + EXPECT_EQUAL(info.size(), 1u); +} + +void verify_const(const vespalib::string &expr, const vespalib::string &expect) { + EvalFixture fixture(prod_engine, expr, param_repo, true); + EXPECT_EQUAL(fixture.result(), EvalFixture::ref(expr, param_repo)); + EXPECT_EQUAL(fixture.result(), EvalFixture::ref(expect, param_repo)); + auto info = fixture.find_all<ConstValue>(); + EXPECT_EQUAL(info.size(), 1u); +} + +TEST("require that simple constant tensor lambda works") { + TEST_DO(verify_const("tensor(x[3])(x+1)", "tensor(x[3]):[1,2,3]")); +} + +TEST("require that simple dynamic tensor lambda works") { + TEST_DO(verify_dynamic("tensor(x[3])(x+a)", "tensor(x[3]):[1,2,3]")); +} + +TEST("require that tensor lambda can be used for tensor slicing") { + TEST_DO(verify_dynamic("tensor(x[2])(x3{x:(x+a)})", "tensor(x[2]):[2,3]")); + TEST_DO(verify_dynamic("tensor(x[2])(a+x3{x:(x)})", "tensor(x[2]):[2,3]")); +} + +TEST("require that tensor lambda can be used for tensor casting") { + TEST_DO(verify_dynamic("tensor(x[3])(x3f{x:(x)})", "tensor(x[3]):[1,2,3]")); + TEST_DO(verify_dynamic("tensor<float>(x[3])(x3{x:(x)})", "tensor<float>(x[3]):[1,2,3]")); +} + +TEST("require that constant nested tensor lambda using tensor peek works") { + TEST_DO(verify_const("tensor(x[2])(tensor(y[2])((x+y)+1){y:(x)})", "tensor(x[2]):[1,3]")); +} + +TEST("require that dynamic nested tensor lambda using tensor peek works") { + TEST_DO(verify_dynamic("tensor(x[2])(tensor(y[2])((x+y)+a){y:(x)})", "tensor(x[2]):[1,3]")); +} + +TEST("require that non-double result from inner tensor lambda function fails type resolving") { + auto fun_a = Function::parse("tensor(x[2])(a)"); + auto fun_b = Function::parse("tensor(x[2])(a{y:(x)})"); + NodeTypes types_ad(*fun_a, {ValueType::from_spec("double")}); + NodeTypes types_at(*fun_a, {ValueType::from_spec("tensor(y[2])")}); + NodeTypes types_bd(*fun_b, {ValueType::from_spec("double")}); + NodeTypes types_bt(*fun_b, {ValueType::from_spec("tensor(y[2])")}); + EXPECT_EQUAL(types_ad.get_type(fun_a->root()).to_spec(), "tensor(x[2])"); + EXPECT_EQUAL(types_at.get_type(fun_a->root()).to_spec(), "error"); + EXPECT_EQUAL(types_bd.get_type(fun_b->root()).to_spec(), "error"); + EXPECT_EQUAL(types_bt.get_type(fun_b->root()).to_spec(), "tensor(x[2])"); +} + +TEST("require that type resolving also include nodes in the inner tensor lambda function") { + auto fun = Function::parse("tensor(x[2])(a)"); + NodeTypes types(*fun, {ValueType::from_spec("double")}); + auto lambda = nodes::as<nodes::TensorLambda>(fun->root()); + ASSERT_TRUE(lambda != nullptr); + EXPECT_EQUAL(types.get_type(*lambda).to_spec(), "tensor(x[2])"); + auto symbol = nodes::as<nodes::Symbol>(lambda->lambda().root()); + ASSERT_TRUE(symbol != nullptr); + EXPECT_EQUAL(types.get_type(*symbol).to_spec(), "double"); +} + +TEST_MAIN() { TEST_RUN_ALL(); } |