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
|
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/eval/eval/simple_value.h>
#include <vespa/eval/eval/fast_value.h>
#include <vespa/eval/eval/value_codec.h>
#include <vespa/eval/instruction/generic_map.h>
#include <vespa/eval/eval/interpreted_function.h>
#include <vespa/eval/eval/test/tensor_model.hpp>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/gtest/gtest.h>
using namespace vespalib;
using namespace vespalib::eval;
using namespace vespalib::eval::instruction;
using namespace vespalib::eval::test;
using vespalib::make_string_short::fmt;
std::vector<Layout> map_layouts = {
{x(3)},
{x(3),y(5)},
{x(3),y(5),z(7)},
float_cells({x(3),y(5),z(7)}),
{x({"a","b","c"})},
{x({"a","b","c"}),y({"foo","bar"})},
{x({"a","b","c"}),y({"foo","bar"}),z({"i","j","k","l"})},
float_cells({x({"a","b","c"}),y({"foo","bar"}),z({"i","j","k","l"})}),
{x(3),y({"foo", "bar"}),z(7)},
{x({"a","b","c"}),y(5),z({"i","j","k","l"})},
float_cells({x({"a","b","c"}),y(5),z({"i","j","k","l"})})
};
TensorSpec reference_map(const TensorSpec &a, map_fun_t func) {
ValueType res_type = ValueType::from_spec(a.type());
EXPECT_FALSE(res_type.is_error());
TensorSpec result(res_type.to_spec());
for (const auto &cell: a.cells()) {
result.add(cell.first, func(cell.second));
}
return result;
}
TensorSpec perform_generic_map(const TensorSpec &a, map_fun_t func, const ValueBuilderFactory &factory)
{
auto lhs = value_from_spec(a, factory);
auto my_op = GenericMap::make_instruction(lhs->type(), func);
InterpretedFunction::EvalSingle single(factory, my_op);
return spec_from_value(single.eval(std::vector<Value::CREF>({*lhs})));
}
void test_generic_map_with(const ValueBuilderFactory &factory) {
for (const auto & layout : map_layouts) {
TensorSpec lhs = spec(layout, Div16(N()));
ValueType lhs_type = ValueType::from_spec(lhs.type());
for (auto func : {operation::Floor::f, operation::Fabs::f, operation::Square::f, operation::Inv::f}) {
SCOPED_TRACE(fmt("\n===\nLHS: %s\n===\n", lhs.to_string().c_str()));
auto expect = reference_map(lhs, func);
auto actual = perform_generic_map(lhs, func, factory);
EXPECT_EQ(actual, expect);
}
}
}
TEST(GenericMapTest, generic_map_works_for_simple_values) {
test_generic_map_with(SimpleValueBuilderFactory::get());
}
TEST(GenericMapTest, generic_map_works_for_fast_values) {
test_generic_map_with(FastValueBuilderFactory::get());
}
TensorSpec immediate_generic_map(const TensorSpec &a, map_fun_t func, const ValueBuilderFactory &factory)
{
auto lhs = value_from_spec(a, factory);
auto up = GenericMap::perform_map(*lhs, func, factory);
return spec_from_value(*up);
}
TEST(GenericMapTest, immediate_generic_map_works) {
for (const auto & layout : map_layouts) {
TensorSpec lhs = spec(layout, Div16(N()));
ValueType lhs_type = ValueType::from_spec(lhs.type());
for (auto func : {operation::Floor::f, operation::Fabs::f, operation::Square::f, operation::Inv::f}) {
SCOPED_TRACE(fmt("\n===\nLHS: %s\n===\n", lhs.to_string().c_str()));
auto expect = reference_map(lhs, func);
auto actual = immediate_generic_map(lhs, func, SimpleValueBuilderFactory::get());
EXPECT_EQ(actual, expect);
}
}
}
GTEST_MAIN_RUN_ALL_TESTS()
|