1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
5 #include <gtest/gtest.h>
6 #include <gmock/gmock-spec-builders.h>
7 #include "mkldnn_plugin/mkldnn_graph.h"
9 #include "test_graph.hpp"
11 #include "single_layer_common.hpp"
12 #include <mkldnn_plugin/mkldnn_extension_utils.h>
13 #include <inference_engine/cnn_network_impl.hpp>
14 #include "tests_common.hpp"
17 using namespace ::testing;
19 using namespace mkldnn;
21 struct crop_test_params {
29 std::vector<int> axis;
30 std::vector<int> offsets;
31 std::vector<int> dims;
35 MKLDNNPlugin::impl_desc_type selectedType;
37 std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
42 template <typename data_t>
43 void ref_crop(InferenceEngine::TBlob<data_t> &src, InferenceEngine::TBlob<data_t> &dst, crop_test_params prm) {
44 data_t *dst_ptr = dst.data();
46 std::vector<int> offsets(4);
47 for (size_t i = 0; i < prm.offsets.size(); i++) {
48 offsets[prm.axis[i]] = prm.offsets[i];
50 int OFFSET_N = offsets.at(0);
51 int OFFSET_C = offsets.at(1);
52 int OFFSET_H = offsets.at(2);
53 int OFFSET_W = offsets.at(3);
55 const int ON = dst.dims().at(3);
56 const int OC = dst.dims().at(2);
57 const int OH = dst.dims().at(1);
58 const int OW = dst.dims().at(0);
60 const int _IN = src.dims().at(0);
61 const int IC = src.dims().at(1);
62 const int IH = src.dims().at(2);
63 const int IW = src.dims().at(3);
65 auto dst_off = [=](int n, int c, int h, int w) {
66 return (n * OW * OH * OC + c * OW * OH + h * OW + w);
68 auto src_off = [=](int n, int c, int h, int w) {
69 return (n * IW * IH * IC + c * IW * IH + h * IW + w);
72 ASSERT_GE(_IN - OFFSET_N, ON);
73 ASSERT_GE(IC - OFFSET_C, OC);
74 ASSERT_GE(IH - OFFSET_H, OH);
75 ASSERT_GE(IW - OFFSET_W, OW);
77 data_t* src_ptr = src.data();
78 for (int n = 0; n < ON; ++n) {
79 for (int c = 0; c < OC; ++c) {
80 for (int h = 0; h < OH; ++h) {
81 for (int w = 0; w < OW; ++w) {
82 dst_ptr[dst_off(n, c, h, w)] = src_ptr[src_off(n + OFFSET_N, c + OFFSET_C,
83 h + OFFSET_H, w + OFFSET_W)];
90 class MKLDNNGraphCropTests: public TestsCommon,
91 public WithParamInterface<crop_test_params> {
92 std::string model_t = R"V0G0N(
93 <Net Name="Crop_Only" version="2" precision="FP32" batch="1">
95 <layer name="in1" type="Input" precision="FP32" id="0">
105 <layer name="crop" id="1" type="Crop" precision="FP32">
106 <data axis="_AXC_" offset="_OFC_" dim="_DIMC_" />
126 <edge from-layer="0" from-port="0" to-layer="1" to-port="1"/>
132 std::string getModel(crop_test_params p) {
133 std::string model = model_t;
135 std::string axis, offset, dim;
136 std::vector<size_t> outDims = {p.in.n, p.in.c, p.in.h, p.in.w};
137 for (size_t i = 0; i < p.offsets.size(); i++) {
140 axis += std::to_string(p.axis[i]);
143 offset += std::to_string(p.offsets[i]);
146 dim += std::to_string(p.dims[i]);
147 outDims[p.axis[i]] = p.dims[i];
150 REPLACE_WITH_NUM(model, "_IW_", p.in.w);
151 REPLACE_WITH_NUM(model, "_IH_", p.in.h);
152 REPLACE_WITH_NUM(model, "_IC_", p.in.c);
153 REPLACE_WITH_NUM(model, "_IN_", p.in.n);
154 REPLACE_WITH_NUM(model, "_DN_", outDims[0]);
155 REPLACE_WITH_NUM(model, "_DC_", outDims[1]);
156 REPLACE_WITH_NUM(model, "_DH_", outDims[2]);
157 REPLACE_WITH_NUM(model, "_DW_", outDims[3]);
158 REPLACE_WITH_STR(model, "_AXC_", axis);
159 REPLACE_WITH_STR(model, "_OFC_", offset);
160 REPLACE_WITH_STR(model, "_DIMC_", dim);
164 virtual void TearDown() {
167 virtual void SetUp() {
169 TestsCommon::SetUp();
170 crop_test_params p = ::testing::WithParamInterface<crop_test_params>::GetParam();
171 std::string model = getModel(p);
173 InferenceEngine::CNNNetReader net_reader;
174 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
176 MKLDNNGraphTestClass graph;
177 graph.CreateGraph(net_reader.getNetwork());
179 auto& nodes = graph.getNodes();
180 for (int i = 0; i < nodes.size(); i++) {
181 if (nodes[i]->getType() == MKLDNNPlugin::Crop) {
182 ASSERT_EQ(p.num_prim_desc, nodes[i]->getSupportedPrimitiveDescriptors().size());
183 for (size_t j = 0; j < p.num_prim_desc && j < p.comp.size(); j++) {
184 p.comp.at(j)(nodes[i]->getSupportedPrimitiveDescriptors().at(j));
186 ASSERT_NE(nullptr, nodes[i]->getSelectedPrimitiveDescriptor());
187 ASSERT_EQ(p.selectedType, nodes[i]->getSelectedPrimitiveDescriptor()->getImplementationType());
191 InferenceEngine::SizeVector dims_src = {p.in.n, p.in.c, p.in.h, p.in.w};
193 InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
195 fill_data(src->buffer(), src->size());
197 InferenceEngine::TBlob<float>* srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
199 if (srcPtr == nullptr)
200 FAIL() << "Cannot cast blob to TBlob<float>.";
202 InferenceEngine::BlobMap srcs;
203 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
205 InferenceEngine::OutputsDataMap out;
206 out = net_reader.getNetwork().getOutputsInfo();
207 InferenceEngine::BlobMap outputBlobs;
209 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
211 InferenceEngine::TBlob<float>::Ptr output;
212 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
214 outputBlobs[item.first] = output;
216 graph.Infer(srcs, outputBlobs);
218 InferenceEngine::TBlob<float> dst_ref(item.second->getTensorDesc());
221 ref_crop(*srcPtr, dst_ref, p);
223 compare(*output, dst_ref);
224 } catch (const InferenceEngine::details::InferenceEngineException &e) {
230 TEST_P(MKLDNNGraphCropTests, TestCrop) {}
233 INSTANTIATE_TEST_CASE_P(
234 TestCrop, MKLDNNGraphCropTests,
236 crop_test_params{{1, 5, 32, 32}, {1, 2, 3}, {2, 5, 4}, {2, 23, 23}, 1, MKLDNNPlugin::impl_desc_type::unknown, {
237 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
238 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
239 ASSERT_EQ(1, impl.getConfig().inConfs.size());
240 ASSERT_EQ(1, impl.getConfig().outConfs.size());
241 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
242 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
244 crop_test_params{{3, 8, 32, 32}, {0, 1, 2, 3}, {1, 0, 20, 20}, {2, 8, 5, 5}, 2, MKLDNNPlugin::impl_desc_type::unknown, {
245 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
246 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
247 ASSERT_EQ(1, impl.getConfig().inConfs.size());
248 ASSERT_EQ(1, impl.getConfig().outConfs.size());
249 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
250 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
252 crop_test_params{{1, 5, 32, 32}, {3}, {10}, {20}, 1, MKLDNNPlugin::impl_desc_type::unknown },
253 crop_test_params{{1, 5, 32, 20}, {2, 3}, {30, 10}, {2, 10}, 1, MKLDNNPlugin::impl_desc_type::unknown }));
255 class MKLDNNGraphDynBatchCropTests: public MKLDNNGraphCropTests {
258 virtual void SetUp() {
260 TestsCommon::SetUp();
261 crop_test_params p = ::testing::WithParamInterface<crop_test_params>::GetParam();
262 std::string model = getModel(p);
267 InferenceEngine::CNNNetReader net_reader;
268 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
269 InferenceEngine::CNNNetwork network = net_reader.getNetwork();
270 network.setBatchSize(MB);
272 MKLDNNGraphTestClass graph;
273 graph.setProperty({{InferenceEngine::PluginConfigParams::KEY_DYN_BATCH_ENABLED, InferenceEngine::PluginConfigParams::YES}});
274 graph.CreateGraph(network);
276 InferenceEngine::SizeVector dims_src = {MB, p.in.c, p.in.h, p.in.w};
277 InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
278 InferenceEngine::TBlob<float>* srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
279 if (srcPtr == nullptr)
280 FAIL() << "Cannot cast blob to TBlob<float>.";
283 fill_data(src->buffer(), src->size());
285 InferenceEngine::BlobMap srcs;
286 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
288 InferenceEngine::OutputsDataMap out;
289 out = net_reader.getNetwork().getOutputsInfo();
290 InferenceEngine::BlobMap outputBlobs;
292 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
294 InferenceEngine::TBlob<float>::Ptr output;
295 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
297 outputBlobs[item.first] = output;
299 auto checkCrop = [](const MKLDNNPlugin::MKLDNNNodePtr& node) {
300 return node->getType() == MKLDNNPlugin::Crop;
303 graph.checkDynBatch(srcs, outputBlobs, MB, MB, checkCrop);
304 graph.checkDynBatch(srcs, outputBlobs, 1, MB, checkCrop);
305 } catch (const InferenceEngine::details::InferenceEngineException &e) {
311 TEST_P(MKLDNNGraphDynBatchCropTests, TestsDynBatchCrop) {}
313 INSTANTIATE_TEST_CASE_P(
314 TestsDynBatchCrop, MKLDNNGraphDynBatchCropTests,
316 crop_test_params{{1, 5, 32, 32}, {1, 2, 3}, {2, 5, 4}, {2, 23, 23}, 1, MKLDNNPlugin::impl_desc_type::unknown, {
317 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
318 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
319 ASSERT_EQ(1, impl.getConfig().inConfs.size());
320 ASSERT_EQ(1, impl.getConfig().outConfs.size());
321 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
322 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
324 crop_test_params{{1, 5, 32, 32}, {3}, {10}, {20}, 1, MKLDNNPlugin::impl_desc_type::unknown },
325 crop_test_params{{1, 5, 32, 20}, {2, 3}, {30, 10}, {2, 10}, 1, MKLDNNPlugin::impl_desc_type::unknown }));