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 "tests_common.hpp"
16 using namespace ::testing;
18 using namespace mkldnn;
21 struct interp_test_params {
41 std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
44 void interpolate(const int N, const int C, const float *src, const int x1, const int y1, const int IH_pad, const int IW_pad,
45 const int IH, const int IW, float *dst, const int x2, const int y2, const int OH_pad, const int OW_pad, const int OH, const int OW) {
46 if (IH_pad == OH_pad && IW_pad == OW_pad) {
47 for (int i = 0; i < N * C * OH * OW; i++) {
53 const float rh = (OH_pad > 1) ? static_cast<float>(IH_pad - 1) / (OH_pad - 1) : 0.0f;
54 const float rw = (OW_pad > 1) ? static_cast<float>(IW_pad - 1) / (OW_pad - 1) : 0.0f;
56 const int block_size = 1;
58 // Align channel number to block size to deal with channels padding in IE with multiple blobs
59 int CB = (C + block_size - 1) & (-block_size);
61 int CH = (C + block_size - 1) / block_size;
63 for (int n = 0; n < N; n++) {
64 for (int cb = 0; cb < CH; ++cb) {
65 for (int h = 0; h < OH_pad; ++h) {
66 const float *psrc = src + n * CB * IH * IW;
69 int ih0 = static_cast<int>(fh);
70 int ih1 = (ih0 < IH_pad - 1) ? ih0 + 1 : ih0;
72 float h_lambda0 = fh - ih0;
73 float h_lambda1 = 1.0f - h_lambda0;
75 for (int w = 0; w < OW_pad; ++w) {
77 int iw0 = static_cast<int>(fw);
78 int iw1 = (iw0 < IW_pad - 1) ? iw0 + 1 : iw0;
80 float w_lambda0 = fw - iw0;
81 float w_lambda1 = 1.0f - w_lambda0;
84 psrc + cb * block_size * IW * IH + (y1 + ih0) * IW * block_size + (x1 + iw0) * block_size;
86 psrc + cb * block_size * IW * IH + (y1 + ih0) * IW * block_size + (x1 + iw1) * block_size;
88 psrc + cb * block_size * IW * IH + (y1 + ih1) * IW * block_size + (x1 + iw0) * block_size;
90 psrc + cb * block_size * IW * IH + (y1 + ih1) * IW * block_size + (x1 + iw1) * block_size;
92 float *pdst = dst + n * CB * OH * OW + cb * block_size * OW * OH + (y2 + h) * OW * block_size +
93 (x2 + w) * block_size;
95 for (int c = 0; c < block_size; ++c) {
96 pdst[c] = h_lambda1 * (w_lambda1 * psrc00[c] + w_lambda0 * psrc01[c]) +
97 h_lambda0 * (w_lambda1 * psrc10[c] + w_lambda0 * psrc11[c]);
105 template <typename data_t>
106 void ref_interp(const InferenceEngine::TBlob<data_t> &src, InferenceEngine::TBlob<data_t> &dst, interp_test_params prm) {
107 int IB = static_cast<int>(src.getTensorDesc().getDims()[0]);
108 int IC = static_cast<int>(src.getTensorDesc().getDims()[1]);
109 int IH = static_cast<int>(src.getTensorDesc().getDims()[2]);
110 int IW = static_cast<int>(src.getTensorDesc().getDims()[3]);
112 int OH = static_cast<int>(dst.getTensorDesc().getDims()[2]);
113 int OW = static_cast<int>(dst.getTensorDesc().getDims()[3]);
115 int IH_pad = IH + prm.pad_beg + prm.pad_end;
116 int IW_pad = IW + prm.pad_beg + prm.pad_end;
118 const data_t *src_data = src.readOnly();
119 data_t *dst_data = dst.data();
121 interpolate(IB, IC, src_data, -prm.pad_beg, -prm.pad_beg, IH_pad, IW_pad, IH, IW, dst_data, 0, 0, OH, OW, OH, OW);
124 class MKLDNNCPUExtInterpTests: public TestsCommon, public WithParamInterface<interp_test_params> {
125 std::string model_t = R"V0G0N(
126 <Net Name="Convolution_Only" version="2" precision="FP32" batch="1">
128 <layer name="in1" type="Input" precision="FP32" id="0">
138 <layer name="interp1" id="1" type="Interp" precision="FP32">
139 <data pad_beg="_PB_" pad_end="_PE_" height="_OH_" width="_OW_"/>
160 <edge from-layer="0" from-port="0" to-layer="1" to-port="1"/>
165 std::string getModel(interp_test_params p) {
166 std::string model = model_t;
167 REPLACE_WITH_NUM(model, "_IW_", p.in.w);
168 REPLACE_WITH_NUM(model, "_IH_", p.in.h);
169 REPLACE_WITH_NUM(model, "_IC_", p.in.c);
170 REPLACE_WITH_NUM(model, "_IN_", p.in.n);
172 REPLACE_WITH_NUM(model, "_OH_", p.out.h);
173 REPLACE_WITH_NUM(model, "_OW_", p.out.w);
175 REPLACE_WITH_NUM(model, "_PB_", p.pad_beg);
176 REPLACE_WITH_NUM(model, "_PE_", p.pad_end);
181 virtual void TearDown() {
184 virtual void SetUp() {
186 TestsCommon::SetUp();
187 interp_test_params p = ::testing::WithParamInterface<interp_test_params>::GetParam();
188 std::string model = getModel(p);
190 InferenceEngine::CNNNetReader net_reader;
191 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
193 InferenceEngine::Extension cpuExt(make_so_name("cpu_extension"));
194 MKLDNNPlugin::MKLDNNExtensionManager::Ptr extMgr(new MKLDNNPlugin::MKLDNNExtensionManager());
195 extMgr->AddExtension(InferenceEngine::IExtensionPtr(&cpuExt, [](InferenceEngine::IExtension*){}));
197 MKLDNNGraphTestClass graph;
198 graph.CreateGraph(net_reader.getNetwork(), extMgr);
200 auto& nodes = graph.getNodes();
201 nodes = graph.getNodes();
202 for (auto &node : nodes) {
203 if (node->getName() == "interp1") {
204 ASSERT_LE(p.num_prim_desc, node->getSupportedPrimitiveDescriptors().size());
205 for (size_t j = 0; j < p.num_prim_desc && j < p.comp.size(); j++) {
206 p.comp.at(j)(node->getSupportedPrimitiveDescriptors().at(j));
208 ASSERT_NE(nullptr, node->getSelectedPrimitiveDescriptor());
209 ASSERT_EQ(p.selectedType,
210 node->getSelectedPrimitiveDescriptor()->getImplementationType() & p.selectedType);
213 ASSERT_LE(4, nodes.size());
215 InferenceEngine::SizeVector dims_src = {p.in.w, p.in.h, p.in.c, p.in.n};
217 InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
219 fill_data(src->buffer(), src->size());
221 auto * srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
223 if (srcPtr == nullptr)
224 FAIL() << "Cannot cast blob to TBlob<float>.";
226 InferenceEngine::BlobMap srcs;
227 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
229 InferenceEngine::OutputsDataMap out;
230 out = net_reader.getNetwork().getOutputsInfo();
231 InferenceEngine::BlobMap outputBlobs;
233 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
235 InferenceEngine::TBlob<float>::Ptr output;
236 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
238 outputBlobs[item.first] = output;
240 graph.Infer(srcs, outputBlobs);
243 InferenceEngine::TBlob<float> dst_ref(item.second->getTensorDesc());
245 ref_interp(*srcPtr, dst_ref, p);
246 compare(*output, dst_ref);
247 } catch (const InferenceEngine::details::InferenceEngineException &e) {
253 TEST_P(MKLDNNCPUExtInterpTests, TestsInterp) {}
255 INSTANTIATE_TEST_CASE_P(
256 TestsInterp, MKLDNNCPUExtInterpTests,
258 interp_test_params{{1, 256, 1, 1}, {33, 65}, 0, 0, 1, MKLDNNPlugin::impl_desc_type::unknown },
259 interp_test_params{{1, 2, 33, 65}, {33, 65}, 0, 0, 1, MKLDNNPlugin::impl_desc_type::unknown }));