Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / one_op_tests / Pad.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 // Input shape: {1, 2, 2, 1}
20 // Padding: {0, 0, 1, 1, 1, 1, 0, 0}
21 // Output shape: {1, 4, 4, 1}
22 struct PadParam
23 {
24   TestCaseData tcd;
25   circle::TensorType data_type = circle::TensorType::TensorType_FLOAT32;
26   float scale = 0.0f;
27   int64_t zero_point = 0;
28 };
29
30 class PadVariation : public GenModelTest, public ::testing::WithParamInterface<PadParam>
31 {
32 };
33
34 // Test with different value type
35 INSTANTIATE_TEST_CASE_P(
36   GenModelTest, PadVariation,
37   ::testing::Values(
38     // float value
39     PadParam{uniformTCD<float>({{1, 2, 3, 4}}, {{0, 0, 0, 0, 0, 1, 2, 0, 0, 3, 4, 0, 0, 0, 0, 0}})},
40     // uint8 value
41     PadParam{
42       uniformTCD<uint8_t>({{1, 2, 3, 4}}, {{8, 8, 8, 8, 8, 1, 2, 8, 8, 3, 4, 8, 8, 8, 8, 8}}),
43       circle::TensorType::TensorType_UINT8, 1.0, 8},
44     // int8 value
45     PadParam{uniformTCD<int8_t>({{-2, -1, 1, 2}},
46                                 {{-5, -5, -5, -5, -5, -2, -1, -5, -5, 1, 2, -5, -5, -5, -5, -5}}),
47              circle::TensorType::TensorType_INT8, 1.0, -5}));
48
49 TEST_P(PadVariation, Test)
50 {
51   auto &param = GetParam();
52
53   CircleGen cgen;
54   int in = cgen.addTensor({{1, 2, 2, 1}, param.data_type}, param.scale, param.zero_point);
55   std::vector<int32_t> padding_data{0, 0, 1, 1, 1, 1, 0, 0};
56   uint32_t padding_buf = cgen.addBuffer(padding_data);
57   int padding = cgen.addTensor({{4, 2}, circle::TensorType::TensorType_INT32, padding_buf});
58   int out = cgen.addTensor({{1, 4, 4, 1}, param.data_type}, param.scale, param.zero_point);
59
60   cgen.addOperatorPad({{in, padding}, {out}});
61   cgen.setInputsAndOutputs({in}, {out});
62   _context = std::make_unique<GenModelTestContext>(cgen.finish());
63   _context->addTestCase(param.tcd);
64   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
65
66   SUCCEED();
67 }
68
69 TEST_P(PadVariation, neg_InvalidPadRank)
70 {
71   auto &param = GetParam();
72
73   CircleGen cgen;
74   int in = cgen.addTensor({{1, 2, 2, 1}, param.data_type}, param.scale, param.zero_point);
75   std::vector<int32_t> padding_data{1, 1, 1, 1};
76   uint32_t padding_buf = cgen.addBuffer(padding_data);
77   int padding = cgen.addTensor({{4}, circle::TensorType::TensorType_INT32, padding_buf});
78   int out = cgen.addTensor({{1, 4, 4, 1}, param.data_type}, param.scale, param.zero_point);
79
80   cgen.addOperatorPad({{in, padding}, {out}});
81   cgen.setInputsAndOutputs({in}, {out});
82
83   _context = std::make_unique<GenModelTestContext>(cgen.finish());
84   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
85   _context->expectFailCompile();
86
87   SUCCEED();
88 }
89
90 TEST_P(PadVariation, neg_InvalidPadDim0)
91 {
92   auto &param = GetParam();
93
94   CircleGen cgen;
95   int in = cgen.addTensor({{1, 2, 2, 1}, param.data_type}, param.scale, param.zero_point);
96   std::vector<int32_t> padding_data{1, 1, 1, 1};
97   uint32_t padding_buf = cgen.addBuffer(padding_data);
98   int padding = cgen.addTensor({{2, 2}, circle::TensorType::TensorType_INT32, padding_buf});
99   int out = cgen.addTensor({{1, 4, 4, 1}, param.data_type}, param.scale, param.zero_point);
100
101   cgen.addOperatorPad({{in, padding}, {out}});
102   cgen.setInputsAndOutputs({in}, {out});
103
104   _context = std::make_unique<GenModelTestContext>(cgen.finish());
105   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
106   _context->expectFailCompile();
107
108   SUCCEED();
109 }
110
111 TEST_P(PadVariation, neg_InvalidPadDim1)
112 {
113   auto &param = GetParam();
114
115   CircleGen cgen;
116   int in = cgen.addTensor({{1, 2, 2, 1}, param.data_type}, param.scale, param.zero_point);
117   std::vector<int32_t> padding_data{1, 1, 1, 1};
118   uint32_t padding_buf = cgen.addBuffer(padding_data);
119   int padding = cgen.addTensor({{4, 1}, circle::TensorType::TensorType_INT32, padding_buf});
120   int out = cgen.addTensor({{1, 4, 4, 1}, param.data_type}, param.scale, param.zero_point);
121
122   cgen.addOperatorPad({{in, padding}, {out}});
123   cgen.setInputsAndOutputs({in}, {out});
124
125   _context = std::make_unique<GenModelTestContext>(cgen.finish());
126   _context->setBackends({"acl_cl", "acl_neon", "cpu"});
127   _context->expectFailCompile();
128
129   SUCCEED();
130 }
131
132 TEST_P(PadVariation, neg_Type)
133 {
134   auto &param = GetParam();
135
136   const circle::TensorType output_type = ((param.data_type == circle::TensorType::TensorType_UINT8)
137                                             ? circle::TensorType::TensorType_INT8
138                                             : circle::TensorType::TensorType_UINT8);
139
140   CircleGen cgen;
141   int in = cgen.addTensor({{1, 2, 2, 1}, param.data_type}, param.scale, param.zero_point);
142   std::vector<int32_t> padding_data{0, 0, 1, 1, 1, 1, 0, 0};
143   uint32_t padding_buf = cgen.addBuffer(padding_data);
144   int padding = cgen.addTensor({{4, 2}, circle::TensorType::TensorType_INT32, padding_buf});
145   int out = cgen.addTensor({{1, 4, 4, 1}, output_type}, 1.0, 0);
146
147   cgen.addOperatorPad({{in, padding}, {out}});
148   cgen.setInputsAndOutputs({in}, {out});
149
150   _context = std::make_unique<GenModelTestContext>(cgen.finish());
151   _context->expectFailModelLoad();
152
153   SUCCEED();
154 }
155
156 TEST_F(GenModelTest, neg_OneOp_Pad_QuantParam)
157 {
158   CircleGen cgen;
159   int in = cgen.addTensor({{1, 2, 2, 1}, circle::TensorType::TensorType_UINT8}, 1.0, 1);
160   std::vector<int32_t> padding_data{0, 0, 1, 1, 1, 1, 0, 0};
161   uint32_t padding_buf = cgen.addBuffer(padding_data);
162   int padding = cgen.addTensor({{4, 2}, circle::TensorType::TensorType_INT32, padding_buf});
163   int out = cgen.addTensor({{1, 4, 4, 1}, circle::TensorType::TensorType_UINT8}, 1.0, 3);
164
165   cgen.addOperatorPad({{in, padding}, {out}});
166   cgen.setInputsAndOutputs({in}, {out});
167
168   _context = std::make_unique<GenModelTestContext>(cgen.finish());
169   _context->expectFailModelLoad();
170
171   SUCCEED();
172 }