67b02cd7d4896e8cc643854f863e9b404ddbfb1f
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / one_op_tests / ArgMax.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_ArgMax_AxisToConst)
22 {
23   CircleGen cgen;
24   const auto output_type = circle::TensorType::TensorType_INT32;
25   std::vector<int32_t> axis_data{1};
26   uint32_t axis_buf = cgen.addBuffer(axis_data);
27   int axis = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32, axis_buf});
28   int in = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
29   int out = cgen.addTensor({{1, 2, 1}, output_type});
30   cgen.addOperatorArgMax({{in, axis}, {out}}, output_type);
31   cgen.setInputsAndOutputs({in}, {out});
32
33   _context = std::make_unique<GenModelTestContext>(cgen.finish());
34   _context->addTestCase(TestCaseData{}.addInput<float>({1, 4, 2, 3}).addOutput<int32_t>({1, 0}));
35   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
36
37   SUCCEED();
38 }
39
40 TEST_F(GenModelTest, OneOp_ArgMax_Int64_AxisToConst)
41 {
42   CircleGen cgen;
43   const auto output_type = circle::TensorType::TensorType_INT64;
44   std::vector<int32_t> axis_data{1};
45   uint32_t axis_buf = cgen.addBuffer(axis_data);
46   int axis = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32, axis_buf});
47   int in = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
48   int out = cgen.addTensor({{1, 2, 1}, output_type});
49   cgen.addOperatorArgMax({{in, axis}, {out}}, output_type);
50   cgen.setInputsAndOutputs({in}, {out});
51
52   _context = std::make_unique<GenModelTestContext>(cgen.finish());
53   _context->addTestCase(TestCaseData{}.addInput<float>({1, 4, 2, 3}).addOutput<int64_t>({1, 0}));
54   _context->setBackends({"acl_cl"});
55
56   SUCCEED();
57 }
58
59 TEST_F(GenModelTest, OneOp_ArgMax_AxisToVar)
60 {
61   CircleGen cgen;
62   const auto output_type = circle::TensorType::TensorType_INT32;
63   int axis = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32});
64   int in = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
65   int out = cgen.addTensor({{1, 2, 1}, output_type});
66   cgen.addOperatorArgMax({{in, axis}, {out}}, output_type);
67   cgen.setInputsAndOutputs({in, axis}, {out});
68
69   _context = std::make_unique<GenModelTestContext>(cgen.finish());
70   _context->addTestCase(TestCaseData{}
71                             .addInput<float>({1, 4, 2, 3})
72                             .addInput<int32_t>({-3})
73                             .addOutput<int32_t>({1, 0}));
74   _context->setBackends({"cpu"});
75
76   SUCCEED();
77 }
78
79 TEST_F(GenModelTest, neg_OneOp_ArgMax_InvalidAxis0)
80 {
81   CircleGen cgen;
82   const auto output_type = circle::TensorType::TensorType_INT32;
83   std::vector<int32_t> axis_data{4};
84   uint32_t axis_buf = cgen.addBuffer(axis_data);
85   int axis = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32, axis_buf});
86   int in = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_FLOAT32});
87   int out = cgen.addTensor({{1, 2, 1}, output_type});
88   cgen.addOperatorArgMax({{in, axis}, {out}}, output_type);
89   cgen.setInputsAndOutputs({in}, {out});
90
91   _context = std::make_unique<GenModelTestContext>(cgen.finish());
92   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
93   _context->expectFailCompile();
94
95   SUCCEED();
96 }
97
98 TEST_F(GenModelTest, neg_OneOp_ArgMax_InvalidAxis1)
99 {
100   CircleGen cgen;
101   const auto output_type = circle::TensorType::TensorType_INT32;
102   std::vector<int32_t> axis_data{-3};
103   uint32_t axis_buf = cgen.addBuffer(axis_data);
104   int axis = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32, axis_buf});
105   int in = cgen.addTensor({{2, 2}, circle::TensorType::TensorType_FLOAT32});
106   int out = cgen.addTensor({{2}, output_type});
107   cgen.addOperatorArgMax({{in, axis}, {out}}, output_type);
108   cgen.setInputsAndOutputs({in}, {out});
109
110   _context = std::make_unique<GenModelTestContext>(cgen.finish());
111   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
112   _context->expectFailCompile();
113
114   SUCCEED();
115 }