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