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"
10 #include "test_graph.hpp"
12 #include "single_layer_common.hpp"
13 #include <mkldnn_plugin/mkldnn_extension_utils.h>
14 #include <extension/ext_list.hpp>
15 #include "tests_common.hpp"
17 using namespace ::testing;
19 using namespace mkldnn;
21 struct resample_test_params {
37 std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
41 static inline float triangleCoeff(float x) {
42 return std::max(0.0f, 1 - std::abs(x));
45 template <typename data_t>
46 void ref_resample(const InferenceEngine::TBlob<data_t> &src, InferenceEngine::TBlob<data_t> &dst, resample_test_params prm) {
47 const data_t *src_data = src.readOnly();
48 data_t *dst_data = dst.data();
54 size_t OH = prm.in.h / prm.factor;
55 size_t OW = prm.in.w / prm.factor;
57 float fx = static_cast<float>(IW) / static_cast<float>(OW);
58 float fy = static_cast<float>(IH) / static_cast<float>(OH);
60 if (prm.type == "caffe.ResampleParameter.NEAREST") {
61 for (size_t b = 0; b < N; b++) {
62 for (size_t c = 0; c < C; c++) {
63 const float *in_ptr = src_data + IW * IH * C * b + IW * IH * c;
64 float *out_ptr = dst_data + OW * OH * C * b + OW * OH * c;
66 for (size_t oy = 0; oy < OH; oy++) {
67 for (size_t ox = 0; ox < OW; ox++) {
68 float ix = ox * fx + fy / 2.0f - 0.5f;
69 float iy = oy * fy + fx / 2.0f - 0.5f;
71 size_t ix_r = static_cast<size_t>(round(ix));
72 size_t iy_r = static_cast<size_t>(round(iy));
74 out_ptr[oy * OW + ox] = in_ptr[iy_r * IW + ix_r];
79 } else if (prm.type == "caffe.ResampleParameter.LINEAR") {
80 size_t kernel_width = 2;
81 bool isDownsample = (fx > 1) || (fy > 1);
82 bool antialias = isDownsample && prm.antialias;
84 for (size_t b = 0; b < N; b++) {
85 for (size_t c = 0; c < C; c++) {
86 const float *in_ptr = src_data + IW * IH * C * b + IW * IH * c;
87 float *out_ptr = dst_data + OW * OH * C * b + OW * OH * c;
89 for (size_t oy = 0; oy < OH; oy++) {
90 for (size_t ox = 0; ox < OW; ox++) {
91 float ix = ox * fx + fy / 2.0f - 0.5f;
92 float iy = oy * fy + fx / 2.0f - 0.5f;
94 int ix_r = static_cast<int>(round(ix));
95 int iy_r = static_cast<int>(round(iy));
100 float ax = 1.0f / (antialias ? fx : 1.0f);
101 float ay = 1.0f / (antialias ? fy : 1.0f);
103 int rx = (fx < 1.0f) ? 2 : ceil(static_cast<float>(kernel_width) / ax);
104 int ry = (fy < 1.0f) ? 2 : ceil(static_cast<float>(kernel_width) / ay);
106 for (int y = iy_r - ry; y <= iy_r + ry; y++) {
107 for (int x = ix_r - rx; x <= ix_r + rx; x++) {
108 if (y < 0 || x < 0 || y >= static_cast<int>(IH) || x >= static_cast<int>(IW))
114 float w = ax * triangleCoeff(ax * dx) * ay * triangleCoeff(ay * dy);
116 sum += w * in_ptr[y * IW + x];
121 out_ptr[oy * OW + ox] = (!wsum) ? 0 : (sum / wsum);
127 assert(!"Unsupported resample operation type");
131 class MKLDNNCPUExtResampleTests: public TestsCommon, public WithParamInterface<resample_test_params> {
132 std::string model_t = R"V0G0N(
133 <Net Name="MVN_net" version="2" precision="FP32" batch="1">
135 <layer name="in1" type="Input" precision="FP32" id="0">
145 <layer name="fakeLayer" id="1" type="_FL_" precision="FP32">
163 <layer name="resample" id="2" type="Resample" precision="FP32">
164 <data antialias="_AN_" factor="_F_" type="_T_"/>
184 <edge from-layer="0" from-port="0" to-layer="1" to-port="1"/>
185 <edge from-layer="1" from-port="2" to-layer="2" to-port="3"/>
190 std::string getModel(resample_test_params p) {
191 std::string model = model_t;
192 if (p.isBlockedFormat)
193 REPLACE_WITH_STR(model, "_FL_", "FakeLayerBLK");
195 REPLACE_WITH_STR(model, "_FL_", "FakeLayerPLN");
197 REPLACE_WITH_NUM(model, "_IW_", p.in.w);
198 REPLACE_WITH_NUM(model, "_IH_", p.in.h);
199 REPLACE_WITH_NUM(model, "_IC_", p.in.c);
200 REPLACE_WITH_NUM(model, "_IN_", p.in.n);
202 REPLACE_WITH_NUM(model, "_OW_", (int)(p.in.w / p.factor));
203 REPLACE_WITH_NUM(model, "_OH_", (int)(p.in.h / p.factor));
205 REPLACE_WITH_NUM(model, "_AN_", p.antialias);
206 REPLACE_WITH_NUM(model, "_F_", p.factor);
207 REPLACE_WITH_STR(model, "_T_", p.type);
213 virtual void TearDown() {
216 virtual void SetUp() {
218 TestsCommon::SetUp();
219 resample_test_params p = ::testing::WithParamInterface<resample_test_params>::GetParam();
220 std::string model = getModel(p);
222 InferenceEngine::CNNNetReader net_reader;
223 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
225 std::shared_ptr<InferenceEngine::IExtension> cpuExt(new InferenceEngine::Extensions::Cpu::CpuExtensions());
226 MKLDNNPlugin::MKLDNNExtensionManager::Ptr extMgr(new MKLDNNPlugin::MKLDNNExtensionManager());
227 extMgr->AddExtension(cpuExt);
229 MKLDNNGraphTestClass graph;
230 graph.CreateGraph(net_reader.getNetwork(), extMgr);
232 auto& nodes = graph.getNodes();
233 nodes = graph.getNodes();
235 for (auto &node : nodes) {
236 if (node->getName() == "resample") {
237 ASSERT_EQ(p.num_prim_desc, node->getSupportedPrimitiveDescriptors().size());
238 for (size_t j = 0; j < p.num_prim_desc && j < p.comp.size(); j++) {
239 p.comp.at(j)(node->getSupportedPrimitiveDescriptors().at(j));
241 ASSERT_NE(nullptr, node->getSelectedPrimitiveDescriptor());
242 ASSERT_EQ(p.selectedType,
243 node->getSelectedPrimitiveDescriptor()->getImplementationType() & p.selectedType);
247 if (p.isBlockedFormat)
248 ASSERT_EQ(6, nodes.size());
250 ASSERT_EQ(4, nodes.size());
252 InferenceEngine::SizeVector dims_src = {p.in.w, p.in.h, p.in.c, p.in.n};
254 InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NHWC, dims_src);
256 fill_data(src->buffer(), src->size());
258 auto * srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
260 if (srcPtr == nullptr)
261 FAIL() << "Cannot cast blob to TBlob<float>.";
263 InferenceEngine::BlobMap srcs;
264 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
266 InferenceEngine::OutputsDataMap out;
267 out = net_reader.getNetwork().getOutputsInfo();
268 InferenceEngine::BlobMap outputBlobs;
270 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
272 InferenceEngine::TBlob<float>::Ptr output;
273 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
275 outputBlobs[item.first] = output;
277 graph.Infer(srcs, outputBlobs);
279 InferenceEngine::TBlob<float> dst_ref(item.second->getTensorDesc());
281 ref_resample(*srcPtr, dst_ref, p);
282 compare(*output, dst_ref);
283 } catch (const InferenceEngine::details::InferenceEngineException &e) {
289 TEST_P(MKLDNNCPUExtResampleTests, TestsResample) {}
291 INSTANTIATE_TEST_CASE_P(
292 TestsResample, MKLDNNCPUExtResampleTests,
294 resample_test_params{{2, 64, 15, 25}, 1.f, 0, "caffe.ResampleParameter.NEAREST", 2, false, MKLDNNPlugin::impl_desc_type::unknown },
295 resample_test_params{{2, 64, 15, 25}, 1.f, 0, "caffe.ResampleParameter.NEAREST", 2, true, MKLDNNPlugin::impl_desc_type::unknown },
296 resample_test_params{{2, 64, 15, 25}, 1.f, 1, "caffe.ResampleParameter.LINEAR", 1, false, MKLDNNPlugin::impl_desc_type::unknown },
297 resample_test_params{{2, 64, 10, 20}, 0.25f, 0, "caffe.ResampleParameter.NEAREST", 2, false, MKLDNNPlugin::impl_desc_type::unknown },
298 resample_test_params{{2, 64, 10, 20}, 0.25f, 0, "caffe.ResampleParameter.NEAREST", 2, true, MKLDNNPlugin::impl_desc_type::unknown },
299 resample_test_params{{2, 64, 10, 20}, 0.25f, 1, "caffe.ResampleParameter.LINEAR", 1, false, MKLDNNPlugin::impl_desc_type::unknown },
300 resample_test_params{{2, 64, 10, 20}, 4.f, 0, "caffe.ResampleParameter.NEAREST", 2, false, MKLDNNPlugin::impl_desc_type::unknown },
301 resample_test_params{{2, 64, 10, 20}, 4.f, 0, "caffe.ResampleParameter.NEAREST", 2, true, MKLDNNPlugin::impl_desc_type::unknown },
302 resample_test_params{{2, 64, 10, 20}, 4.f, 1, "caffe.ResampleParameter.LINEAR", 1, false, MKLDNNPlugin::impl_desc_type::unknown },
303 resample_test_params{{2, 3, 15, 25}, 1.f, 0, "caffe.ResampleParameter.NEAREST", 2, false, MKLDNNPlugin::impl_desc_type::unknown },
304 resample_test_params{{2, 3, 15, 25}, 1.f, 0, "caffe.ResampleParameter.NEAREST", 2, true, MKLDNNPlugin::impl_desc_type::unknown },
305 resample_test_params{{2, 3, 15, 25}, 1.f, 1, "caffe.ResampleParameter.LINEAR", 1, false, MKLDNNPlugin::impl_desc_type::unknown },
306 resample_test_params{{2, 3, 10, 20}, 0.25f, 0, "caffe.ResampleParameter.NEAREST", 2, false, MKLDNNPlugin::impl_desc_type::unknown },
307 resample_test_params{{2, 3, 10, 20}, 0.25f, 0, "caffe.ResampleParameter.NEAREST", 2, true, MKLDNNPlugin::impl_desc_type::unknown },
308 resample_test_params{{2, 3, 10, 20}, 0.25f, 1, "caffe.ResampleParameter.LINEAR", 1, false, MKLDNNPlugin::impl_desc_type::unknown },
309 resample_test_params{{2, 3, 10, 20}, 4.f, 0, "caffe.ResampleParameter.NEAREST", 2, false, MKLDNNPlugin::impl_desc_type::unknown },
310 resample_test_params{{2, 3, 10, 20}, 4.f, 0, "caffe.ResampleParameter.NEAREST", 2, true, MKLDNNPlugin::impl_desc_type::unknown },
311 resample_test_params{{2, 3, 10, 20}, 4.f, 1, "caffe.ResampleParameter.LINEAR", 1, false, MKLDNNPlugin::impl_desc_type::unknown }));