1 //*****************************************************************************
2 // Copyright 2017-2020 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.
15 //*****************************************************************************
24 #include "gtest/gtest.h"
25 #include "ngraph/ngraph.hpp"
26 #include "ngraph/runtime/tensor.hpp"
27 #include "runtime/backend.hpp"
28 #include "util/all_close.hpp"
29 #include "util/all_close_f.hpp"
30 #include "util/ndarray.hpp"
31 #include "util/test_control.hpp"
32 #include "util/test_tools.hpp"
34 NGRAPH_SUPPRESS_DEPRECATED_START
37 using namespace ngraph;
39 static string s_manifest = "${MANIFEST}";
41 NGRAPH_TEST(${BACKEND_NAME}, one_hot_scalar_2_in_3)
44 auto A = make_shared<op::Parameter>(element::i32, shape_a);
46 auto r = make_shared<op::OneHot>(A, Shape{3}, 0);
47 auto f = make_shared<Function>(r, ParameterVector{A});
49 auto backend = runtime::Backend::create("${BACKEND_NAME}");
51 // Create some tensors for input/output
52 auto a = backend->create_tensor(element::i32, shape_a);
53 copy_data(a, vector<int32_t>{2});
54 auto result = backend->create_tensor(element::i32, shape_r);
56 auto handle = backend->compile(f);
57 handle->call_with_validate({result}, {a});
58 EXPECT_EQ((vector<int32_t>{0, 0, 1}), read_vector<int32_t>(result));
61 NGRAPH_TEST(${BACKEND_NAME}, one_hot_scalar_1_in_3)
64 auto A = make_shared<op::Parameter>(element::i32, shape_a);
66 auto r = make_shared<op::OneHot>(A, Shape{3}, 0);
67 auto f = make_shared<Function>(r, ParameterVector{A});
69 auto backend = runtime::Backend::create("${BACKEND_NAME}");
71 // Create some tensors for input/output
72 auto a = backend->create_tensor(element::i32, shape_a);
73 copy_data(a, vector<int32_t>{1});
74 auto result = backend->create_tensor(element::i32, shape_r);
76 auto handle = backend->compile(f);
77 handle->call_with_validate({result}, {a});
78 EXPECT_EQ((vector<int32_t>{0, 1, 0}), read_vector<int32_t>(result));
81 NGRAPH_TEST(${BACKEND_NAME}, one_hot_scalar_0_in_3)
84 auto A = make_shared<op::Parameter>(element::i32, shape_a);
86 auto r = make_shared<op::OneHot>(A, Shape{3}, 0);
87 auto f = make_shared<Function>(r, ParameterVector{A});
89 auto backend = runtime::Backend::create("${BACKEND_NAME}");
91 // Create some tensors for input/output
92 auto a = backend->create_tensor(element::i32, shape_a);
93 copy_data(a, vector<int32_t>{0});
94 auto result = backend->create_tensor(element::i32, shape_r);
96 auto handle = backend->compile(f);
97 handle->call_with_validate({result}, {a});
98 EXPECT_EQ((vector<int32_t>{1, 0, 0}), read_vector<int32_t>(result));
101 NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_0)
104 auto A = make_shared<op::Parameter>(element::i32, shape_a);
106 auto r = make_shared<op::OneHot>(A, Shape{3, 8}, 0);
107 auto f = make_shared<Function>(r, ParameterVector{A});
109 auto backend = runtime::Backend::create("${BACKEND_NAME}");
111 // Create some tensors for input/output
112 auto a = backend->create_tensor(element::i32, shape_a);
113 copy_data(a, vector<int32_t>{2, 1, 0, 0, 2, 2, 1, 0});
114 auto result = backend->create_tensor(element::i32, shape_r);
116 auto handle = backend->compile(f);
117 handle->call_with_validate({result}, {a});
119 (vector<int32_t>{0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0}),
120 read_vector<int32_t>(result));
123 NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_1)
126 auto A = make_shared<op::Parameter>(element::i32, shape_a);
128 auto r = make_shared<op::OneHot>(A, Shape{8, 3}, 1);
129 auto f = make_shared<Function>(r, ParameterVector{A});
131 auto backend = runtime::Backend::create("${BACKEND_NAME}");
133 // Create some tensors for input/output
134 auto a = backend->create_tensor(element::i32, shape_a);
135 copy_data(a, vector<int32_t>{2, 1, 0, 0, 2, 2, 1, 0});
136 auto result = backend->create_tensor(element::i32, shape_r);
138 auto handle = backend->compile(f);
139 handle->call_with_validate({result}, {a});
141 (vector<int32_t>{0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0}),
142 read_vector<int32_t>(result));
145 NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_1_barely_oob)
148 auto A = make_shared<op::Parameter>(element::i32, shape_a);
150 auto r = make_shared<op::OneHot>(A, Shape{8, 3}, 1);
151 auto f = make_shared<Function>(r, ParameterVector{A});
153 auto backend = runtime::Backend::create("${BACKEND_NAME}");
155 // Create some tensors for input/output
156 auto a = backend->create_tensor(element::i32, shape_a);
157 copy_data(a, vector<int32_t>{2, 1, 0, 0, 3, 2, 1, 0});
158 auto result = backend->create_tensor(element::i32, shape_r);
160 auto handle = backend->compile(f);
161 handle->call_with_validate({result}, {a});
162 vector<int32_t> rv = read_vector<int32_t>(result);
177 EXPECT_EQ(rv[10], 0);
178 EXPECT_EQ(rv[11], 0);
180 // These are undefined since value is out of bounds
181 // EXPECT_EQ(rv[12], 0);
182 // EXPECT_EQ(rv[13], 0);
183 // EXPECT_EQ(rv[14], 0);
185 EXPECT_EQ(rv[15], 0);
186 EXPECT_EQ(rv[16], 0);
187 EXPECT_EQ(rv[17], 1);
189 EXPECT_EQ(rv[18], 0);
190 EXPECT_EQ(rv[19], 1);
191 EXPECT_EQ(rv[20], 0);
193 EXPECT_EQ(rv[21], 1);
194 EXPECT_EQ(rv[22], 0);
195 EXPECT_EQ(rv[23], 0);
198 NGRAPH_TEST(${BACKEND_NAME}, one_hot_matrix_0)
201 auto A = make_shared<op::Parameter>(element::i32, shape_a);
202 Shape shape_r{3, 3, 3};
203 auto r = make_shared<op::OneHot>(A, Shape{3, 3, 3}, 0);
204 auto f = make_shared<Function>(r, ParameterVector{A});
206 auto backend = runtime::Backend::create("${BACKEND_NAME}");
208 // Create some tensors for input/output
209 auto a = backend->create_tensor(element::i32, shape_a);
212 0, 1, 1, 2, 1, 0, 0, 2, 1,
214 auto result = backend->create_tensor(element::i32, shape_r);
216 auto handle = backend->compile(f);
217 handle->call_with_validate({result}, {a});
218 EXPECT_EQ((vector<int32_t>{1, 0, 0, 0, 0, 1, 1, 0, 0,
220 0, 1, 1, 0, 1, 0, 0, 0, 1,
222 0, 0, 0, 1, 0, 0, 0, 1, 0}),
223 read_vector<int32_t>(result));
226 NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_many_categories)
228 // Imagenet has roughly 20,000 categories
229 uint32_t category_count = 20000;
231 auto A = make_shared<op::Parameter>(element::i32, shape_a);
232 Shape shape_r{6, category_count};
233 auto r = make_shared<op::OneHot>(A, Shape{6, category_count}, 1);
234 auto f = make_shared<Function>(r, ParameterVector{A});
236 auto backend = runtime::Backend::create("${BACKEND_NAME}");
238 // Create some tensors for input/output
239 auto a = backend->create_tensor(element::i32, shape_a);
240 vector<int32_t> input_data{0, 11, 101, 1001, 10001, static_cast<int32_t>(category_count - 1)};
241 copy_data(a, input_data);
242 auto result = backend->create_tensor(element::i32, shape_r);
244 auto handle = backend->compile(f);
245 handle->call_with_validate({result}, {a});
246 vector<int32_t> data = read_vector<int32_t>(result);
248 vector<int32_t> bit_positions;
249 for (size_t i = 0; i < shape_size(shape_r); ++i)
253 bit_positions.push_back(i % category_count);
256 EXPECT_EQ(bit_positions, input_data);