forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensor_desc_test.cpp
112 lines (94 loc) · 4.59 KB
/
tensor_desc_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include <random>
#include <chrono>
#include <ie_layouts.h>
#include <ie_blob.h>
using namespace ::testing;
using namespace std;
using namespace InferenceEngine;
using TensorDescTests = ::testing::Test;
TEST_F(TensorDescTests, CreateBlobWithIncorrectLayout) {
ASSERT_THROW(make_shared_blob<float>({ Precision::FP32, {1, 3, 32}, Layout::NC }), Exception);
}
TEST_F(TensorDescTests, CreateBlockedBlobNCHW) {
TensorDesc desc(Precision::FP32, {1, 4, 2, 1}, {{1, 2, 2, 1, 2}, {0, 1, 2, 3, 1}});
float data[8] = {1, 2, 3, 4, 5, 6, 7, 8};
Blob::Ptr blockedBlob = make_shared_blob<float>(desc, data);
Blob::Ptr nchwBlob = make_shared_blob<float>({Precision::FP32, {1, 4, 2, 1}, Layout::NCHW}, data);
ASSERT_NE(blockedBlob->getTensorDesc().offset(5), nchwBlob->getTensorDesc().offset(5));
ASSERT_EQ(6, blockedBlob->getTensorDesc().offset(5));
ASSERT_EQ(5, nchwBlob->getTensorDesc().offset(5));
ASSERT_EQ(Layout::NCHW, nchwBlob->getTensorDesc().getLayout());
ASSERT_EQ(Layout::BLOCKED, blockedBlob->getTensorDesc().getLayout());
}
TEST_F(TensorDescTests, CreateBlockedBlobNCDHW) {
TensorDesc desc(Precision::FP32, {1, 4, 2, 2, 1}, {{1, 2, 2, 2, 1, 2}, {0, 1, 2, 3, 4, 1}});
float data[8] = {1, 2, 3, 4, 5, 6, 7, 8};
Blob::Ptr blockedBlob = make_shared_blob<float>(desc, data);
Blob::Ptr ncdhwBlob = make_shared_blob<float>({Precision::FP32, {1, 4, 2, 2, 1}, Layout::NCDHW}, data);
ASSERT_NE(blockedBlob->getTensorDesc().offset(6), ncdhwBlob->getTensorDesc().offset(6));
ASSERT_EQ(5, blockedBlob->getTensorDesc().offset(6));
ASSERT_EQ(6, ncdhwBlob->getTensorDesc().offset(6));
ASSERT_EQ(Layout::NCDHW, ncdhwBlob->getTensorDesc().getLayout());
ASSERT_EQ(Layout::BLOCKED, blockedBlob->getTensorDesc().getLayout());
}
TEST_F(TensorDescTests, CompareHWCandCHWLayouts) {
TensorDesc descCHW(Precision::FP32, {1, 3, 4}, Layout::CHW);
TensorDesc descHWC(Precision::FP32, {1, 3, 4}, Layout::HWC);
SizeVector chw = {0, 1, 2};
SizeVector hwc = {1, 2, 0};
ASSERT_NE(descCHW, descHWC);
ASSERT_NE(descCHW.getBlockingDesc(), descHWC.getBlockingDesc());
ASSERT_NE(descCHW.getBlockingDesc().getOrder(), descHWC.getBlockingDesc().getOrder());
ASSERT_EQ(descCHW.getBlockingDesc().getOrder(), chw);
ASSERT_EQ(descHWC.getBlockingDesc().getOrder(), hwc);
}
TEST_F(TensorDescTests, CompareNHWCandNCHWLayouts) {
TensorDesc descNCHW(Precision::FP32, {1, 3, 4, 2}, Layout::NCHW);
TensorDesc descNHWC(Precision::FP32, {1, 3, 4, 2}, Layout::NHWC);
SizeVector nchw = {0, 1, 2, 3};
SizeVector nhwc = {0, 2, 3, 1};
ASSERT_NE(descNCHW, descNHWC);
ASSERT_NE(descNCHW.getBlockingDesc(), descNHWC.getBlockingDesc());
ASSERT_NE(descNCHW.getBlockingDesc().getOrder(), descNHWC.getBlockingDesc().getOrder());
ASSERT_EQ(descNCHW.getBlockingDesc().getOrder(), nchw);
ASSERT_EQ(descNHWC.getBlockingDesc().getOrder(), nhwc);
}
TEST_F(TensorDescTests, CompareNDHWCandNCDHWLayouts) {
TensorDesc descNCDHW(Precision::FP32, {1, 3, 4, 4, 2}, Layout::NCDHW);
TensorDesc descNDHWC(Precision::FP32, {1, 3, 4, 4, 2}, Layout::NDHWC);
SizeVector ncdhw = {0, 1, 2, 3, 4};
SizeVector ndhwc = {0, 2, 3, 4, 1};
ASSERT_NE(descNCDHW, descNDHWC);
ASSERT_NE(descNCDHW.getBlockingDesc(), descNDHWC.getBlockingDesc());
ASSERT_NE(descNCDHW.getBlockingDesc().getOrder(), descNDHWC.getBlockingDesc().getOrder());
ASSERT_EQ(descNCDHW.getBlockingDesc().getOrder(), ncdhw);
ASSERT_EQ(descNDHWC.getBlockingDesc().getOrder(), ndhwc);
}
TEST_F(TensorDescTests, SetLayout) {
TensorDesc descNCHW(Precision::FP32, {1, 2, 3, 4}, Layout::NCHW);
TensorDesc decsNHWC = descNCHW;
decsNHWC.setLayout(Layout::NHWC);
TensorDesc refNHWC(descNCHW.getPrecision(), descNCHW.getDims(), Layout::NHWC);
ASSERT_EQ(decsNHWC, refNHWC);
}
TEST_F(TensorDescTests, setDimsForBLOCKED) {
TensorDesc desc(Precision::FP32, {1, 2, 3, 4, 5, 6}, Layout::BLOCKED);
SizeVector newDims {7, 7, 7, 7, 7, 7};
desc.setDims(newDims);
EXPECT_EQ(desc.getDims(), newDims);
EXPECT_EQ(desc.getBlockingDesc().getBlockDims(), newDims);
}
TEST_F(TensorDescTests, setDimsForNHWC) {
TensorDesc desc(Precision::FP32, {1, 2, 3, 4}, Layout::NHWC);
auto refOrder = desc.getBlockingDesc().getOrder();
SizeVector newDims {7, 7, 7, 7};
desc.setDims(newDims);
EXPECT_EQ(desc.getDims(), newDims);
EXPECT_EQ(desc.getLayout(), Layout::NHWC);
EXPECT_EQ(desc.getBlockingDesc().getBlockDims(), newDims);
EXPECT_EQ(desc.getBlockingDesc().getOrder(), refOrder);
}