aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/tensor/tensor_mapper/tensor_mapper_test.cpp
blob: 190ac0bf0153884597dbd29d084cd56b2c2e3ee9 (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
// 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/tensor/tensor_type.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/tensor/simple/simple_tensor.h>
#include <vespa/vespalib/tensor/simple/simple_tensor_builder.h>
#include <vespa/vespalib/tensor/compact/compact_tensor.h>
#include <vespa/vespalib/tensor/compact/compact_tensor_builder.h>
#include <vespa/vespalib/tensor/compact/compact_tensor_v2.h>
#include <vespa/vespalib/tensor/compact/compact_tensor_v2_builder.h>
#include <vespa/vespalib/tensor/dense/dense_tensor.h>
#include <vespa/vespalib/tensor/dense/dense_tensor_builder.h>
#include <vespa/vespalib/tensor/types.h>
#include <vespa/vespalib/tensor/tensor_factory.h>
#include <vespa/vespalib/tensor/tensor_function.h>
#include <vespa/vespalib/tensor/tensor_mapper.h>
#include <vespa/vespalib/tensor/default_tensor.h>
#include <ostream>

using namespace vespalib::tensor;

namespace vespalib {
namespace tensor {

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

}
}

template <typename BuilderType>
bool defaultBuilder() { return false; }

template <>
bool defaultBuilder<DefaultTensor::builder>() { return true; }

template <typename BuilderType>
struct TensorTFromBuilder;

template <>
struct TensorTFromBuilder<SimpleTensorBuilder> {
    using TensorT = SimpleTensor;
};

template <>
struct TensorTFromBuilder<CompactTensorBuilder> {
    using TensorT = CompactTensor;
};

template <>
struct TensorTFromBuilder<CompactTensorV2Builder> {
    using TensorT = CompactTensorV2;
};

template <typename BuilderType>
using TensorTFromBuilder_t = typename TensorTFromBuilder<BuilderType>::TensorT;

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

template <typename BuilderType>
struct Fixture : public FixtureBase
{
    BuilderType _builder;
    using TensorT = TensorTFromBuilder_t<BuilderType>;
    Fixture() : FixtureBase(), _builder() {}

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

    void assertSparseMapImpl(const Tensor &exp,
                             const TensorType &tensorType,
                             const Tensor &rhs, bool isDefaultBuilder)
    {
        EXPECT_TRUE(tensorType.type() == TensorType::Type::SPARSE);
        if (isDefaultBuilder) {
            TensorMapper mapper(tensorType);
            std::unique_ptr<Tensor> mapped = mapper.map(rhs);
            EXPECT_TRUE(!!mapped);
            EXPECT_EQUAL(exp, *mapped);
        }
        std::unique_ptr<Tensor> mapped =
            TensorMapper::mapToSparse<TensorT>(rhs, tensorType);
        EXPECT_TRUE(!!mapped);
        EXPECT_EQUAL(exp, *mapped);
    }

    void assertDenseMapImpl(const Tensor &exp,
                            const TensorType &tensorType,
                            const Tensor &rhs)
    {
        EXPECT_TRUE(tensorType.type() == TensorType::Type::DENSE);
        TensorMapper mapper(tensorType);
        std::unique_ptr<Tensor> mapped = mapper.map(rhs);
        EXPECT_TRUE(!!mapped);
        EXPECT_EQUAL(exp, *mapped);
    }

    void
    assertSparseMap(const TensorCells &expTensor,
                    const TensorDimensions &expDimensions,
                    const vespalib::string &typeSpec,
                    const TensorCells &rhsTensor,
                    const TensorDimensions &rhsDimensions)
    {
        assertSparseMapImpl(*createTensor(expTensor, expDimensions),
                            TensorType::fromSpec(typeSpec),
                            *createTensor(rhsTensor, rhsDimensions),
                            defaultBuilder<BuilderType>());
    }

    void
    assertDenseMap(const DenseTensorCells &expTensor,
                   const vespalib::string &typeSpec,
                   const TensorCells &rhsTensor,
                   const TensorDimensions &rhsDimensions)
    {
        assertDenseMapImpl(*createDenseTensor(expTensor),
                           TensorType::fromSpec(typeSpec),
                           *createTensor(rhsTensor, rhsDimensions));
    }
};

using SimpleFixture = Fixture<SimpleTensorBuilder>;
using CompactFixture = Fixture<CompactTensorBuilder>;
using CompactV2Fixture = Fixture<CompactTensorV2Builder>;

template <typename FixtureType>
void
testTensorMapper(FixtureType &f)
{
    TEST_DO(f.assertSparseMap({
                                   {{{"y","1"}}, 4},
                                   {{{"y","2"}}, 12}
                               },
                              { "y" },
                              "tensor(y{})",
                              {
                                  {{{"x","1"},{"y","1"}}, 1},
                                  {{{"x","2"},{"y","1"}}, 3},
                                  {{{"x","1"},{"y","2"}}, 5},
                                  {{{"x","2"},{"y","2"}}, 7}
                              },
                              { "x", "y" }));
    TEST_DO(f.assertSparseMap({
                                   {{{"x","1"}}, 6},
                                   {{{"x","2"}}, 10}
                               },
                              { "x" },
                              "tensor(x{})",
                              {
                                  {{{"x","1"},{"y","1"}}, 1},
                                  {{{"x","2"},{"y","1"}}, 3},
                                  {{{"x","1"},{"y","2"}}, 5},
                                  {{{"x","2"},{"y","2"}}, 7}
                              },
                              { "x", "y" }));
    TEST_DO(f.assertDenseMap({
                                  {{{"y",0}}, 4},
                                  {{{"y",1}}, 12},
                                  {{{"y",2}}, 0}
                              },
                             "tensor(y[3])",
                              {
                                  {{{"x","1"},{"y","0"}}, 1},
                                  {{{"x","2"},{"y","0"}}, 3},
                                  {{{"x","1"},{"y","1"}}, 5},
                                  {{{"x","2"},{"y","1"}}, 7}
                              },
                              { "x", "y" }));
    TEST_DO(f.assertDenseMap({
                                  {{{"y",0}}, 3},
                                  {{{"y",1}}, 5},
                                  {{{"y",2}}, 0}
                              },
                             "tensor(y[3])",
                              {
                                  {{{"x","1"},{"y","0x"}}, 1},
                                  {{{"x","2"},{"y",""}}, 3},
                                  {{{"x","1"},{"y","1"}}, 5},
                                  {{{"x","2"},{"y","10"}}, 7}
                              },
                              { "x", "y" }));
    TEST_DO(f.assertDenseMap({
                                  {{{"x",0},{"y",0}}, 1},
                                  {{{"x",0},{"y",1}}, 5},
                                  {{{"x",0},{"y",2}}, 0},
                                  {{{"x",1},{"y",0}}, 3},
                                  {{{"x",1},{"y",1}}, 0},
                                  {{{"x",1},{"y",2}}, 0}
                              },
                             "tensor(x[2], y[3])",
                              {
                                  {{{"x","0"},{"y","0"}}, 1},
                                  {{{"x","1"},{"y","0"}}, 3},
                                  {{{"x","0"},{"y","1"}}, 5},
                                  {{{"x","10"},{"y","1"}}, 7}
                              },
                              { "x", "y" }));
}

TEST_F("test tensor mapper for SimpleTensor", SimpleFixture)
{
    testTensorMapper(f);
}

TEST_F("test tensor mapper for CompactTensor", CompactFixture)
{
    testTensorMapper(f);
}

TEST_F("test tensor mapper for CompactTensorV2", CompactV2Fixture)
{
    testTensorMapper(f);
}

TEST_MAIN() { TEST_RUN_ALL(); }