Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / tests / test_cases / fully_connected_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/fully_connected_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
29 using namespace cldnn;
30 using namespace tests;
31
32 TEST(fully_connected_grad_input_gpu, basic_bfyx) {
33     //  Filter : 2x2
34     //  Input  : 2x2x1x2
35     //  Output : 2x2x1x2
36     //  Stride : 2x2
37     //
38     //  Input:
39     //  -0.5     2    0.5
40     //
41     //  Input_grad:
42     //   1.5   0.75  -2.25  3
43     //
44     //  Weights:
45     //   1.5     1    0.5
46     //  -1       0    0.5
47     //   0.5    -0.5 -2
48     //  -0.5     1    1.5
49     //
50     //  Output:
51     //  -1.125  5.625   10.125
52
53
54     const auto& engine = get_test_engine();
55
56     auto input_grad = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 4, 1 } });
57     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 3, 1 } });
58     auto weights = memory::allocate(engine, { data_types::f32, format::bfyx,{ 4, 1, 3, 1 } });
59
60     set_values(input, { -0.5f, 2.0f, 0.5f });
61     set_values(input_grad, { 1.5f, 0.75f, -2.25f, 3.0f });
62     set_values(weights, { 1.5f, 1.0f, 0.5f, -1.0f, 0.0f, 0.5f, 0.5f, -0.5f, -2.0f, -0.5f, 1.0f, 1.5f });
63
64     topology topology(
65         input_layout("input_grad", input_grad.get_layout()),
66         data("input", input),
67         data("weights", weights),
68         fully_connected_grad_input("fully_connected_grad_input", "input_grad", "input", { "weights" })
69     );
70
71     network network(engine, topology);
72     network.set_input_data("input_grad", input_grad);
73
74     auto outputs = network.execute();
75     EXPECT_EQ(outputs.size(), size_t(1));
76     EXPECT_EQ(outputs.begin()->first, "fully_connected_grad_input");
77
78     auto output_prim = outputs.begin()->second.get_memory();
79
80     auto output_ptr = output_prim.pointer<float>();
81
82     std::vector<float> expected_output_vec = {
83         -1.125f, 5.625f, 10.125f
84     };
85
86     for (unsigned int i = 0; i < expected_output_vec.size(); i++)
87     {
88         EXPECT_FLOAT_EQ(expected_output_vec[i], output_ptr[i]);
89     }
90 }