+++ /dev/null
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "mir/Deserializer.h"
-#include "mir.pb.h"
-
-#include "mir/ShapeRange.h"
-
-namespace mir
-{
-
-//
-// Shape Deserialization
-//
-
-static Shape deserializeFromMessage(const proto::TensorShapeProto &object_as_message)
-{
- int rank = object_as_message.dims_size();
- Shape res(rank);
- for (int i = 0; i < rank; ++i)
- res.dim(i) = object_as_message.dims(i);
-
- return res;
-}
-
-template <> Shape Deserializer<Shape>::deserializeFromStream(std::istream &stream)
-{
- proto::TensorShapeProto object_as_message;
-
- object_as_message.ParseFromIstream(&stream);
-
- return deserializeFromMessage(object_as_message);
-}
-
-template <> Shape Deserializer<Shape>::deserializeFromString(const std::string &bytes)
-{
- proto::TensorShapeProto object_as_message;
-
- object_as_message.ParseFromString(bytes);
-
- return deserializeFromMessage(object_as_message);
-}
-
-//
-// Tensor Deserialization
-//
-
-static TensorVariant deserializeFromMessage(const proto::TensorProto &object_as_message)
-{
- DTYPE dtype;
- switch (object_as_message.dtype())
- {
- case proto::DataType::DT_INT32:
- dtype = DTYPE::INT32;
- break;
- case proto::DataType::DT_FLOAT:
- dtype = DTYPE::FLOAT32;
- break;
- case proto::DataType::DT_DOUBLE:
- dtype = DTYPE::FLOAT64;
- break;
- default:
- throw std::logic_error("Deserializer<TensorVariant>: received unsupported data type");
- }
-
- const std::string &tensor_content = object_as_message.tensor_content();
- Shape shape = deserializeFromMessage(object_as_message.shape());
- return TensorVariant(dtype, shape, tensor_content.data());
-}
-
-template <> TensorVariant Deserializer<TensorVariant>::deserializeFromStream(std::istream &stream)
-{
- proto::TensorProto object_as_message;
-
- object_as_message.ParseFromIstream(&stream);
-
- return deserializeFromMessage(object_as_message);
-}
-
-template <>
-TensorVariant Deserializer<TensorVariant>::deserializeFromString(const std::string &bytes)
-{
- proto::TensorProto object_as_message;
-
- object_as_message.ParseFromString(bytes);
-
- return deserializeFromMessage(object_as_message);
-}
-
-} // namespace mir
+++ /dev/null
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "mir/Serializer.h"
-#include "mir.pb.h"
-
-#include "mir/ShapeRange.h"
-
-namespace mir
-{
-
-template <class T> void Serializer<T>::serializeToStream(const T &obj, std::ostream &stream)
-{
- std::string serializedObject = getSerializedObject(obj);
- stream.write(serializedObject.c_str(), serializedObject.size());
-}
-
-template <> std::string Serializer<Shape>::getSerializedObject(const Shape &shape)
-{
- proto::TensorShapeProto shapeProto;
-
- int32_t rank = shape.rank();
- for (int32_t i = 0; i < rank; i++)
- {
- shapeProto.add_dims(shape.dim(i));
- }
-
- return shapeProto.SerializeAsString();
-}
-
-static void setShapeToTensorProto(proto::TensorProto &tensor_proto, const Shape &shape)
-{
- Serializer<Shape> shape_serializer;
- tensor_proto.mutable_shape()->ParseFromString(shape_serializer.getSerializedObject(shape));
-}
-
-template <typename T, proto::DataType dtype>
-static proto::TensorProto serializeTensorContent(const Tensor<T> &tensor)
-{
- proto::TensorProto tp;
- setShapeToTensorProto(tp, tensor.getShape());
-
- tp.set_dtype(dtype);
-
- size_t data_size = tensor.getShape().numElements();
- auto tensor_data = new T[data_size];
- size_t i = 0;
- ShapeRange shapeRange(tensor.getShape());
- for (auto &idx : shapeRange)
- {
- tensor_data[i] = tensor.at(idx);
- i++;
- }
-
- size_t raw_data_size = data_size * sizeof(T);
- std::string raw_data(reinterpret_cast<char *>(tensor_data), raw_data_size);
- delete[] tensor_data;
- tp.set_tensor_content(raw_data);
-
- return tp;
-}
-
-template <> std::string Serializer<Tensor<int>>::getSerializedObject(const Tensor<int> &tensor)
-{
- return serializeTensorContent<int, proto::DT_INT32>(tensor).SerializeAsString();
-}
-
-template <> std::string Serializer<Tensor<float>>::getSerializedObject(const Tensor<float> &tensor)
-{
- return serializeTensorContent<float, proto::DT_FLOAT>(tensor).SerializeAsString();
-}
-
-template <>
-std::string Serializer<Tensor<double>>::getSerializedObject(const Tensor<double> &tensor)
-{
- return serializeTensorContent<double, proto::DT_DOUBLE>(tensor).SerializeAsString();
-}
-
-} // namespace mir
+++ /dev/null
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <gtest/gtest.h>
-#include <cmath>
-
-#include "mir/Serializer.h"
-#include "mir/ShapeRange.h"
-
-using namespace mir;
-
-const double EPS = 0.0000001;
-
-static void checkShape(const Shape &shape, const proto::TensorShapeProto &proto_shape)
-{
- ASSERT_EQ(shape.rank(), proto_shape.dims_size());
- for (int i = 0; i < shape.rank(); i++)
- {
- ASSERT_EQ(shape.dim(i), proto_shape.dims(i));
- }
-}
-
-template <typename T>
-static void checkTensorContent(const Tensor<T> &tensor, const proto::TensorProto &proto_tensor)
-{
- ShapeRange range(tensor.getShape());
- auto data = (T *)proto_tensor.tensor_content().c_str();
- int i = 0;
- for (auto &idx : range)
- {
- ASSERT_EQ(tensor.at(idx), data[i++]);
- }
-}
-
-static void checkIntTensor(const Tensor<int> &tensor, const proto::TensorProto &proto_tensor)
-{
- ASSERT_EQ(proto_tensor.dtype(), proto::DataType::DT_INT32);
- checkTensorContent<int>(tensor, proto_tensor);
-}
-
-static void checkFloatTensor(const Tensor<float> &tensor, const proto::TensorProto &proto_tensor)
-{
- ASSERT_EQ(proto_tensor.dtype(), proto::DataType::DT_FLOAT);
- checkTensorContent<float>(tensor, proto_tensor);
-}
-
-static void checkDoubleTensor(const Tensor<double> &tensor, const proto::TensorProto &proto_tensor)
-{
- ASSERT_EQ(proto_tensor.dtype(), proto::DataType::DT_DOUBLE);
- checkTensorContent<double>(tensor, proto_tensor);
-}
-
-TEST(Serializer, ShapeSerializationTest)
-{
- Serializer<Shape> serializer;
-
- Shape shape_0{};
- std::string serializedShape_0 = serializer.getSerializedObject(shape_0);
- proto::TensorShapeProto proto_shape_0;
- proto_shape_0.ParseFromString(serializedShape_0);
- checkShape(shape_0, proto_shape_0);
-
- Shape shape_1{1};
- std::string serializedShape_1 = serializer.getSerializedObject(shape_1);
- proto::TensorShapeProto proto_shape_1;
- proto_shape_1.ParseFromString(serializedShape_1);
- checkShape(shape_1, proto_shape_1);
-
- Shape shape_2{5};
- std::string serializedShape_2 = serializer.getSerializedObject(shape_2);
- proto::TensorShapeProto proto_shape_2;
- proto_shape_2.ParseFromString(serializedShape_2);
- checkShape(shape_2, proto_shape_2);
-
- Shape shape_3{2, 4};
- std::string serializedShape_3 = serializer.getSerializedObject(shape_3);
- proto::TensorShapeProto proto_shape_3;
- proto_shape_3.ParseFromString(serializedShape_3);
- checkShape(shape_3, proto_shape_3);
-
- Shape shape_4{1, 1, 1, 1};
- std::string serializedShape_4 = serializer.getSerializedObject(shape_4);
- proto::TensorShapeProto proto_shape_4;
- proto_shape_4.ParseFromString(serializedShape_4);
- checkShape(shape_4, proto_shape_4);
-
- Shape shape_5{1, 2, 3, 4, 5};
- std::string serializedShape_5 = serializer.getSerializedObject(shape_5);
- proto::TensorShapeProto proto_shape_5;
- proto_shape_5.ParseFromString(serializedShape_5);
- checkShape(shape_5, proto_shape_5);
-}
-
-TEST(Serializer, IntTensorSerializationTest)
-{
- Serializer<Tensor<int>> serializer;
- int tmp = 0;
-
- Shape shape_1{3};
- TensorVariant tv_1(DTYPE::INT32, shape_1);
- Tensor<int> tensor_1(tv_1);
- for (auto &idx : ShapeRange(shape_1))
- {
- tensor_1.at(idx) = tmp++;
- }
- std::string serializedTensor_1 = serializer.getSerializedObject(tensor_1);
- proto::TensorProto proto_tensor_1;
- proto_tensor_1.ParseFromString(serializedTensor_1);
- checkShape(shape_1, proto_tensor_1.shape());
- checkIntTensor(tensor_1, proto_tensor_1);
-
- Shape shape_2{3, 4, 5};
- TensorVariant tv_2(DTYPE::INT32, shape_2);
- Tensor<int> tensor_2(tv_2);
- for (auto &idx : ShapeRange(tensor_2.getShape()))
- {
- tensor_2.at(idx) = tmp--;
- }
- std::string serializedTensor_2 = serializer.getSerializedObject(tensor_2);
- proto::TensorProto proto_tensor_2;
- proto_tensor_2.ParseFromString(serializedTensor_2);
- checkShape(shape_2, proto_tensor_2.shape());
- checkIntTensor(tensor_2, proto_tensor_2);
-
- Shape shape_3{1, 1, 1, 1, 1};
- TensorVariant tv_3(DTYPE::INT32, shape_3);
- Tensor<int> tensor_3(tv_3);
- for (auto &idx : ShapeRange(tensor_3.getShape()))
- {
- tensor_3.at(idx) = tmp++;
- }
- std::string serializedTensor_3 = serializer.getSerializedObject(tensor_3);
- proto::TensorProto proto_tensor_3;
- proto_tensor_3.ParseFromString(serializedTensor_3);
- checkShape(shape_3, proto_tensor_3.shape());
- checkIntTensor(tensor_3, proto_tensor_3);
-}
-
-TEST(Serializer, FloatTensorSerializationTest)
-{
- Serializer<Tensor<float>> serializer;
- float tmp = 1.0f;
-
- Shape shape_1{3};
- TensorVariant tv_1(DTYPE::FLOAT32, shape_1);
- Tensor<float> tensor_1(tv_1);
- for (auto &idx : ShapeRange(tensor_1.getShape()))
- {
- tensor_1.at(idx) = tmp;
- tmp += 10.3f;
- }
- std::string serializedTensor_1 = serializer.getSerializedObject(tensor_1);
- proto::TensorProto proto_tensor_1;
- proto_tensor_1.ParseFromString(serializedTensor_1);
- checkShape(shape_1, proto_tensor_1.shape());
- checkFloatTensor(tensor_1, proto_tensor_1);
-
- Shape shape_2{3, 4, 5};
- TensorVariant tv_2(DTYPE::FLOAT32, shape_2);
- Tensor<float> tensor_2(tv_2);
- for (auto &idx : ShapeRange(tensor_2.getShape()))
- {
- tensor_2.at(idx) = tmp;
- tmp *= -1.21f;
- }
- std::string serializedTensor_2 = serializer.getSerializedObject(tensor_2);
- proto::TensorProto proto_tensor_2;
- proto_tensor_2.ParseFromString(serializedTensor_2);
- checkShape(shape_2, proto_tensor_2.shape());
- checkFloatTensor(tensor_2, proto_tensor_2);
-
- Shape shape_3{1, 1, 1, 1, 1};
- TensorVariant tv_3(DTYPE::FLOAT32, shape_3);
- Tensor<float> tensor_3(tv_3);
- for (auto &idx : ShapeRange(tensor_3.getShape()))
- {
- tmp -= 6.66f;
- tensor_3.at(idx) = tmp;
- }
- std::string serializedTensor_3 = serializer.getSerializedObject(tensor_3);
- proto::TensorProto proto_tensor_3;
- proto_tensor_3.ParseFromString(serializedTensor_3);
- checkShape(shape_3, proto_tensor_3.shape());
- checkFloatTensor(tensor_3, proto_tensor_3);
-}
-
-TEST(Serializer, DoubleTensorSerializationTest)
-{
- Serializer<Tensor<double>> serializer;
- double tmp = 1.0f;
-
- Shape shape_1{3};
- TensorVariant tv_1(DTYPE::FLOAT64, shape_1);
- Tensor<double> tensor_1(tv_1);
- for (auto &idx : ShapeRange(tensor_1.getShape()))
- {
- tensor_1.at(idx) = tmp;
- tmp += 10.3f;
- }
- std::string serializedTensor_1 = serializer.getSerializedObject(tensor_1);
- proto::TensorProto proto_tensor_1;
- proto_tensor_1.ParseFromString(serializedTensor_1);
- checkShape(shape_1, proto_tensor_1.shape());
- checkDoubleTensor(tensor_1, proto_tensor_1);
-
- Shape shape_2{3, 4, 5};
- TensorVariant tv_2(DTYPE::FLOAT64, shape_2);
- Tensor<double> tensor_2(tv_2);
- for (auto &idx : ShapeRange(tensor_2.getShape()))
- {
- tensor_2.at(idx) = tmp;
- tmp *= -1.21f;
- }
- std::string serializedTensor_2 = serializer.getSerializedObject(tensor_2);
- proto::TensorProto proto_tensor_2;
- proto_tensor_2.ParseFromString(serializedTensor_2);
- checkShape(shape_2, proto_tensor_2.shape());
- checkDoubleTensor(tensor_2, proto_tensor_2);
-
- Shape shape_3{1, 1, 1, 1, 1};
- TensorVariant tv_3(DTYPE::FLOAT64, shape_3);
- Tensor<double> tensor_3(tv_3);
- for (auto &idx : ShapeRange(tensor_3.getShape()))
- {
- tmp -= 6.66f;
- tensor_3.at(idx) = tmp;
- }
- std::string serializedTensor_3 = serializer.getSerializedObject(tensor_3);
- proto::TensorProto proto_tensor_3;
- proto_tensor_3.ParseFromString(serializedTensor_3);
- checkShape(shape_3, proto_tensor_3.shape());
- checkDoubleTensor(tensor_3, proto_tensor_3);
-}