Imported Upstream version 1.20.0
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / one_op_tests / Equal.test.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 struct EqualVariationParam
20 {
21   TestCaseData tcd;
22   circle::TensorType input_type = circle::TensorType::TensorType_FLOAT32;
23   const std::vector<std::string> backends = {"acl_cl", "acl_neon", "cpu"};
24 };
25
26 class EqualVariation : public GenModelTest,
27                        public ::testing::WithParamInterface<EqualVariationParam>
28 {
29 };
30
31 // Input shape:
32 //   Base: {1, 2, 2, 1}
33 //   Brodcast: {1} on of two input
34 // Output shape: {1, 2, 2, 1}
35 // Input type: Non-quantization type
36 // Output type: BOOL
37 // Test with different input type and value
38 INSTANTIATE_TEST_SUITE_P(GenModelTest, EqualVariation,
39                          ::testing::Values(
40                            // Float type
41                            EqualVariationParam{TestCaseData{}
42                                                  .addInput<float>({0.1, 0.3, 0.5, 0.7})
43                                                  .addInput<float>({0.1, 0.2, 0.3, 0.4})
44                                                  .addOutput<bool>({true, false, false, false})},
45                            // Float type - broadcast
46                            EqualVariationParam{TestCaseData{}
47                                                  .addInput<float>({0.1, 0.3, 0.5, 0.7})
48                                                  .addInput<float>({0.3})
49                                                  .addOutput<bool>({false, true, false, false})},
50                            // Int32 type
51                            EqualVariationParam{TestCaseData{}
52                                                  .addInput<int32_t>({1, 3, 5, 7})
53                                                  .addInput<int32_t>({1, 2, 3, 4})
54                                                  .addOutput<bool>({true, false, false, false}),
55                                                circle::TensorType::TensorType_INT32},
56                            // Int32 type - broadcast
57                            EqualVariationParam{TestCaseData{}
58                                                  .addInput<int32_t>({1, 3, 5, 7})
59                                                  .addInput<int32_t>({5})
60                                                  .addOutput<bool>({false, false, true, false}),
61                                                circle::TensorType::TensorType_INT32},
62                            // Int64 type
63                            // NYI: acl backend
64                            EqualVariationParam{TestCaseData{}
65                                                  .addInput<int64_t>({1, 3, 5, 7})
66                                                  .addInput<int64_t>({1, 2, 3, 4})
67                                                  .addOutput<bool>({true, false, false, false}),
68                                                circle::TensorType::TensorType_INT64,
69                                                {"cpu"}},
70                            // Int64 type - broadcast
71                            // NYI: acl backend
72                            EqualVariationParam{TestCaseData{}
73                                                  .addInput<int64_t>({1, 3, 5, 7})
74                                                  .addInput<int64_t>({1})
75                                                  .addOutput<bool>({true, false, false, false}),
76                                                circle::TensorType::TensorType_INT64,
77                                                {"cpu"}},
78                            // Bool type
79                            EqualVariationParam{TestCaseData{}
80                                                  .addInput<bool>({true, true, false, false})
81                                                  .addInput<bool>({true, false, true, false})
82                                                  .addOutput<bool>({true, false, false, true}),
83                                                circle::TensorType::TensorType_BOOL},
84                            // Bool type - broadcast
85                            EqualVariationParam{TestCaseData{}
86                                                  .addInput<bool>({true, true, false, false})
87                                                  .addInput<bool>({true})
88                                                  .addOutput<bool>({true, true, false, false}),
89                                                circle::TensorType::TensorType_BOOL}
90
91                            ));
92
93 TEST_P(EqualVariation, Test)
94 {
95   auto &param = GetParam();
96
97   auto lhs_data = param.tcd.inputs.at(0);
98   auto rhs_data = param.tcd.inputs.at(1);
99
100   bool broadcast_lhs = false;
101   bool broadcast_rhs = false;
102   if (lhs_data.size() != rhs_data.size())
103   {
104     if (lhs_data.size() < rhs_data.size())
105       broadcast_lhs = true;
106     else
107       broadcast_rhs = true;
108   }
109
110   CircleGen cgen;
111   const auto output_type = circle::TensorType::TensorType_BOOL;
112
113   int lhs = broadcast_lhs ? cgen.addTensor({{1}, param.input_type})
114                           : cgen.addTensor({{1, 2, 2, 1}, param.input_type});
115   int rhs = broadcast_rhs ? cgen.addTensor({{1}, param.input_type})
116                           : cgen.addTensor({{1, 2, 2, 1}, param.input_type});
117   int out = cgen.addTensor({{1, 2, 2, 1}, output_type});
118   cgen.addOperatorEqual({{lhs, rhs}, {out}});
119   cgen.setInputsAndOutputs({lhs, rhs}, {out});
120
121   _context = std::make_unique<GenModelTestContext>(cgen.finish());
122   _context->addTestCase(param.tcd);
123   _context->setBackends(param.backends);
124
125   SUCCEED();
126 }
127
128 TEST_F(GenModelTest, neg_OneOp_Equal_DifferentType)
129 {
130   CircleGen cgen;
131   int lhs = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
132   int rhs = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_INT32});
133   int out = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_BOOL});
134   cgen.addOperatorEqual({{lhs, rhs}, {out}});
135   cgen.setInputsAndOutputs({lhs, rhs}, {out});
136
137   _context = std::make_unique<GenModelTestContext>(cgen.finish());
138   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
139   _context->expectFailModelLoad();
140
141   SUCCEED();
142 }
143
144 TEST_F(GenModelTest, neg_OneOp_Equal_InvalidType)
145 {
146   CircleGen cgen;
147   int lhs = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
148   int rhs = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
149   int out = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_INT32});
150   cgen.addOperatorEqual({{lhs, rhs}, {out}});
151   cgen.setInputsAndOutputs({lhs, rhs}, {out});
152
153   _context = std::make_unique<GenModelTestContext>(cgen.finish());
154   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
155   _context->expectFailModelLoad();
156
157   SUCCEED();
158 }