diff options
Diffstat (limited to 'eval')
15 files changed, 317 insertions, 104 deletions
diff --git a/eval/src/tests/eval/inline_operation/inline_operation_test.cpp b/eval/src/tests/eval/inline_operation/inline_operation_test.cpp index bfcb3a09a52..8895bd4bcbd 100644 --- a/eval/src/tests/eval/inline_operation/inline_operation_test.cpp +++ b/eval/src/tests/eval/inline_operation/inline_operation_test.cpp @@ -3,27 +3,29 @@ #include <vespa/eval/eval/operation.h> #include <vespa/eval/eval/inline_operation.h> #include <vespa/eval/eval/function.h> +#include <vespa/vespalib/util/typify.h> #include <vespa/vespalib/gtest/gtest.h> +using vespalib::typify_invoke; using namespace vespalib::eval; using namespace vespalib::eval::operation; -template <typename T> struct IsInlined { constexpr static bool value = true; }; -template <> struct IsInlined<CallOp1> { constexpr static bool value = false; }; -template <> struct IsInlined<CallOp2> { constexpr static bool value = false; }; +const int my_value = 42; +struct AsValue { template <typename T> static int invoke() { return my_value; } }; +struct AsRef { template <typename T> static const int &invoke() { return my_value; } }; -template <typename T> double test_op1(op1_t ref, double a, bool inlined) { - T op(ref); - EXPECT_EQ(IsInlined<T>::value, inlined); - EXPECT_EQ(op(a), ref(a)); - return op(a); +template <typename T> void test_op1(op1_t ref, double a, double expect) { + bool need_ref = std::is_same_v<T,CallOp1>; + T op = need_ref ? T(ref) : T(nullptr); + EXPECT_DOUBLE_EQ(ref(a), expect); + EXPECT_DOUBLE_EQ(op(a), expect); }; -template <typename T> double test_op2(op2_t ref, double a, double b, bool inlined) { - T op(ref); - EXPECT_EQ(IsInlined<T>::value, inlined); - EXPECT_EQ(op(a,b), ref(a,b)); - return op(a,b); +template <typename T> void test_op2(op2_t ref, double a, double b, double expect) { + bool need_ref = std::is_same_v<T,CallOp2>; + T op = need_ref ? T(ref) : T(nullptr); + EXPECT_DOUBLE_EQ(ref(a, b), expect); + EXPECT_DOUBLE_EQ(op(a, b), expect); }; op1_t as_op1(const vespalib::string &str) { @@ -63,6 +65,9 @@ TEST(InlineOperationTest, op1_lambdas_are_recognized) { EXPECT_EQ(as_op1("relu(a)"), &Relu::f); EXPECT_EQ(as_op1("sigmoid(a)"), &Sigmoid::f); EXPECT_EQ(as_op1("elu(a)"), &Elu::f); + //------------------------------------------- + EXPECT_EQ(as_op1("1/a"), &Inv::f); + EXPECT_EQ(as_op1("1.0/a"), &Inv::f); } TEST(InlineOperationTest, op1_lambdas_are_recognized_with_different_parameter_names) { @@ -121,11 +126,37 @@ TEST(InlineOperationTest, generic_op2_wrapper_works) { EXPECT_EQ(op(3,7), 10); } +TEST(InlineOperationTest, op1_typifier_forwards_return_value_correctly) { + auto a = typify_invoke<1,TypifyOp1,AsValue>(Neg::f); + auto b = typify_invoke<1,TypifyOp1,AsRef>(Neg::f); + EXPECT_EQ(a, my_value); + EXPECT_EQ(b, my_value); + bool same_memory = (&(typify_invoke<1,TypifyOp1,AsRef>(Neg::f)) == &my_value); + EXPECT_EQ(same_memory, true); +} + +TEST(InlineOperationTest, op2_typifier_forwards_return_value_correctly) { + auto a = typify_invoke<1,TypifyOp2,AsValue>(Add::f); + auto b = typify_invoke<1,TypifyOp2,AsRef>(Add::f); + EXPECT_EQ(a, my_value); + EXPECT_EQ(b, my_value); + bool same_memory = (&(typify_invoke<1,TypifyOp2,AsRef>(Add::f)) == &my_value); + EXPECT_EQ(same_memory, true); +} + +TEST(InlineOperationTest, inline_op1_example_works) { + op1_t ignored = nullptr; + InlineOp1<Inv> op(ignored); + EXPECT_EQ(op(2.0), 0.5); + EXPECT_EQ(op(4.0f), 0.25f); + EXPECT_EQ(op(8.0), 0.125); +} + TEST(InlineOperationTest, inline_op2_example_works) { op2_t ignored = nullptr; InlineOp2<Add> op(ignored); - EXPECT_EQ(op(2,3), 5); - EXPECT_EQ(op(3,7), 10); + EXPECT_EQ(op(2.0, 3.0), 5.0); + EXPECT_EQ(op(3.0, 7.0), 10.0); } TEST(InlineOperationTest, parameter_swap_wrapper_works) { @@ -137,20 +168,151 @@ TEST(InlineOperationTest, parameter_swap_wrapper_works) { EXPECT_EQ(swap_op(3,7), 4); } -TEST(InlineOperationTest, resolved_op1_works) { - auto a = TypifyOp1::resolve(Neg::f, [](auto t){ return test_op1<typename decltype(t)::type>(Neg::f, 2.0, false); }); - // putting the lambda inside the EXPECT does not work - EXPECT_EQ(a, -2.0); +//----------------------------------------------------------------------------- + +TEST(InlineOperationTest, op1_exp_is_inlined) { + TypifyOp1::resolve(Exp::f, [](auto t) + { + using T = typename decltype(t)::type; + bool type_ok = std::is_same_v<T,InlineOp1<Exp>>; + op1_t ref = Exp::f; + EXPECT_TRUE(type_ok); + test_op1<T>(ref, 2.0, std::exp(2.0)); + test_op1<T>(ref, 3.0, std::exp(3.0)); + test_op1<T>(ref, 7.0, std::exp(7.0)); + }); +} + +TEST(InlineOperationTest, op1_inv_is_inlined) { + TypifyOp1::resolve(Inv::f, [](auto t) + { + using T = typename decltype(t)::type; + bool type_ok = std::is_same_v<T,InlineOp1<Inv>>; + op1_t ref = Inv::f; + EXPECT_TRUE(type_ok); + test_op1<T>(ref, 2.0, 1.0/2.0); + test_op1<T>(ref, 4.0, 1.0/4.0); + test_op1<T>(ref, 8.0, 1.0/8.0); + }); +} + +TEST(InlineOperationTest, op1_sqrt_is_inlined) { + TypifyOp1::resolve(Sqrt::f, [](auto t) + { + using T = typename decltype(t)::type; + bool type_ok = std::is_same_v<T,InlineOp1<Sqrt>>; + op1_t ref = Sqrt::f; + EXPECT_TRUE(type_ok); + test_op1<T>(ref, 2.0, sqrt(2.0)); + test_op1<T>(ref, 4.0, sqrt(4.0)); + test_op1<T>(ref, 64.0, sqrt(64.0)); + }); +} + +TEST(InlineOperationTest, op1_tanh_is_inlined) { + TypifyOp1::resolve(Tanh::f, [](auto t) + { + using T = typename decltype(t)::type; + bool type_ok = std::is_same_v<T,InlineOp1<Tanh>>; + op1_t ref = Tanh::f; + EXPECT_TRUE(type_ok); + test_op1<T>(ref, 0.1, std::tanh(0.1)); + test_op1<T>(ref, 0.3, std::tanh(0.3)); + test_op1<T>(ref, 0.7, std::tanh(0.7)); + }); +} + +TEST(InlineOperationTest, op1_neg_is_not_inlined) { + TypifyOp1::resolve(Neg::f, [](auto t) + { + using T = typename decltype(t)::type; + bool type_ok = std::is_same_v<T,CallOp1>; + op1_t ref = Neg::f; + EXPECT_TRUE(type_ok); + test_op1<T>(ref, 3.0, -3.0); + test_op1<T>(ref, 5.0, -5.0); + test_op1<T>(ref, -2.0, 2.0); + }); +} + +//----------------------------------------------------------------------------- + +TEST(InlineOperationTest, op2_add_is_inlined) { + TypifyOp2::resolve(Add::f, [](auto t) + { + using T = typename decltype(t)::type; + bool type_ok = std::is_same_v<T,InlineOp2<Add>>; + op2_t ref = Add::f; + EXPECT_TRUE(type_ok); + test_op2<T>(ref, 2.0, 2.0, 4.0); + test_op2<T>(ref, 3.0, 8.0, 11.0); + test_op2<T>(ref, 7.0, 1.0, 8.0); + }); +} + +TEST(InlineOperationTest, op2_div_is_inlined) { + TypifyOp2::resolve(Div::f, [](auto t) + { + using T = typename decltype(t)::type; + bool type_ok = std::is_same_v<T,InlineOp2<Div>>; + op2_t ref = Div::f; + EXPECT_TRUE(type_ok); + test_op2<T>(ref, 2.0, 2.0, 1.0); + test_op2<T>(ref, 3.0, 8.0, 3.0 / 8.0); + test_op2<T>(ref, 7.0, 5.0, 7.0 / 5.0); + }); +} + +TEST(InlineOperationTest, op2_mul_is_inlined) { + TypifyOp2::resolve(Mul::f, [](auto t) + { + using T = typename decltype(t)::type; + bool type_ok = std::is_same_v<T,InlineOp2<Mul>>; + op2_t ref = Mul::f; + EXPECT_TRUE(type_ok); + test_op2<T>(ref, 2.0, 2.0, 4.0); + test_op2<T>(ref, 3.0, 8.0, 24.0); + test_op2<T>(ref, 7.0, 5.0, 35.0); + }); +} + +TEST(InlineOperationTest, op2_pow_is_inlined) { + TypifyOp2::resolve(Pow::f, [](auto t) + { + using T = typename decltype(t)::type; + bool type_ok = std::is_same_v<T,InlineOp2<Pow>>; + op2_t ref = Pow::f; + EXPECT_TRUE(type_ok); + test_op2<T>(ref, 2.0, 2.0, std::pow(2.0, 2.0)); + test_op2<T>(ref, 3.0, 8.0, std::pow(3.0, 8.0)); + test_op2<T>(ref, 7.0, 5.0, std::pow(7.0, 5.0)); + }); +} + +TEST(InlineOperationTest, op2_sub_is_inlined) { + TypifyOp2::resolve(Sub::f, [](auto t) + { + using T = typename decltype(t)::type; + bool type_ok = std::is_same_v<T,InlineOp2<Sub>>; + op2_t ref = Sub::f; + EXPECT_TRUE(type_ok); + test_op2<T>(ref, 3.0, 2.0, 1.0); + test_op2<T>(ref, 3.0, 8.0, -5.0); + test_op2<T>(ref, 7.0, 5.0, 2.0); + }); } -TEST(InlineOperationTest, resolved_op2_works) { - auto a = TypifyOp2::resolve(Add::f, [](auto t){ return test_op2<typename decltype(t)::type>(Add::f, 2.0, 5.0, true); }); - auto b = TypifyOp2::resolve(Mul::f, [](auto t){ return test_op2<typename decltype(t)::type>(Mul::f, 5.0, 3.0, true); }); - auto c = TypifyOp2::resolve(Sub::f, [](auto t){ return test_op2<typename decltype(t)::type>(Sub::f, 8.0, 5.0, false); }); - // putting the lambda inside the EXPECT does not work - EXPECT_EQ(a, 7.0); - EXPECT_EQ(b, 15.0); - EXPECT_EQ(c, 3.0); +TEST(InlineOperationTest, op2_mod_is_not_inlined) { + TypifyOp2::resolve(Mod::f, [](auto t) + { + using T = typename decltype(t)::type; + bool type_ok = std::is_same_v<T,CallOp2>; + op2_t ref = Mod::f; + EXPECT_TRUE(type_ok); + test_op2<T>(ref, 3.0, 2.0, std::fmod(3.0, 2.0)); + test_op2<T>(ref, 3.0, 8.0, std::fmod(3.0, 8.0)); + test_op2<T>(ref, 7.0, 5.0, std::fmod(7.0, 5.0)); + }); } GTEST_MAIN_RUN_ALL_TESTS() diff --git a/eval/src/vespa/eval/eval/aggr.cpp b/eval/src/vespa/eval/eval/aggr.cpp index d10bbc4abb8..8efb0ec9fe7 100644 --- a/eval/src/vespa/eval/eval/aggr.cpp +++ b/eval/src/vespa/eval/eval/aggr.cpp @@ -71,15 +71,11 @@ Aggregator::~Aggregator() Aggregator & Aggregator::create(Aggr aggr, Stash &stash) { - switch (aggr) { - case Aggr::AVG: return stash.create<Wrapper<aggr::Avg<double>>>(); - case Aggr::COUNT: return stash.create<Wrapper<aggr::Count<double>>>(); - case Aggr::PROD: return stash.create<Wrapper<aggr::Prod<double>>>(); - case Aggr::SUM: return stash.create<Wrapper<aggr::Sum<double>>>(); - case Aggr::MAX: return stash.create<Wrapper<aggr::Max<double>>>(); - case Aggr::MIN: return stash.create<Wrapper<aggr::Min<double>>>(); - } - LOG_ABORT("should not be reached"); + return TypifyAggr::resolve(aggr, [&stash](auto t)->Aggregator& + { + using T = typename decltype(t)::template templ<double>; + return stash.create<Wrapper<T>>(); + }); } std::vector<Aggr> diff --git a/eval/src/vespa/eval/eval/aggr.h b/eval/src/vespa/eval/eval/aggr.h index 8dea54d8abc..e7431c2c23b 100644 --- a/eval/src/vespa/eval/eval/aggr.h +++ b/eval/src/vespa/eval/eval/aggr.h @@ -118,5 +118,24 @@ public: }; } // namespave vespalib::eval::aggr + +struct TypifyAggr { + template <template<typename> typename A> struct Result { + static constexpr bool is_type = false; + template <typename T> using templ = A<T>; + }; + template <typename F> static decltype(auto) resolve(Aggr aggr, F &&f) { + switch (aggr) { + case Aggr::AVG: return f(Result<aggr::Avg>()); + case Aggr::COUNT: return f(Result<aggr::Count>()); + case Aggr::PROD: return f(Result<aggr::Prod>()); + case Aggr::SUM: return f(Result<aggr::Sum>()); + case Aggr::MAX: return f(Result<aggr::Max>()); + case Aggr::MIN: return f(Result<aggr::Min>()); + } + abort(); + } +}; + } // namespace vespalib::eval } // namespace vespalib diff --git a/eval/src/vespa/eval/eval/inline_operation.h b/eval/src/vespa/eval/eval/inline_operation.h index 71e81b223e2..fccf1874242 100644 --- a/eval/src/vespa/eval/eval/inline_operation.h +++ b/eval/src/vespa/eval/eval/inline_operation.h @@ -4,6 +4,7 @@ #include "operation.h" #include <vespa/vespalib/util/typify.h> +#include <cmath> namespace vespalib::eval::operation { @@ -15,11 +16,38 @@ struct CallOp1 { double operator()(double a) const { return my_op1(a); } }; +template <typename T> struct InlineOp1; +template <> struct InlineOp1<Exp> { + InlineOp1(op1_t) {} + template <typename A> constexpr auto operator()(A a) const { return exp(a); } +}; +template <> struct InlineOp1<Inv> { + InlineOp1(op1_t) {} + template <typename A> constexpr auto operator()(A a) const { return (A{1}/a); } +}; +template <> struct InlineOp1<Sqrt> { + InlineOp1(op1_t) {} + template <typename A> constexpr auto operator()(A a) const { return std::sqrt(a); } +}; +template <> struct InlineOp1<Tanh> { + InlineOp1(op1_t) {} + template <typename A> constexpr auto operator()(A a) const { return std::tanh(a); } +}; + struct TypifyOp1 { template <typename T> using Result = TypifyResultType<T>; template <typename F> static decltype(auto) resolve(op1_t value, F &&f) { - (void) value; - return f(Result<CallOp1>()); + if (value == Exp::f) { + return f(Result<InlineOp1<Exp>>()); + } else if (value == Inv::f) { + return f(Result<InlineOp1<Inv>>()); + } else if (value == Sqrt::f) { + return f(Result<InlineOp1<Sqrt>>()); + } else if (value == Tanh::f) { + return f(Result<InlineOp1<Tanh>>()); + } else { + return f(Result<CallOp1>()); + } } }; @@ -44,18 +72,36 @@ template <> struct InlineOp2<Add> { InlineOp2(op2_t) {} template <typename A, typename B> constexpr auto operator()(A a, B b) const { return (a+b); } }; +template <> struct InlineOp2<Div> { + InlineOp2(op2_t) {} + template <typename A, typename B> constexpr auto operator()(A a, B b) const { return (a/b); } +}; template <> struct InlineOp2<Mul> { InlineOp2(op2_t) {} template <typename A, typename B> constexpr auto operator()(A a, B b) const { return (a*b); } }; +template <> struct InlineOp2<Pow> { + InlineOp2(op2_t) {} + template <typename A, typename B> constexpr auto operator()(A a, B b) const { return std::pow(a,b); } +}; +template <> struct InlineOp2<Sub> { + InlineOp2(op2_t) {} + template <typename A, typename B> constexpr auto operator()(A a, B b) const { return (a-b); } +}; struct TypifyOp2 { template <typename T> using Result = TypifyResultType<T>; template <typename F> static decltype(auto) resolve(op2_t value, F &&f) { if (value == Add::f) { return f(Result<InlineOp2<Add>>()); + } else if (value == Div::f) { + return f(Result<InlineOp2<Div>>()); } else if (value == Mul::f) { return f(Result<InlineOp2<Mul>>()); + } else if (value == Pow::f) { + return f(Result<InlineOp2<Pow>>()); + } else if (value == Sub::f) { + return f(Result<InlineOp2<Sub>>()); } else { return f(Result<CallOp2>()); } diff --git a/eval/src/vespa/eval/eval/operation.cpp b/eval/src/vespa/eval/eval/operation.cpp index 581f65c0e31..bbd37ab68b2 100644 --- a/eval/src/vespa/eval/eval/operation.cpp +++ b/eval/src/vespa/eval/eval/operation.cpp @@ -49,6 +49,8 @@ double IsNan::f(double a) { return std::isnan(a) ? 1.0 : 0.0; } double Relu::f(double a) { return std::max(a, 0.0); } double Sigmoid::f(double a) { return 1.0 / (1.0 + std::exp(-1.0 * a)); } double Elu::f(double a) { return (a < 0) ? std::exp(a) - 1 : a; } +//----------------------------------------------------------------------------- +double Inv::f(double a) { return (1 / a); } namespace { @@ -102,6 +104,8 @@ std::map<vespalib::string,op1_t> make_op1_map() { add_op1(map, "relu(a)", Relu::f); add_op1(map, "sigmoid(a)", Sigmoid::f); add_op1(map, "elu(a)", Elu::f); + //------------------------------------- + add_op1(map, "1/a", Inv::f); return map; } diff --git a/eval/src/vespa/eval/eval/operation.h b/eval/src/vespa/eval/eval/operation.h index a80193e704d..b00bb5e26fc 100644 --- a/eval/src/vespa/eval/eval/operation.h +++ b/eval/src/vespa/eval/eval/operation.h @@ -48,6 +48,8 @@ struct IsNan { static double f(double a); }; struct Relu { static double f(double a); }; struct Sigmoid { static double f(double a); }; struct Elu { static double f(double a); }; +//----------------------------------------------------------------------------- +struct Inv { static double f(double a); }; using op1_t = double (*)(double); using op2_t = double (*)(double, double); diff --git a/eval/src/vespa/eval/tensor/dense/dense_cell_range_function.cpp b/eval/src/vespa/eval/tensor/dense/dense_cell_range_function.cpp index 9b93f5e7d72..84da53c8488 100644 --- a/eval/src/vespa/eval/tensor/dense/dense_cell_range_function.cpp +++ b/eval/src/vespa/eval/tensor/dense/dense_cell_range_function.cpp @@ -25,7 +25,7 @@ void my_cell_range_op(eval::InterpretedFunction::State &state, uint64_t param) { struct MyCellRangeOp { template <typename CT> - static auto get_fun() { return my_cell_range_op<CT>; } + static auto invoke() { return my_cell_range_op<CT>; } }; } // namespace vespalib::tensor::<unnamed> @@ -46,7 +46,9 @@ DenseCellRangeFunction::compile_self(const TensorEngine &, Stash &) const { static_assert(sizeof(uint64_t) == sizeof(this)); assert(result_type().cell_type() == child().result_type().cell_type()); - auto op = select_1<MyCellRangeOp>(result_type().cell_type()); + + using MyTypify = eval::TypifyCellType; + auto op = typify_invoke<1,MyTypify,MyCellRangeOp>(result_type().cell_type()); return eval::InterpretedFunction::Instruction(op, (uint64_t)this); } diff --git a/eval/src/vespa/eval/tensor/dense/dense_dot_product_function.cpp b/eval/src/vespa/eval/tensor/dense/dense_dot_product_function.cpp index c9ff57e4a65..9e30451cd67 100644 --- a/eval/src/vespa/eval/tensor/dense/dense_dot_product_function.cpp +++ b/eval/src/vespa/eval/tensor/dense/dense_dot_product_function.cpp @@ -48,7 +48,7 @@ void my_cblas_float_dot_product_op(eval::InterpretedFunction::State &state, uint struct MyDotProductOp { template <typename LCT, typename RCT> - static auto get_fun() { return my_dot_product_op<LCT,RCT>; } + static auto invoke() { return my_dot_product_op<LCT,RCT>; } }; eval::InterpretedFunction::op_function my_select(CellType lct, CellType rct) { @@ -60,7 +60,8 @@ eval::InterpretedFunction::op_function my_select(CellType lct, CellType rct) { return my_cblas_float_dot_product_op; } } - return select_2<MyDotProductOp>(lct, rct); + using MyTypify = eval::TypifyCellType; + return typify_invoke<2,MyTypify,MyDotProductOp>(lct, rct); } } // namespace vespalib::tensor::<unnamed> diff --git a/eval/src/vespa/eval/tensor/dense/dense_lambda_function.cpp b/eval/src/vespa/eval/tensor/dense/dense_lambda_function.cpp index b60d732d7a9..e373ca09e11 100644 --- a/eval/src/vespa/eval/tensor/dense/dense_lambda_function.cpp +++ b/eval/src/vespa/eval/tensor/dense/dense_lambda_function.cpp @@ -95,7 +95,7 @@ void my_compiled_lambda_op(eval::InterpretedFunction::State &state, uint64_t par struct MyCompiledLambdaOp { template <typename CT> - static auto get_fun() { return my_compiled_lambda_op<CT>; } + static auto invoke() { return my_compiled_lambda_op<CT>; } }; //----------------------------------------------------------------------------- @@ -131,7 +131,7 @@ void my_interpreted_lambda_op(eval::InterpretedFunction::State &state, uint64_t struct MyInterpretedLambdaOp { template <typename CT> - static auto get_fun() { return my_interpreted_lambda_op<CT>; } + static auto invoke() { return my_interpreted_lambda_op<CT>; } }; //----------------------------------------------------------------------------- @@ -163,15 +163,16 @@ DenseLambdaFunction::compile_self(const TensorEngine &engine, Stash &stash) cons { assert(&engine == &prod_engine); auto mode = eval_mode(); + using MyTypify = eval::TypifyCellType; if (mode == EvalMode::COMPILED) { CompiledParams ¶ms = stash.create<CompiledParams>(_lambda); - auto op = select_1<MyCompiledLambdaOp>(result_type().cell_type()); + auto op = typify_invoke<1,MyTypify,MyCompiledLambdaOp>(result_type().cell_type()); static_assert(sizeof(¶ms) == sizeof(uint64_t)); return Instruction(op, (uint64_t)(¶ms)); } else { assert(mode == EvalMode::INTERPRETED); InterpretedParams ¶ms = stash.create<InterpretedParams>(_lambda); - auto op = select_1<MyInterpretedLambdaOp>(result_type().cell_type()); + auto op = typify_invoke<1,MyTypify,MyInterpretedLambdaOp>(result_type().cell_type()); static_assert(sizeof(¶ms) == sizeof(uint64_t)); return Instruction(op, (uint64_t)(¶ms)); } diff --git a/eval/src/vespa/eval/tensor/dense/dense_lambda_peek_function.cpp b/eval/src/vespa/eval/tensor/dense/dense_lambda_peek_function.cpp index a5f532e643a..70bdc8ae7d6 100644 --- a/eval/src/vespa/eval/tensor/dense/dense_lambda_peek_function.cpp +++ b/eval/src/vespa/eval/tensor/dense/dense_lambda_peek_function.cpp @@ -45,7 +45,7 @@ void my_lambda_peek_op(InterpretedFunction::State &state, uint64_t param) { struct MyLambdaPeekOp { template <typename DST_CT, typename SRC_CT> - static auto get_fun() { return my_lambda_peek_op<DST_CT, SRC_CT>; } + static auto invoke() { return my_lambda_peek_op<DST_CT, SRC_CT>; } }; } // namespace vespalib::tensor::<unnamed> @@ -64,7 +64,8 @@ InterpretedFunction::Instruction DenseLambdaPeekFunction::compile_self(const TensorEngine &, Stash &stash) const { const Self &self = stash.create<Self>(result_type(), *_idx_fun); - auto op = select_2<MyLambdaPeekOp>(result_type().cell_type(), child().result_type().cell_type()); + using MyTypify = eval::TypifyCellType; + auto op = typify_invoke<2,MyTypify,MyLambdaPeekOp>(result_type().cell_type(), child().result_type().cell_type()); static_assert(sizeof(uint64_t) == sizeof(&self)); assert(child().result_type().is_dense()); return InterpretedFunction::Instruction(op, (uint64_t)&self); diff --git a/eval/src/vespa/eval/tensor/dense/dense_single_reduce_function.cpp b/eval/src/vespa/eval/tensor/dense/dense_single_reduce_function.cpp index 663993b6c26..571bcb79c9f 100644 --- a/eval/src/vespa/eval/tensor/dense/dense_single_reduce_function.cpp +++ b/eval/src/vespa/eval/tensor/dense/dense_single_reduce_function.cpp @@ -2,6 +2,7 @@ #include "dense_single_reduce_function.h" #include "dense_tensor_view.h" +#include <vespa/vespalib/util/typify.h> #include <vespa/eval/eval/value.h> namespace vespalib::tensor { @@ -12,6 +13,8 @@ using eval::TensorEngine; using eval::TensorFunction; using eval::Value; using eval::ValueType; +using eval::TypifyCellType; +using eval::TypifyAggr; using eval::as; using namespace eval::tensor_function; @@ -66,28 +69,13 @@ void my_single_reduce_op(InterpretedFunction::State &state, uint64_t param) { state.pop_push(state.stash.create<DenseTensorView>(params.result_type, TypedCells(dst_cells))); } -template <typename CT> -InterpretedFunction::op_function my_select_2(Aggr aggr) { - switch (aggr) { - case Aggr::AVG: return my_single_reduce_op<CT, Avg<CT>>; - case Aggr::COUNT: return my_single_reduce_op<CT, Count<CT>>; - case Aggr::PROD: return my_single_reduce_op<CT, Prod<CT>>; - case Aggr::SUM: return my_single_reduce_op<CT, Sum<CT>>; - case Aggr::MAX: return my_single_reduce_op<CT, Max<CT>>; - case Aggr::MIN: return my_single_reduce_op<CT, Min<CT>>; +struct MyGetFun { + template <typename R1, typename R2> static auto invoke() { + return my_single_reduce_op<R1, typename R2::template templ<R1>>; } - abort(); -} +}; -InterpretedFunction::op_function my_select(CellType cell_type, Aggr aggr) { - if (cell_type == ValueType::CellType::DOUBLE) { - return my_select_2<double>(aggr); - } - if (cell_type == ValueType::CellType::FLOAT) { - return my_select_2<float>(aggr); - } - abort(); -} +using MyTypify = TypifyValue<TypifyCellType,TypifyAggr>; bool check_input_type(const ValueType &type) { return (type.is_dense() && ((type.cell_type() == CellType::FLOAT) || (type.cell_type() == CellType::DOUBLE))); @@ -109,7 +97,7 @@ DenseSingleReduceFunction::~DenseSingleReduceFunction() = default; InterpretedFunction::Instruction DenseSingleReduceFunction::compile_self(const TensorEngine &, Stash &stash) const { - auto op = my_select(result_type().cell_type(), _aggr); + auto op = typify_invoke<2,MyTypify,MyGetFun>(result_type().cell_type(), _aggr); auto ¶ms = stash.create<Params>(result_type(), child().result_type(), _dim_idx); static_assert(sizeof(uint64_t) == sizeof(¶ms)); return InterpretedFunction::Instruction(op, (uint64_t)¶ms); diff --git a/eval/src/vespa/eval/tensor/dense/dense_tensor_create_function.cpp b/eval/src/vespa/eval/tensor/dense/dense_tensor_create_function.cpp index 3533ab20175..7e887d4df34 100644 --- a/eval/src/vespa/eval/tensor/dense/dense_tensor_create_function.cpp +++ b/eval/src/vespa/eval/tensor/dense/dense_tensor_create_function.cpp @@ -34,7 +34,7 @@ void my_tensor_create_op(eval::InterpretedFunction::State &state, uint64_t param struct MyTensorCreateOp { template <typename CT> - static auto get_fun() { return my_tensor_create_op<CT>; } + static auto invoke() { return my_tensor_create_op<CT>; } }; size_t get_index(const TensorSpec::Address &addr, const ValueType &type) { @@ -72,7 +72,9 @@ eval::InterpretedFunction::Instruction DenseTensorCreateFunction::compile_self(const TensorEngine &, Stash &) const { static_assert(sizeof(uint64_t) == sizeof(&_self)); - auto op = select_1<MyTensorCreateOp>(result_type().cell_type()); + + using MyTypify = eval::TypifyCellType; + auto op = typify_invoke<1,MyTypify,MyTensorCreateOp>(result_type().cell_type()); return eval::InterpretedFunction::Instruction(op, (uint64_t)&_self); } diff --git a/eval/src/vespa/eval/tensor/dense/dense_tensor_peek_function.cpp b/eval/src/vespa/eval/tensor/dense/dense_tensor_peek_function.cpp index 5cb1cbfd88f..16c0b01b169 100644 --- a/eval/src/vespa/eval/tensor/dense/dense_tensor_peek_function.cpp +++ b/eval/src/vespa/eval/tensor/dense/dense_tensor_peek_function.cpp @@ -44,7 +44,7 @@ void my_tensor_peek_op(eval::InterpretedFunction::State &state, uint64_t param) struct MyTensorPeekOp { template <typename CT> - static auto get_fun() { return my_tensor_peek_op<CT>; } + static auto invoke() { return my_tensor_peek_op<CT>; } }; } // namespace vespalib::tensor::<unnamed> @@ -71,7 +71,8 @@ eval::InterpretedFunction::Instruction DenseTensorPeekFunction::compile_self(const TensorEngine &, Stash &) const { static_assert(sizeof(uint64_t) == sizeof(&_spec)); - auto op = select_1<MyTensorPeekOp>(_children[0].get().result_type().cell_type()); + using MyTypify = eval::TypifyCellType; + auto op = typify_invoke<1,MyTypify,MyTensorPeekOp>(_children[0].get().result_type().cell_type()); return eval::InterpretedFunction::Instruction(op, (uint64_t)&_spec); } diff --git a/eval/src/vespa/eval/tensor/dense/dense_xw_product_function.cpp b/eval/src/vespa/eval/tensor/dense/dense_xw_product_function.cpp index a0d63a1ce1e..968308d69c9 100644 --- a/eval/src/vespa/eval/tensor/dense/dense_xw_product_function.cpp +++ b/eval/src/vespa/eval/tensor/dense/dense_xw_product_function.cpp @@ -76,33 +76,6 @@ void my_cblas_float_xw_product_op(eval::InterpretedFunction::State &state, uint6 state.pop_pop_push(state.stash.create<DenseTensorView>(self.result_type, TypedCells(dst_cells))); } -template <bool common_inner> -struct MyXWProductOp { - template <typename LCT, typename RCT> - static auto get_fun() { return my_xw_product_op<LCT,RCT,common_inner>; } -}; - -template <bool common_inner> -eval::InterpretedFunction::op_function my_select2(CellType lct, CellType rct) { - if (lct == rct) { - if (lct == ValueType::CellType::DOUBLE) { - return my_cblas_double_xw_product_op<common_inner>; - } - if (lct == ValueType::CellType::FLOAT) { - return my_cblas_float_xw_product_op<common_inner>; - } - } - return select_2<MyXWProductOp<common_inner>>(lct, rct); -} - -eval::InterpretedFunction::op_function my_select(CellType lct, CellType rct, bool common_inner) { - if (common_inner) { - return my_select2<true>(lct, rct); - } else { - return my_select2<false>(lct, rct); - } -} - bool isDenseTensor(const ValueType &type, size_t d) { return (type.is_dense() && (type.dimensions().size() == d)); } @@ -132,6 +105,18 @@ const TensorFunction &createDenseXWProduct(const ValueType &res, const TensorFun common_inner); } +struct MyXWProductOp { + template<typename R1, typename R2, typename R3> static auto invoke() { + if (std::is_same_v<R1,double> && std::is_same_v<R2,double>) { + return my_cblas_double_xw_product_op<R3::value>; + } else if (std::is_same_v<R1,float> && std::is_same_v<R2,float>) { + return my_cblas_float_xw_product_op<R3::value>; + } else { + return my_xw_product_op<R1, R2, R3::value>; + } + } +}; + } // namespace vespalib::tensor::<unnamed> DenseXWProductFunction::Self::Self(const eval::ValueType &result_type_in, @@ -160,8 +145,10 @@ eval::InterpretedFunction::Instruction DenseXWProductFunction::compile_self(const TensorEngine &, Stash &stash) const { Self &self = stash.create<Self>(result_type(), _vector_size, _result_size); - auto op = my_select(lhs().result_type().cell_type(), - rhs().result_type().cell_type(), _common_inner); + using MyTypify = TypifyValue<eval::TypifyCellType,vespalib::TypifyBool>; + auto op = typify_invoke<3,MyTypify,MyXWProductOp>(lhs().result_type().cell_type(), + rhs().result_type().cell_type(), + _common_inner); return eval::InterpretedFunction::Instruction(op, (uint64_t)(&self)); } diff --git a/eval/src/vespa/eval/tensor/dense/vector_from_doubles_function.cpp b/eval/src/vespa/eval/tensor/dense/vector_from_doubles_function.cpp index 7a4b5917f00..57f727f7968 100644 --- a/eval/src/vespa/eval/tensor/dense/vector_from_doubles_function.cpp +++ b/eval/src/vespa/eval/tensor/dense/vector_from_doubles_function.cpp @@ -19,7 +19,7 @@ namespace { struct CallVectorFromDoubles { template <typename CT> static TypedCells - call(eval::InterpretedFunction::State &state, size_t numCells) { + invoke(eval::InterpretedFunction::State &state, size_t numCells) { ArrayRef<CT> outputCells = state.stash.create_array<CT>(numCells); for (size_t i = numCells; i-- > 0; ) { outputCells[i] = (CT) state.peek(0).as_double(); @@ -33,7 +33,8 @@ void my_vector_from_doubles_op(eval::InterpretedFunction::State &state, uint64_t const auto *self = (const VectorFromDoublesFunction::Self *)(param); CellType ct = self->resultType.cell_type(); size_t numCells = self->resultSize; - TypedCells cells = dispatch_0<CallVectorFromDoubles>(ct, state, numCells); + using MyTypify = eval::TypifyCellType; + TypedCells cells = typify_invoke<1,MyTypify,CallVectorFromDoubles>(ct, state, numCells); const Value &result = state.stash.create<DenseTensorView>(self->resultType, cells); state.stack.emplace_back(result); } |