Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / one_op_tests / OneHot.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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 #include "GenModelTest.h"
18
19 #include <memory>
20
21 TEST_F(GenModelTest, OneOp_OneHot_OffValueToConst)
22 {
23   CircleGen cgen;
24   std::vector<int32_t> depth_data{3};
25   uint32_t depth_buf = cgen.addBuffer(depth_data);
26   std::vector<float> off_value_data{0};
27   uint32_t off_value_buf = cgen.addBuffer(off_value_data);
28   int indices = cgen.addTensor({{1, 2, 2}, circle::TensorType::TensorType_INT32});
29   int depth = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32, depth_buf});
30   int on_value = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32});
31   int off_value = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32, off_value_buf});
32   int axis = 2;
33   int out = cgen.addTensor({{1, 2, 3, 2}, circle::TensorType::TensorType_FLOAT32});
34   cgen.addOperatorOneHot({{indices, depth, on_value, off_value}, {out}}, axis);
35   cgen.setInputsAndOutputs({indices, on_value}, {out});
36
37   _context = std::make_unique<GenModelTestContext>(cgen.finish());
38   _context->addTestCase(TestCaseData{}
39                           .addInput<int32_t>({1, 2, 0, 2})
40                           .addInput<float>({1})
41                           .addOutput<float>({0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1}));
42   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
43
44   SUCCEED();
45 }
46
47 TEST_F(GenModelTest, OneOp_OneHot_OffValueToNotZero)
48 {
49   CircleGen cgen;
50   std::vector<int32_t> depth_data{3};
51   uint32_t depth_buf = cgen.addBuffer(depth_data);
52   int indices = cgen.addTensor({{1, 2, 2}, circle::TensorType::TensorType_INT32});
53   int depth = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32, depth_buf});
54   int on_value = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32});
55   int off_value = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32});
56   int axis = 2;
57   int out = cgen.addTensor({{1, 2, 3, 2}, circle::TensorType::TensorType_FLOAT32});
58   cgen.addOperatorOneHot({{indices, depth, on_value, off_value}, {out}}, axis);
59   cgen.setInputsAndOutputs({indices, on_value, off_value}, {out});
60
61   _context = std::make_unique<GenModelTestContext>(cgen.finish());
62   _context->addTestCase(TestCaseData{}
63                           .addInput<int32_t>({1, 2, 0, 2})
64                           .addInput<float>({1})
65                           .addInput<float>({-1})
66                           .addOutput<float>({-1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1}));
67   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
68
69   SUCCEED();
70 }
71
72 TEST_F(GenModelTest, OneOp_OneHot_IndicesValueToNeg_OffValueToConst)
73 {
74   CircleGen cgen;
75   std::vector<int32_t> depth_data{3};
76   uint32_t depth_buf = cgen.addBuffer(depth_data);
77   std::vector<float> off_value_data{0};
78   uint32_t off_value_buf = cgen.addBuffer(off_value_data);
79   int indices = cgen.addTensor({{2, 2}, circle::TensorType::TensorType_INT32});
80   int depth = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32, depth_buf});
81   int on_value = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32});
82   int off_value = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32, off_value_buf});
83   int axis = 2;
84   int out = cgen.addTensor({{2, 2, 3}, circle::TensorType::TensorType_FLOAT32});
85   cgen.addOperatorOneHot({{indices, depth, on_value, off_value}, {out}}, axis);
86   cgen.setInputsAndOutputs({indices, on_value}, {out});
87
88   _context = std::make_unique<GenModelTestContext>(cgen.finish());
89   _context->addTestCase(TestCaseData{}
90                           .addInput<int32_t>({1, 2, 0, -1})
91                           .addInput<float>({1})
92                           .addOutput<float>({0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0}));
93   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
94
95   SUCCEED();
96 }
97
98 TEST_F(GenModelTest, OneOp_OneHot_IndicesValueToNeg_OffValueToVar)
99 {
100   CircleGen cgen;
101   std::vector<int32_t> depth_data{3};
102   uint32_t depth_buf = cgen.addBuffer(depth_data);
103   int indices = cgen.addTensor({{2, 2}, circle::TensorType::TensorType_INT32});
104   int depth = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32, depth_buf});
105   int on_value = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32});
106   int off_value = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32});
107   int axis = 2;
108   int out = cgen.addTensor({{2, 2, 3}, circle::TensorType::TensorType_FLOAT32});
109   cgen.addOperatorOneHot({{indices, depth, on_value, off_value}, {out}}, axis);
110   cgen.setInputsAndOutputs({indices, on_value, off_value}, {out});
111
112   _context = std::make_unique<GenModelTestContext>(cgen.finish());
113   _context->addTestCase(TestCaseData{}
114                           .addInput<int32_t>({1, 2, 0, -1})
115                           .addInput<float>({1})
116                           .addInput<float>({0})
117                           .addOutput<float>({0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0}));
118   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
119
120   SUCCEED();
121 }
122
123 TEST_F(GenModelTest, neg_OneOp_OneHot_OneOperand)
124 {
125   CircleGen cgen;
126   int indices = cgen.addTensor({{1, 2, 2}, circle::TensorType::TensorType_INT32});
127   int axis = 2;
128   int out = cgen.addTensor({{1, 2, 3, 2}, circle::TensorType::TensorType_FLOAT32});
129   cgen.addOperatorOneHot({{indices}, {out}}, axis);
130   cgen.setInputsAndOutputs({indices}, {out});
131
132   _context = std::make_unique<GenModelTestContext>(cgen.finish());
133   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
134   _context->expectFailModelLoad();
135
136   SUCCEED();
137 }
138
139 TEST_F(GenModelTest, neg_OneOp_OneHot_TwoOperands)
140 {
141   CircleGen cgen;
142   std::vector<int> depth_data{3};
143   uint32_t depth_buf = cgen.addBuffer(depth_data);
144   int indices = cgen.addTensor({{1, 2, 2}, circle::TensorType::TensorType_INT32});
145   int depth = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32, depth_buf});
146   int axis = 2;
147   int out = cgen.addTensor({{1, 2, 3, 2}, circle::TensorType::TensorType_FLOAT32});
148   cgen.addOperatorOneHot({{indices, depth}, {out}}, axis);
149   cgen.setInputsAndOutputs({indices}, {out});
150
151   _context = std::make_unique<GenModelTestContext>(cgen.finish());
152   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
153   _context->expectFailModelLoad();
154
155   SUCCEED();
156 }
157
158 TEST_F(GenModelTest, neg_OneOp_OneHot_ThreeOperands)
159 {
160   CircleGen cgen;
161   std::vector<int> depth_data{3};
162   uint32_t depth_buf = cgen.addBuffer(depth_data);
163   int indices = cgen.addTensor({{1, 2, 2}, circle::TensorType::TensorType_INT32});
164   int depth = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32, depth_buf});
165   int on_value = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32});
166   int axis = 2;
167   int out = cgen.addTensor({{1, 2, 3, 2}, circle::TensorType::TensorType_FLOAT32});
168   cgen.addOperatorOneHot({{indices, depth, on_value}, {out}}, axis);
169   cgen.setInputsAndOutputs({indices, on_value}, {out});
170
171   _context = std::make_unique<GenModelTestContext>(cgen.finish());
172   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
173   _context->expectFailModelLoad();
174
175   SUCCEED();
176 }
177
178 TEST_F(GenModelTest, neg_OneOp_OneHot_InvalidAxis)
179 {
180   CircleGen cgen;
181   std::vector<int> depth_data{3};
182   uint32_t depth_buf = cgen.addBuffer(depth_data);
183   int indices = cgen.addTensor({{1, 2, 2}, circle::TensorType::TensorType_INT32});
184   int depth = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32, depth_buf});
185   int on_value = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32});
186   int off_value = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32});
187   int axis = 4;
188   int out = cgen.addTensor({{1, 2, 3, 2}, circle::TensorType::TensorType_FLOAT32});
189   cgen.addOperatorOneHot({{indices, depth, on_value, off_value}, {out}}, axis);
190   cgen.setInputsAndOutputs({indices, on_value, off_value}, {out});
191
192   _context = std::make_unique<GenModelTestContext>(cgen.finish());
193   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
194   _context->expectFailCompile();
195
196   SUCCEED();
197 }