5cbe09d1e1b0f2b668208fff886aa773243c1d09
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / one_op_tests / Cast.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 CircleGen genSimpleCastModel(circle::TensorType from_t, circle::TensorType to_t)
22 {
23   CircleGen cgen;
24   int in = cgen.addTensor({{1, 2, 2, 1}, from_t});
25   int out = cgen.addTensor({{1, 2, 2, 1}, to_t});
26   cgen.addOperatorCast({{in}, {out}}, from_t, to_t);
27   cgen.setInputsAndOutputs({in}, {out});
28   return cgen;
29 }
30
31 TEST_F(GenModelTest, OneOp_Cast_Int32ToFloat32)
32 {
33   CircleGen cgen = genSimpleCastModel(circle::TensorType_INT32, circle::TensorType_FLOAT32);
34
35   _context = std::make_unique<GenModelTestContext>(cgen.finish());
36   _context->addTestCase(
37       TestCaseData{}.addInput<int32_t>({1, 2, 3, 4}).addOutput<float>({1, 2, 3, 4}));
38   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
39
40   SUCCEED();
41 }
42
43 TEST_F(GenModelTest, OneOp_Cast_Float32ToInt32)
44 {
45   CircleGen cgen = genSimpleCastModel(circle::TensorType_FLOAT32, circle::TensorType_INT32);
46
47   _context = std::make_unique<GenModelTestContext>(cgen.finish());
48   _context->addTestCase(
49       TestCaseData{}.addInput<float>({1, 2, 3, 4}).addOutput<int32_t>({1, 2, 3, 4}));
50   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
51
52   SUCCEED();
53 }
54
55 TEST_F(GenModelTest, OneOp_Cast_BoolToFloat32)
56 {
57   CircleGen cgen = genSimpleCastModel(circle::TensorType_BOOL, circle::TensorType_FLOAT32);
58
59   _context = std::make_unique<GenModelTestContext>(cgen.finish());
60   _context->addTestCase(
61       TestCaseData{}.addInput<bool>({true, false, true, true}).addOutput<float>({1, 0, 1, 1}));
62   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
63
64   SUCCEED();
65 }
66
67 TEST_F(GenModelTest, OneOp_Cast_BoolToUInt8)
68 {
69   CircleGen cgen = genSimpleCastModel(circle::TensorType_BOOL, circle::TensorType_UINT8);
70
71   _context = std::make_unique<GenModelTestContext>(cgen.finish());
72   _context->addTestCase(TestCaseData{}
73                             .addInput<bool>({true, false, true, true})
74                             .addOutput(std::vector<uint8_t>{1, 0, 1, 1}));
75   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
76
77   SUCCEED();
78 }
79
80 TEST_F(GenModelTest, OneOp_Cast_BoolToInt32)
81 {
82   CircleGen cgen = genSimpleCastModel(circle::TensorType_BOOL, circle::TensorType_INT32);
83
84   _context = std::make_unique<GenModelTestContext>(cgen.finish());
85   _context->addTestCase(
86       TestCaseData{}.addInput<bool>({true, false, true, true}).addOutput<int32_t>({1, 0, 1, 1}));
87   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
88
89   SUCCEED();
90 }
91
92 TEST_F(GenModelTest, OneOp_Cast_AfterEqual)
93 {
94   CircleGen cgen;
95   int lhs = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
96   int rhs = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
97   int equal_out = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_BOOL});
98   int out = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
99   cgen.addOperatorEqual({{lhs, rhs}, {equal_out}});
100   cgen.addOperatorCast({{equal_out}, {out}}, circle::TensorType::TensorType_BOOL,
101                        circle::TensorType::TensorType_FLOAT32);
102   cgen.setInputsAndOutputs({lhs, rhs}, {out});
103
104   _context = std::make_unique<GenModelTestContext>(cgen.finish());
105   _context->addTestCase(uniformTCD<float>({{1, 3, 2, 4}, {2, 3, 1, 4}}, {{0, 1, 0, 1}}));
106   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
107
108   SUCCEED();
109 }
110
111 TEST_F(GenModelTest, neg_OneOp_Cast_InvalidInputCount0)
112 {
113   CircleGen cgen;
114   int out = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_INT32});
115   cgen.addOperatorCast({{}, {out}}, circle::TensorType::TensorType_FLOAT32,
116                        circle::TensorType::TensorType_INT32);
117   cgen.setInputsAndOutputs({}, {out});
118
119   _context = std::make_unique<GenModelTestContext>(cgen.finish());
120   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
121   _context->expectFailModelLoad();
122
123   SUCCEED();
124 }
125
126 TEST_F(GenModelTest, neg_OneOp_Cast_InvalidInputCount2)
127 {
128   CircleGen cgen;
129   int lhs = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_INT32});
130   int rhs = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_INT32});
131   int out = cgen.addTensor({{1, 2, 2, 3}, circle::TensorType::TensorType_FLOAT32});
132   cgen.addOperatorCast({{lhs, rhs}, {out}}, circle::TensorType::TensorType_INT32,
133                        circle::TensorType::TensorType_FLOAT32);
134   cgen.setInputsAndOutputs({lhs, rhs}, {out});
135
136   _context = std::make_unique<GenModelTestContext>(cgen.finish());
137   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
138   _context->expectFailModelLoad();
139
140   SUCCEED();
141 }
142
143 TEST_F(GenModelTest, neg_OneOp_Cast_InvalidOutputCount0)
144 {
145   CircleGen cgen;
146   int in = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_INT32});
147   cgen.addOperatorCast({{in}, {}}, circle::TensorType::TensorType_INT32,
148                        circle::TensorType::TensorType_FLOAT32);
149   cgen.setInputsAndOutputs({in}, {});
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_Cast_InvalidOutputCount2)
159 {
160   CircleGen cgen;
161   int in = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_INT32});
162   int out1 = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
163   int out2 = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_INT32});
164   cgen.addOperatorCast({{in}, {out1, out2}}, circle::TensorType::TensorType_INT32,
165                        circle::TensorType::TensorType_FLOAT32);
166   cgen.setInputsAndOutputs({in}, {out1, out2});
167
168   _context = std::make_unique<GenModelTestContext>(cgen.finish());
169   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
170   _context->expectFailModelLoad();
171
172   SUCCEED();
173 }