Publishing 2019 R3 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / tests / test_cases / convolution_grad_input_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
19 #include <gtest/gtest.h>
20 #include "api/memory.hpp"
21 #include <api/input_layout.hpp>
22 #include "api/convolution_grad_input.hpp"
23 #include <api/data.hpp>
24 #include <api/topology.hpp>
25 #include <api/network.hpp>
26 #include <api/engine.hpp>
27 #include "test_utils/test_utils.h"
28 #include "api/eltwise.hpp"
29
30 using namespace cldnn;
31 using namespace tests;
32
33 TEST(convolution_grad_input_f32_fw_gpu, basic_wsiz2x2_in2x2x1x2_bfyx_stride2_pad1) {
34     //  Filter : 2x2
35     //  Input  : 2x2x1x2
36     //  Output : 2x2x1x2
37     //  Stride : 2x2
38     //
39     //  Input:
40     //  8  0.5    1   3
41     //  6  9      2   4
42     //
43     //  Filter
44     //  -2   2
45     //   7  -0.5
46     //
47     //  Output:
48     //  -4    3.5    -0.5   21
49     //   12  -18      4     -9
50
51     const auto& engine = get_test_engine();
52
53     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 1, 2, 2 } });
54     auto weights = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 2, 2 } });
55
56     set_values(input, { 8.f, 0.5f, 6.f, 9.f, 1.f, 3.f, 2.f, 4.f });
57     set_values(weights, { -2.f, 2.f, 7.f, -0.5f });
58
59     topology topology(
60         input_layout("input", input.get_layout()),
61         data("weights", weights),
62         convolution_grad_input("deconv", "input", { "weights" }, { 1, 1, 2, 2 }, { 0, 0, -1, -1 })
63     );
64
65     network network(engine, topology);
66     network.set_input_data("input", input);
67
68     auto outputs = network.execute();
69     EXPECT_EQ(outputs.size(), size_t(1));
70     EXPECT_EQ(outputs.begin()->first, "deconv");
71
72     auto output_prim = outputs.begin()->second.get_memory();
73
74     auto output_ptr = output_prim.pointer<float>();
75
76     std::vector<float> expected_output_vec = {
77         -4.f, 3.5f, 12.f, -18.f,
78         -.5f, 21.f, 4.f, -8.f
79     };
80
81     for (unsigned int i = 0; i < expected_output_vec.size(); i++)
82     {
83         EXPECT_FLOAT_EQ(expected_output_vec[i], output_ptr[i]);
84     }
85 }
86
87 TEST(convolution_grad_input_f32_fw_gpu, basic_wsiz2x2_in2x2x1x2_bfyx_stride2_pad1_output_size) {
88     //  Filter : 2x2
89     //  Input  : 2x2x1x2
90     //  Output : 2x2x1x2
91     //  Stride : 2x2
92     //
93     //  Input:
94     //  8  0.5    1   3
95     //  6  9      2   4
96     //
97     //  Filter
98     //  -2   2
99     //   7  -0.5
100     //
101     //  Output:
102     //  -4    3.5    -0.5   21
103     //   12  -18      4     -9
104
105     const auto& engine = get_test_engine();
106
107     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 1, 2, 2 } });
108     auto weights = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 2, 2 } });
109
110     set_values(input, { 8.f, 0.5f, 6.f, 9.f, 1.f, 3.f, 2.f, 4.f });
111     set_values(weights, { -2.f, 2.f, 7.f, -0.5f });
112
113     topology topology(
114         input_layout("input", input.get_layout()),
115         data("weights", weights),
116         convolution_grad_input("deconv", "input", { "weights" }, { 1, 1, 2, 2 }, { 0, 0, -1, -1 }, { 2, 1, 2, 2 })
117     );
118
119     network network(engine, topology);
120     network.set_input_data("input", input);
121
122     auto outputs = network.execute();
123     EXPECT_EQ(outputs.size(), size_t(1));
124     EXPECT_EQ(outputs.begin()->first, "deconv");
125
126     auto output_prim = outputs.begin()->second.get_memory();
127
128     auto output_ptr = output_prim.pointer<float>();
129
130     std::vector<float> expected_output_vec = {
131         -4.f, 3.5f, 12.f, -18.f,
132         -.5f, 21.f, 4.f, -8.f
133     };
134
135     for (unsigned int i = 0; i < expected_output_vec.size(); i++)
136     {
137         EXPECT_FLOAT_EQ(expected_output_vec[i], output_ptr[i]);
138     }
139 }
140
141 TEST(convolution_grad_input_f32_fw_gpu, DISABLED_basic_wsiz2x2_in2x2x1x2_bfyx_stride2_fusion) {
142     //  Filter : 2x2
143     //  Input  : 2x2x1x2
144     //  Output : 2x2x1x2
145     //  Stride : 2x2
146     //
147     //  Input:
148     //  8  0.5    1   3
149     //  6  9      2   4
150     //
151     //  Filter
152     //  -2   2
153     //   7  -0.5
154     //
155     //  Output:
156     //  -4    3.5    -0.5   21
157     //   12  -18      4     -9
158
159     const auto& engine = get_test_engine();
160
161     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 1, 2, 2 } });
162     auto weights = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 2, 2 } });
163     auto scale_in = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 1, 1 } });
164     auto elt_data = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 2, 1, 2 } });
165
166     set_values(input, { 8.f, 0.5f, 6.f, 9.f, 1.f, 3.f, 2.f, 4.f });
167     set_values(weights, { -2.f, 2.f, 7.f, -0.5f });
168     set_values(scale_in, { 1.0f });
169     set_values(elt_data, { 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f });
170
171     topology topology(
172         input_layout("input", input.get_layout()),
173         data("weights", weights),
174         data("scale_in", scale_in),
175         data("elt_data", elt_data),
176         convolution_grad_input("conv", "input", { "weights" }, { 1, 1, 2, 2 }, { 0, 0, -1, -1 }),
177         eltwise("elt", "conv", "elt_data", eltwise_mode::sum),
178         scale("scale", "elt", "scale_in")
179     );
180
181     build_options options;
182     options.set_option(build_option::optimize_data(true));
183        
184     network network(engine, topology, options);
185     network.set_input_data("input", input);
186
187     auto outputs = network.execute();
188     auto primitives = network.get_all_primitive_ids();
189     auto exec_prim = network.get_executed_primitive_ids();
190     EXPECT_EQ(outputs.size(), size_t(1));
191     EXPECT_EQ(outputs.begin()->first, "scale");
192     EXPECT_TRUE(std::find(primitives.begin(), primitives.end(), "elt") == primitives.end());
193     EXPECT_TRUE(std::find(exec_prim.begin(), exec_prim.end(), "elt") == exec_prim.end());
194         
195     auto output_prim = outputs.begin()->second.get_memory();
196
197     auto output_ptr = output_prim.pointer<float>();
198
199     std::vector<float> expected_output_vec = {
200         -3.f, 5.5f, 14.f, -15.f,
201         4.5f, 27.f, 10.f, -1.f
202     };
203
204     for (unsigned int i = 0; i < expected_output_vec.size(); i++)
205     {
206         EXPECT_FLOAT_EQ(expected_output_vec[i], output_ptr[i]);
207     }
208 }