Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / one_op_tests / Slice.cc
1 /*
2  * Copyright (c) 2021 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 SliceVariationParam
20 {
21   std::vector<int32_t> input_shape;
22   std::vector<int32_t> begins;
23   std::vector<int32_t> sizes;
24   TestCaseData tcd;
25
26   circle::TensorType input_type = circle::TensorType::TensorType_FLOAT32;
27   float scale = 0.0f;
28   int64_t zero_point = 0;
29   circle::TensorType begins_type = circle::TensorType::TensorType_INT32;
30 };
31
32 class SliceVariation : public GenModelTest,
33                        public ::testing::WithParamInterface<SliceVariationParam>
34 {
35 };
36
37 INSTANTIATE_TEST_CASE_P(
38   GenModelTest, SliceVariation,
39   ::testing::Values(
40     SliceVariationParam{
41       {2, 2, 3, 1},
42       {0, 1, 1, 0},
43       {1, 1, 2, 1},
44       uniformTCD<float>({{1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33}}, {{12, 13}})},
45     SliceVariationParam{
46       {2, 2, 3, 1},
47       {0, 1, 1, 0},
48       {1, 1, 2, 1},
49       uniformTCD<uint8_t>({{1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33}}, {{12, 13}}),
50       circle::TensorType::TensorType_UINT8,
51       1,
52       0},
53     SliceVariationParam{
54       {2, 2, 3, 1},
55       {0, 1, 1, 0},
56       {1, 1, 2, 1},
57       uniformTCD<float>({{1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33}}, {{12, 13}}),
58       circle::TensorType::TensorType_FLOAT32,
59       0,
60       0,
61       circle::TensorType::TensorType_INT64}));
62
63 TEST_P(SliceVariation, Test)
64 {
65   auto &param = GetParam();
66
67   CircleGen cgen;
68
69   int in = cgen.addTensor({param.input_shape, param.input_type}, param.scale, param.zero_point);
70   int out = cgen.addTensor({param.sizes, param.input_type}, param.scale, param.zero_point);
71   if (param.begins_type == circle::TensorType::TensorType_INT32)
72   {
73     uint32_t begins_buf = cgen.addBuffer(param.begins);
74     int rank = param.begins.size();
75     int begins = cgen.addTensor({{rank}, param.begins_type, begins_buf});
76
77     uint32_t sizes_buf = cgen.addBuffer(param.sizes);
78     int sizes = cgen.addTensor({{rank}, param.begins_type, sizes_buf});
79
80     cgen.addOperatorSlice({{in, begins, sizes}, {out}});
81   }
82   else if (param.begins_type == circle::TensorType::TensorType_INT64)
83   {
84     std::vector<int64_t> begins_64(param.begins.size());
85     std::vector<int64_t> sizes_64(param.sizes.size());
86     for (int i = 0; i < param.begins.size(); i++)
87     {
88       begins_64[i] = param.begins[i];
89       sizes_64[i] = param.sizes[i];
90     }
91
92     uint32_t begins_buf = cgen.addBuffer(begins_64);
93     int rank = param.begins.size();
94     int begins = cgen.addTensor({{rank}, param.begins_type, begins_buf});
95
96     uint32_t sizes_buf = cgen.addBuffer(sizes_64);
97     int sizes = cgen.addTensor({{rank}, param.begins_type, sizes_buf});
98
99     cgen.addOperatorSlice({{in, begins, sizes}, {out}});
100   }
101   cgen.setInputsAndOutputs({in}, {out});
102
103   _context = std::make_unique<GenModelTestContext>(cgen.finish());
104   _context->addTestCase(param.tcd);
105
106   // acl don't support int64 yet
107   if (param.begins_type == circle::TensorType::TensorType_INT64)
108   {
109     _context->setBackends({"cpu"});
110   }
111   else
112   {
113     _context->setBackends({"cpu", "acl_cl", "acl_neon"});
114   }
115
116   SUCCEED();
117 }
118
119 TEST_F(GenModelTest, neg_OneOp_Slice_Type)
120 {
121   CircleGen cgen;
122   int in = cgen.addTensor({{1, 3, 3, 2}, circle::TensorType::TensorType_FLOAT32});
123   std::vector<float> begins_data = {0, 0, 1, 0};
124   uint32_t begins_buf = cgen.addBuffer(begins_data);
125   int begins = cgen.addTensor({{4}, circle::TensorType::TensorType_FLOAT32, begins_buf});
126   std::vector<float> sizes_data = {1, 2, 1, 1};
127   uint32_t sizes_buf = cgen.addBuffer(sizes_data);
128   int sizes = cgen.addTensor({{4}, circle::TensorType::TensorType_FLOAT32, sizes_buf});
129   int out = cgen.addTensor({{1, 2, 1, 1}, circle::TensorType::TensorType_FLOAT32});
130   cgen.addOperatorSlice({{in, begins, sizes}, {out}});
131   cgen.setInputsAndOutputs({in}, {out});
132
133   _context = std::make_unique<GenModelTestContext>(cgen.finish());
134   _context->expectFailModelLoad();
135
136   SUCCEED();
137 }
138
139 TEST_P(SliceVariation, neg_DiffType)
140 {
141   auto &param = GetParam();
142
143   CircleGen cgen;
144
145   int in = cgen.addTensor({param.input_shape, param.input_type}, param.scale, param.zero_point);
146   int out = cgen.addTensor({param.sizes, param.input_type}, param.scale, param.zero_point);
147   if (param.begins_type == circle::TensorType::TensorType_INT32)
148   {
149     uint32_t begins_buf = cgen.addBuffer(param.begins);
150     std::vector<int64_t> sizes_64(param.sizes.size());
151     for (int i = 0; i < param.begins.size(); i++)
152     {
153       sizes_64[i] = param.sizes[i];
154     }
155
156     int rank = param.begins.size();
157     int begins = cgen.addTensor({{rank}, param.begins_type, begins_buf});
158
159     uint32_t sizes_buf = cgen.addBuffer(sizes_64);
160     int sizes = cgen.addTensor({{rank}, circle::TensorType::TensorType_INT64, sizes_buf});
161
162     cgen.addOperatorSlice({{in, begins, sizes}, {out}});
163   }
164   else if (param.begins_type == circle::TensorType::TensorType_INT64)
165   {
166     std::vector<int64_t> begins_64(param.begins.size());
167     for (int i = 0; i < param.begins.size(); i++)
168     {
169       begins_64[i] = param.begins[i];
170     }
171
172     uint32_t begins_buf = cgen.addBuffer(begins_64);
173     int rank = param.begins.size();
174     int begins = cgen.addTensor({{rank}, param.begins_type, begins_buf});
175
176     uint32_t sizes_buf = cgen.addBuffer(param.sizes);
177     int sizes = cgen.addTensor({{rank}, circle::TensorType::TensorType_INT32, sizes_buf});
178
179     cgen.addOperatorSlice({{in, begins, sizes}, {out}});
180   }
181   cgen.setInputsAndOutputs({in}, {out});
182
183   _context = std::make_unique<GenModelTestContext>(cgen.finish());
184   _context->expectFailModelLoad();
185
186   SUCCEED();
187 }