1 // Copyright (c) 2019 Intel Corporation
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 ///////////////////////////////////////////////////////////////////////////////////////////////////
16 #include <gtest/gtest.h>
18 #include <api/CPP/engine.hpp>
19 #include <api/CPP/input_layout.hpp>
20 #include <api/CPP/memory.hpp>
21 #include <api/CPP/one_hot.hpp>
22 #include <api/CPP/topology.hpp>
23 #include <api/CPP/network.hpp>
25 #include "test_utils/test_utils.h"
26 #include "test_utils/uniform_quantized_real_distribution.hpp"
30 using namespace cldnn;
31 using namespace ::tests;
34 VVVVF<T> one_hot_cpu(VVVVF<T> &input, uint16_t axis,
35 int32_t one_hot_limit, int input_padding_y = 0,
36 int input_padding_x = 0, int output_padding_y = 0,
37 int output_padding_x = 0) {
39 size_t padding_y = input_padding_y + output_padding_y;
40 size_t padding_x = input_padding_x + output_padding_x;
42 out_sizes[0] = input.size();
43 out_sizes[1] = input[0].size();
44 out_sizes[2] = input[0][0].size() + 2 * padding_y;
45 out_sizes[3] = input[0][0][0].size() + 2 * padding_x;
46 for (uint16_t i = 0; i < axis; ++i)
47 out_sizes[i] = out_sizes[i + 1];
48 out_sizes[axis] = one_hot_limit;
49 VVVVF<T> output(out_sizes[0], VVVF<T>(out_sizes[1], VVF<T>(out_sizes[2], VF<T>(out_sizes[3]))));
53 for (size_t b = 0; b < out_sizes[0]; ++b)
54 for (size_t f = 0; f < out_sizes[1]; ++f)
55 for (size_t y = 0; y < out_sizes[2]; ++y)
56 for (size_t x = 0; x < out_sizes[3]; ++x)
57 output[b][f][y][x] = input[0][f][y][x] == (T)b ? 1 : 0;
60 for (size_t b = 0; b < out_sizes[0]; ++b)
61 for (size_t f = 0; f < out_sizes[1]; ++f)
62 for (size_t y = 0; y < out_sizes[2]; ++y)
63 for (size_t x = 0; x < out_sizes[3]; ++x)
64 output[b][f][y][x] = input[0][b][y][x] == (T)f ? 1 : 0;
67 for (size_t b = 0; b < out_sizes[0]; ++b)
68 for (size_t f = 0; f < out_sizes[1]; ++f)
69 for (size_t y = 0; y < out_sizes[2]; ++y)
70 for (size_t x = 0; x < out_sizes[3]; ++x)
71 output[b][f][y][x] = input[0][b][f][x] == (T)y ? 1 : 0;
74 for (size_t b = 0; b < out_sizes[0]; ++b)
75 for (size_t f = 0; f < out_sizes[1]; ++f)
76 for (size_t y = 0; y < out_sizes[2]; ++y)
77 for (size_t x = 0; x < out_sizes[3]; ++x)
78 output[b][f][y][x] = input[0][b][f][y] == (T)x ? 1 : 0;
86 void generic_one_hot_test_int(cldnn::format test_input_fmt, int input_b, int input_f, int input_y, int input_x, tensor shape,
87 uint16_t one_hot_axis, int input_padding_y = 0, int input_padding_x = 0, int output_padding_y = 0, int output_padding_x = 0) {
88 std::vector<tensor::value_type> output_dims = { shape.batch[0], shape.feature[0],
89 shape.spatial[1], shape.spatial[0] };
90 int32_t one_hot_limit = output_dims[one_hot_axis];
92 int min_random = -2, max_random = one_hot_limit + 2;
93 VVVVF<T> input_rnd = generate_random_4d<T>(input_b, input_f, input_y, input_x, min_random, max_random);
94 VF<T> input_rnd_vec = flatten_4d<T>(test_input_fmt, input_rnd);
96 const auto& engine = get_test_engine();
97 tensor input_tensor(input_b, input_f, input_x, input_y);
98 auto input = memory::allocate(engine, { type_to_data_type<T>::value, test_input_fmt, input_tensor });
99 set_values(input, input_rnd_vec);
102 topology.add(input_layout("input", input.get_layout()));
103 topology.add(one_hot("output", "input", shape, one_hot_axis));
105 network network(engine, topology);
106 network.set_input_data("input", input);
107 auto outputs = network.execute();
108 EXPECT_EQ(outputs.size(), size_t(1));
109 EXPECT_EQ(outputs.begin()->first, "output");
111 auto output_memory = outputs.at("output").get_memory();
112 auto output_layout = output_memory.get_layout();
113 auto output_ptr = output_memory.pointer<T>();
115 VVVVF<T> output_cpu = one_hot_cpu<T>(input_rnd, one_hot_axis, one_hot_limit, input_padding_y, input_padding_x, output_padding_y, output_padding_x);
116 EXPECT_EQ(output_layout.format.value, test_input_fmt.value);
117 tensor output_tensor = output_layout.get_buffer_size();
118 int y_size = output_tensor.spatial[1];
119 int x_size = output_tensor.spatial[0];
120 int f_size = output_tensor.feature[0];
121 int b_size = output_tensor.batch[0];
122 EXPECT_EQ(y_size, (int)output_cpu[0][0].size());
123 EXPECT_EQ(x_size, (int)output_cpu[0][0][0].size());
124 EXPECT_EQ(f_size, (int)output_cpu[0].size());
125 EXPECT_EQ(b_size, (int)output_cpu.size());
128 bool test_is_correct = true;
129 VF<T> output_cpu_vec = flatten_4d<T>(test_input_fmt, output_cpu);
131 for (size_t i = 0; i < output_cpu_vec.size(); ++i) {
132 if (output_cpu_vec[i] != output_ptr[i]) {
133 test_is_correct = false;
137 EXPECT_EQ(test_is_correct, true) << std::endl
138 << "failing test parameters:" << std::endl
139 << "input_b = " << input_b << std::endl
140 << "input_f = " << input_f << std::endl
141 << "input_y = " << input_y << std::endl
142 << "input_x = " << input_x << std::endl
143 << "one_hot_limit = " << one_hot_limit << std::endl
144 << "one_hot_axis = " << one_hot_axis << std::endl
145 << "input_padding_y = " << input_padding_y << std::endl
146 << "input_padding_x = " << input_padding_x << std::endl
147 << "output_padding_y = " << output_padding_y << std::endl
148 << "output_padding_x = " << output_padding_x << std::endl;
151 TEST(one_hot_gpu_i32, generic_y_in10_oh5) {
152 generic_one_hot_test_int<int32_t>(format::bfyx, 1, 10, 10, 10, tensor(10, 10, 10, 5), 2);
156 TEST(one_hot_error, basic_error_wrong_batch_size) {
158 const auto& engine = get_test_engine();
159 auto input = memory::allocate(engine, { data_types::i32, format::bfyx, { 10, 1, 1, 1 } });
162 topology.add(input_layout("input", input.get_layout()));
163 topology.add(one_hot("output", "input", tensor(10, 1, 1, 50), 2));
165 std::string msg_to_find = "Incorrect parameters configuration: input batch size should be equal to 1.";
166 EXPECT_ANY_THROW(check_exception_massage(engine, topology, msg_to_find));
169 TEST(one_hot_error, basic_error_wrong_axis) {
171 const auto& engine = get_test_engine();
172 auto input = memory::allocate(engine, { data_types::i32, format::bfyx,{ 1, 1, 1, 1 } });
175 topology.add(input_layout("input", input.get_layout()));
176 topology.add(one_hot("output", "input", tensor(1, 1, 1, 50), 4));
178 std::string msg_to_find = "Incorrect parameters configuration: one_hot_axis should be less or equal to 3.";
179 EXPECT_ANY_THROW(check_exception_massage(engine, topology, msg_to_find));
182 TEST(one_hot_error, basic_error_bad_shape) {
184 const auto& engine = get_test_engine();
185 auto input = memory::allocate(engine, { data_types::i32, format::bfyx,{ 1, 1, 1, 1 } });
188 topology.add(input_layout("input", input.get_layout()));
189 topology.add(one_hot("output", "input", tensor(1, 5, 1, 50), 2));
191 std::string msg_to_find = "Incorrect parameters configuration: shape does not fit input size.";
192 EXPECT_ANY_THROW(check_exception_massage(engine, topology, msg_to_find));