summaryrefslogtreecommitdiffstats
path: root/eval/src/tests/tensor/tensor_modify_operation/tensor_modify_operation_test.cpp
blob: 0b68088f93bcf8c0349aa291a63046c7ef1cd334 (plain) (blame)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/eval/eval/tensor_spec.h>
#include <vespa/eval/tensor/cell_values.h>
#include <vespa/eval/tensor/default_tensor_engine.h>
#include <vespa/eval/tensor/sparse/sparse_tensor.h>
#include <vespa/eval/tensor/test/test_utils.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/util/stringfmt.h>

using vespalib::eval::Value;
using vespalib::eval::TensorSpec;
using vespalib::tensor::test::makeTensor;
using namespace vespalib::tensor;

namespace {

double
replace(double, double b)
{
    return b;
}

}

void
checkUpdate(const TensorSpec &source, const TensorSpec &update, const TensorSpec &expect)
{
    auto sourceTensor = makeTensor<Tensor>(source);
    auto updateTensor = makeTensor<SparseTensor>(update);
    const CellValues cellValues(*updateTensor);

    auto actualTensor = sourceTensor->modify(replace, cellValues);
    auto actual = actualTensor->toSpec();
    auto expectTensor = makeTensor<Tensor>(expect);
    auto expectPadded = expectTensor->toSpec();
    EXPECT_EQ(actual, expectPadded);
}

TEST(TensorModifyTest, sparse_tensors_can_be_modified)
{
    checkUpdate(TensorSpec("tensor(x{},y{})")
                        .add({{"x","8"},{"y","9"}}, 11)
                        .add({{"x","9"},{"y","9"}}, 11),
                TensorSpec("tensor(x{},y{})")
                        .add({{"x","8"},{"y","9"}}, 2),
                TensorSpec("tensor(x{},y{})")
                        .add({{"x","8"},{"y","9"}}, 2)
                        .add({{"x","9"},{"y","9"}}, 11));
}

TEST(TensorModifyTest, dense_tensors_can_be_modified)
{
    checkUpdate(TensorSpec("tensor(x[10],y[10])")
                        .add({{"x",8},{"y",9}}, 11)
                        .add({{"x",9},{"y",9}}, 11),
                TensorSpec("tensor(x{},y{})")
                        .add({{"x","8"},{"y","9"}}, 2),
                TensorSpec("tensor(x[10],y[10])")
                        .add({{"x",8},{"y",9}}, 2)
                        .add({{"x",9},{"y",9}}, 11));
}

TEST(TensorModifyTest, mixed_tensors_can_be_modified)
{
    checkUpdate(TensorSpec("tensor(x{},y[2])")
                        .add({{"x","a"},{"y",0}}, 2)
                        .add({{"x","a"},{"y",1}}, 3)
                        .add({{"x","b"},{"y",0}}, 4)
                        .add({{"x","b"},{"y",1}}, 5),
                TensorSpec("tensor(x{},y{})")
                        .add({{"x","a"},{"y","0"}}, 6)
                        .add({{"x","b"},{"y","1"}}, 7),
                TensorSpec("tensor(x{},y[2])")
                        .add({{"x","a"},{"y",0}}, 6)
                        .add({{"x","a"},{"y",1}}, 3)
                        .add({{"x","b"},{"y",0}}, 4)
                        .add({{"x","b"},{"y",1}}, 7));
}

TEST(TensorModifyTest, sparse_tensors_ignore_updates_to_missing_cells)
{
    checkUpdate(TensorSpec("tensor(x{},y{})")
                        .add({{"x","8"},{"y","9"}}, 11)
                        .add({{"x","9"},{"y","9"}}, 11),
                TensorSpec("tensor(x{},y{})")
                        .add({{"x","7"},{"y","9"}}, 2)
                        .add({{"x","8"},{"y","9"}}, 2),
                TensorSpec("tensor(x{},y{})")
                        .add({{"x","8"},{"y","9"}}, 2)
                        .add({{"x","9"},{"y","9"}}, 11));
}

TEST(TensorModifyTest, dense_tensors_ignore_updates_to_out_of_range_cells)
{
    checkUpdate(TensorSpec("tensor(x[10],y[10])")
                        .add({{"x",8},{"y",9}}, 11)
                        .add({{"x",9},{"y",9}}, 11),
                TensorSpec("tensor(x{},y{})")
                        .add({{"x","8"},{"y","9"}}, 2)
                        .add({{"x","10"},{"y","9"}}, 2),
                TensorSpec("tensor(x[10],y[10])")
                        .add({{"x",8},{"y",9}}, 2)
                        .add({{"x",9},{"y",9}}, 11));
}

TEST(TensorModifyTest, mixed_tensors_ignore_updates_to_missing_or_out_of_range_cells)
{
    checkUpdate(TensorSpec("tensor(x{},y[2])")
                        .add({{"x","a"},{"y",0}}, 2)
                        .add({{"x","a"},{"y",1}}, 3),
                TensorSpec("tensor(x{},y{})")
                        .add({{"x","a"},{"y","2"}}, 4)
                        .add({{"x","c"},{"y","0"}}, 5),
                TensorSpec("tensor(x{},y[2])")
                        .add({{"x","a"},{"y",0}}, 2)
                        .add({{"x","a"},{"y",1}}, 3));
}

GTEST_MAIN_RUN_ALL_TESTS