aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/apps/make_tensor_binary_format_test_spec/make_tensor_binary_format_test_spec.cpp
blob: 97daa3cea5dfbcea42ce7e4e75ee13e384e21563 (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/util/bfloat16.h>
#include <vespa/eval/eval/int8float.h>
#include <vespa/eval/eval/tensor_spec.h>
#include <vespa/eval/eval/value_type.h>
#include <vespa/eval/eval/test/test_io.h>
#include <iostream>

using namespace vespalib;
using namespace vespalib::eval;
using namespace vespalib::slime::convenience;

using Options = std::initializer_list<std::reference_wrapper<const nbostream>>;
using Dict = std::vector<vespalib::string>;

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

template <typename T> std::vector<bool> with_cell_type_opts();
template <> std::vector<bool> with_cell_type_opts<double>() { return {false, true}; }
template <> std::vector<bool> with_cell_type_opts<float>() { return {true}; }
template <> std::vector<bool> with_cell_type_opts<BFloat16>() { return {true}; }
template <> std::vector<bool> with_cell_type_opts<Int8Float>() { return {true}; }

template <typename T> uint8_t cell_type_id();
template <> uint8_t cell_type_id<double>() { return 0; }
template <> uint8_t cell_type_id<float>() { return 1; }
template <> uint8_t cell_type_id<BFloat16>() { return 2; }
template <> uint8_t cell_type_id<Int8Float>() { return 3; }

template <typename T> const char *cell_type_str();
template <> const char *cell_type_str<double>() { return ""; }
template <> const char *cell_type_str<float>() { return "<float>"; }
template <> const char *cell_type_str<BFloat16>() { return "<bfloat16>"; }
template <> const char *cell_type_str<Int8Float>() { return "<int8>"; }

template <typename T> nbostream make_sparse(bool with_cell_type) {
    nbostream data;
    if (with_cell_type) {
        data << uint8_t(0x5);
        data << cell_type_id<T>();
    } else {
        data << uint8_t(0x1);
    }
    return data;
}

template <typename T> nbostream make_dense(bool with_cell_type) {
    nbostream data;
    if (with_cell_type) {
        data << uint8_t(0x6);
        data << cell_type_id<T>();
    } else {
        data << uint8_t(0x2);
    }
    return data;
}

template <typename T> nbostream make_mixed(bool with_cell_type) {
    nbostream data;
    if (with_cell_type) {
        data << uint8_t(0x7);
        data << cell_type_id<T>();
    } else {
        data << uint8_t(0x3);
    }
    return data;
}

void set_tensor(Cursor &test, const TensorSpec &spec_in) {
    auto spec = spec_in.normalize();
    const Inspector &old_tensor = test["tensor"];
    if (old_tensor.valid()) {
        TensorSpec old_spec = TensorSpec::from_slime(old_tensor);
        if (!(old_spec == spec)) {
            fprintf(stderr, "inconsistent specs\n");
            std::cerr << old_spec;
            std::cerr << spec;
            abort(); // inconsistent specs across binary permutations
        }
    } else {
        Cursor &tensor = test.setObject("tensor");
        spec.to_slime(tensor);
    }
}

void add_binary(Cursor &test, const nbostream &data) {
    if (!test["binary"].valid()) {
        test.setArray("binary");
    }
    test["binary"].addData(Memory(data.peek(), data.size()));
}

void add_binary(Cursor &test, Options opts) {
    for (const nbostream &opt: opts) {
        add_binary(test, opt);
    }
}

std::vector<Dict> make_permutations(const Dict &dict) {
    std::vector<Dict> list;
    if (dict.empty()) {
    } else if (dict.size() == 1) {
        list.push_back(dict);
    } else if (dict.size() == 2) {
        list.push_back({dict[0], dict[1]});
        list.push_back({dict[1], dict[0]});
    } else if (dict.size() == 3) {
        list.push_back({dict[0], dict[1], dict[2]});
        list.push_back({dict[0], dict[2], dict[1]});
        list.push_back({dict[1], dict[0], dict[2]});
        list.push_back({dict[1], dict[2], dict[0]});
        list.push_back({dict[2], dict[0], dict[1]});
        list.push_back({dict[2], dict[1], dict[0]});
    } else {
        fprintf(stderr, "unsupported permutation size: %zu\n", dict.size());
        abort(); // only implemented for sizes (0,1,2,3)
    }
    return list;
};

const std::map<std::string, double> val_map{
    {"a",    1.0},
    {"b",    2.0},
    {"c",    3.0},
    {"foo",  1.0},
    {"bar",  2.0}};

double val(size_t idx) { return double(idx + 1); }
double val(const vespalib::string &label) {
    auto res = val_map.find(label);
    if (res == val_map.end()) {
        fprintf(stderr, "unsupported label: '%s'\n", label.c_str());
        abort(); // unsupported label
    }
    return res->second;
}
double mix(std::initializer_list<double> vals) {
    double value = 0.0;
    for (double val: vals) {
        value = ((value * 10) + val);
    }
    return value;
}

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

void make_number_test(Cursor &test, double value) {
    for (bool with_cell_type: with_cell_type_opts<double>()) {
        TensorSpec spec("double");
        spec.add({{}}, value);
        nbostream sparse = make_sparse<double>(with_cell_type);
        sparse.putInt1_4Bytes(0);
        sparse.putInt1_4Bytes(1);
        sparse << value;
        nbostream dense = make_dense<double>(with_cell_type);
        dense.putInt1_4Bytes(0);
        dense << value;
        nbostream mixed = make_mixed<double>(with_cell_type);
        mixed.putInt1_4Bytes(0);
        mixed.putInt1_4Bytes(0);
        mixed << value;
        set_tensor(test, spec);
        add_binary(test, {sparse, dense, mixed});
        if (value == 0.0) {
            nbostream empty = make_sparse<double>(with_cell_type);
            empty.putInt1_4Bytes(0);
            empty.putInt1_4Bytes(0);
            add_binary(test, empty);
        }
    }
}

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

template <typename T>
void make_vector_test(Cursor &test, size_t x_size) {
    for (bool with_cell_type: with_cell_type_opts<T>()) {
        TensorSpec spec(vespalib::make_string("tensor%s(x[%zu])", cell_type_str<T>(), x_size));
        nbostream dense = make_dense<T>(with_cell_type);
        dense.putInt1_4Bytes(1);
        dense.writeSmallString("x");
        dense.putInt1_4Bytes(x_size);
        nbostream mixed = make_mixed<T>(with_cell_type);
        mixed.putInt1_4Bytes(0);
        mixed.putInt1_4Bytes(1);
        mixed.writeSmallString("x");
        mixed.putInt1_4Bytes(x_size);
        for (size_t x = 0; x < x_size; ++x) {
            double value = val(x);
            spec.add({{"x", x}}, value);
            dense << T(value);
            mixed << T(value);
        }
        set_tensor(test, spec);
        add_binary(test, {dense, mixed});
    }
}

template <typename T>
void make_matrix_test(Cursor &test, size_t x_size, size_t y_size) {
    for (bool with_cell_type: with_cell_type_opts<T>()) {
        TensorSpec spec(vespalib::make_string("tensor%s(x[%zu],y[%zu])", cell_type_str<T>(), x_size, y_size));
        nbostream dense = make_dense<T>(with_cell_type);
        dense.putInt1_4Bytes(2);
        dense.writeSmallString("x");
        dense.putInt1_4Bytes(x_size);
        dense.writeSmallString("y");
        dense.putInt1_4Bytes(y_size);
        nbostream mixed = make_mixed<T>(with_cell_type);
        mixed.putInt1_4Bytes(0);
        mixed.putInt1_4Bytes(2);
        mixed.writeSmallString("x");
        mixed.putInt1_4Bytes(x_size);
        mixed.writeSmallString("y");
        mixed.putInt1_4Bytes(y_size);
        for (size_t x = 0; x < x_size; ++x) {
            for (size_t y = 0; y < y_size; ++y) {
                double value = mix({val(x), val(y)});
                spec.add({{"x", x}, {"y", y}}, value);
                dense << T(value);
                mixed << T(value);
            }
        }
        set_tensor(test, spec);
        add_binary(test, {dense, mixed});
    }
}

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

template <typename T>
void make_map_test(Cursor &test, const Dict &x_dict_in) {
    for (bool with_cell_type: with_cell_type_opts<T>()) {
        nbostream sparse_base = make_sparse<T>(with_cell_type);
        sparse_base.putInt1_4Bytes(1);
        sparse_base.writeSmallString("x");
        sparse_base.putInt1_4Bytes(x_dict_in.size());
        nbostream mixed_base = make_mixed<T>(with_cell_type);
        mixed_base.putInt1_4Bytes(1);
        mixed_base.writeSmallString("x");
        mixed_base.putInt1_4Bytes(0);
        mixed_base.putInt1_4Bytes(x_dict_in.size());
        auto x_perm = make_permutations(x_dict_in);
        for (const Dict &x_dict: x_perm) {
            TensorSpec spec(vespalib::make_string("tensor%s(x{})", cell_type_str<T>()));
            nbostream sparse = sparse_base;
            nbostream mixed = mixed_base;
            for (vespalib::string x: x_dict) {
                double value = val(x);
                spec.add({{"x", x}}, value);
                sparse.writeSmallString(x);
                mixed.writeSmallString(x);
                sparse << T(value);
                mixed << T(value);
            }
            set_tensor(test, spec);
            add_binary(test, {sparse, mixed});
        }
        if (x_dict_in.empty()) {
            TensorSpec spec(vespalib::make_string("tensor%s(x{})", cell_type_str<T>()));
            set_tensor(test, spec);
            add_binary(test, {sparse_base, mixed_base});
        }
    }
}

template <typename T>
void make_mesh_test(Cursor &test, const Dict &x_dict_in, const vespalib::string &y) {
    for (bool with_cell_type: with_cell_type_opts<T>()) {
        nbostream sparse_base = make_sparse<T>(with_cell_type);
        sparse_base.putInt1_4Bytes(2);
        sparse_base.writeSmallString("x");
        sparse_base.writeSmallString("y");
        sparse_base.putInt1_4Bytes(x_dict_in.size() * 1);
        nbostream mixed_base = make_mixed<T>(with_cell_type);
        mixed_base.putInt1_4Bytes(2);
        mixed_base.writeSmallString("x");
        mixed_base.writeSmallString("y");
        mixed_base.putInt1_4Bytes(0);
        mixed_base.putInt1_4Bytes(x_dict_in.size() * 1);
        auto x_perm = make_permutations(x_dict_in);
        for (const Dict &x_dict: x_perm) {
            TensorSpec spec(vespalib::make_string("tensor%s(x{},y{})", cell_type_str<T>()));
            nbostream sparse = sparse_base;
            nbostream mixed = mixed_base;
            for (vespalib::string x: x_dict) {
                double value = mix({val(x), val(y)});
                spec.add({{"x", x}, {"y", y}}, value);
                sparse.writeSmallString(x);
                sparse.writeSmallString(y);
                mixed.writeSmallString(x);
                mixed.writeSmallString(y);
                sparse << T(value);
                mixed << T(value);
            }
            set_tensor(test, spec);
            add_binary(test, {sparse, mixed});
        }
        if (x_dict_in.empty()) {
            TensorSpec spec(vespalib::make_string("tensor%s(x{},y{})", cell_type_str<T>()));
            set_tensor(test, spec);
            add_binary(test, {sparse_base, mixed_base});
        }
    }
}

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

template <typename T>
void make_vector_map_test(Cursor &test,
                          const vespalib::string &mapped_name, const Dict &mapped_dict,
                          const vespalib::string &indexed_name, size_t indexed_size)
{
    for (bool with_cell_type: with_cell_type_opts<T>()) {
        auto type_str = vespalib::make_string("tensor%s(%s{},%s[%zu])", cell_type_str<T>(),
                                              mapped_name.c_str(), indexed_name.c_str(), indexed_size);
        ValueType type = ValueType::from_spec(type_str);
        nbostream mixed_base = make_mixed<T>(with_cell_type);
        mixed_base.putInt1_4Bytes(1);
        mixed_base.writeSmallString(mapped_name);
        mixed_base.putInt1_4Bytes(1);
        mixed_base.writeSmallString(indexed_name);
        mixed_base.putInt1_4Bytes(indexed_size);
        mixed_base.putInt1_4Bytes(mapped_dict.size());
        auto mapped_perm = make_permutations(mapped_dict);
        for (const Dict &dict: mapped_perm) {
            TensorSpec spec(type.to_spec()); // ensures type string is normalized
            nbostream mixed = mixed_base;
            for (vespalib::string label: dict) {
                mixed.writeSmallString(label);
                for (size_t idx = 0; idx < indexed_size; ++idx) {
                    double value = mix({val(label), val(idx)});
                    spec.add({{mapped_name, label}, {indexed_name, idx}}, value);
                    mixed << T(value);
                }
            }
            set_tensor(test, spec);
            add_binary(test, mixed);
        }
        if (mapped_dict.empty()) {
            TensorSpec spec(type.to_spec()); // ensures type string is normalized
            set_tensor(test, spec);
            add_binary(test, mixed_base);
        }
    }
}

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

template <typename T> void make_typed_tests(test::TestWriter &writer) {
    make_vector_test<T>(writer.create(), 3);
    make_matrix_test<T>(writer.create(), 2, 3);
    make_map_test<T>(writer.create(), {});
    make_map_test<T>(writer.create(), {"a", "b", "c"});
    make_mesh_test<T>(writer.create(), {}, "a");
    make_mesh_test<T>(writer.create(), {"foo", "bar"}, "a");
    make_vector_map_test<T>(writer.create(), "x", {}, "y", 10);
    make_vector_map_test<T>(writer.create(), "y", {}, "x", 10);
    make_vector_map_test<T>(writer.create(), "x", {"a", "b"}, "y", 3);
    make_vector_map_test<T>(writer.create(), "y", {"a", "b"}, "x", 3);
}

void make_tests(test::TestWriter &writer) {
    make_number_test(writer.create(), 0.0);
    make_number_test(writer.create(), 42.0);
    make_typed_tests<double>(writer);
    make_typed_tests<float>(writer);
    make_typed_tests<BFloat16>(writer);
    make_typed_tests<Int8Float>(writer);
}

int main(int, char **) {
    test::StdOut std_out;
    test::TestWriter writer(std_out);
    make_tests(writer);
    return 0;
}