+++ /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 "mir/Deserializer.h"
-#include "mir/ShapeRange.h"
-#include "mir/Tensor.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(Deserializer, ShapeDeserializationTest)
-{
- Deserializer<Shape> deserializer;
-
- proto::TensorShapeProto proto_shape;
- std::string serializedShape;
- Shape shape;
- proto_shape.SerializeToString(&serializedShape);
- shape = deserializer.deserializeFromString(serializedShape);
- checkShape(shape, proto_shape);
-
- proto_shape.add_dims(5);
- proto_shape.SerializeToString(&serializedShape);
- shape = deserializer.deserializeFromString(serializedShape);
- checkShape(shape, proto_shape);
-
- proto_shape.add_dims(2);
- proto_shape.add_dims(4);
- proto_shape.SerializeToString(&serializedShape);
- shape = deserializer.deserializeFromString(serializedShape);
- checkShape(shape, proto_shape);
-
- proto_shape.clear_dims();
- proto_shape.add_dims(1);
- proto_shape.add_dims(1);
- proto_shape.add_dims(1);
- proto_shape.add_dims(1);
- proto_shape.SerializeToString(&serializedShape);
- shape = deserializer.deserializeFromString(serializedShape);
- checkShape(shape, proto_shape);
-}
-
-TEST(Deserializer, IntTensorDeserializationTest)
-{
- Deserializer<TensorVariant> deserializer;
- int tmp = 0;
-
- std::vector<int> values;
- proto::TensorProto proto_tensor;
- proto_tensor.set_dtype(proto::DataType::DT_INT32);
- proto::TensorShapeProto *proto_shapePtr = proto_tensor.mutable_shape();
- std::string serializedTensor;
-
- Shape shape_1{3};
- for (auto &idx : ShapeRange(shape_1))
- {
- (void)idx;
- values.push_back(tmp++);
- }
- proto_tensor.set_tensor_content(
- std::string((char *)values.data(), sizeof(int) * shape_1.numElements()));
- for (int32_t i = 0; i < shape_1.rank(); i++)
- {
- proto_shapePtr->add_dims(shape_1.dim(i));
- }
- proto_tensor.SerializeToString(&serializedTensor);
- TensorVariant tensor_1 = deserializer.deserializeFromString(serializedTensor);
- checkShape(shape_1, proto_tensor.shape());
- checkIntTensor(Tensor<int>(tensor_1), proto_tensor);
-
- Shape shape_2{3, 4, 5};
- values.clear();
- proto_shapePtr->clear_dims();
- for (auto &idx : ShapeRange(shape_2))
- {
- (void)idx;
- values.push_back(tmp--);
- }
- proto_tensor.set_tensor_content(
- std::string((char *)values.data(), sizeof(int) * shape_2.numElements()));
- for (int32_t i = 0; i < shape_2.rank(); i++)
- {
- proto_shapePtr->add_dims(shape_2.dim(i));
- }
- proto_tensor.SerializeToString(&serializedTensor);
- TensorVariant tensor_2 = deserializer.deserializeFromString(serializedTensor);
- checkShape(shape_2, proto_tensor.shape());
- checkIntTensor(Tensor<int>(tensor_2), proto_tensor);
-
- Shape shape_3{1, 1, 1, 1, 1};
- values.clear();
- proto_shapePtr->clear_dims();
- for (auto &idx : ShapeRange(shape_3))
- {
- (void)idx;
- values.push_back(tmp++);
- }
- proto_tensor.set_tensor_content(
- std::string((char *)values.data(), sizeof(int) * shape_3.numElements()));
- for (int32_t i = 0; i < shape_3.rank(); i++)
- {
- proto_shapePtr->add_dims(shape_3.dim(i));
- }
- proto_tensor.SerializeToString(&serializedTensor);
- TensorVariant tensor_3 = deserializer.deserializeFromString(serializedTensor);
- checkShape(shape_3, proto_tensor.shape());
- checkIntTensor(Tensor<int>(tensor_3), proto_tensor);
-}
-
-TEST(Deserializer, FloatTensorDeserializationTest)
-{
- Deserializer<TensorVariant> deserializer;
- float tmp = 1.0f;
-
- std::vector<float> values;
- proto::TensorProto proto_tensor;
- proto_tensor.set_dtype(proto::DataType::DT_FLOAT);
- proto::TensorShapeProto *proto_shapePtr = proto_tensor.mutable_shape();
- std::string serializedTensor;
-
- Shape shape_1{3};
- for (auto &idx : ShapeRange(shape_1))
- {
- (void)idx;
- values.push_back(tmp);
- tmp += 7.3f;
- }
- proto_tensor.set_tensor_content(
- std::string((char *)values.data(), sizeof(float) * shape_1.numElements()));
- for (int32_t i = 0; i < shape_1.rank(); i++)
- {
- proto_shapePtr->add_dims(shape_1.dim(i));
- }
- proto_tensor.SerializeToString(&serializedTensor);
- TensorVariant tensor_1 = deserializer.deserializeFromString(serializedTensor);
- checkShape(shape_1, proto_tensor.shape());
- checkFloatTensor(Tensor<float>(tensor_1), proto_tensor);
-
- Shape shape_2{3, 4, 5};
- values.clear();
- proto_shapePtr->clear_dims();
- for (auto &idx : ShapeRange(shape_2))
- {
- (void)idx;
- values.push_back(tmp);
- tmp *= -1.32f;
- }
- proto_tensor.set_tensor_content(
- std::string((char *)values.data(), sizeof(float) * shape_2.numElements()));
- for (int32_t i = 0; i < shape_2.rank(); i++)
- {
- proto_shapePtr->add_dims(shape_2.dim(i));
- }
- proto_tensor.SerializeToString(&serializedTensor);
- TensorVariant tensor_2 = deserializer.deserializeFromString(serializedTensor);
- checkShape(shape_2, proto_tensor.shape());
- checkFloatTensor(Tensor<float>(tensor_2), proto_tensor);
-
- Shape shape_3{1, 1, 1, 1, 1};
- values.clear();
- proto_shapePtr->clear_dims();
- for (auto &idx : ShapeRange(shape_3))
- {
- (void)idx;
- tmp /= 2;
- values.push_back(tmp);
- }
- proto_tensor.set_tensor_content(
- std::string((char *)values.data(), sizeof(float) * shape_3.numElements()));
- for (int32_t i = 0; i < shape_3.rank(); i++)
- {
- proto_shapePtr->add_dims(shape_3.dim(i));
- }
- proto_tensor.SerializeToString(&serializedTensor);
- TensorVariant tensor_3 = deserializer.deserializeFromString(serializedTensor);
- checkShape(shape_3, proto_tensor.shape());
- checkFloatTensor(Tensor<float>(tensor_3), proto_tensor);
-}
-
-TEST(Deserializer, DoubleTensorDeserializationTest)
-{
- Deserializer<TensorVariant> deserializer;
- double tmp = 1.0f;
-
- std::vector<double> values;
- proto::TensorProto proto_tensor;
- proto_tensor.set_dtype(proto::DataType::DT_DOUBLE);
- proto::TensorShapeProto *proto_shapePtr = proto_tensor.mutable_shape();
- std::string serializedTensor;
-
- Shape shape_1{3};
- for (auto &idx : ShapeRange(shape_1))
- {
- (void)idx;
- values.push_back(tmp);
- tmp += 7.3f;
- }
- proto_tensor.set_tensor_content(
- std::string((char *)values.data(), sizeof(double) * shape_1.numElements()));
- for (int32_t i = 0; i < shape_1.rank(); i++)
- {
- proto_shapePtr->add_dims(shape_1.dim(i));
- }
- proto_tensor.SerializeToString(&serializedTensor);
- TensorVariant tensor_1 = deserializer.deserializeFromString(serializedTensor);
- checkShape(shape_1, proto_tensor.shape());
- checkDoubleTensor(Tensor<double>(tensor_1), proto_tensor);
-
- Shape shape_2{3, 4, 5};
- values.clear();
- proto_shapePtr->clear_dims();
- for (auto &idx : ShapeRange(shape_2))
- {
- (void)idx;
- values.push_back(tmp);
- tmp *= -1.32f;
- }
- proto_tensor.set_tensor_content(
- std::string((char *)values.data(), sizeof(double) * shape_2.numElements()));
- for (int32_t i = 0; i < shape_2.rank(); i++)
- {
- proto_shapePtr->add_dims(shape_2.dim(i));
- }
- proto_tensor.SerializeToString(&serializedTensor);
- TensorVariant tensor_2 = deserializer.deserializeFromString(serializedTensor);
- checkShape(shape_2, proto_tensor.shape());
- checkDoubleTensor(Tensor<double>(tensor_2), proto_tensor);
-
- Shape shape_3{1, 1, 1, 1, 1};
- values.clear();
- proto_shapePtr->clear_dims();
- for (auto &idx : ShapeRange(shape_3))
- {
- (void)idx;
- tmp /= 2;
- values.push_back(tmp);
- }
- proto_tensor.set_tensor_content(
- std::string((char *)values.data(), sizeof(double) * shape_3.numElements()));
- for (int32_t i = 0; i < shape_3.rank(); i++)
- {
- proto_shapePtr->add_dims(shape_3.dim(i));
- }
- proto_tensor.SerializeToString(&serializedTensor);
- TensorVariant tensor_3 = deserializer.deserializeFromString(serializedTensor);
- checkShape(shape_3, proto_tensor.shape());
- checkDoubleTensor(Tensor<double>(tensor_3), proto_tensor);
-}