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 "tests_common.hpp"
18 using namespace ::testing;
20 using namespace mkldnn;
23 struct relu_test_params {
35 MKLDNNPlugin::impl_desc_type selectedType;
37 std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
40 template <typename data_t>
41 void ref_relu(const InferenceEngine::TBlob<data_t> &src, InferenceEngine::TBlob<data_t> &dst, relu_test_params prm)
47 const data_t *src_data = src.readOnly();
48 data_t *dst_data = dst.data();
50 for (uint32_t c = 0; c < IC; c++) {
51 for (uint32_t h = 0; h < IH; h++) {
52 for (uint32_t w = 0; w < IW; w++) {
53 uint32_t oidx = c * IH * IW
56 dst_data[oidx] = src_data[oidx] >= 0.0 ?
58 src_data[oidx] * prm.n_clope;
64 class MKLDNNGraphReluTests: public TestsCommon,
65 public WithParamInterface<relu_test_params> {
66 std::string model_t = R"V0G0N(
67 <Net Name="Relu_Only" version="2" precision="FP32" batch="1">
69 <layer name="in1" type="Input" precision="FP32" id="0">
79 <layer name="norm" id="1" type="ReLU" precision="FP32">
99 <edge from-layer="0" from-port="0" to-layer="1" to-port="1"/>
104 std::string getModel(relu_test_params p) {
105 std::string model = model_t;
107 REPLACE_WITH_NUM(model, "_IW_", p.in.w);
108 REPLACE_WITH_NUM(model, "_IH_", p.in.h);
109 REPLACE_WITH_NUM(model, "_IC_", p.in.c);
110 REPLACE_WITH_NUM(model, "_IN_", p.in.n);
116 virtual void TearDown() {
119 virtual void SetUp() {
121 TestsCommon::SetUp();
122 relu_test_params p = ::testing::WithParamInterface<relu_test_params>::GetParam();
123 std::string model = getModel(p);
125 InferenceEngine::CNNNetReader net_reader;
126 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
128 MKLDNNGraphTestClass graph;
129 graph.CreateGraph(net_reader.getNetwork());
130 auto& nodes = graph.getNodes();
131 for (int i = 0; i < nodes.size(); i++) {
132 if (nodes[i]->getType() == MKLDNNPlugin::Activation) {
133 ASSERT_LE(p.num_prim_desc, nodes[i]->getSupportedPrimitiveDescriptors().size());
134 for (size_t j = 0; j < p.num_prim_desc && j < p.comp.size(); j++) {
135 p.comp.at(j)(nodes[i]->getSupportedPrimitiveDescriptors().at(j));
137 ASSERT_NE(nullptr, nodes[i]->getSelectedPrimitiveDescriptor());
138 ASSERT_TRUE(nodes[i]->getSelectedPrimitiveDescriptor()->getImplementationType() | p.selectedType);
142 InferenceEngine::SizeVector dims_src = {p.in.n, p.in.c, p.in.h, p.in.w};
144 InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
146 fill_data(src->buffer(), src->size());
148 InferenceEngine::TBlob<float>* srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
150 if (srcPtr == nullptr)
151 FAIL() << "Cannot cast blob to TBlob<float>.";
153 InferenceEngine::BlobMap srcs;
154 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
156 InferenceEngine::OutputsDataMap out;
157 out = net_reader.getNetwork().getOutputsInfo();
158 InferenceEngine::BlobMap outputBlobs;
160 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
162 InferenceEngine::TBlob<float>::Ptr output;
163 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
165 outputBlobs[item.first] = output;
167 graph.Infer(srcs, outputBlobs);
169 InferenceEngine::TBlob<float> dst_ref(item.second->getTensorDesc());
172 ref_relu(*srcPtr, dst_ref, p);
174 compare(*output, dst_ref);
175 } catch (const InferenceEngine::details::InferenceEngineException &e) {
181 TEST_P(MKLDNNGraphReluTests, TestsRelu) {}
184 INSTANTIATE_TEST_CASE_P(
185 TestsRelu, MKLDNNGraphReluTests,
188 {1, 3, 228, 228}, 0.0f, 9, MKLDNNPlugin::impl_desc_type::jit, {
189 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
190 ASSERT_TRUE(impl.getImplementationType() | MKLDNNPlugin::impl_desc_type::jit);
191 ASSERT_EQ(1, impl.getConfig().inConfs.size());
192 ASSERT_EQ(1, impl.getConfig().outConfs.size());
193 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
194 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
196 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
197 ASSERT_TRUE(impl.getImplementationType() | MKLDNNPlugin::impl_desc_type::jit);
198 ASSERT_EQ(1, impl.getConfig().inConfs.size());
199 ASSERT_EQ(1, impl.getConfig().outConfs.size());
200 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
201 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());