Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / engines / mkldnn / convert_desc_test.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #define NOMINMAX
6 #include <gtest/gtest.h>
7 #include <gmock/gmock-spec-builders.h>
8 #include "mkldnn_plugin/mkldnn_graph.h"
9
10 #include "../../../thirdparty/mkl-dnn/src/common/memory_desc_wrapper.hpp"
11 #undef UNUSED
12 #include "tests_common.hpp"
13
14 using namespace ::testing;
15 using namespace std;
16 using namespace mkldnn;
17
18 struct mkldnn2TD_test_params {
19     std::vector<size_t> dims;
20
21     mkldnn::memory::format mkldnnFormat;
22 };
23
24 class MKLDNN2TensorDescConvertTests: public TestsCommon,
25                                      public WithParamInterface<mkldnn2TD_test_params> {
26 protected:
27     virtual void TearDown() {
28     }
29
30     virtual void SetUp() {
31         try {
32             TestsCommon::SetUp();
33             mkldnn2TD_test_params p = ::testing::WithParamInterface<mkldnn2TD_test_params>::GetParam();
34
35             mkldnn::memory::dims mkldnnDims;
36             for (auto dim : p.dims) {
37                 mkldnnDims.push_back(dim);
38             }
39             MKLDNNPlugin::MKLDNNMemoryDesc mkldnnMemoryDesc(mkldnnDims, mkldnn::memory::data_type::f32, p.mkldnnFormat);
40             mkldnn::memory::desc mkldnnDesc = mkldnnMemoryDesc;
41             mkldnn::impl::memory_desc_wrapper dst_d(mkldnnDesc.data);
42             InferenceEngine::TensorDesc tDesc = mkldnnMemoryDesc;
43
44             size_t total_size = std::accumulate(std::begin(p.dims), std::end(p.dims), (size_t) 1, std::multiplies<size_t>());
45
46             for (size_t i = 0; i < total_size; i++) {
47                 ASSERT_EQ(tDesc.offset(i), dst_d.off_l(i));
48             }
49         } catch (const InferenceEngine::details::InferenceEngineException &e) {
50             FAIL() << e.what();
51         }
52     }
53 };
54
55 TEST_P(MKLDNN2TensorDescConvertTests, TestsConvertation) {}
56
57
58 INSTANTIATE_TEST_CASE_P(
59         TestsConvertation, MKLDNN2TensorDescConvertTests,
60         ::testing::Values(
61                 mkldnn2TD_test_params{{5}, mkldnn::memory::format::x},
62                 mkldnn2TD_test_params{{10, 3}, mkldnn::memory::format::nc},
63                 mkldnn2TD_test_params{{1, 3, 8, 8}, mkldnn::memory::format::nhwc},
64                 mkldnn2TD_test_params{{1, 3, 8, 8}, mkldnn::memory::format::nchw},
65                 mkldnn2TD_test_params{{1, 32, 8, 8}, mkldnn::memory::format::nChw8c},
66                 mkldnn2TD_test_params{{1, 8, 8, 8}, mkldnn::memory::format::nChw8c},
67                 mkldnn2TD_test_params{{1, 32, 8, 8}, mkldnn::memory::format::nChw16c},
68                 mkldnn2TD_test_params{{67, 34}, mkldnn::memory::format::oi},
69                 mkldnn2TD_test_params{{1, 3, 8, 8}, mkldnn::memory::format::oihw},
70                 mkldnn2TD_test_params{{1, 3, 8, 8}, mkldnn::memory::format::nChw8c},
71                 mkldnn2TD_test_params{{1, 32, 8, 8}, mkldnn::memory::format::nChw16c},
72                 mkldnn2TD_test_params{{1, 16, 8, 8}, mkldnn::memory::format::oIhw8i}
73         ));
74
75 struct TD2mkldnn_test_params {
76     std::vector<size_t> dims;
77     std::vector<size_t> blocked_dims;
78     std::vector<size_t> order;
79 };
80
81 class TensorDesc2MKLDNNConvertTests: public TestsCommon,
82                                      public WithParamInterface<TD2mkldnn_test_params> {
83 protected:
84     virtual void TearDown() {
85     }
86
87     virtual void SetUp() {
88         try {
89             TestsCommon::SetUp();
90             TD2mkldnn_test_params p = ::testing::WithParamInterface<TD2mkldnn_test_params>::GetParam();
91
92             mkldnn::memory::dims mkldnnDims;
93             for (auto dim : p.dims) {
94                 mkldnnDims.push_back(dim);
95             }
96             InferenceEngine::TensorDesc tDesc(InferenceEngine::Precision::FP32, p.dims, {p.blocked_dims, p.order});
97             MKLDNNPlugin::MKLDNNMemoryDesc desc(tDesc);
98
99             mkldnn::impl::memory_desc_wrapper dst_d(((mkldnn::memory::desc&)desc).data);
100
101             size_t total_size = std::accumulate(std::begin(p.dims), std::end(p.dims), (size_t) 1, std::multiplies<size_t>());
102
103             for (size_t i = 0; i < total_size; i++) {
104                 ASSERT_EQ(tDesc.offset(i), dst_d.off_l(i));
105             }
106         } catch (const InferenceEngine::details::InferenceEngineException &e) {
107             FAIL() << e.what();
108         }
109     }
110 };
111
112 TEST_P(TensorDesc2MKLDNNConvertTests, TestsConvertation) {}
113
114
115 INSTANTIATE_TEST_CASE_P(
116         TestsConvertation, TensorDesc2MKLDNNConvertTests,
117         ::testing::Values(
118                 TD2mkldnn_test_params{{5}, {5}, {0}},
119                 TD2mkldnn_test_params{{10, 3}, {10, 3}, {0, 1}},
120                 TD2mkldnn_test_params{{1, 3, 8, 8}, {1, 8, 8, 3}, {0, 2, 3, 1}},
121                 TD2mkldnn_test_params{{1, 3, 8, 8}, {1, 3, 8, 8}, {0, 1, 2, 3}},
122                 TD2mkldnn_test_params{{1, 8, 8, 8}, {1, 1, 8, 8, 8}, {0, 1, 2, 3, 1}},
123                 TD2mkldnn_test_params{{1, 32, 8, 8}, {1, 2, 8, 8, 16}, {0, 1, 2, 3, 1}},
124                 TD2mkldnn_test_params{{1, 3, 8}, {1, 3, 8}, {0, 1, 2}}
125 //                TD2mkldnn_test_params{{1, 3, 8, 8}, mkldnn::memory::format::nchw},
126 //                TD2mkldnn_test_params{{1, 3, 8, 8}, mkldnn::memory::format::nChw8c},
127 //                TD2mkldnn_test_params{{1, 32, 8, 8}, mkldnn::memory::format::nChw16c},
128 //                TD2mkldnn_test_params{{67, 34}, mkldnn::memory::format::oi},
129 //                TD2mkldnn_test_params{{1, 3, 8, 8}, mkldnn::memory::format::oihw},
130 //                TD2mkldnn_test_params{{5, 1, 3, 8, 8}, mkldnn::memory::format::goihw},
131 //                TD2mkldnn_test_params{{1, 16, 8, 8}, mkldnn::memory::format::oIhw8i},
132 //                TD2mkldnn_test_params{{1, 3, 8, 8}, mkldnn::memory::format::OhIw16o4i}
133         ));