Imported Upstream version 1.20.0
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / one_op_tests / Reduce.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 CircleBuffer genSimpleReduceModel(circle::BuiltinOperator op, bool keep_dims)
22 {
23   CircleGen cgen;
24   uint32_t axis_buf = cgen.addBuffer(std::vector<int32_t>{0, 1, 2, 3});
25   int in = cgen.addTensor({{2, 1, 1, 3}, circle::TensorType::TensorType_FLOAT32});
26   int axis = cgen.addTensor({{4}, circle::TensorType::TensorType_INT32, axis_buf});
27   int out = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32});
28   cgen.addOperatorReduce({{in, axis}, {out}}, op, keep_dims);
29   cgen.setInputsAndOutputs({in}, {out});
30   return cgen.finish();
31 }
32
33 TEST_F(GenModelTest, OneOp_ReduceMax)
34 {
35   auto model = genSimpleReduceModel(circle::BuiltinOperator_REDUCE_MAX, false);
36   _context = std::make_unique<GenModelTestContext>(std::move(model));
37   _context->addTestCase(uniformTCD<float>({{1, 2, 3, 4, 5, 6}}, {{6}}));
38   _context->addTestCase(uniformTCD<float>({{100, 98, 55, 200, 3, 40}}, {{200}}));
39   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
40
41   SUCCEED();
42 }
43
44 class ReduceMaxBadIndex : public GenModelTest,
45                           public ::testing::WithParamInterface<std::vector<int>>
46 {
47 };
48
49 TEST_P(ReduceMaxBadIndex, neg_Test)
50 {
51   CircleGen cgen;
52   // Axis cannot be equal or bigger than input's rank - 4
53   uint32_t axis_buf = cgen.addBuffer(GetParam());
54   int in = cgen.addTensor({{2, 1, 1, 3}, circle::TensorType::TensorType_FLOAT32});
55   int axis = cgen.addTensor({{4}, circle::TensorType::TensorType_INT32, axis_buf});
56   int out = cgen.addTensor({{1}, circle::TensorType::TensorType_FLOAT32});
57   cgen.addOperatorReduce({{in, axis}, {out}}, circle::BuiltinOperator_REDUCE_MAX, false);
58   cgen.setInputsAndOutputs({in}, {out});
59
60   _context = std::make_unique<GenModelTestContext>(cgen.finish());
61   _context->expectFailCompile();
62
63   SUCCEED();
64 }
65
66 INSTANTIATE_TEST_CASE_P(GenModelTest, ReduceMaxBadIndex,
67                         ::testing::Values(std::vector<int32_t>{0, 1, 2, 4},
68                                           std::vector<int32_t>{0, -5, 2, 3},
69                                           std::vector<int32_t>{-88, 1, 2, 3},
70                                           std::vector<int32_t>{0, 1, 88, 3}));