aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/tests/tensor/tensor_serialization/tensor_serialization_test.cpp
blob: b27221f6ba59afd54c6ace43e76c12c1054ea052 (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
// 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/eval/tensor/sparse/sparse_tensor.h>
#include <vespa/eval/tensor/sparse/sparse_tensor_builder.h>
#include <vespa/eval/tensor/types.h>
#include <vespa/eval/tensor/default_tensor.h>
#include <vespa/eval/tensor/tensor_factory.h>
#include <vespa/eval/tensor/serialization/typed_binary_format.h>
#include <vespa/eval/tensor/serialization/sparse_binary_format.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/objects/hexdump.h>
#include <ostream>

using namespace vespalib::tensor;
using vespalib::nbostream;
using ExpBuffer = std::vector<uint8_t>;

namespace std {

bool operator==(const std::vector<uint8_t> &exp, const nbostream &stream)
{
    return ((exp.size() == stream.size()) &&
            (memcmp(&exp[0], stream.peek(), exp.size()) == 0));
}

std::ostream &operator<<(std::ostream &out, const std::vector<uint8_t> &rhs)
{
    out << vespalib::HexDump(&rhs[0], rhs.size());
    return out;
}

}

namespace vespalib {

namespace tensor {

static bool operator==(const Tensor &lhs, const Tensor &rhs)
{
    return lhs.equals(rhs);
}

}
}

template <class BuilderType>
void
checkDeserialize(vespalib::nbostream &stream, const Tensor &rhs)
{
    (void) stream;
    (void) rhs;
}

template <>
void
checkDeserialize<DefaultTensor::builder>(nbostream &stream, const Tensor &rhs)
{
    nbostream wrapStream(stream.peek(), stream.size());
    auto chk = TypedBinaryFormat::deserialize(wrapStream);
    EXPECT_EQUAL(0u, wrapStream.size());
    EXPECT_EQUAL(*chk, rhs);
}

template <typename BuilderType>
struct Fixture
{
    BuilderType _builder;
    Fixture() : _builder() {}

    Tensor::UP createTensor(const TensorCells &cells) {
        return vespalib::tensor::TensorFactory::create(cells, _builder);
    }
    Tensor::UP createTensor(const TensorCells &cells, const TensorDimensions &dimensions) {
        return TensorFactory::create(cells, dimensions, _builder);
    }

    void serialize(nbostream &stream, const Tensor &tensor) {
        TypedBinaryFormat::serialize(stream, tensor);
    }
    Tensor::UP deserialize(nbostream &stream) {
        BuilderType builder;
        nbostream wrapStream(stream.peek(), stream.size());
        auto formatId = wrapStream.getInt1_4Bytes();
        ASSERT_EQUAL(formatId, 1u); // sparse format
        SparseBinaryFormat::deserialize(wrapStream, builder);
        EXPECT_TRUE(wrapStream.size() == 0);
        auto ret = builder.build();
        checkDeserialize<BuilderType>(stream, *ret);
        stream.adjustReadPos(stream.size());
        return ret;
    }
    void assertSerialized(const ExpBuffer &exp, const TensorCells &rhs,
                          const TensorDimensions &rhsDimensions) {
        Tensor::UP rhsTensor(createTensor(rhs, rhsDimensions));
        nbostream rhsStream;
        serialize(rhsStream, *rhsTensor);
        EXPECT_EQUAL(exp, rhsStream);
        auto rhs2 = deserialize(rhsStream);
        EXPECT_EQUAL(*rhs2, *rhsTensor);
    }
};

using SparseFixture = Fixture<SparseTensorBuilder>;


template <typename FixtureType>
void
testTensorSerialization(FixtureType &f)
{
    TEST_DO(f.assertSerialized({ 0x01, 0x00, 0x00 }, {}, {}));
    TEST_DO(f.assertSerialized({ 0x01, 0x01, 0x01, 0x78, 0x00 },
                               {}, { "x" }));
    TEST_DO(f.assertSerialized({ 0x01, 0x02, 0x01, 0x78, 0x01, 0x79, 0x00 },
                               {}, { "x", "y" }));
    TEST_DO(f.assertSerialized({ 0x01, 0x01, 0x01, 0x78, 0x01, 0x01, 0x31, 0x40,
                                 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
                               { {{{"x","1"}}, 3} }, { "x" }));
    TEST_DO(f.assertSerialized({ 0x01, 0x02, 0x01, 0x78, 0x01, 0x79, 0x01, 0x00,
                                 0x00, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
                                 0x00 },
                               { {{}, 3} }, { "x", "y"}));
    TEST_DO(f.assertSerialized({ 0x01, 0x02, 0x01, 0x78, 0x01, 0x79, 0x01, 0x01,
                                 0x31, 0x00, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00,
                                 0x00, 0x00 },
                               { {{{"x","1"}}, 3} }, { "x", "y" }));
    TEST_DO(f.assertSerialized({ 0x01, 0x02, 0x01, 0x78, 0x01, 0x79, 0x01, 0x00,
                                 0x01, 0x33, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00,
                                 0x00, 0x00 },
                               { {{{"y","3"}}, 3} }, { "x", "y" }));
    TEST_DO(f.assertSerialized({ 0x01, 0x02, 0x01, 0x78, 0x01, 0x79, 0x01, 0x01,
                                 0x32, 0x01, 0x34, 0x40, 0x08, 0x00, 0x00, 0x00,
                                 0x00, 0x00, 0x00 },
                               { {{{"x","2"}, {"y", "4"}}, 3} }, { "x", "y" }));
    TEST_DO(f.assertSerialized({        0x01, 0x02, 0x01, 0x78, 0x01, 0x79,
                                        0x01, 0x01, 0x31, 0x00, 0x40, 0x08,
                                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
                               { {{{"x","1"}}, 3} }, {"x", "y"}));
}

TEST_F("test tensor serialization for SparseTensor", SparseFixture)
{
    testTensorSerialization(f);
}


struct DenseFixture
{
    Tensor::UP createTensor(const DenseTensorCells &cells) {
        return TensorFactory::createDense(cells);
    }

    void serialize(nbostream &stream, const Tensor &tensor) {
        TypedBinaryFormat::serialize(stream, tensor);
    }

    Tensor::UP deserialize(nbostream &stream) {
        nbostream wrapStream(stream.peek(), stream.size());
        auto ret = TypedBinaryFormat::deserialize(wrapStream);
        EXPECT_TRUE(wrapStream.size() == 0);
        stream.adjustReadPos(stream.size());
        return ret;
    }
    void assertSerialized(const ExpBuffer &exp, const DenseTensorCells &rhs) {
        Tensor::UP rhsTensor(createTensor(rhs));
        nbostream rhsStream;
        serialize(rhsStream, *rhsTensor);
        EXPECT_EQUAL(exp, rhsStream);
        auto rhs2 = deserialize(rhsStream);
        EXPECT_EQUAL(*rhs2, *rhsTensor);
    }
};


TEST_F("test tensor serialization for DenseTensor", DenseFixture)
{
    TEST_DO(f.assertSerialized({        0x02, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00},
                               {}));
    TEST_DO(f.assertSerialized({        0x02, 0x01, 0x01, 0x78, 0x01,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00},
                               { {{{"x",0}}, 0} }));
    TEST_DO(f.assertSerialized({        0x02, 0x02, 0x01, 0x78, 0x01,
                                        0x01, 0x79, 0x01,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00 },
                               { {{{"x",0},{"y", 0}}, 0} }));
    TEST_DO(f.assertSerialized({        0x02, 0x01, 0x01, 0x78, 0x02,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x40, 0x08, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00 },
                               { {{{"x",1}}, 3} }));
    TEST_DO(f.assertSerialized({        0x02, 0x02, 0x01, 0x78, 0x01,
                                        0x01, 0x79, 0x01,
                                        0x40, 0x08, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00 },
                               { {{{"x",0},{"y",0}}, 3} }));
    TEST_DO(f.assertSerialized({        0x02, 0x02, 0x01, 0x78, 0x02,
                                        0x01, 0x79, 0x01,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x40, 0x08, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00 },
                               { {{{"x",1},{"y",0}}, 3} }));
    TEST_DO(f.assertSerialized({        0x02, 0x02, 0x01, 0x78, 0x01,
                                        0x01, 0x79, 0x04,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x40, 0x08, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00 },
                               { {{{"x",0},{"y",3}}, 3} }));
    TEST_DO(f.assertSerialized({        0x02, 0x02, 0x01, 0x78, 0x03,
                                        0x01, 0x79, 0x05,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00,
                                        0x40, 0x08, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00 },
                               { {{{"x",2}, {"y",4}}, 3} }));
}


TEST_MAIN() { TEST_RUN_ALL(); }