Publishing 2019 R1 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/CPP/memory.hpp"
21 #include <api/CPP/input_layout.hpp>
22 #include "api/CPP/convolution_grad_input.hpp"
23 #include <api/CPP/data.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/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
88 TEST(convolution_grad_input_f32_fw_gpu, basic_wsiz2x2_in2x2x1x2_bfyx_stride2_pad1_output_size) {
89     //  Filter : 2x2
90     //  Input  : 2x2x1x2
91     //  Output : 2x2x1x2
92     //  Stride : 2x2
93     //
94     //  Input:
95     //  8  0.5    1   3
96     //  6  9      2   4
97     //
98     //  Filter
99     //  -2   2
100     //   7  -0.5
101     //
102     //  Output:
103     //  -4    3.5    -0.5   21
104     //   12  -18      4     -9
105
106     const auto& engine = get_test_engine();
107
108     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 1, 2, 2 } });
109     auto weights = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 2, 2 } });
110
111     set_values(input, { 8.f, 0.5f, 6.f, 9.f, 1.f, 3.f, 2.f, 4.f });
112     set_values(weights, { -2.f, 2.f, 7.f, -0.5f });
113
114     topology topology(
115         input_layout("input", input.get_layout()),
116         data("weights", weights),
117         convolution_grad_input("deconv", "input", { "weights" }, { 1, 1, 2, 2 }, { 0, 0, -1, -1 }, { 2, 1, 2, 2 })
118     );
119
120     network network(engine, topology);
121     network.set_input_data("input", input);
122
123     auto outputs = network.execute();
124     EXPECT_EQ(outputs.size(), size_t(1));
125     EXPECT_EQ(outputs.begin()->first, "deconv");
126
127     auto output_prim = outputs.begin()->second.get_memory();
128
129     auto output_ptr = output_prim.pointer<float>();
130
131     std::vector<float> expected_output_vec = {
132         -4.f, 3.5f, 12.f, -18.f,
133         -.5f, 21.f, 4.f, -8.f
134     };
135
136     for (unsigned int i = 0; i < expected_output_vec.size(); i++)
137     {
138         EXPECT_FLOAT_EQ(expected_output_vec[i], output_ptr[i]);
139     }
140 }
141
142 TEST(convolution_grad_input_f32_fw_gpu, DISABLED_basic_wsiz2x2_in2x2x1x2_bfyx_stride2_fusion) {
143     //  Filter : 2x2
144     //  Input  : 2x2x1x2
145     //  Output : 2x2x1x2
146     //  Stride : 2x2
147     //
148     //  Input:
149     //  8  0.5    1   3
150     //  6  9      2   4
151     //
152     //  Filter
153     //  -2   2
154     //   7  -0.5
155     //
156     //  Output:
157     //  -4    3.5    -0.5   21
158     //   12  -18      4     -9
159
160     const auto& engine = get_test_engine();
161
162     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 1, 2, 2 } });
163     auto weights = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 2, 2 } });
164     auto scale_in = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 1, 1 } });
165     auto elt_data = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 2, 1, 2 } });
166
167     set_values(input, { 8.f, 0.5f, 6.f, 9.f, 1.f, 3.f, 2.f, 4.f });
168     set_values(weights, { -2.f, 2.f, 7.f, -0.5f });
169     set_values(scale_in, { 1.0f });
170     set_values(elt_data, { 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f });
171
172     topology topology(
173         input_layout("input", input.get_layout()),
174         data("weights", weights),
175         data("scale_in", scale_in),
176         data("elt_data", elt_data),
177         convolution_grad_input("conv", "input", { "weights" }, { 1, 1, 2, 2 }, { 0, 0, -1, -1 }),
178         eltwise("elt", "conv", "elt_data", eltwise_mode::sum),
179         scale("scale", "elt", "scale_in")
180     );
181
182     build_options options;
183     options.set_option(build_option::optimize_data(true));
184        
185     network network(engine, topology, options);
186     network.set_input_data("input", input);
187
188     auto outputs = network.execute();
189     auto primitives = network.get_all_primitive_ids();
190     auto exec_prim = network.get_executed_primitive_ids();
191     EXPECT_EQ(outputs.size(), size_t(1));
192     EXPECT_EQ(outputs.begin()->first, "scale");
193     EXPECT_TRUE(std::find(primitives.begin(), primitives.end(), "elt") == primitives.end());
194     EXPECT_TRUE(std::find(exec_prim.begin(), exec_prim.end(), "elt") == exec_prim.end());
195         
196     auto output_prim = outputs.begin()->second.get_memory();
197
198     auto output_ptr = output_prim.pointer<float>();
199
200     std::vector<float> expected_output_vec = {
201         -3.f, 5.5f, 14.f, -15.f,
202         4.5f, 27.f, 10.f, -1.f
203     };
204
205     for (unsigned int i = 0; i < expected_output_vec.size(); i++)
206     {
207         EXPECT_FLOAT_EQ(expected_output_vec[i], output_ptr[i]);
208     }
209 }