summaryrefslogtreecommitdiffstats
path: root/eval/src/vespa/eval/tensor/serialization/sparse_binary_format.cpp
blob: 50d4a91965ee88caabba396affd9343de391ae37 (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
// 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 "sparse_binary_format.h"
#include <vespa/eval/tensor/types.h>
#include <vespa/eval/tensor/tensor.h>
#include <vespa/eval/tensor/tensor_builder.h>
#include <vespa/eval/tensor/tensor_visitor.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <sstream>


using vespalib::nbostream;

namespace vespalib {
namespace tensor {


namespace {

vespalib::string undefinedLabel("");

void
writeTensorAddress(nbostream &output,
                   const eval::ValueType &type,
                   const TensorAddress &value)
{
    auto elemItr = value.elements().cbegin();
    auto elemItrEnd = value.elements().cend();
    for (const auto &dimension : type.dimensions()) {
        if (elemItr != elemItrEnd && dimension.name == elemItr->dimension()) {
            output.writeSmallString(elemItr->label());
            ++elemItr;
        } else {
            output.writeSmallString(undefinedLabel);
        }
    }
    assert(elemItr == elemItrEnd);
}

}

class SparseBinaryFormatSerializer : public TensorVisitor
{
    uint32_t _numCells;
    nbostream _cells;
    eval::ValueType _type;

public:
    SparseBinaryFormatSerializer();
    virtual ~SparseBinaryFormatSerializer() override;
    virtual void visit(const TensorAddress &address, double value) override;
    void serialize(nbostream &stream, const Tensor &tensor);
};

SparseBinaryFormatSerializer::SparseBinaryFormatSerializer()
    : _numCells(0u),
      _cells(),
      _type(eval::ValueType::error_type())
{
}


SparseBinaryFormatSerializer::~SparseBinaryFormatSerializer()
{
}

void
SparseBinaryFormatSerializer::visit(const TensorAddress &address,
                                     double value)
{
    ++_numCells;
    writeTensorAddress(_cells, _type, address);
    _cells << value;
}


void
SparseBinaryFormatSerializer::serialize(nbostream &stream,
                                         const Tensor &tensor)
{
    _type = tensor.getType();
    tensor.accept(*this);
    stream.putInt1_4Bytes(_type.dimensions().size());
    for (const auto &dimension : _type.dimensions()) {
        stream.writeSmallString(dimension.name);
    }
    stream.putInt1_4Bytes(_numCells);
    stream.write(_cells.peek(), _cells.size());
}


void
SparseBinaryFormat::serialize(nbostream &stream, const Tensor &tensor)
{
    SparseBinaryFormatSerializer serializer;
    serializer.serialize(stream, tensor);
}


void
SparseBinaryFormat::deserialize(nbostream &stream, TensorBuilder &builder)
{
    vespalib::string str;
    size_t dimensionsSize = stream.getInt1_4Bytes();
    std::vector<TensorBuilder::Dimension> dimensions;
    while (dimensions.size() < dimensionsSize) {
        stream.readSmallString(str);
        dimensions.emplace_back(builder.define_dimension(str));
    }
    size_t cellsSize = stream.getInt1_4Bytes();
    double cellValue = 0.0;
    for (size_t cellIdx = 0; cellIdx < cellsSize; ++cellIdx) {
        for (size_t dimension = 0; dimension < dimensionsSize; ++dimension) {
            stream.readSmallString(str);
            if (!str.empty()) {
                builder.add_label(dimensions[dimension], str);
            }
        }
        stream >> cellValue;
        builder.add_cell(cellValue);
    }
}


} // namespace vespalib::tensor
} // namespace vespalib