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/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"
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]);
88 TEST(convolution_grad_input_f32_fw_gpu, basic_wsiz2x2_in2x2x1x2_bfyx_stride2_pad1_output_size) {
106 const auto& engine = get_test_engine();
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 } });
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 });
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 })
120 network network(engine, topology);
121 network.set_input_data("input", input);
123 auto outputs = network.execute();
124 EXPECT_EQ(outputs.size(), size_t(1));
125 EXPECT_EQ(outputs.begin()->first, "deconv");
127 auto output_prim = outputs.begin()->second.get_memory();
129 auto output_ptr = output_prim.pointer<float>();
131 std::vector<float> expected_output_vec = {
132 -4.f, 3.5f, 12.f, -18.f,
133 -.5f, 21.f, 4.f, -8.f
136 for (unsigned int i = 0; i < expected_output_vec.size(); i++)
138 EXPECT_FLOAT_EQ(expected_output_vec[i], output_ptr[i]);
142 TEST(convolution_grad_input_f32_fw_gpu, DISABLED_basic_wsiz2x2_in2x2x1x2_bfyx_stride2_fusion) {
160 const auto& engine = get_test_engine();
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 } });
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 });
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")
182 build_options options;
183 options.set_option(build_option::optimize_data(true));
185 network network(engine, topology, options);
186 network.set_input_data("input", input);
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());
196 auto output_prim = outputs.begin()->second.get_memory();
198 auto output_ptr = output_prim.pointer<float>();
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
205 for (unsigned int i = 0; i < expected_output_vec.size(); i++)
207 EXPECT_FLOAT_EQ(expected_output_vec[i], output_ptr[i]);