summaryrefslogtreecommitdiffstats
path: root/eval/src/tests/eval/simple_tensor/simple_tensor_test.cpp
blob: aa4c3b8c0211c1da68fe4b5bea2f525337df731a (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright 2017 Yahoo Holdings. 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/simple_tensor.h>
#include <vespa/eval/eval/simple_tensor_engine.h>
#include <vespa/eval/eval/operation.h>
#include <vespa/vespalib/util/stash.h>
#include <vespa/vespalib/data/memory.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <iostream>

using namespace vespalib::eval;

using Cell = SimpleTensor::Cell;
using Cells = SimpleTensor::Cells;
using Address = SimpleTensor::Address;
using Stash = vespalib::Stash;
using vespalib::nbostream;
using vespalib::Memory;

TensorSpec to_spec(const Value &a) { return SimpleTensorEngine::ref().to_spec(a); }

const Tensor &unwrap(const Value &value) {
    ASSERT_TRUE(value.is_tensor());
    return *value.as_tensor();
}

struct CellBuilder {
    Cells cells;
    CellBuilder &add(const Address &addr, double value) {
        cells.emplace_back(addr, value);
        return *this;
    }
    Cells build() { return cells; }
};

TEST("require that simple tensors can be built using tensor spec") {
    TensorSpec spec("tensor(w{},x[2],y{},z[2])");
    spec.add({{"w", "xxx"}, {"x", 0}, {"y", "xxx"}, {"z", 0}}, 1.0)
        .add({{"w", "xxx"}, {"x", 0}, {"y", "yyy"}, {"z", 1}}, 2.0)
        .add({{"w", "yyy"}, {"x", 1}, {"y", "xxx"}, {"z", 0}}, 3.0)
        .add({{"w", "yyy"}, {"x", 1}, {"y", "yyy"}, {"z", 1}}, 4.0);
    Value::UP tensor = SimpleTensorEngine::ref().from_spec(spec);
    TensorSpec full_spec("tensor(w{},x[2],y{},z[2])");
    full_spec
        .add({{"w", "xxx"}, {"x", 0}, {"y", "xxx"}, {"z", 0}}, 1.0)
        .add({{"w", "xxx"}, {"x", 0}, {"y", "xxx"}, {"z", 1}}, 0.0)
        .add({{"w", "xxx"}, {"x", 0}, {"y", "yyy"}, {"z", 0}}, 0.0)
        .add({{"w", "xxx"}, {"x", 0}, {"y", "yyy"}, {"z", 1}}, 2.0)
        .add({{"w", "xxx"}, {"x", 1}, {"y", "xxx"}, {"z", 0}}, 0.0)
        .add({{"w", "xxx"}, {"x", 1}, {"y", "xxx"}, {"z", 1}}, 0.0)
        .add({{"w", "xxx"}, {"x", 1}, {"y", "yyy"}, {"z", 0}}, 0.0)
        .add({{"w", "xxx"}, {"x", 1}, {"y", "yyy"}, {"z", 1}}, 0.0)
        .add({{"w", "yyy"}, {"x", 0}, {"y", "xxx"}, {"z", 0}}, 0.0)
        .add({{"w", "yyy"}, {"x", 0}, {"y", "xxx"}, {"z", 1}}, 0.0)
        .add({{"w", "yyy"}, {"x", 0}, {"y", "yyy"}, {"z", 0}}, 0.0)
        .add({{"w", "yyy"}, {"x", 0}, {"y", "yyy"}, {"z", 1}}, 0.0)
        .add({{"w", "yyy"}, {"x", 1}, {"y", "xxx"}, {"z", 0}}, 3.0)
        .add({{"w", "yyy"}, {"x", 1}, {"y", "xxx"}, {"z", 1}}, 0.0)
        .add({{"w", "yyy"}, {"x", 1}, {"y", "yyy"}, {"z", 0}}, 0.0)
        .add({{"w", "yyy"}, {"x", 1}, {"y", "yyy"}, {"z", 1}}, 4.0);
    Value::UP full_tensor = SimpleTensorEngine::ref().from_spec(full_spec);
    EXPECT_EQUAL(full_spec, to_spec(*tensor));
    EXPECT_EQUAL(full_spec, to_spec(*full_tensor));
};

TEST("require that simple tensors can have their values negated") {
    auto tensor = SimpleTensor::create(
            TensorSpec("tensor(x{},y{})")
            .add({{"x","1"},{"y","1"}}, 1)
            .add({{"x","2"},{"y","1"}}, -3)
            .add({{"x","1"},{"y","2"}}, 5));
    auto expect = SimpleTensor::create(
            TensorSpec("tensor(x{},y{})")
            .add({{"x","1"},{"y","1"}}, -1)
            .add({{"x","2"},{"y","1"}}, 3)
            .add({{"x","1"},{"y","2"}}, -5));
    auto result = tensor->map([](double a){ return -a; });
    EXPECT_EQUAL(to_spec(*expect), to_spec(*result));
    Stash stash;
    const Value &result2 = SimpleTensorEngine::ref().map(*tensor, operation::Neg::f, stash);
    EXPECT_EQUAL(to_spec(*expect), to_spec(unwrap(result2)));    
}

TEST("require that simple tensors can be multiplied with each other") {
    auto lhs = SimpleTensor::create(
            TensorSpec("tensor(x{},y{})")
            .add({{"x","1"},{"y","1"}}, 1)
            .add({{"x","2"},{"y","1"}}, 3)
            .add({{"x","1"},{"y","2"}}, 5));
    auto rhs = SimpleTensor::create(
            TensorSpec("tensor(y{},z{})")
            .add({{"y","1"},{"z","1"}}, 7)
            .add({{"y","2"},{"z","1"}}, 11)
            .add({{"y","1"},{"z","2"}}, 13));
    auto expect = SimpleTensor::create(
            TensorSpec("tensor(x{},y{},z{})")
            .add({{"x","1"},{"y","1"},{"z","1"}}, 7)
            .add({{"x","1"},{"y","1"},{"z","2"}}, 13)
            .add({{"x","2"},{"y","1"},{"z","1"}}, 21)
            .add({{"x","2"},{"y","1"},{"z","2"}}, 39)
            .add({{"x","1"},{"y","2"},{"z","1"}}, 55));
    auto result = SimpleTensor::join(*lhs, *rhs, [](double a, double b){ return (a * b); });
    EXPECT_EQUAL(to_spec(*expect), to_spec(*result));
    Stash stash;
    const Value &result2 = SimpleTensorEngine::ref().join(*lhs, *rhs, operation::Mul::f, stash);
    EXPECT_EQUAL(to_spec(*expect), to_spec(unwrap(result2)));
}

TEST("require that simple tensors support dimension reduction") {
    auto tensor = SimpleTensor::create(
            TensorSpec("tensor(x[3],y[2])")
            .add({{"x",0},{"y",0}}, 1)
            .add({{"x",1},{"y",0}}, 2)
            .add({{"x",2},{"y",0}}, 3)
            .add({{"x",0},{"y",1}}, 4)
            .add({{"x",1},{"y",1}}, 5)
            .add({{"x",2},{"y",1}}, 6));
    auto expect_sum_y = SimpleTensor::create(
            TensorSpec("tensor(x[3])")
            .add({{"x",0}}, 5)
            .add({{"x",1}}, 7)
            .add({{"x",2}}, 9));
    auto expect_sum_x = SimpleTensor::create(
            TensorSpec("tensor(y[2])")
            .add({{"y",0}}, 6)
            .add({{"y",1}}, 15));
    auto expect_sum_all = SimpleTensor::create(TensorSpec("double").add({}, 21));
    Stash stash;
    Aggregator &aggr_sum = Aggregator::create(Aggr::SUM, stash);
    auto result_sum_y = tensor->reduce(aggr_sum, {"y"});
    auto result_sum_x = tensor->reduce(aggr_sum, {"x"});
    auto result_sum_all = tensor->reduce(aggr_sum, {"x", "y"});
    EXPECT_EQUAL(to_spec(*expect_sum_y), to_spec(*result_sum_y));
    EXPECT_EQUAL(to_spec(*expect_sum_x), to_spec(*result_sum_x));
    EXPECT_EQUAL(to_spec(*expect_sum_all), to_spec(*result_sum_all));
    const Value &result_sum_y_2 = SimpleTensorEngine::ref().reduce(*tensor, Aggr::SUM, {"y"}, stash);
    const Value &result_sum_x_2 = SimpleTensorEngine::ref().reduce(*tensor, Aggr::SUM, {"x"}, stash);
    const Value &result_sum_all_2 = SimpleTensorEngine::ref().reduce(*tensor, Aggr::SUM, {"x", "y"}, stash); 
    const Value &result_sum_all_3 = SimpleTensorEngine::ref().reduce(*tensor, Aggr::SUM, {}, stash);
    EXPECT_EQUAL(to_spec(*expect_sum_y), to_spec(unwrap(result_sum_y_2)));
    EXPECT_EQUAL(to_spec(*expect_sum_x), to_spec(unwrap(result_sum_x_2)));
    EXPECT_TRUE(result_sum_all_2.is_double());
    EXPECT_TRUE(result_sum_all_3.is_double());
    EXPECT_EQUAL(21, result_sum_all_2.as_double());
    EXPECT_EQUAL(21, result_sum_all_3.as_double());
    EXPECT_EQUAL(to_spec(*result_sum_y), to_spec(*result_sum_y));
    EXPECT_NOT_EQUAL(to_spec(*result_sum_y), to_spec(*result_sum_x));
}

//-----------------------------------------------------------------------------

struct SparseTensorExample {
    TensorSpec make_spec() const {
        return TensorSpec("tensor(x{},y{})")
            .add({{"x","a"},{"y","a"}}, 1)
            .add({{"x","a"},{"y","b"}}, 2)
            .add({{"x","b"},{"y","a"}}, 3);
    }
    std::unique_ptr<SimpleTensor> make_tensor() const {
        return SimpleTensor::create(make_spec());
    }
    template <typename T>
    void encode_inner(nbostream &dst) const {
        dst.putInt1_4Bytes(2);
        dst.writeSmallString("x");
        dst.writeSmallString("y");
        dst.putInt1_4Bytes(3);
        dst.writeSmallString("a");
        dst.writeSmallString("a");
        dst << (T) 1;
        dst.writeSmallString("a");
        dst.writeSmallString("b");
        dst << (T) 2;
        dst.writeSmallString("b");
        dst.writeSmallString("a");
        dst << (T) 3;
    }
    void encode_default(nbostream &dst) const {
        dst.putInt1_4Bytes(1);
        encode_inner<double>(dst);
    }
    void encode_with_double(nbostream &dst) const {
        dst.putInt1_4Bytes(5);
        dst.putInt1_4Bytes(0);
        encode_inner<double>(dst);
    }
    void encode_with_float(nbostream &dst) const {
        dst.putInt1_4Bytes(5);
        dst.putInt1_4Bytes(1);
        encode_inner<float>(dst);
    }
};

TEST_F("require that sparse tensors can be decoded", SparseTensorExample()) {
    nbostream data1;
    nbostream data2;
    nbostream data3;
    f1.encode_default(data1);
    f1.encode_with_double(data2);
    f1.encode_with_float(data3);
    EXPECT_EQUAL(to_spec(*SimpleTensor::decode(data1)), f1.make_spec());
    EXPECT_EQUAL(to_spec(*SimpleTensor::decode(data2)), f1.make_spec());
    EXPECT_EQUAL(to_spec(*SimpleTensor::decode(data3)), f1.make_spec());
}

TEST_F("require that sparse tensors can be encoded", SparseTensorExample()) {
    nbostream data;
    nbostream expect;
    SimpleTensor::encode(*f1.make_tensor(), data);
    f1.encode_default(expect);
    EXPECT_EQUAL(Memory(data.peek(), data.size()), Memory(expect.peek(), expect.size()));
}

//-----------------------------------------------------------------------------

struct DenseTensorExample {
    TensorSpec make_spec() const {
        return TensorSpec("tensor(x[3],y[2])")
            .add({{"x",0},{"y",0}}, 1)
            .add({{"x",0},{"y",1}}, 2)
            .add({{"x",1},{"y",0}}, 3)
            .add({{"x",1},{"y",1}}, 4)
            .add({{"x",2},{"y",0}}, 5)
            .add({{"x",2},{"y",1}}, 6);
    }
    std::unique_ptr<SimpleTensor> make_tensor() const {
        return SimpleTensor::create(make_spec());
    }
    template <typename T>
    void encode_inner(nbostream &dst) const {
        dst.putInt1_4Bytes(2);
        dst.writeSmallString("x");
        dst.putInt1_4Bytes(3);
        dst.writeSmallString("y");
        dst.putInt1_4Bytes(2);
        dst << (T) 1;
        dst << (T) 2;
        dst << (T) 3;
        dst << (T) 4;
        dst << (T) 5;
        dst << (T) 6;
    }
    void encode_default(nbostream &dst) const {
        dst.putInt1_4Bytes(2);
        encode_inner<double>(dst);
    }
    void encode_with_double(nbostream &dst) const {
        dst.putInt1_4Bytes(6);
        dst.putInt1_4Bytes(0);
        encode_inner<double>(dst);
    }
    void encode_with_float(nbostream &dst) const {
        dst.putInt1_4Bytes(6);
        dst.putInt1_4Bytes(1);
        encode_inner<float>(dst);
    }
};

TEST_F("require that dense tensors can be decoded", DenseTensorExample()) {
    nbostream data1;
    nbostream data2;
    nbostream data3;
    f1.encode_default(data1);
    f1.encode_with_double(data2);
    f1.encode_with_float(data3);
    EXPECT_EQUAL(to_spec(*SimpleTensor::decode(data1)), f1.make_spec());
    EXPECT_EQUAL(to_spec(*SimpleTensor::decode(data2)), f1.make_spec());
    EXPECT_EQUAL(to_spec(*SimpleTensor::decode(data3)), f1.make_spec());
}

TEST_F("require that dense tensors can be encoded", DenseTensorExample()) {
    nbostream data;
    nbostream expect;
    SimpleTensor::encode(*f1.make_tensor(), data);
    f1.encode_default(expect);
    EXPECT_EQUAL(Memory(data.peek(), data.size()), Memory(expect.peek(), expect.size()));
}

//-----------------------------------------------------------------------------

struct MixedTensorExample {
    TensorSpec make_spec() const {
        return TensorSpec("tensor(x{},y{},z[2])")
            .add({{"x","a"},{"y","a"},{"z",0}}, 1)
            .add({{"x","a"},{"y","a"},{"z",1}}, 2)
            .add({{"x","a"},{"y","b"},{"z",0}}, 3)
            .add({{"x","a"},{"y","b"},{"z",1}}, 4)
            .add({{"x","b"},{"y","a"},{"z",0}}, 5)
            .add({{"x","b"},{"y","a"},{"z",1}}, 6);
    }
    std::unique_ptr<SimpleTensor> make_tensor() const {
        return SimpleTensor::create(make_spec());
    }
    template <typename T>
    void encode_inner(nbostream &dst) const {
        dst.putInt1_4Bytes(2);
        dst.writeSmallString("x");
        dst.writeSmallString("y");
        dst.putInt1_4Bytes(1);
        dst.writeSmallString("z");
        dst.putInt1_4Bytes(2);
        dst.putInt1_4Bytes(3);
        dst.writeSmallString("a");
        dst.writeSmallString("a");
        dst << (T) 1;
        dst << (T) 2;
        dst.writeSmallString("a");
        dst.writeSmallString("b");
        dst << (T) 3;
        dst << (T) 4;
        dst.writeSmallString("b");
        dst.writeSmallString("a");
        dst << (T) 5;
        dst << (T) 6;
    }
    void encode_default(nbostream &dst) const {
        dst.putInt1_4Bytes(3);
        encode_inner<double>(dst);
    }
    void encode_with_double(nbostream &dst) const {
        dst.putInt1_4Bytes(7);
        dst.putInt1_4Bytes(0);
        encode_inner<double>(dst);
    }
    void encode_with_float(nbostream &dst) const {
        dst.putInt1_4Bytes(7);
        dst.putInt1_4Bytes(1);
        encode_inner<float>(dst);
    }
};

TEST_F("require that mixed tensors can be decoded", MixedTensorExample()) {
    nbostream data1;
    nbostream data2;
    nbostream data3;
    f1.encode_default(data1);
    f1.encode_with_double(data2);
    f1.encode_with_float(data3);
    EXPECT_EQUAL(to_spec(*SimpleTensor::decode(data1)), f1.make_spec());
    EXPECT_EQUAL(to_spec(*SimpleTensor::decode(data2)), f1.make_spec());
    EXPECT_EQUAL(to_spec(*SimpleTensor::decode(data3)), f1.make_spec());
}

TEST_F("require that mixed tensors can be encoded", MixedTensorExample()) {
    nbostream data;
    nbostream expect;
    SimpleTensor::encode(*f1.make_tensor(), data);
    f1.encode_default(expect);
    EXPECT_EQUAL(Memory(data.peek(), data.size()), Memory(expect.peek(), expect.size()));
}

//-----------------------------------------------------------------------------

TEST_MAIN() { TEST_RUN_ALL(); }