Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / tests / test_cases / batch_norm_grad_gpu_test.cpp
1 /*
2 // Copyright (c) 2018 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 #include <gtest/gtest.h>
19 #include "api/CPP/memory.hpp"
20 #include <api/CPP/input_layout.hpp>
21 #include "api/CPP/batch_norm_grad.hpp"
22 #include <api/CPP/topology.hpp>
23 #include <api/CPP/network.hpp>
24 #include <api/CPP/engine.hpp>
25 #include "test_utils/test_utils.h"
26 #include <api/CPP/reorder.hpp>
27 #include <api/CPP/data.hpp>
28
29 using namespace cldnn;
30 using namespace tests;
31
32 TEST(batch_normalization_backward_gpu, basic_in2x2x2x3) {
33     //  Grad input  : 2x2x2x3
34     //  Input : 2x2x2x3
35     //  Inverted variance : 1x2x1x1
36     //  Output : 2x2x2x3
37
38     //  Input:
39     //  f0: b0:  1    2  -10   b1:   0    0     -11
40     //  f0: b0:  3    4  -14   b1:   0.5 -0.5   -15  
41     //  f1: b0:  5    6  -12   b1:   1.5  5.2   -13     
42     //  f1: b0:  7    8  -16   b1:   12   9     -17
43     //
44     //  Grad input
45     //  f0: b0:  1    2  3   b1:  -1    -2     -3
46     //  f0: b0:  5    6  7   b1:   0.5  -0.5   -4  
47     //  f1: b0:  8    9  10  b1:   1.5   5     -5     
48     //  f1: b0: 11   12  13  b1:   2    -7.2   -6
49     //
50     //  Inverted variance
51     //  f0: 0.1491862
52     //  f1: 0.0966454
53
54     const auto& engine = get_test_engine();
55
56     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 2, 3, 2 } });
57     auto grad_input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 2, 3, 2 } });
58     auto inv_var = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 2, 1, 1 } });
59
60     topology topology;
61     topology.add(input_layout("input", input.get_layout()));
62     topology.add(data("grad_input", grad_input));
63     topology.add(data("inv_var", inv_var));
64     topology.add(batch_norm_grad("batch_norm_grad", "grad_input", "input", "inv_var"));
65
66     set_values(input, {
67         1.f, 2.f, -10.f,
68         3.f, 4.f, -14.f, 
69         5.f, 6.f, -12.f, 
70         7.f, 8.f, -16.f,
71         0.f, 0.f, -11.f, 
72         0.5f, -0.5f, -15.f, 
73         1.5f, 5.2f, -13.f, 
74         12.f, 9.f, -17.f
75     });
76
77     set_values(grad_input, {
78         1.f, 2.f, 3.f,
79         5.f, 6.f, 7.f,
80         8.f, 9.f, 10.f,
81         11.f, 12.f, 13.f,
82         -1.f, -2.f, -3.f,
83         0.5f, -0.5f, -4.f,
84         1.5f, 5.f, -5.f,
85         2.f, -7.2f, -6.f
86     });
87
88     set_values(inv_var, { 0.1491862f, 0.0966454f });
89
90     network network(engine, topology);
91
92     network.set_input_data("input", input);
93
94     auto outputs = network.execute();
95
96     auto output = outputs.at("batch_norm_grad").get_memory();
97     auto output_ptr = output.pointer<float>();
98
99     std::vector<float> expected_out = {
100         -0.142969f, -0.111888f, 1.45456f, 
101          0.217566f, 0.248648f, 2.52372f,
102         -3.41923f, -4.07521f, 5.f,
103         -4.63455f, -5.f, 5.f,
104         -0.323237f, -0.472423f, 0.677543f, 
105         -0.15851f, -0.189591f, 1.00078f,
106         -1.41324f, -3.85969f, 5.f,
107         -5.f, -5.f, 5.f
108     };
109
110     for (int i = 0; i < 2 * 2 * 3 * 2; i++)
111     {    
112         EXPECT_NEAR(expected_out[i], output_ptr[i], 1e-03F);
113     }
114 }