summaryrefslogtreecommitdiffstats
path: root/eval/src/tests/tensor/tensor_mapper/tensor_mapper_test.cpp
blob: 6572ab1ed92f9eef66c1780bd2b5d2edd400f734 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// 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/vespalib/util/stringfmt.h>
#include <vespa/eval/tensor/tensor_mapper.h>
#include <vespa/eval/tensor/wrapped_simple_tensor.h>
#include <vespa/eval/tensor/default_tensor_engine.h>
#include <vespa/eval/eval/tensor_spec.h>
#include <vespa/eval/eval/simple_tensor.h>

using vespalib::eval::ValueType;
using vespalib::eval::TensorSpec;
using vespalib::eval::SimpleTensor;
using namespace vespalib::tensor;

void verify_wrapped(const TensorSpec &source, const vespalib::string &type, const TensorSpec &expect) {
    auto tensor = std::make_unique<WrappedSimpleTensor>(SimpleTensor::create(source));
    auto mapped = TensorMapper::mapToWrapped(*tensor, ValueType::from_spec(type));
    TensorSpec actual = mapped->toSpec();
    EXPECT_EQUAL(actual, expect);
}

void verify(const TensorSpec &source, const vespalib::string &type, const TensorSpec &expect) {
    auto tensor = DefaultTensorEngine::ref().create(source);
    const Tensor *tensor_impl = dynamic_cast<const Tensor *>(tensor.get());
    ASSERT_TRUE(tensor_impl);
    TensorMapper mapper(ValueType::from_spec(type));
    auto mapped = mapper.map(*tensor_impl);
    TensorSpec actual = mapped->toSpec();
    EXPECT_EQUAL(actual, expect);
    TEST_DO(verify_wrapped(source, type, expect));
}

TEST("require that sparse tensors can be mapped to sparse type") {
    TEST_DO(verify(TensorSpec("tensor(x{},y{})")
                   .add({{"x","1"},{"y","1"}}, 1)
                   .add({{"x","2"},{"y","1"}}, 3)
                   .add({{"x","1"},{"y","2"}}, 5)
                   .add({{"x","2"},{"y","2"}}, 7),
                   "tensor(y{})",
                   TensorSpec("tensor(y{})")
                   .add({{"y","1"}}, 4)
                   .add({{"y","2"}}, 12)));

    TEST_DO(verify(TensorSpec("tensor(x{},y{})")
                   .add({{"x","1"},{"y","1"}}, 1)
                   .add({{"x","2"},{"y","1"}}, 3)
                   .add({{"x","1"},{"y","2"}}, 5)
                   .add({{"x","2"},{"y","2"}}, 7),
                   "tensor(x{})",
                   TensorSpec("tensor(x{})")
                   .add({{"x","1"}}, 6)
                   .add({{"x","2"}}, 10)));
}

TEST("require that sparse tensors can be mapped to dense type") {
    TEST_DO(verify(TensorSpec("tensor(x{},y{})")
                   .add({{"x","1"},{"y","0"}}, 1)
                   .add({{"x","2"},{"y","0"}}, 3)
                   .add({{"x","1"},{"y","1"}}, 5)
                   .add({{"x","2"},{"y","1"}}, 7),
                   "tensor(y[3])",
                   TensorSpec("tensor(y[3])")
                   .add({{"y",0}}, 4)
                   .add({{"y",1}}, 12)
                   .add({{"y",2}}, 0)));

    TEST_DO(verify(TensorSpec("tensor(x{},y{})")
                   .add({{"x","1"},{"y","0x"}}, 1)
                   .add({{"x","2"},{"y",""}}, 3)
                   .add({{"x","1"},{"y","1"}}, 5)
                   .add({{"x","2"},{"y","10"}}, 7),
                   "tensor(y[3])",
                   TensorSpec("tensor(y[3])")
                   .add({{"y",0}}, 3)
                   .add({{"y",1}}, 5)
                   .add({{"y",2}}, 0)));

    TEST_DO(verify(TensorSpec("tensor(x{},y{})")
                   .add({{"x","0"},{"y","0"}}, 1)
                   .add({{"x","1"},{"y","0"}}, 3)
                   .add({{"x","0"},{"y","1"}}, 5)
                   .add({{"x","10"},{"y","1"}}, 7),
                   "tensor(x[2],y[3])",
                   TensorSpec("tensor(x[2],y[3])")
                   .add({{"x",0},{"y",0}}, 1)
                   .add({{"x",0},{"y",1}}, 5)
                   .add({{"x",0},{"y",2}}, 0)
                   .add({{"x",1},{"y",0}}, 3)
                   .add({{"x",1},{"y",1}}, 0)
                   .add({{"x",1},{"y",2}}, 0)));
}

TEST("require that sparse tensors can be mapped to abstract dense type") {
    TEST_DO(verify(TensorSpec("tensor(x{},y{})")
                   .add({{"x","0"},{"y","0"}}, 1)
                   .add({{"x","1"},{"y","0"}}, 3)
                   .add({{"x","0"},{"y","1"}}, 5)
                   .add({{"x","10"},{"y","1"}}, 7),
                   "tensor(x[2],y[])",
                   TensorSpec("tensor(x[2],y[2])")
                   .add({{"x",0},{"y",0}}, 1)
                   .add({{"x",0},{"y",1}}, 5)
                   .add({{"x",1},{"y",0}}, 3)
                   .add({{"x",1},{"y",1}}, 0)));

    TEST_DO(verify(TensorSpec("tensor(x{},y{})")
                   .add({{"x","0"},{"y","0"}}, 1)
                   .add({{"x","1"},{"y","0"}}, 3)
                   .add({{"x","0"},{"y","1"}}, 5)
                   .add({{"x","2"},{"y","0"}}, 7),
                   "tensor(x[],y[])",
                   TensorSpec("tensor(x[3],y[2])")
                   .add({{"x",0},{"y",0}}, 1)
                   .add({{"x",0},{"y",1}}, 5)
                   .add({{"x",1},{"y",0}}, 3)
                   .add({{"x",1},{"y",1}}, 0)
                   .add({{"x",2},{"y",0}}, 7)
                   .add({{"x",2},{"y",1}}, 0)));

    TEST_DO(verify(TensorSpec("tensor(x{},y{})")
                   .add({{"x","0"},{"y","0"}}, 1)
                   .add({{"x","1"},{"y","0"}}, 3)
                   .add({{"x","0"},{"y","1"}}, 5)
                   .add({{"x","10"},{"y","3"}}, 7),
                   "tensor(x[],y[3])",
                   TensorSpec("tensor(x[2],y[3])")
                   .add({{"x",0},{"y",0}}, 1)
                   .add({{"x",0},{"y",1}}, 5)
                   .add({{"x",0},{"y",2}}, 0)
                   .add({{"x",1},{"y",0}}, 3)
                   .add({{"x",1},{"y",1}}, 0)
                   .add({{"x",1},{"y",2}}, 0)));
}

TEST("require that dense tensors can be mapped to sparse type") {
    TEST_DO(verify(TensorSpec("tensor(x[2],y[2])")
                   .add({{"x",0},{"y",0}}, 1)
                   .add({{"x",0},{"y",1}}, 3)
                   .add({{"x",1},{"y",0}}, 5)
                   .add({{"x",1},{"y",1}}, 7),
                   "tensor(x{})",
                   TensorSpec("tensor(x{})")
                   .add({{"x","0"}}, 4)
                   .add({{"x","1"}}, 12)));
}

TEST("require that mixed tensors can be mapped to sparse type") {
    TEST_DO(verify(TensorSpec("tensor(x[2],y{})")
                   .add({{"x",0},{"y","0"}}, 1)
                   .add({{"x",0},{"y","1"}}, 3)
                   .add({{"x",1},{"y","0"}}, 5)
                   .add({{"x",1},{"y","1"}}, 7),
                   "tensor(x{})",
                   TensorSpec("tensor(x{})")
                   .add({{"x","0"}}, 4)
                   .add({{"x","1"}}, 12)));
}

TEST("require that mixed tensors can be mapped to dense type") {
    TEST_DO(verify(TensorSpec("tensor(x[2],y{})")
                   .add({{"x",0},{"y","0"}}, 1)
                   .add({{"x",0},{"y","1"}}, 3)
                   .add({{"x",1},{"y","0"}}, 5)
                   .add({{"x",1},{"y","1"}}, 7),
                   "tensor(y[])",
                   TensorSpec("tensor(y[2])")
                   .add({{"y",0}}, 6)
                   .add({{"y",1}}, 10)));
}

TEST("require that mixed tensors can be mapped to mixed type") {
    TEST_DO(verify(TensorSpec("tensor(x[2],y{})")
                   .add({{"x",0},{"y","0"}}, 1)
                   .add({{"x",0},{"y","1"}}, 3)
                   .add({{"x",1},{"y","0"}}, 5)
                   .add({{"x",1},{"y","1"}}, 7),
                   "tensor(x{},y[])",
                   TensorSpec("tensor(x{},y[2])")
                   .add({{"x","0"},{"y",0}}, 1)
                   .add({{"x","0"},{"y",1}}, 3)
                   .add({{"x","1"},{"y",0}}, 5)
                   .add({{"x","1"},{"y",1}}, 7)));
}

TEST("require that dense tensors can be mapped to mixed type") {
    TEST_DO(verify(TensorSpec("tensor(x[2],y[2])")
                   .add({{"x",0},{"y",0}}, 1)
                   .add({{"x",0},{"y",1}}, 3)
                   .add({{"x",1},{"y",0}}, 5)
                   .add({{"x",1},{"y",1}}, 7),
                   "tensor(x{},y[])",
                   TensorSpec("tensor(x{},y[2])")
                   .add({{"x","0"},{"y",0}}, 1)
                   .add({{"x","0"},{"y",1}}, 3)
                   .add({{"x","1"},{"y",0}}, 5)
                   .add({{"x","1"},{"y",1}}, 7)));
}

TEST("require that sparse tensors can be mapped to mixed type") {
    TEST_DO(verify(TensorSpec("tensor(x{},y{})")
                   .add({{"x","0"},{"y","0"}}, 1)
                   .add({{"x","0"},{"y","1"}}, 3)
                   .add({{"x","1"},{"y","0"}}, 5)
                   .add({{"x","1"},{"y","1"}}, 7),
                   "tensor(x[],y{})",
                   TensorSpec("tensor(x[2],y{})")
                   .add({{"x",0},{"y","0"}}, 1)
                   .add({{"x",0},{"y","1"}}, 3)
                   .add({{"x",1},{"y","0"}}, 5)
                   .add({{"x",1},{"y","1"}}, 7)));
}

TEST("require that missing dimensions are added appropriately") {
    TEST_DO(verify(TensorSpec("tensor(x{})")
                   .add({{"x","foo"}}, 42),
                   "tensor(x{},y{})",
                   TensorSpec("tensor(x{},y{})")
                   .add({{"x","foo"},{"y",""}}, 42)));

    TEST_DO(verify(TensorSpec("tensor(x[1])")
                   .add({{"x",0}}, 42),
                   "tensor(x[1],y[],z[2])",
                   TensorSpec("tensor(x[1],y[1],z[2])")
                   .add({{"x",0},{"y",0},{"z",0}}, 42)
                   .add({{"x",0},{"y",0},{"z",1}}, 0)));

    TEST_DO(verify(TensorSpec("tensor(a{})")
                   .add({{"a","foo"}}, 42),
                   "tensor(a{},b[],c{},d[2])",
                   TensorSpec("tensor(a{},b[1],c{},d[2])")
                   .add({{"a","foo"},{"b",0},{"c",""},{"d",0}}, 42)
                   .add({{"a","foo"},{"b",0},{"c",""},{"d",1}}, 0)));
}

TEST_MAIN() { TEST_RUN_ALL(); }