diff options
author | Arne Juul <arnej@verizonmedia.com> | 2020-09-23 13:20:40 +0000 |
---|---|---|
committer | Arne Juul <arnej@verizonmedia.com> | 2020-09-23 13:23:32 +0000 |
commit | b81b7f5e10eaefb611616fa516b4a0851cd1e645 (patch) | |
tree | ea2ed761f7403aad4a87e3825a41ff925f82c910 /eval | |
parent | dff267e0d6323c3ad1ae10d3193e4c18c823a1b8 (diff) |
revert moving code into codec.h
Diffstat (limited to 'eval')
-rw-r--r-- | eval/src/vespa/eval/eval/CMakeLists.txt | 1 | ||||
-rw-r--r-- | eval/src/vespa/eval/eval/codec.cpp | 46 | ||||
-rw-r--r-- | eval/src/vespa/eval/eval/codec.h | 111 | ||||
-rw-r--r-- | eval/src/vespa/eval/eval/simple_tensor.cpp | 136 | ||||
-rw-r--r-- | eval/src/vespa/eval/eval/simple_value.cpp | 1 | ||||
-rw-r--r-- | eval/src/vespa/eval/eval/value_codec.cpp | 139 |
6 files changed, 269 insertions, 165 deletions
diff --git a/eval/src/vespa/eval/eval/CMakeLists.txt b/eval/src/vespa/eval/eval/CMakeLists.txt index 4660a2ac9bf..84cebe8cfd0 100644 --- a/eval/src/vespa/eval/eval/CMakeLists.txt +++ b/eval/src/vespa/eval/eval/CMakeLists.txt @@ -4,7 +4,6 @@ vespa_add_library(eval_eval OBJECT aggr.cpp basic_nodes.cpp call_nodes.cpp - codec.cpp compile_tensor_function.cpp delete_node.cpp fast_forest.cpp diff --git a/eval/src/vespa/eval/eval/codec.cpp b/eval/src/vespa/eval/eval/codec.cpp deleted file mode 100644 index 1cf60f476a1..00000000000 --- a/eval/src/vespa/eval/eval/codec.cpp +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#include "codec.h" - -namespace vespalib::eval::codec { - -void encode_type(nbostream &output, const Format &format, const ValueType &type, const TypeMeta &meta) { - maybe_encode_cell_type(output, format, meta); - if (format.is_sparse) { - output.putInt1_4Bytes(meta.mapped.size()); - for (size_t idx: meta.mapped) { - output.writeSmallString(type.dimensions()[idx].name); - } - } - if (format.is_dense) { - output.putInt1_4Bytes(meta.indexed.size()); - for (size_t idx: meta.indexed) { - output.writeSmallString(type.dimensions()[idx].name); - output.putInt1_4Bytes(type.dimensions()[idx].size); - } - } -} - -ValueType decode_type(nbostream &input, const Format &format) { - CellType cell_type = maybe_decode_cell_type(input, format); - std::vector<ValueType::Dimension> dim_list; - if (format.is_sparse) { - size_t cnt = input.getInt1_4Bytes(); - for (size_t i = 0; i < cnt; ++i) { - vespalib::string name; - input.readSmallString(name); - dim_list.emplace_back(name); - } - } - if (format.is_dense) { - size_t cnt = input.getInt1_4Bytes(); - for (size_t i = 0; i < cnt; ++i) { - vespalib::string name; - input.readSmallString(name); - dim_list.emplace_back(name, input.getInt1_4Bytes()); - } - } - return ValueType::tensor_type(std::move(dim_list), cell_type); -} - -} // namespace vespalib::eval::codec diff --git a/eval/src/vespa/eval/eval/codec.h b/eval/src/vespa/eval/eval/codec.h deleted file mode 100644 index fad370f6d8b..00000000000 --- a/eval/src/vespa/eval/eval/codec.h +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#pragma once - -#include "simple_tensor.h" -#include <vespa/vespalib/objects/nbostream.h> -#include <cassert> - -namespace vespalib::eval::codec { - -using CellType = ValueType::CellType; -using IndexList = std::vector<size_t>; - -constexpr uint32_t DOUBLE_CELL_TYPE = 0; -constexpr uint32_t FLOAT_CELL_TYPE = 1; - -inline uint32_t cell_type_to_id(CellType cell_type) { - switch (cell_type) { - case CellType::DOUBLE: return DOUBLE_CELL_TYPE; - case CellType::FLOAT: return FLOAT_CELL_TYPE; - } - abort(); -} - -inline CellType id_to_cell_type(uint32_t id) { - switch (id) { - case DOUBLE_CELL_TYPE: return CellType::DOUBLE; - case FLOAT_CELL_TYPE: return CellType::FLOAT; - } - abort(); -} - -/** - * Meta information about how a type can be decomposed into mapped and - * indexed dimensions and also how large each block is. A block is a - * dense-subspace consisting of all indexed dimensions that is - * uniquely specified by the labels of all mapped dimensions. - **/ -struct TypeMeta { - IndexList mapped; - IndexList indexed; - size_t block_size; - CellType cell_type; - explicit TypeMeta(const ValueType &type) - : mapped(), - indexed(), - block_size(1), - cell_type(type.cell_type()) - { - for (size_t i = 0; i < type.dimensions().size(); ++i) { - const auto &dimension = type.dimensions()[i]; - if (dimension.is_mapped()) { - mapped.push_back(i); - } else { - block_size *= dimension.size; - indexed.push_back(i); - } - } - } - ~TypeMeta() {} -}; - -struct Format { - bool is_sparse; - bool is_dense; - bool with_cell_type; - uint32_t tag; - explicit Format(const TypeMeta &meta) - : is_sparse(meta.mapped.size() > 0), - is_dense((meta.indexed.size() > 0) || !is_sparse), - with_cell_type(meta.cell_type != CellType::DOUBLE), - tag((is_sparse ? 0x1 : 0) | (is_dense ? 0x2 : 0) | (with_cell_type ? 0x4 : 0)) {} - explicit Format(uint32_t tag_in) - : is_sparse((tag_in & 0x1) != 0), - is_dense((tag_in & 0x2) != 0), - with_cell_type((tag_in & 0x4) != 0), - tag(tag_in) {} - ~Format() {} -}; - -inline void maybe_encode_cell_type(nbostream &output, const Format &format, const TypeMeta &meta) { - if (format.with_cell_type) { - output.putInt1_4Bytes(cell_type_to_id(meta.cell_type)); - } -} - -void encode_type(nbostream &output, const Format &format, const ValueType &type, const TypeMeta &meta); - -inline void maybe_encode_num_blocks(nbostream &output, const TypeMeta &meta, size_t num_blocks) { - if ((meta.mapped.size() > 0)) { - output.putInt1_4Bytes(num_blocks); - } -} - -inline CellType maybe_decode_cell_type(nbostream &input, const Format &format) { - if (format.with_cell_type) { - return id_to_cell_type(input.getInt1_4Bytes()); - } - return CellType::DOUBLE; -} - -ValueType decode_type(nbostream &input, const Format &format); - -inline size_t maybe_decode_num_blocks(nbostream &input, const TypeMeta &meta, const Format &format) { - if ((meta.mapped.size() > 0) || !format.is_dense) { - return input.getInt1_4Bytes(); - } - return 1; -} - -} // namespace vespalib::eval::codec diff --git a/eval/src/vespa/eval/eval/simple_tensor.cpp b/eval/src/vespa/eval/eval/simple_tensor.cpp index 498119f0733..64b2b6f8865 100644 --- a/eval/src/vespa/eval/eval/simple_tensor.cpp +++ b/eval/src/vespa/eval/eval/simple_tensor.cpp @@ -3,26 +3,44 @@ #include "simple_tensor.h" #include "simple_tensor_engine.h" #include "operation.h" -#include "codec.h" #include <vespa/vespalib/util/overload.h> #include <vespa/vespalib/util/visit_ranges.h> #include <vespa/vespalib/objects/nbostream.h> #include <algorithm> #include <cassert> -using namespace vespalib::eval::codec; - namespace vespalib { namespace eval { using Address = SimpleTensor::Address; using Cell = SimpleTensor::Cell; using Cells = SimpleTensor::Cells; +using IndexList = std::vector<size_t>; using Label = SimpleTensor::Label; using CellRef = std::reference_wrapper<const Cell>; +using CellType = ValueType::CellType; namespace { +constexpr uint32_t DOUBLE_CELL_TYPE = 0; +constexpr uint32_t FLOAT_CELL_TYPE = 1; + +uint32_t cell_type_to_id(CellType cell_type) { + switch (cell_type) { + case CellType::DOUBLE: return DOUBLE_CELL_TYPE; + case CellType::FLOAT: return FLOAT_CELL_TYPE; + } + abort(); +} + +CellType id_to_cell_type(uint32_t id) { + switch (id) { + case DOUBLE_CELL_TYPE: return CellType::DOUBLE; + case FLOAT_CELL_TYPE: return CellType::FLOAT; + } + abort(); +} + void assert_type(const ValueType &type) { (void) type; assert(type.is_double() || type.is_tensor()); @@ -87,6 +105,35 @@ const vespalib::string &reverse_rename(const vespalib::string &name, return name; } +/** + * Meta information about how a type can be decomposed into mapped and + * indexed dimensions and also how large each block is. A block is a + * dense-subspace consisting of all indexed dimensions that is + * uniquely specified by the labels of all mapped dimensions. + **/ +struct TypeMeta { + IndexList mapped; + IndexList indexed; + size_t block_size; + CellType cell_type; + explicit TypeMeta(const ValueType &type) + : mapped(), + indexed(), + block_size(1), + cell_type(type.cell_type()) + { + for (size_t i = 0; i < type.dimensions().size(); ++i) { + const auto &dimension = type.dimensions()[i]; + if (dimension.is_mapped()) { + mapped.push_back(i); + } else { + block_size *= dimension.size; + indexed.push_back(i); + } + } + } + ~TypeMeta() {} +}; /** * Helper class used when building SimpleTensors. While a tensor @@ -391,12 +438,95 @@ public: } }; +struct Format { + bool is_sparse; + bool is_dense; + bool with_cell_type; + uint32_t tag; + explicit Format(const TypeMeta &meta) + : is_sparse(meta.mapped.size() > 0), + is_dense((meta.indexed.size() > 0) || !is_sparse), + with_cell_type(meta.cell_type != CellType::DOUBLE), + tag((is_sparse ? 0x1 : 0) | (is_dense ? 0x2 : 0) | (with_cell_type ? 0x4 : 0)) {} + explicit Format(uint32_t tag_in) + : is_sparse((tag_in & 0x1) != 0), + is_dense((tag_in & 0x2) != 0), + with_cell_type((tag_in & 0x4) != 0), + tag(tag_in) {} + ~Format() {} +}; + +void maybe_encode_cell_type(nbostream &output, const Format &format, const TypeMeta &meta) { + if (format.with_cell_type) { + output.putInt1_4Bytes(cell_type_to_id(meta.cell_type)); + } +} + +void encode_type(nbostream &output, const Format &format, const ValueType &type, const TypeMeta &meta) { + maybe_encode_cell_type(output, format, meta); + if (format.is_sparse) { + output.putInt1_4Bytes(meta.mapped.size()); + for (size_t idx: meta.mapped) { + output.writeSmallString(type.dimensions()[idx].name); + } + } + if (format.is_dense) { + output.putInt1_4Bytes(meta.indexed.size()); + for (size_t idx: meta.indexed) { + output.writeSmallString(type.dimensions()[idx].name); + output.putInt1_4Bytes(type.dimensions()[idx].size); + } + } +} + +void maybe_encode_num_blocks(nbostream &output, const TypeMeta &meta, size_t num_blocks) { + if ((meta.mapped.size() > 0)) { + output.putInt1_4Bytes(num_blocks); + } +} + void encode_mapped_labels(nbostream &output, const TypeMeta &meta, const Address &addr) { for (size_t idx: meta.mapped) { output.writeSmallString(addr[idx].name); } } +CellType maybe_decode_cell_type(nbostream &input, const Format &format) { + if (format.with_cell_type) { + return id_to_cell_type(input.getInt1_4Bytes()); + } + return CellType::DOUBLE; +} + +ValueType decode_type(nbostream &input, const Format &format) { + CellType cell_type = maybe_decode_cell_type(input, format); + std::vector<ValueType::Dimension> dim_list; + if (format.is_sparse) { + size_t cnt = input.getInt1_4Bytes(); + for (size_t i = 0; i < cnt; ++i) { + vespalib::string name; + input.readSmallString(name); + dim_list.emplace_back(name); + } + } + if (format.is_dense) { + size_t cnt = input.getInt1_4Bytes(); + for (size_t i = 0; i < cnt; ++i) { + vespalib::string name; + input.readSmallString(name); + dim_list.emplace_back(name, input.getInt1_4Bytes()); + } + } + return ValueType::tensor_type(std::move(dim_list), cell_type); +} + +size_t maybe_decode_num_blocks(nbostream &input, const TypeMeta &meta, const Format &format) { + if ((meta.mapped.size() > 0) || !format.is_dense) { + return input.getInt1_4Bytes(); + } + return 1; +} + void decode_mapped_labels(nbostream &input, const TypeMeta &meta, Address &addr) { for (size_t idx: meta.mapped) { vespalib::string name; diff --git a/eval/src/vespa/eval/eval/simple_value.cpp b/eval/src/vespa/eval/eval/simple_value.cpp index 2c958f2150a..343e61fb8fc 100644 --- a/eval/src/vespa/eval/eval/simple_value.cpp +++ b/eval/src/vespa/eval/eval/simple_value.cpp @@ -3,7 +3,6 @@ #include "simple_value.h" #include "tensor_spec.h" #include "inline_operation.h" -#include "codec.h" #include <vespa/vespalib/util/typify.h> #include <vespa/vespalib/util/visit_ranges.h> #include <vespa/vespalib/util/overload.h> diff --git a/eval/src/vespa/eval/eval/value_codec.cpp b/eval/src/vespa/eval/eval/value_codec.cpp index 90a3016d0ab..a9121bb4aa9 100644 --- a/eval/src/vespa/eval/eval/value_codec.cpp +++ b/eval/src/vespa/eval/eval/value_codec.cpp @@ -1,15 +1,148 @@ // Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "value_codec.h" -#include "codec.h" +#include <vespa/vespalib/objects/nbostream.h> #include <vespa/vespalib/util/typify.h> -using namespace vespalib::eval::codec; - namespace vespalib::eval { namespace { +using CellType = ValueType::CellType; +using IndexList = std::vector<size_t>; + +constexpr uint32_t DOUBLE_CELL_TYPE = 0; +constexpr uint32_t FLOAT_CELL_TYPE = 1; + +inline uint32_t cell_type_to_id(CellType cell_type) { + switch (cell_type) { + case CellType::DOUBLE: return DOUBLE_CELL_TYPE; + case CellType::FLOAT: return FLOAT_CELL_TYPE; + } + abort(); +} + +inline CellType id_to_cell_type(uint32_t id) { + switch (id) { + case DOUBLE_CELL_TYPE: return CellType::DOUBLE; + case FLOAT_CELL_TYPE: return CellType::FLOAT; + } + abort(); +} + +/** + * Meta information about how a type can be decomposed into mapped and + * indexed dimensions and also how large each block is. A block is a + * dense-subspace consisting of all indexed dimensions that is + * uniquely specified by the labels of all mapped dimensions. + **/ +struct TypeMeta { + IndexList mapped; + IndexList indexed; + size_t block_size; + CellType cell_type; + explicit TypeMeta(const ValueType &type) + : mapped(), + indexed(), + block_size(1), + cell_type(type.cell_type()) + { + for (size_t i = 0; i < type.dimensions().size(); ++i) { + const auto &dimension = type.dimensions()[i]; + if (dimension.is_mapped()) { + mapped.push_back(i); + } else { + block_size *= dimension.size; + indexed.push_back(i); + } + } + } + ~TypeMeta() {} +}; + +struct Format { + bool is_sparse; + bool is_dense; + bool with_cell_type; + uint32_t tag; + explicit Format(const TypeMeta &meta) + : is_sparse(meta.mapped.size() > 0), + is_dense((meta.indexed.size() > 0) || !is_sparse), + with_cell_type(meta.cell_type != CellType::DOUBLE), + tag((is_sparse ? 0x1 : 0) | (is_dense ? 0x2 : 0) | (with_cell_type ? 0x4 : 0)) {} + explicit Format(uint32_t tag_in) + : is_sparse((tag_in & 0x1) != 0), + is_dense((tag_in & 0x2) != 0), + with_cell_type((tag_in & 0x4) != 0), + tag(tag_in) {} + ~Format() {} +}; + +void maybe_encode_cell_type(nbostream &output, const Format &format, const TypeMeta &meta) { + if (format.with_cell_type) { + output.putInt1_4Bytes(cell_type_to_id(meta.cell_type)); + } +} + +void encode_type(nbostream &output, const Format &format, const ValueType &type, const TypeMeta &meta) { + maybe_encode_cell_type(output, format, meta); + if (format.is_sparse) { + output.putInt1_4Bytes(meta.mapped.size()); + for (size_t idx: meta.mapped) { + output.writeSmallString(type.dimensions()[idx].name); + } + } + if (format.is_dense) { + output.putInt1_4Bytes(meta.indexed.size()); + for (size_t idx: meta.indexed) { + output.writeSmallString(type.dimensions()[idx].name); + output.putInt1_4Bytes(type.dimensions()[idx].size); + } + } +} + +void maybe_encode_num_blocks(nbostream &output, const TypeMeta &meta, size_t num_blocks) { + if ((meta.mapped.size() > 0)) { + output.putInt1_4Bytes(num_blocks); + } +} + +CellType maybe_decode_cell_type(nbostream &input, const Format &format) { + if (format.with_cell_type) { + return id_to_cell_type(input.getInt1_4Bytes()); + } + return CellType::DOUBLE; +} + +ValueType decode_type(nbostream &input, const Format &format) { + CellType cell_type = maybe_decode_cell_type(input, format); + std::vector<ValueType::Dimension> dim_list; + if (format.is_sparse) { + size_t cnt = input.getInt1_4Bytes(); + for (size_t i = 0; i < cnt; ++i) { + vespalib::string name; + input.readSmallString(name); + dim_list.emplace_back(name); + } + } + if (format.is_dense) { + size_t cnt = input.getInt1_4Bytes(); + for (size_t i = 0; i < cnt; ++i) { + vespalib::string name; + input.readSmallString(name); + dim_list.emplace_back(name, input.getInt1_4Bytes()); + } + } + return ValueType::tensor_type(std::move(dim_list), cell_type); +} + +size_t maybe_decode_num_blocks(nbostream &input, const TypeMeta &meta, const Format &format) { + if ((meta.mapped.size() > 0) || !format.is_dense) { + return input.getInt1_4Bytes(); + } + return 1; +} + void encode_mapped_labels(nbostream &output, size_t num_mapped_dims, const std::vector<vespalib::stringref> &addr) { for (size_t i = 0; i < num_mapped_dims; ++i) { output.writeSmallString(addr[i]); |