2 // Copyright (c) 2018 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 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #include <gtest/gtest.h>
19 #include "api/CPP/memory.hpp"
20 #include <api/CPP/data.hpp>
21 #include <api/CPP/input_layout.hpp>
22 #include <api/CPP/mutable_data.hpp>
23 #include "api/CPP/scale_grad_weights.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"
31 using namespace cldnn;
32 using namespace tests;
34 TEST(scale_grad_weights_gpu, basic_in2x3x2x2) {
40 // f0: b0: 1 2 -10 b1: 0 0 -11
41 // f0: b0: 3 4 -14 b1: 0.5 -0.5 -15
42 // f1: b0: 5 6 -12 b1: 1.5 5.2 -13
43 // f1: b0: 7 8 -16 b1: 12 8 -17
46 // f0: b0: 1 2 3 b1: 0 0 -11
47 // f0: b0: 4 5 -6 b1: 0.5 -0.5 -15
48 // f1: b0: -7 8 -9 b1: 1.5 5.2 -13
49 // f1: b0: 12 11 10 b1: 12 8 -17
57 auto input = memory::allocate(engine, { data_types::f32, format::bfyx, { 2, 2, 3, 2 } });
58 auto grad_input = memory::allocate(engine, { data_types::f32, format::bfyx, { 2, 2, 3, 2 } });
59 auto scale_input = memory::allocate(engine, { data_types::f32, format::bfyx, { 1, 2, 1, 1 } });
62 topology.add(input_layout("input", input.get_layout()));
63 topology.add(mutable_data("scale_input", scale_input));
64 topology.add(data("grad_input", grad_input));
65 topology.add(scale_grad_weights("scale_grad", "input", "grad_input", "scale_input"));
67 std::vector<float> input_vec = {
77 set_values(input, input_vec);
79 std::vector<float> grad_vec = {
89 set_values(grad_input, grad_vec);
91 std::vector<float> scale_input_vec = {
94 set_values(scale_input, scale_input_vec);
96 build_options options;
97 network network(engine, topology);
99 network.set_learning_rate(0.0001f);
100 network.set_input_data("input", input);
102 std::vector<float> expected_out = {
106 auto outputs = network.execute();
108 auto output_ptr = scale_input.pointer<float>();
110 for (unsigned int i = 0; i < expected_out.size(); ++i) {
111 EXPECT_NEAR(output_ptr[i], expected_out[i], 1e-04F);
115 TEST(scale_grad_weights_gpu, basic_in2x3x2x2_bias) {
121 // f0: b0: 1 2 -10 b1: 0 0 -11
122 // f0: b0: 3 4 -14 b1: 0.5 -0.5 -15
123 // f1: b0: 5 6 -12 b1: 1.5 5.2 -13
124 // f1: b0: 7 8 -16 b1: 12 8 -17
127 // f0: b0: 1 2 3 b1: 0 0 -11
128 // f0: b0: 4 5 -6 b1: 0.5 -0.5 -15
129 // f1: b0: -7 8 -9 b1: 1.5 5.2 -13
130 // f1: b0: 12 11 10 b1: 12 8 -17
142 auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 2, 3, 2 } });
143 auto grad_input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 2, 3, 2 } });
144 auto scale_input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 2, 1, 1 } });
145 auto bias = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 2, 1, 1 } });
148 topology.add(input_layout("input", input.get_layout()));
149 topology.add(mutable_data("scale_input", scale_input));
150 topology.add(data("grad_input", grad_input));
151 topology.add(mutable_data("bias", bias));
152 topology.add(scale_grad_weights("scale_grad", "input", "grad_input", "scale_input", "bias", ""));
154 std::vector<float> input_vec = {
164 set_values(input, input_vec);
166 std::vector<float> grad_vec = {
176 set_values(grad_input, grad_vec);
178 std::vector<float> scale_input_vec = {
181 set_values(scale_input, scale_input_vec);
183 std::vector<float> bias_vec = {
186 set_values(bias, bias_vec);
188 build_options options;
189 network network(engine, topology);
191 network.set_learning_rate(0.0001f);
192 network.set_input_data("input", input);
194 std::vector<float> expected_scale = {
198 std::vector<float> expected_bias = {
202 auto outputs = network.execute();
204 auto scale_ptr = scale_input.pointer<float>();
205 auto bias_ptr = bias.pointer<float>();
207 for (unsigned int i = 0; i < expected_scale.size(); ++i) {
208 EXPECT_NEAR(scale_ptr[i], expected_scale[i], 1e-04F);
210 for (unsigned int i = 0; i < expected_bias.size(); ++i) {
211 EXPECT_NEAR(bias_ptr[i], expected_bias[i], 1e-04F);
215 TEST(scale_grad_weights_gpu, basic_in2x3x2x2_bias_momentum) {
221 // f0: b0: 1 2 -10 b1: 0 0 -11
222 // f0: b0: 3 4 -14 b1: 0.5 -0.5 -15
223 // f1: b0: 5 6 -12 b1: 1.5 5.2 -13
224 // f1: b0: 7 8 -16 b1: 12 8 -17
227 // f0: b0: 1 2 3 b1: 0 0 -11
228 // f0: b0: 4 5 -6 b1: 0.5 -0.5 -15
229 // f1: b0: -7 8 -9 b1: 1.5 5.2 -13
230 // f1: b0: 12 11 10 b1: 12 8 -17
242 auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 2, 3, 2 } });
243 auto grad_input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 2, 3, 2 } });
244 auto scale_input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 2, 1, 1 } });
245 auto bias = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 2, 1, 1 } });
246 auto prev_scale = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 2, 1, 1 } });
247 auto prev_bias = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 2, 1, 1 } });
250 topology.add(input_layout("input", input.get_layout()));
251 topology.add(mutable_data("scale_input", scale_input));
252 topology.add(data("grad_input", grad_input));
253 topology.add(mutable_data("bias", bias));
254 topology.add(mutable_data("prev_scale", prev_scale));
255 topology.add(mutable_data("prev_bias", prev_bias));
256 topology.add(scale_grad_weights("scale_grad", "input", "grad_input", "scale_input", "bias", "prev_scale", "prev_bias"));
258 std::vector<float> input_vec = {
268 set_values(input, input_vec);
270 std::vector<float> grad_vec = {
280 set_values(grad_input, grad_vec);
282 std::vector<float> scale_input_vec = {
285 set_values(scale_input, scale_input_vec);
287 std::vector<float> bias_vec = {
290 set_values(bias, bias_vec);
292 build_options options;
293 network network(engine, topology);
295 network.set_learning_rate(0.0001f);
296 network.set_input_data("input", input);
298 std::vector<float> expected_scale = {
302 std::vector<float> expected_bias = {
306 auto outputs = network.execute();
308 auto scale_ptr = scale_input.pointer<float>();
309 auto bias_ptr = bias.pointer<float>();
310 auto mom_scale_ptr = prev_scale.pointer<float>();
311 auto mom_bias_ptr = prev_bias.pointer<float>();
313 for (unsigned int i = 0; i < expected_scale.size(); ++i) {
314 EXPECT_NEAR(scale_ptr[i], expected_scale[i], 1e-04F);
316 for (unsigned int i = 0; i < expected_bias.size(); ++i) {
317 EXPECT_NEAR(bias_ptr[i], expected_bias[i], 1e-04F);
319 for (unsigned int i = 0; i < mom_scale_ptr.size(); ++i) {
320 EXPECT_NEAR(mom_scale_ptr[i], scale_input_vec[i] - expected_scale[i], 1e-04F);
322 for (unsigned int i = 0; i < mom_bias_ptr.size(); ++i) {
323 EXPECT_NEAR(mom_bias_ptr[i], bias_vec[i] - expected_bias[i], 1e-04F);