Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / tests / test_cases / softmax_loss_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
19 #include <gtest/gtest.h>
20 #include "api/CPP/memory.hpp"
21 #include <api/CPP/input_layout.hpp>
22 #include "api/CPP/softmax_loss_grad.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(softmax_loss_grad_f32_fw_gpu, basic1) {
33
34     const auto& engine = get_test_engine();
35
36     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 1, 4, 1 } });
37     auto labels = memory::allocate(engine, { data_types::f32, format::bfyx,{ 2, 1, 1, 1 } });
38
39     set_values(input, { 8.f, 0.5f, 6.f, 9.f, 1.f, 3.f, 2.f, 4.f });
40     set_values(labels, { 1.f, 3.f });
41
42     topology topology(
43         input_layout("input", input.get_layout()),
44         data("labels", labels),
45         softmax_loss_grad("softmax_loss_grad", "input", "labels")
46     );
47
48     network network(engine, topology);
49     network.set_input_data("input", input);
50
51     auto outputs = network.execute();
52     EXPECT_EQ(outputs.size(), size_t(1));
53     EXPECT_EQ(outputs.begin()->first, "softmax_loss_grad");
54
55     auto output_prim = outputs.begin()->second.get_memory();
56
57     auto output_ptr = output_prim.pointer<float>();
58
59     std::vector<float> expected_output_vec = { 8.f, -0.5f, 6.f, 9.f, 1.f, 3.f, 2.f, 3.f };
60
61     for (unsigned int i = 0; i < expected_output_vec.size(); i++)
62     {
63         EXPECT_FLOAT_EQ(expected_output_vec[i], output_ptr[i]);
64     }
65 }