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 <extension/ext_list.hpp>
16 #include "tests_common.hpp"
19 using namespace ::testing;
21 using namespace mkldnn;
24 struct interp_test_params {
44 std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
47 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,
48 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) {
49 if (IH_pad == OH_pad && IW_pad == OW_pad) {
50 for (int i = 0; i < N * C * OH * OW; i++) {
56 const float rh = (OH_pad > 1) ? static_cast<float>(IH_pad - 1) / (OH_pad - 1) : 0.0f;
57 const float rw = (OW_pad > 1) ? static_cast<float>(IW_pad - 1) / (OW_pad - 1) : 0.0f;
59 const int block_size = 1;
61 // Align channel number to block size to deal with channels padding in IE with multiple blobs
62 int CB = (C + block_size - 1) & (-block_size);
64 int CH = (C + block_size - 1) / block_size;
66 for (int n = 0; n < N; n++) {
67 for (int cb = 0; cb < CH; ++cb) {
68 for (int h = 0; h < OH_pad; ++h) {
69 const float *psrc = src + n * CB * IH * IW;
72 int ih0 = static_cast<int>(fh);
73 int ih1 = (ih0 < IH_pad - 1) ? ih0 + 1 : ih0;
75 float h_lambda0 = fh - ih0;
76 float h_lambda1 = 1.0f - h_lambda0;
78 for (int w = 0; w < OW_pad; ++w) {
80 int iw0 = static_cast<int>(fw);
81 int iw1 = (iw0 < IW_pad - 1) ? iw0 + 1 : iw0;
83 float w_lambda0 = fw - iw0;
84 float w_lambda1 = 1.0f - w_lambda0;
87 psrc + cb * block_size * IW * IH + (y1 + ih0) * IW * block_size + (x1 + iw0) * block_size;
89 psrc + cb * block_size * IW * IH + (y1 + ih0) * IW * block_size + (x1 + iw1) * block_size;
91 psrc + cb * block_size * IW * IH + (y1 + ih1) * IW * block_size + (x1 + iw0) * block_size;
93 psrc + cb * block_size * IW * IH + (y1 + ih1) * IW * block_size + (x1 + iw1) * block_size;
95 float *pdst = dst + n * CB * OH * OW + cb * block_size * OW * OH + (y2 + h) * OW * block_size +
96 (x2 + w) * block_size;
98 for (int c = 0; c < block_size; ++c) {
99 pdst[c] = h_lambda1 * (w_lambda1 * psrc00[c] + w_lambda0 * psrc01[c]) +
100 h_lambda0 * (w_lambda1 * psrc10[c] + w_lambda0 * psrc11[c]);
108 template <typename data_t>
109 void ref_interp(const InferenceEngine::TBlob<data_t> &src, InferenceEngine::TBlob<data_t> &dst, interp_test_params prm) {
110 int IB = static_cast<int>(src.getTensorDesc().getDims()[0]);
111 int IC = static_cast<int>(src.getTensorDesc().getDims()[1]);
112 int IH = static_cast<int>(src.getTensorDesc().getDims()[2]);
113 int IW = static_cast<int>(src.getTensorDesc().getDims()[3]);
115 int OH = static_cast<int>(dst.getTensorDesc().getDims()[2]);
116 int OW = static_cast<int>(dst.getTensorDesc().getDims()[3]);
118 int IH_pad = IH + prm.pad_beg + prm.pad_end;
119 int IW_pad = IW + prm.pad_beg + prm.pad_end;
121 const data_t *src_data = src.readOnly();
122 data_t *dst_data = dst.data();
124 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);
127 class MKLDNNCPUExtInterpTests: public TestsCommon, public WithParamInterface<interp_test_params> {
128 std::string model_t = R"V0G0N(
129 <Net Name="Convolution_Only" version="2" precision="FP32" batch="1">
131 <layer name="in1" type="Input" precision="FP32" id="0">
141 <layer name="interp1" id="1" type="Interp" precision="FP32">
142 <data pad_beg="_PB_" pad_end="_PE_"/>
163 <edge from-layer="0" from-port="0" to-layer="1" to-port="1"/>
168 std::string getModel(interp_test_params p) {
169 std::string model = model_t;
170 REPLACE_WITH_NUM(model, "_IW_", p.in.w);
171 REPLACE_WITH_NUM(model, "_IH_", p.in.h);
172 REPLACE_WITH_NUM(model, "_IC_", p.in.c);
173 REPLACE_WITH_NUM(model, "_IN_", p.in.n);
175 REPLACE_WITH_NUM(model, "_OH_", p.out.h);
176 REPLACE_WITH_NUM(model, "_OW_", p.out.w);
178 REPLACE_WITH_NUM(model, "_PB_", p.pad_beg);
179 REPLACE_WITH_NUM(model, "_PE_", p.pad_end);
184 virtual void TearDown() {
187 virtual void SetUp() {
189 TestsCommon::SetUp();
190 interp_test_params p = ::testing::WithParamInterface<interp_test_params>::GetParam();
191 std::string model = getModel(p);
193 InferenceEngine::CNNNetReader net_reader;
194 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
196 std::shared_ptr<InferenceEngine::IExtension> cpuExt(new InferenceEngine::Extensions::Cpu::CpuExtensions());
197 MKLDNNPlugin::MKLDNNExtensionManager::Ptr extMgr(new MKLDNNPlugin::MKLDNNExtensionManager());
198 extMgr->AddExtension(cpuExt);
200 MKLDNNGraphTestClass graph;
201 graph.CreateGraph(net_reader.getNetwork(), extMgr);
203 auto& nodes = graph.getNodes();
204 nodes = graph.getNodes();
205 for (auto &node : nodes) {
206 if (node->getName() == "interp1") {
207 ASSERT_LE(p.num_prim_desc, node->getSupportedPrimitiveDescriptors().size());
208 for (size_t j = 0; j < p.num_prim_desc && j < p.comp.size(); j++) {
209 p.comp.at(j)(node->getSupportedPrimitiveDescriptors().at(j));
211 ASSERT_NE(nullptr, node->getSelectedPrimitiveDescriptor());
212 ASSERT_EQ(p.selectedType,
213 node->getSelectedPrimitiveDescriptor()->getImplementationType() & p.selectedType);
216 ASSERT_LE(4, nodes.size());
218 InferenceEngine::SizeVector dims_src = {p.in.w, p.in.h, p.in.c, p.in.n};
220 InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
222 fill_data(src->buffer(), src->size());
224 auto * srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
226 if (srcPtr == nullptr)
227 FAIL() << "Cannot cast blob to TBlob<float>.";
229 InferenceEngine::BlobMap srcs;
230 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
232 InferenceEngine::OutputsDataMap out;
233 out = net_reader.getNetwork().getOutputsInfo();
234 InferenceEngine::BlobMap outputBlobs;
236 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
238 InferenceEngine::TBlob<float>::Ptr output;
239 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
241 outputBlobs[item.first] = output;
243 graph.Infer(srcs, outputBlobs);
246 InferenceEngine::TBlob<float> dst_ref(item.second->getTensorDesc());
248 ref_interp(*srcPtr, dst_ref, p);
249 compare(*output, dst_ref);
250 } catch (const InferenceEngine::details::InferenceEngineException &e) {
256 TEST_P(MKLDNNCPUExtInterpTests, TestsInterp) {}
258 INSTANTIATE_TEST_CASE_P(
259 TestsInterp, MKLDNNCPUExtInterpTests,
261 interp_test_params{{1, 256, 1, 1}, {33, 65}, 0, 0, 1, MKLDNNPlugin::impl_desc_type::unknown },
262 interp_test_params{{1, 2, 33, 65}, {33, 65}, 0, 0, 1, MKLDNNPlugin::impl_desc_type::unknown }));