summaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/attribute/tensorattribute/tensorattribute_test.cpp
blob: 7304593bda92085a7ffce2af539c642547c3a0ee (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include <vespa/log/log.h>
LOG_SETUP("tensorattribute_test");
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/searchlib/tensor/tensor_attribute.h>
#include <vespa/searchlib/tensor/generic_tensor_attribute.h>
#include <vespa/searchlib/attribute/attributeguard.h>
#include <vespa/vespalib/tensor/tensor_factory.h>
#include <vespa/vespalib/tensor/default_tensor.h>

using search::attribute::TensorAttribute;
using search::attribute::GenericTensorAttribute;
using search::AttributeGuard;
using search::AttributeVector;
using vespalib::eval::ValueType;
using vespalib::tensor::Tensor;
using vespalib::tensor::TensorCells;
using vespalib::tensor::TensorDimensions;
using vespalib::tensor::TensorFactory;

namespace vespalib {
namespace tensor {

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

}
}


struct Fixture
{
    using BasicType = search::attribute::BasicType;
    using CollectionType = search::attribute::CollectionType;
    using Config = search::attribute::Config;

    Config _cfg;
    vespalib::string _name;
    std::shared_ptr<TensorAttribute> _tensorAttr;
    std::shared_ptr<AttributeVector> _attr;
    vespalib::tensor::DefaultTensor::builder _builder;

    Fixture(const vespalib::string &typeSpec)
        : _cfg(BasicType::TENSOR, CollectionType::SINGLE),
          _name("test"),
          _tensorAttr(),
          _attr()
    {
        _cfg.setTensorType(ValueType::from_spec(typeSpec));
        _tensorAttr = std::make_shared<GenericTensorAttribute>(_name, _cfg);
        _attr = _tensorAttr;
        _attr->addReservedDoc();
    }

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

    void ensureSpace(uint32_t docId) {
        while (_attr->getNumDocs() <= docId) {
            uint32_t newDocId = 0u;
            _attr->addDoc(newDocId);
            _attr->commit();
        }
    }

    void clearTensor(uint32_t docId) {
        ensureSpace(docId);
        _tensorAttr->clearDoc(docId);
        _attr->commit();
    }

    void setTensor(uint32_t docId, const Tensor &tensor) {
        ensureSpace(docId);
        _tensorAttr->setTensor(docId, tensor);
        _attr->commit();
    }

    search::attribute::Status getStatus() {
        _attr->commit(true);
        return _attr->getStatus();
    }

    void
    assertGetNoTensor(uint32_t docId) {
        AttributeGuard guard(_attr);
        Tensor::UP actTensor = _tensorAttr->getTensor(docId);
        EXPECT_FALSE(actTensor);
    }

    void
    assertGetTensor(const Tensor &expTensor, uint32_t docId)
    {
        AttributeGuard guard(_attr);
        Tensor::UP actTensor = _tensorAttr->getTensor(docId);
        EXPECT_TRUE(static_cast<bool>(actTensor));
        EXPECT_EQUAL(expTensor, *actTensor);
    }

    void
    assertGetTensor(const TensorCells &expCells,
                    const TensorDimensions &expDimensions,
                    uint32_t docId)
    {
        Tensor::UP expTensor = createTensor(expCells, expDimensions);
        assertGetTensor(*expTensor, docId);
    }

    void save() {
        bool saveok = _attr->save();
        EXPECT_TRUE(saveok);
    }

    void load() {
        _tensorAttr = std::make_shared<GenericTensorAttribute>(_name, _cfg);
        _attr = _tensorAttr;
        bool loadok = _attr->load();
        EXPECT_TRUE(loadok);
    }
};


TEST_F("Test empty tensor attribute", Fixture("tensor()"))
{
    EXPECT_EQUAL(1u, f._attr->getNumDocs());
    EXPECT_EQUAL(1u, f._attr->getCommittedDocIdLimit());
}


TEST_F("Test setting tensor value", Fixture("tensor(x{}, y{})"))
{
    f.ensureSpace(4);
    EXPECT_EQUAL(5u, f._attr->getNumDocs());
    EXPECT_EQUAL(5u, f._attr->getCommittedDocIdLimit());
    TEST_DO(f.assertGetNoTensor(4));
    f.setTensor(4, *f.createTensor({}, {}));
    TEST_DO(f.assertGetTensor({}, {"x", "y"}, 4));
    f.setTensor(3, *f.createTensor({ {{}, 3} }, { "x", "y"}));
    TEST_DO(f.assertGetTensor({ {{}, 3} }, { "x", "y"}, 3));
    TEST_DO(f.assertGetNoTensor(2));
    TEST_DO(f.clearTensor(3));
    TEST_DO(f.assertGetNoTensor(3));
}


TEST_F("Test saving / loading tensor attribute", Fixture("tensor(x{}, y{})"))
{
    f.ensureSpace(4);
    f.setTensor(4, *f.createTensor({}, {}));
    f.setTensor(3, *f.createTensor({ {{}, 3} }, { "x", "y"}));
    TEST_DO(f.save());
    TEST_DO(f.load());
    EXPECT_EQUAL(5u, f._attr->getNumDocs());
    EXPECT_EQUAL(5u, f._attr->getCommittedDocIdLimit());
    TEST_DO(f.assertGetTensor({ {{}, 3} }, { "x", "y"}, 3));
    TEST_DO(f.assertGetTensor({}, {"x", "y"}, 4));
    TEST_DO(f.assertGetNoTensor(2));
}


TEST_F("Test compaction of tensor attribute", Fixture("tensor(x{}, y{})"))
{
    f.ensureSpace(4);
    Tensor::UP emptytensor = f.createTensor({}, {});
    Tensor::UP emptyxytensor = f.createTensor({}, {"x", "y"});
    Tensor::UP simpletensor = f.createTensor({ {{}, 3} }, { "x", "y"});
    Tensor::UP filltensor = f.createTensor({ {{}, 5} }, { "x", "y"});
    f.setTensor(4, *emptytensor);
    f.setTensor(3, *simpletensor);
    f.setTensor(2, *filltensor);
    f.clearTensor(2);
    f.setTensor(2, *filltensor);
    search::attribute::Status oldStatus = f.getStatus();
    search::attribute::Status newStatus = oldStatus;
    uint64_t iter = 0;
    uint64_t iterLimit = 100000;
    for (; iter < iterLimit; ++iter) {
        f.clearTensor(2);
        f.setTensor(2, *filltensor);
        newStatus = f.getStatus();
        if (newStatus.getUsed() < oldStatus.getUsed()) {
            break;
        }
        oldStatus = newStatus;
    }
    EXPECT_GREATER(iterLimit, iter);
    LOG(info,
        "iter = %" PRIu64 ", memory usage %" PRIu64 ", -> %" PRIu64,
        iter, oldStatus.getUsed(), newStatus.getUsed());
    TEST_DO(f.assertGetNoTensor(1));
    TEST_DO(f.assertGetTensor(*filltensor, 2));
    TEST_DO(f.assertGetTensor(*simpletensor, 3));
    TEST_DO(f.assertGetTensor(*emptyxytensor, 4));
}

TEST_F("Test tensortype file header tag", Fixture("tensor(x[10])"))
{
    f.ensureSpace(4);
    TEST_DO(f.save());

    vespalib::FileHeader header;
    FastOS_File file;
    EXPECT_TRUE(file.OpenReadOnly("test.dat"));
    (void) header.readFile(file);
    file.Close();
    EXPECT_TRUE(header.hasTag("tensortype"));
    EXPECT_EQUAL("tensor(x[10])", header.getTag("tensortype").asString());
}

TEST_F("Require that tensor attribute can provide empty tensor of correct type", Fixture("tensor(x[10])"))
{
    const TensorAttribute &tensorAttr = *f._tensorAttr;
    Tensor::UP emptyTensor = tensorAttr.getEmptyTensor();
    EXPECT_EQUAL(emptyTensor->getType(), tensorAttr.getConfig().tensorType());
    EXPECT_EQUAL(emptyTensor->getType(), ValueType::from_spec("tensor(x[10])"));
}

TEST_MAIN() { TEST_RUN_ALL(); }