aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/vespa/eval/eval/value_cache/constant_tensor_loader.cpp
blob: 74965b79bbc935e46d5b7cf3a79b1ca049d8390b (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "constant_tensor_loader.h"
#include <vespa/eval/eval/tensor_spec.h>
#include <vespa/eval/eval/value_codec.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/io/mapped_file_input.h>
#include <vespa/vespalib/data/lz4_input_decoder.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/util/size_literals.h>
#include <set>

#include <vespa/log/log.h>
LOG_SETUP(".vespalib.eval.value_cache.constant_tensor_loader");

namespace vespalib::eval {

using Inspector = slime::Inspector;
using ObjectTraverser = slime::ObjectTraverser;

namespace {

struct Target {
    const ValueType tensor_type;
    TensorSpec spec;
    void check_add(TensorSpec::Address address, double value) {
        for (const auto &dim : tensor_type.dimensions()) {
            const auto & it = address.find(dim.name);
            if (it == address.end()) {
                LOG(error, "Missing dimension '%s' in address for constant tensor", dim.name.c_str());
                throw std::exception();
            }
            if (it->second.is_mapped() != dim.is_mapped()) {
                LOG(error, "Mismatch mapped/indexed for '%s' in address", dim.name.c_str());
                throw std::exception();
            }
            if (dim.is_indexed()) {
                if (it->second.index >= dim.size) {
                    LOG(error, "Index %zu out of range for dimension %s[%u]",
                        it->second.index, dim.name.c_str(), dim.size);
                    throw std::exception();
                }
            }
        }
        if (address.size() != tensor_type.dimensions().size()) {
            for (const auto & [name, label] : address) {
                if (tensor_type.dimension_index(name) == ValueType::Dimension::npos) {
                    LOG(error, "Extra dimension '%s' in address for constant tensor", name.c_str());
                }
            }
            LOG(error, "Wrong number %zu of dimensions in address for constant tensor, wanted %zu",
                address.size(), tensor_type.dimensions().size());
            throw std::exception();
        }
        spec.add(address, value);
    }
};

struct AddressExtractor : ObjectTraverser {
    const std::set<vespalib::string> &indexed;
    TensorSpec::Address &address;
    AddressExtractor(const std::set<vespalib::string> &indexed_in,
                     TensorSpec::Address &address_out)
        : indexed(indexed_in), address(address_out) {}
    void field(const Memory &symbol, const Inspector &inspector) override {
        vespalib::string dimension = symbol.make_string();
        if (dimension.empty()) {
            LOG(warning, "missing 'dimension' in address");
            throw std::exception();
        }
        if (inspector.type().getId() == vespalib::slime::LONG::ID) {
            size_t index = inspector.asLong();
            if (indexed.contains(dimension)) {
                address.emplace(dimension, TensorSpec::Label(index));
            } else {
                auto label = std::to_string(index);
                address.emplace(dimension, TensorSpec::Label(label));
            }
            return;
        }
        vespalib::string label = inspector.asString().make_string();
        if (label.empty()) {
            auto got = inspector.toString();
            int sz = got.size();
            if (sz > 0) --sz;
            LOG(error, "missing 'label' in address, got '%.*s'", sz, got.c_str());
            throw std::exception();
        }
        if (indexed.find(dimension) == indexed.end()) {
            address.emplace(dimension, TensorSpec::Label(label));
        } else {
            const char *str_beg = label.c_str();
            char *str_end = const_cast<char *>(str_beg);
            size_t index = strtoull(str_beg, &str_end, 10);
            if (str_end == str_beg || *str_end != '\0') {
                LOG(error, "bad index: '%s' cannot be parsed as an unsigned integer", str_beg);
                throw std::exception();
            }
            address.emplace(dimension, TensorSpec::Label(index));
        }
    }
};

struct SingleMappedExtractor : ObjectTraverser {
    const vespalib::string &dimension;
    Target &target;
    SingleMappedExtractor(const vespalib::string &dimension_in, Target &target_in)
        : dimension(dimension_in),
          target(target_in)
    {}
    void field(const Memory &symbol, const Inspector &inspector) override {
        vespalib::string label = symbol.make_string();
        double value = inspector.asDouble();
        TensorSpec::Address address;
        address.emplace(dimension, label);
        target.check_add(address, value);
    }
};


void decodeSingleMappedForm(const Inspector &root, const ValueType &value_type, Target &target) {
    auto extractor = SingleMappedExtractor(value_type.dimensions()[0].name, target);
    root.traverse(extractor);
}

void decodeSingleDenseForm(const Inspector &values, const ValueType &value_type, Target &target) {
    const auto &dimension = value_type.dimensions()[0].name;
    for (size_t i = 0; i < values.entries(); ++i) {
        TensorSpec::Address address;
        address.emplace(dimension, TensorSpec::Label(i));
        target.check_add(address, values[i].asDouble());
    }
}

struct DenseValuesDecoder {
    const std::vector<ValueType::Dimension> _idims;
    Target &_target;
    void decode(const Inspector &input, const TensorSpec::Address &address, size_t dim_idx) {
        if (dim_idx == _idims.size()) {
            _target.check_add(address, input.asDouble());
        } else {
            const auto &dimension = _idims[dim_idx];
            if (input.entries() != dimension.size) {
                return; // TODO: handle mismatch better
            }
            for (size_t i = 0; i < input.entries(); ++i) {
                TensorSpec::Address sub_address = address;
                sub_address.emplace(dimension.name, TensorSpec::Label(i));
                decode(input[i], sub_address, dim_idx + 1);
            }
        }
    }
};

void decodeDenseValues(const Inspector &values, const ValueType &value_type, Target &target) {
    TensorSpec::Address address;
    DenseValuesDecoder decoder{value_type.indexed_dimensions(), target};
    decoder.decode(values, address, 0);
}


template<typename F>
struct TraverserCallback : ObjectTraverser {
    F _f;
    TraverserCallback(F f) : _f(std::move(f)) {}
    void field(const Memory &name, const Inspector &inspector) override {
        _f(name.make_string(), inspector);
    }
};

void decodeSingleMappedBlocks(const Inspector &blocks, const ValueType &value_type, Target &target) {
    if (value_type.count_mapped_dimensions() != 1) {
        return; // TODO handle mismatch
    }
    vespalib::string dim_name = value_type.mapped_dimensions()[0].name;
    DenseValuesDecoder decoder{value_type.indexed_dimensions(), target};
    auto lambda = [&](vespalib::string label, const Inspector &input) {
        TensorSpec::Address address;
        address.emplace(dim_name, std::move(label));
        decoder.decode(input, std::move(address), 0);
    };
    TraverserCallback cb(lambda);
    blocks.traverse(cb);
}

void decodeAddressedBlocks(const Inspector &blocks, const ValueType &value_type, Target &target) {
    const auto & idims = value_type.indexed_dimensions();
    std::set<vespalib::string> indexed;
    for (const auto &dimension: idims) {
        indexed.insert(dimension.name);
    }
    DenseValuesDecoder decoder{value_type.indexed_dimensions(), target};
    for (size_t i = 0; i < blocks.entries(); ++i) {
        TensorSpec::Address address;
        AddressExtractor extractor(indexed, address);
        blocks[i]["address"].traverse(extractor);
        decoder.decode(blocks[i]["values"], address, 0);
    }
}

void decodeLiteralForm(const Inspector &cells, const ValueType &value_type, Target &target) {
    std::set<vespalib::string> indexed;
    for (const auto &dimension: value_type.dimensions()) {
        if (dimension.is_indexed()) {
            indexed.insert(dimension.name);
        }
    }
    for (size_t i = 0; i < cells.entries(); ++i) {
        TensorSpec::Address address;
        AddressExtractor extractor(indexed, address);
        cells[i]["address"].traverse(extractor);
        target.check_add(address, cells[i]["value"].asDouble());
    }
}

void decode_json(const vespalib::string &path, Input &input, Slime &slime) {
    if (slime::JsonFormat::decode(input, slime) == 0) {
        LOG(warning, "file contains invalid json: %s", path.c_str());
    }
}

void decode_json(const vespalib::string &path, Slime &slime) {
    MappedFileInput file(path);
    if (!file.valid()) {
        LOG(warning, "could not read file: %s", path.c_str());
    } else {
        if (ends_with(path, ".lz4")) {
            size_t buffer_size = 64_Ki;
            Lz4InputDecoder lz4_decoder(file, buffer_size);
            decode_json(path, lz4_decoder, slime);
            if (lz4_decoder.failed()) {
                LOG(warning, "file contains lz4 errors (%s): %s",
                    lz4_decoder.reason().c_str(), path.c_str());
            }
        } else {
            decode_json(path, file, slime);
        }
    }
}

} // namespace vespalib::eval::<unnamed>

ConstantTensorLoader::~ConstantTensorLoader() = default;

ConstantValue::UP
ConstantTensorLoader::create(const vespalib::string &path, const vespalib::string &type) const
{
    ValueType value_type = ValueType::from_spec(type);
    if (value_type.is_error()) {
        LOG(warning, "invalid type specification: %s", type.c_str());
        return std::make_unique<BadConstantValue>();
    }
    if (ends_with(path, ".tbf")) {
        vespalib::MappedFileInput file(path);
        vespalib::Memory content = file.get();
        vespalib::nbostream stream(content.data, content.size);
        try {
            return std::make_unique<SimpleConstantValue>(decode_value(stream, _factory));
        } catch (std::exception &) {
            return std::make_unique<BadConstantValue>();
        }
    }
    Slime slime;
    decode_json(path, slime);
    Target target{value_type, TensorSpec(type)};
    bool isSingleDenseType = value_type.is_dense() && (value_type.count_indexed_dimensions() == 1);
    bool isSingleMappedType = value_type.is_sparse() && (value_type.count_mapped_dimensions() == 1);
    const Inspector &root = slime.get();
    if (root.type().getId() == vespalib::slime::OBJECT::ID) {
        const Inspector &cells = root["cells"];
        const Inspector &values = root["values"];
        const Inspector &blocks = root["blocks"];
        if (cells.type().getId() == vespalib::slime::ARRAY::ID) {
            decodeLiteralForm(cells, value_type, target);
        }
        else if (cells.type().getId() == vespalib::slime::OBJECT::ID) {
            if (isSingleMappedType) {
                decodeSingleMappedForm(cells, value_type, target);
            }
        }
        else if (values.type().getId() == vespalib::slime::ARRAY::ID) {
            decodeDenseValues(values, value_type, target);
        }
        else if (blocks.type().getId() == vespalib::slime::OBJECT::ID) {
            decodeSingleMappedBlocks(blocks, value_type, target);
        }
        else if (blocks.type().getId() == vespalib::slime::ARRAY::ID) {
            decodeAddressedBlocks(blocks, value_type, target);
        }
        else if (isSingleMappedType) {
            decodeSingleMappedForm(root, value_type, target);
        }
    }
    else if (root.type().getId() == vespalib::slime::ARRAY::ID && isSingleDenseType) {
        decodeSingleDenseForm(root, value_type, target);
    }
    try {
        return std::make_unique<SimpleConstantValue>(value_from_spec(target.spec, _factory));
    } catch (std::exception &) {
        return std::make_unique<BadConstantValue>();
    }
}

}