2 // Copyright (c) 2016 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
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"
30 using namespace cldnn;
31 using namespace tests;
33 TEST(convolution_grad_input_f32_fw_gpu, basic_wsiz2x2_in2x2x1x2_bfyx_stride2_pad1) {
51 const auto& engine = get_test_engine();
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 } });
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 });
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 })
65 network network(engine, topology);
66 network.set_input_data("input", input);
68 auto outputs = network.execute();
69 EXPECT_EQ(outputs.size(), size_t(1));
70 EXPECT_EQ(outputs.begin()->first, "deconv");
72 auto output_prim = outputs.begin()->second.get_memory();
74 auto output_ptr = output_prim.pointer<float>();
76 std::vector<float> expected_output_vec = {
77 -4.f, 3.5f, 12.f, -18.f,
81 for (unsigned int i = 0; i < expected_output_vec.size(); i++)
83 EXPECT_FLOAT_EQ(expected_output_vec[i], output_ptr[i]);
87 TEST(convolution_grad_input_f32_fw_gpu, basic_wsiz2x2_in2x2x1x2_bfyx_stride2_pad1_output_size) {
105 const auto& engine = get_test_engine();
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 } });
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 });
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 })
119 network network(engine, topology);
120 network.set_input_data("input", input);
122 auto outputs = network.execute();
123 EXPECT_EQ(outputs.size(), size_t(1));
124 EXPECT_EQ(outputs.begin()->first, "deconv");
126 auto output_prim = outputs.begin()->second.get_memory();
128 auto output_ptr = output_prim.pointer<float>();
130 std::vector<float> expected_output_vec = {
131 -4.f, 3.5f, 12.f, -18.f,
132 -.5f, 21.f, 4.f, -8.f
135 for (unsigned int i = 0; i < expected_output_vec.size(); i++)
137 EXPECT_FLOAT_EQ(expected_output_vec[i], output_ptr[i]);
141 TEST(convolution_grad_input_f32_fw_gpu, DISABLED_basic_wsiz2x2_in2x2x1x2_bfyx_stride2_fusion) {
159 const auto& engine = get_test_engine();
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 } });
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 });
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")
181 build_options options;
182 options.set_option(build_option::optimize_data(true));
184 network network(engine, topology, options);
185 network.set_input_data("input", input);
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());
195 auto output_prim = outputs.begin()->second.get_memory();
197 auto output_ptr = output_prim.pointer<float>();
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
204 for (unsigned int i = 0; i < expected_output_vec.size(); i++)
206 EXPECT_FLOAT_EQ(expected_output_vec[i], output_ptr[i]);