1 // Copyright (C) 2018 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
6 #include <gtest/gtest.h>
7 #include <gmock/gmock-spec-builders.h>
8 #include "mkldnn_plugin/mkldnn_graph.h"
9 #include "mock_mkldnn_primitive.hpp"
11 #include "test_graph.hpp"
13 #include "single_layer_common.hpp"
14 #include <mkldnn_plugin/mkldnn_extension_utils.h>
15 #include <inference_engine/cnn_network_impl.hpp>
16 #include "tests_common.hpp"
19 using namespace ::testing;
21 using namespace mkldnn;
24 struct permute_test_params {
25 InferenceEngine::SizeVector dims;
26 InferenceEngine::SizeVector order;
29 MKLDNNPlugin::impl_desc_type selectedType;
31 std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
34 template <typename data_t>
35 void ref_permute(const InferenceEngine::TBlob<data_t> &src, InferenceEngine::TBlob<data_t> &dst, permute_test_params prm) {
36 const data_t *src_data = src.readOnly();
37 data_t *dst_data = dst.data();
39 InferenceEngine::SizeVector orderedDims;
40 for (auto ord : prm.order) {
41 orderedDims.push_back(src.getTensorDesc().getDims()[ord]);
43 InferenceEngine::TensorDesc desc(InferenceEngine::Precision::FP32, src.getTensorDesc().getDims(), {orderedDims, prm.order});
45 #pragma omp parallel for
46 for (int i=0; i < src.size(); i++) {
47 dst_data[desc.offset(i)] = src_data[src.getTensorDesc().offset(i)];
51 class MKLDNNGraphPermuteTests: public TestsCommon,
52 public WithParamInterface<permute_test_params> {
53 std::string model_t = R"V0G0N(
54 <Net Name="Power_Only" version="2" precision="FP32" batch="1">
56 <layer name="in1" type="Input" precision="FP32" id="0">
63 <layer name="permute" id="1" type="Permute" precision="FP32">
64 <data order="_ORDER_"/>
78 <edge from-layer="0" from-port="0" to-layer="1" to-port="1"/>
84 std::string getModel(permute_test_params p) {
85 std::string model = model_t;
88 for (auto& dim : p.dims) {
90 dims += std::to_string(dim) + "</dim>\n";
94 for (auto& ord : p.order) {
97 order += std::to_string(ord);
99 dst_dims += std::to_string(p.dims[ord]) + "</dim>\n";
102 REPLACE_WITH_STR(model, "__DIMS__", dims);
103 REPLACE_WITH_STR(model, "__DST_DIMS__", dst_dims);
104 REPLACE_WITH_STR(model, "_ORDER_", order);
109 virtual void TearDown() {
112 virtual void SetUp() {
114 TestsCommon::SetUp();
115 permute_test_params p = ::testing::WithParamInterface<permute_test_params>::GetParam();
116 std::string model = getModel(p);
118 InferenceEngine::CNNNetReader net_reader;
119 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
121 MKLDNNGraphTestClass graph;
122 graph.CreateGraph(net_reader.getNetwork());
123 auto& nodes = graph.getNodes();
124 for (int i = 0; i < nodes.size(); i++) {
125 if (nodes[i]->getType() == MKLDNNPlugin::Permute) {
126 ASSERT_EQ(p.num_prim_desc, nodes[i]->getSupportedPrimitiveDescriptors().size());
127 for (size_t j = 0; j < p.num_prim_desc && j < p.comp.size(); j++) {
128 p.comp.at(j)(nodes[i]->getSupportedPrimitiveDescriptors().at(j));
130 ASSERT_NE(nullptr, nodes[i]->getSelectedPrimitiveDescriptor());
131 ASSERT_EQ(p.selectedType, nodes[i]->getSelectedPrimitiveDescriptor()->getImplementationType());
135 InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float>({InferenceEngine::Precision::FP32, p.dims, InferenceEngine::TensorDesc::getLayoutByDims(p.dims)});
137 fill_data(src->buffer(), src->size());
139 auto * srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
141 if (srcPtr == nullptr)
142 FAIL() << "Cannot cast blob to TBlob<float>.";
144 InferenceEngine::BlobMap srcs;
145 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
147 InferenceEngine::OutputsDataMap out;
148 out = net_reader.getNetwork().getOutputsInfo();
149 InferenceEngine::BlobMap outputBlobs;
151 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
153 InferenceEngine::TBlob<float>::Ptr output;
154 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
156 outputBlobs[item.first] = output;
158 graph.Infer(srcs, outputBlobs);
160 InferenceEngine::TensorDesc td(InferenceEngine::Precision::FP32, p.dims, InferenceEngine::TensorDesc::getLayoutByDims(p.dims));
161 InferenceEngine::TBlob<float> dst_ref(td);
164 ref_permute(*srcPtr, dst_ref, p);
166 compare(*output, dst_ref);
167 } catch (const InferenceEngine::details::InferenceEngineException &e) {
173 TEST_P(MKLDNNGraphPermuteTests, TestsPermute) {}
175 INSTANTIATE_TEST_CASE_P(
176 TestsPermute, MKLDNNGraphPermuteTests,
178 permute_test_params{{2, 3, 4, 5}, {0, 1, 2, 3}, 1, MKLDNNPlugin::impl_desc_type::unknown},
179 permute_test_params{{2, 3, 4, 5}, {0, 2, 3, 1}, 1, MKLDNNPlugin::impl_desc_type::unknown},
180 permute_test_params{{2, 3, 4, 5}, {3, 0, 1, 2}, 1, MKLDNNPlugin::impl_desc_type::unknown},
181 permute_test_params{{2, 3, 4, 5}, {1, 3, 2, 0}, 1, MKLDNNPlugin::impl_desc_type::unknown},
182 permute_test_params{{2, 3, 4, 5}, {3, 2, 1, 0}, 1, MKLDNNPlugin::impl_desc_type::unknown},
183 permute_test_params{{2, 3, 4, 5}, {0, 2, 1, 3}, 1, MKLDNNPlugin::impl_desc_type::unknown},
184 permute_test_params{{2, 3, 4}, {0, 1, 2}, 1, MKLDNNPlugin::impl_desc_type::unknown},
185 permute_test_params{{2, 3, 4}, {0, 2, 1}, 1, MKLDNNPlugin::impl_desc_type::unknown},
186 permute_test_params{{2, 3, 4}, {2, 1, 0}, 1, MKLDNNPlugin::impl_desc_type::unknown},
187 permute_test_params{{2, 3, 4}, {1, 2, 0}, 1, MKLDNNPlugin::impl_desc_type::unknown},
188 permute_test_params{{2, 3, 4}, {2, 0, 1}, 1, MKLDNNPlugin::impl_desc_type::unknown},
189 permute_test_params{{2, 3, 4}, {1, 0, 2}, 1, MKLDNNPlugin::impl_desc_type::unknown},
190 permute_test_params{{2, 3}, {1, 0}, 1, MKLDNNPlugin::impl_desc_type::unknown},
191 permute_test_params{{2, 3}, {0, 1}, 1, MKLDNNPlugin::impl_desc_type::unknown},
192 permute_test_params{{2, 3, 4, 5, 6}, {0, 1, 2, 3, 4}, 1, MKLDNNPlugin::impl_desc_type::unknown},
193 permute_test_params{{2, 3, 4, 5, 6}, {0, 4, 2, 1, 3}, 1, MKLDNNPlugin::impl_desc_type::unknown},
194 permute_test_params{{2, 3, 4, 5, 6}, {0, 2, 4, 3, 1}, 1, MKLDNNPlugin::impl_desc_type::unknown},
195 permute_test_params{{2, 3, 4, 5, 6}, {0, 3, 2, 4, 1}, 1, MKLDNNPlugin::impl_desc_type::unknown},
196 permute_test_params{{2, 8, 2, 2, 4, 5}, {0, 1, 4, 2, 5, 3}, 1, MKLDNNPlugin::impl_desc_type::unknown},
197 permute_test_params{{2, 8, 3, 3, 4, 5}, {0, 1, 4, 2, 5, 3}, 1, MKLDNNPlugin::impl_desc_type::unknown},
198 permute_test_params{{2, 8, 3, 4}, {3, 0, 1, 2}, 2, MKLDNNPlugin::impl_desc_type::unknown}
201 class MKLDNNGraphDynBatchPermuteTests: public MKLDNNGraphPermuteTests {
203 virtual void SetUp() {
205 TestsCommon::SetUp();
206 permute_test_params p = ::testing::WithParamInterface<permute_test_params>::GetParam();
207 std::string model = getModel(p);
208 size_t MB = p.dims[0];
213 InferenceEngine::CNNNetReader net_reader;
214 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
215 InferenceEngine::CNNNetwork network = net_reader.getNetwork();
216 auto implNet = dynamic_cast<InferenceEngine::details::CNNNetworkImpl *>(&((InferenceEngine::ICNNNetwork&)network));
217 ASSERT_NE(nullptr, implNet) << "Failed to cast ICNNNetwork to CNNNetworkImpl";
218 InferenceEngine::ResponseDesc resp;
219 InferenceEngine::StatusCode sts = implNet->setBatchSizeReshape(MB, &resp);
220 ASSERT_EQ((int)InferenceEngine::StatusCode::OK, sts) << resp.msg;
222 MKLDNNGraphTestClass graph;
223 graph.setProperty({{InferenceEngine::PluginConfigParams::KEY_DYN_BATCH_ENABLED, InferenceEngine::PluginConfigParams::YES}});
224 graph.CreateGraph(net_reader.getNetwork());
226 InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float>({InferenceEngine::Precision::FP32, p.dims, InferenceEngine::TensorDesc::getLayoutByDims(p.dims)});
228 fill_data(src->buffer(), src->size());
230 auto * srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
232 if (srcPtr == nullptr)
233 FAIL() << "Cannot cast blob to TBlob<float>.";
235 InferenceEngine::BlobMap srcs;
236 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
238 InferenceEngine::OutputsDataMap out;
239 out = net_reader.getNetwork().getOutputsInfo();
240 InferenceEngine::BlobMap outputBlobs;
242 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
244 InferenceEngine::TBlob<float>::Ptr output;
245 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
247 outputBlobs[item.first] = output;
249 auto checkPermute = [](const MKLDNNPlugin::MKLDNNNodePtr& node) {
250 return node->getType() == MKLDNNPlugin::Permute;
252 graph.checkDynBatch(srcs, outputBlobs, MB, MB, checkPermute);
253 graph.checkDynBatch(srcs, outputBlobs, 1, MB, checkPermute);
254 } catch (const InferenceEngine::details::InferenceEngineException &e) {
260 TEST_P(MKLDNNGraphDynBatchPermuteTests, TestsDynBatchPermute) {}
262 INSTANTIATE_TEST_CASE_P(
263 TestsDynBatchPermute, MKLDNNGraphDynBatchPermuteTests,
265 permute_test_params{{2, 3, 4, 5}, {0, 1, 2, 3}, 1, MKLDNNPlugin::impl_desc_type::unknown},
266 permute_test_params{{2, 3, 4, 5}, {0, 2, 3, 1}, 1, MKLDNNPlugin::impl_desc_type::unknown},
267 permute_test_params{{2, 3, 4, 5}, {0, 2, 1, 3}, 1, MKLDNNPlugin::impl_desc_type::unknown},
268 permute_test_params{{2, 3, 4}, {0, 1, 2}, 1, MKLDNNPlugin::impl_desc_type::unknown},
269 permute_test_params{{2, 3, 4}, {0, 2, 1}, 1, MKLDNNPlugin::impl_desc_type::unknown},
270 permute_test_params{{2, 3}, {0, 1}, 1, MKLDNNPlugin::impl_desc_type::unknown},
271 permute_test_params{{2, 3, 4, 5, 6}, {0, 1, 2, 3, 4}, 1, MKLDNNPlugin::impl_desc_type::unknown},
272 permute_test_params{{2, 3, 4, 5, 6}, {0, 4, 2, 1, 3}, 1, MKLDNNPlugin::impl_desc_type::unknown},
273 permute_test_params{{2, 3, 4, 5, 6}, {0, 2, 4, 3, 1}, 1, MKLDNNPlugin::impl_desc_type::unknown},
274 permute_test_params{{2, 3, 4, 5, 6}, {0, 3, 2, 4, 1}, 1, MKLDNNPlugin::impl_desc_type::unknown},
275 permute_test_params{{2, 8, 2, 2, 4, 5}, {0, 1, 4, 2, 5, 3}, 1, MKLDNNPlugin::impl_desc_type::unknown},
276 permute_test_params{{2, 8, 3, 3, 4, 5}, {0, 1, 4, 2, 5, 3}, 1, MKLDNNPlugin::impl_desc_type::unknown}