Publishing 2019 R3 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / tests / test_cases / embed_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/memory.hpp"
20 #include <api/input_layout.hpp>
21 #include "api/embed.hpp"
22 #include <api/topology.hpp>
23 #include <api/tensor.hpp>
24 #include <api/network.hpp>
25 #include <api/engine.hpp>
26 #include <api/data.hpp>
27 #include "test_utils/test_utils.h"
28
29 #include <cmath>
30
31 using namespace cldnn;
32 using namespace tests;
33
34 TEST(embed_gpu, seq3num4) {
35     //  Input  : 1x1x1x3
36     //  Weights: 4x1x3x1
37     //  Bias   : 1x1x1x4
38     //  Output : 1x3x4x1
39     //  Input:
40     //   1.0    2.0   0.0
41     //
42     //  Weights:
43     //   1.0    1.0   1.0    1.0
44     //   2.0    2.0   2.0    2.0
45     //   3.0    3.0   3.0    3.0
46     //  Biases:
47     //   1.0    2.0   3.0    4.0
48     //
49     //  Output:
50     //   2.0    4.0   6.0    8.0
51     //   0.0    0.0   0.0    0.0
52     //   6.0    8.0  -2.0   -2.0
53
54     const auto& engine = get_test_engine();
55     auto batch = 1;
56     auto sequence_length = 3;
57     auto num_output_size = 4;
58     auto vocab_size = 3;
59     auto input_prim = memory::allocate(engine, { data_types::f32,format::bfyx,{ batch, 1, sequence_length, 1 } });
60     auto weights_prim = memory::allocate(engine, { data_types::f32,format::bfyx,{ num_output_size, 1, vocab_size, 1 } });
61     auto bias_prim = memory::allocate(engine, { data_types::f32,format::bfyx,{ batch, 1, 1, num_output_size } });
62     auto output_ref = memory::allocate(engine, { data_types::f32,format::bfyx,{ batch, sequence_length, num_output_size, 1 } });
63
64     set_values(input_prim, { 1.0f, 2.0f, 0.0f });
65     set_values(weights_prim, { 1.0f, 1.0f, 1.0f, 1.0f,
66         2.0f, 2.0f, 2.0f, 2.0f,
67         3.0f, 3.0f, 3.0f, 3.0f });
68     set_values(bias_prim, { 1.0f, 2.0f, 3.0f, 4.0f });
69     set_values(output_ref, { 3.0f, 4.0f, 5.0f, 6.0f,
70         4.0f, 5.0f, 6.0f, 7.0f,
71         2.0f, 3.0f, 4.0f, 5.0f });
72
73     auto input = input_layout("input", input_prim.get_layout());
74     auto w_data = data("weights", weights_prim);
75     auto b_data = data("bias", bias_prim);
76
77     auto embed_test = embed("embed_prim", "input", "weights", "bias");
78     topology topology;
79     topology.add(input);
80     topology.add(w_data);
81     topology.add(b_data);
82     topology.add(embed_test);
83
84     network network(engine, topology);
85     network.set_input_data("input", input_prim);
86
87     auto outputs = network.execute();
88     EXPECT_EQ(outputs.size(), size_t(1));
89     EXPECT_EQ(outputs.begin()->first, "embed_prim");
90
91     auto output_prim = outputs.begin()->second.get_memory();
92     auto ref = output_ref.pointer<float>();
93     auto output_ptr = output_prim.pointer<float>();
94     for (auto i = 0; i < batch * sequence_length * num_output_size; i++) {
95         EXPECT_EQ(ref[i], output_ptr[i]);
96     }
97
98 }
99
100 TEST(embed_gpu, b2seq2num3) {
101     //  Input  : 2x1x1x2
102     //  Weights: 3x1x3x1
103     //  Bias   : 1x1x1x4
104     //  Output : 1x3x4x1
105     //  Input:
106     //   0.0    1.0
107     //   2.0    0.0
108     //
109     //  Weights:
110     //  -1.0   -2.0  -3.0 
111     //  -1.0    2.0   0.0 
112     //   10.0   16.0  15.0 
113     //  Biases:
114     //   0.0    2.0   4.0
115     //
116     //  Output:
117     //   -1.0   0.0   1.0   -1.0   4.0   4.0
118     //    10.0  18.0  19.0  -1.0   0.0   1.0
119
120     const auto& engine = get_test_engine();
121     auto batch = 2;
122     auto sequence_length = 2;
123     auto num_output_size = 3;
124     auto vocab_size = 3;
125     auto input_prim = memory::allocate(engine, { data_types::f32,format::bfyx,{ batch, 1, sequence_length, 1 } });
126     auto weights_prim = memory::allocate(engine, { data_types::f32,format::bfyx,{ num_output_size, 1, vocab_size, 1 } });
127     auto bias_prim = memory::allocate(engine, { data_types::f32,format::bfyx,{ 1, 1, 1, num_output_size } });
128     auto output_ref = memory::allocate(engine, { data_types::f32,format::bfyx,{ batch, sequence_length, num_output_size, 1 } });
129
130     set_values(input_prim, { 0.0f, 1.0f, 2.0f, 0.0f });
131     set_values(weights_prim, { -1.0f, -2.0f, -3.0f,
132         -1.0f,  2.0f,  0.0f,
133         10.0f, 16.0f, 15.0f });
134     set_values(bias_prim, { 0.0f, 2.0f, 4.0f });
135     set_values(output_ref, { -1.0f, 0.0f, 1.0f, -1.0f, 4.0f, 4.0f,
136         10.0f, 18.0f, 19.0f, -1.0f, 0.0f, 1.0f });
137
138     auto input = input_layout("input", input_prim.get_layout());
139     auto w_data = data("weights", weights_prim);
140     auto b_data = data("bias", bias_prim);
141
142     auto embed_test = embed("embed_prim", "input", "weights", "bias");
143     topology topology;
144     topology.add(input);
145     topology.add(w_data);
146     topology.add(b_data);
147     topology.add(embed_test);
148
149     network network(engine, topology);
150     network.set_input_data("input", input_prim);
151
152     auto outputs = network.execute();
153     EXPECT_EQ(outputs.size(), size_t(1));
154     EXPECT_EQ(outputs.begin()->first, "embed_prim");
155
156     auto output_prim = outputs.begin()->second.get_memory();
157     auto ref = output_ref.pointer<float>();
158     auto output_ptr = output_prim.pointer<float>();
159     for (auto i = 0; i < batch * sequence_length * num_output_size; i++) {
160         EXPECT_EQ(ref[i], output_ptr[i]);
161     }
162
163 }
164