1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
5 #include <ie_layouts.h>
7 #include <gtest/gtest.h>
11 #include "mock_allocator.hpp"
13 #include <cpp/ie_cnn_net_reader.h>
14 #include <gmock/gmock-spec-builders.h>
19 #define UNUSED __attribute__((unused))
22 using namespace ::testing;
24 using namespace InferenceEngine;
26 class TensorDescTests: public ::testing::Test {
28 virtual void TearDown() {
31 virtual void SetUp() {
38 TEST_F(TensorDescTests, CreateBlobWithIncorrectLayout) {
39 ASSERT_THROW(make_shared_blob<float>(Precision::FP32, Layout::NC, {1, 3, 32}), details::InferenceEngineException);
42 TEST_F(TensorDescTests, CreateEmptyBlob) {
43 Blob::Ptr blob = make_shared_blob<float>(Precision::FP32);
45 ASSERT_EQ(blob->getTensorDesc().getLayout(), Layout::NCHW);
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());
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());
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};
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);
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};
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);