Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / tests / test_cases / fused_conv_eltwise_gpu_test.cpp
1 /*
2 // Copyright (c) 2016 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #include <gtest/gtest.h>
19 #include "api/CPP/memory.hpp"
20 #include <api/CPP/input_layout.hpp>
21 #include "api/CPP/convolution.hpp"
22 #include "api/CPP/eltwise.hpp"
23 #include "api/CPP/reorder.hpp"
24 #include <api/CPP/topology.hpp>
25 #include <api/CPP/network.hpp>
26 #include <api/CPP/engine.hpp>
27 #include "test_utils/test_utils.h"
28 #include <api/CPP/data.hpp>
29
30 #include <cmath>
31 #include <gmock/gmock.h>
32 #include <limits>
33
34 using namespace cldnn;
35 using namespace tests;
36 using namespace testing;
37
38 TEST(fused_conv_eltwise, basic_0)
39 {
40     const auto& engine = get_test_engine();
41
42     auto input = memory::allocate(engine, { data_types::f32, format::bfyx, { 1, 1, 4, 5 } });
43     auto weights = memory::allocate(engine, { data_types::f32, format::bfyx, { 1, 1, 1, 1 } });
44
45     set_values(input, {
46         1.0f,  2.0f, -15.f,  3.0f, 4.0f, -15.f, 5.0f,  6.0f, -15.f, 7.0f,
47         -15.f, 0.0f,  0.0f, -15.f, 0.5f, -0.5f, -15.f, 8.0f,  1.5f,  5.2f
48     });
49
50     topology topology(
51         input_layout("input", input.get_layout()),
52         data("weights", weights),
53         convolution("conv", "input", { "weights" }),
54         eltwise("eltwise", "input", "conv", eltwise_mode::sum),
55         reorder("out", "eltwise", format::bfyx, data_types::f32));
56
57     build_options opt;
58     opt.set_option(build_option::optimize_data(true));
59     network network(engine, topology, opt);
60     network.set_input_data("input", input);
61
62     auto outputs = network.execute();
63     EXPECT_EQ(outputs.size(), size_t(1));
64     EXPECT_EQ(outputs.begin()->first, "out");
65
66     auto output = outputs.begin()->second.get_memory();
67     auto&& out_layout = output.get_layout();
68
69     EXPECT_EQ(out_layout.format, format::bfyx);
70     EXPECT_EQ(out_layout.size.batch[0], 1);
71     EXPECT_EQ(out_layout.size.feature[0], 1);
72     EXPECT_EQ(out_layout.size.spatial[0], 4);
73     EXPECT_EQ(out_layout.size.spatial[1], 5);
74 }
75
76
77 TEST(fused_conv_eltwise, dont_fuse_if_conv_elt_are_outputs)
78 {
79     const auto& engine = get_test_engine();
80
81     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 4, 5 } });
82     auto weights = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 1, 1 } });
83
84     set_values(input, {
85         1.0f,  2.0f, -15.f,  3.0f, 4.0f, -15.f, 5.0f,  6.0f, -15.f, 7.0f,
86         -15.f, 0.0f,  0.0f, -15.f, 0.5f, -0.5f, -15.f, 8.0f,  1.5f,  5.2f
87         });
88
89     topology topology(
90         input_layout("input", input.get_layout()),
91         data("weights", weights),
92         convolution("conv", "input", { "weights" }),
93         eltwise("out", "input", "conv", eltwise_mode::sum));
94
95     build_options opt;
96     opt.set_option(build_option::optimize_data(true));
97     network network(engine, topology, opt);
98     network.set_input_data("input", input);
99
100     auto outputs = network.execute();
101     EXPECT_EQ(outputs.size(), size_t(1));
102     EXPECT_EQ(outputs.begin()->first, "out");
103
104     auto output = outputs.begin()->second.get_memory();
105     auto&& out_layout = output.get_layout();
106
107     EXPECT_EQ(out_layout.format, format::bfyx);
108     EXPECT_EQ(out_layout.size.batch[0], 1);
109     EXPECT_EQ(out_layout.size.feature[0], 1);
110     EXPECT_EQ(out_layout.size.spatial[0], 4);
111     EXPECT_EQ(out_layout.size.spatial[1], 5);
112 }