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/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"
32 using namespace cldnn;
33 using namespace tests;
36 TEST(embed_gpu, seq3num4) {
56 const auto& engine = get_test_engine();
58 auto sequence_length = 3;
59 auto num_output_size = 4;
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 } });
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 });
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);
79 auto embed_test = embed("embed_prim", "input", "weights", "bias");
84 topology.add(embed_test);
86 network network(engine, topology);
87 network.set_input_data("input", input_prim);
89 auto outputs = network.execute();
90 EXPECT_EQ(outputs.size(), size_t(1));
91 EXPECT_EQ(outputs.begin()->first, "embed_prim");
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]);
102 TEST(embed_gpu, b2seq2num3) {
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
122 const auto& engine = get_test_engine();
124 auto sequence_length = 2;
125 auto num_output_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 } });
132 set_values(input_prim, { 0.0f, 1.0f, 2.0f, 0.0f });
133 set_values(weights_prim, { -1.0f, -2.0f, -3.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 });
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);
144 auto embed_test = embed("embed_prim", "input", "weights", "bias");
147 topology.add(w_data);
148 topology.add(b_data);
149 topology.add(embed_test);
151 network network(engine, topology);
152 network.set_input_data("input", input_prim);
154 auto outputs = network.execute();
155 EXPECT_EQ(outputs.size(), size_t(1));
156 EXPECT_EQ(outputs.begin()->first, "embed_prim");
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]);