Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / inference_engine_tests / tensor_desc_test.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <ie_layouts.h>
6 #include <ie_blob.h>
7 #include <gtest/gtest.h>
8 #include <random>
9 #include <chrono>
10
11 #include "mock_allocator.hpp"
12
13 #include <cpp/ie_cnn_net_reader.h>
14 #include <gmock/gmock-spec-builders.h>
15
16 #ifdef WIN32
17 #define UNUSED
18 #else
19 #define UNUSED  __attribute__((unused))
20 #endif
21
22 using namespace ::testing;
23 using namespace std;
24 using namespace InferenceEngine;
25
26 class TensorDescTests: public ::testing::Test {
27 protected:
28     virtual void TearDown() {
29     }
30
31     virtual void SetUp() {
32     }
33
34 public:
35
36 };
37
38 TEST_F(TensorDescTests, CreateBlobWithIncorrectLayout) {
39     ASSERT_THROW(make_shared_blob<float>(Precision::FP32, Layout::NC, {1, 3, 32}), details::InferenceEngineException);
40 }
41
42 TEST_F(TensorDescTests, CreateEmptyBlob) {
43     Blob::Ptr blob = make_shared_blob<float>(Precision::FP32);
44
45     ASSERT_EQ(blob->getTensorDesc().getLayout(), Layout::NCHW);
46 }
47
48 TEST_F(TensorDescTests, CreateBlockedBlobNCHW) {
49     TensorDesc desc(Precision::FP32, {1, 4, 2, 1}, {{1, 2, 2, 1, 2}, {0, 1, 2, 3, 1}});
50     float data[8] = {1, 2, 3, 4, 5, 6, 7, 8};
51     Blob::Ptr blockedBlob = make_shared_blob<float>(desc, data);
52     Blob::Ptr nchwBlob = make_shared_blob<float>({Precision::FP32, {1, 4, 2, 1}, Layout::NCHW}, data);
53     ASSERT_NE(blockedBlob->getTensorDesc().offset(5), nchwBlob->getTensorDesc().offset(5));
54     ASSERT_EQ(6, blockedBlob->getTensorDesc().offset(5));
55     ASSERT_EQ(5, nchwBlob->getTensorDesc().offset(5));
56     ASSERT_EQ(Layout::NCHW, nchwBlob->layout());
57     ASSERT_EQ(Layout::BLOCKED, blockedBlob->layout());
58 }
59
60 TEST_F(TensorDescTests, CreateBlockedBlobNCDHW) {
61     TensorDesc desc(Precision::FP32, {1, 4, 2, 2, 1}, {{1, 2, 2, 2, 1, 2}, {0, 1, 2, 3, 4, 1}});
62     float data[8] = {1, 2, 3, 4, 5, 6, 7, 8};
63     Blob::Ptr blockedBlob = make_shared_blob<float>(desc, data);
64     Blob::Ptr ncdhwBlob = make_shared_blob<float>({Precision::FP32, {1, 4, 2, 2, 1}, Layout::NCDHW}, data);
65     ASSERT_NE(blockedBlob->getTensorDesc().offset(6), ncdhwBlob->getTensorDesc().offset(6));
66     ASSERT_EQ(5, blockedBlob->getTensorDesc().offset(6));
67     ASSERT_EQ(6, ncdhwBlob->getTensorDesc().offset(6));
68     ASSERT_EQ(Layout::NCDHW, ncdhwBlob->layout());
69     ASSERT_EQ(Layout::BLOCKED, blockedBlob->layout());
70 }
71
72 TEST_F(TensorDescTests, CompareNHWCandNCHWLayouts) {
73     TensorDesc descNCHW(Precision::FP32, {1, 3, 4, 2}, Layout::NCHW);
74     TensorDesc descNHWC(Precision::FP32, {1, 3, 4, 2}, Layout::NHWC);
75     SizeVector nchw = {0, 1, 2, 3};
76     SizeVector nhwc = {0, 2, 3, 1};
77
78     ASSERT_NE(descNCHW, descNHWC);
79     ASSERT_NE(descNCHW.getBlockingDesc(), descNHWC.getBlockingDesc());
80     ASSERT_NE(descNCHW.getBlockingDesc().getOrder(), descNHWC.getBlockingDesc().getOrder());
81     ASSERT_EQ(descNCHW.getBlockingDesc().getOrder(), nchw);
82     ASSERT_EQ(descNHWC.getBlockingDesc().getOrder(), nhwc);
83 }
84
85 TEST_F(TensorDescTests, CompareNDHWCandNCDHWLayouts) {
86     TensorDesc descNCDHW(Precision::FP32, {1, 3, 4, 4, 2}, Layout::NCDHW);
87     TensorDesc descNDHWC(Precision::FP32, {1, 3, 4, 4, 2}, Layout::NDHWC);
88     SizeVector ncdhw = {0, 1, 2, 3, 4};
89     SizeVector ndhwc = {0, 2, 3, 4, 1};
90
91     ASSERT_NE(descNCDHW, descNDHWC);
92     ASSERT_NE(descNCDHW.getBlockingDesc(), descNDHWC.getBlockingDesc());
93     ASSERT_NE(descNCDHW.getBlockingDesc().getOrder(), descNDHWC.getBlockingDesc().getOrder());
94     ASSERT_EQ(descNCDHW.getBlockingDesc().getOrder(), ncdhw);
95     ASSERT_EQ(descNDHWC.getBlockingDesc().getOrder(), ndhwc);
96 }