2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "GenModelTest.h"
19 struct SliceVariationParam
21 std::vector<int32_t> input_shape;
22 std::vector<int32_t> begins;
23 std::vector<int32_t> sizes;
26 circle::TensorType input_type = circle::TensorType::TensorType_FLOAT32;
28 int64_t zero_point = 0;
29 circle::TensorType begins_type = circle::TensorType::TensorType_INT32;
32 class SliceVariation : public GenModelTest,
33 public ::testing::WithParamInterface<SliceVariationParam>
37 INSTANTIATE_TEST_CASE_P(
38 GenModelTest, SliceVariation,
44 uniformTCD<float>({{1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33}}, {{12, 13}})},
49 uniformTCD<uint8_t>({{1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33}}, {{12, 13}}),
50 circle::TensorType::TensorType_UINT8,
57 uniformTCD<float>({{1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33}}, {{12, 13}}),
58 circle::TensorType::TensorType_FLOAT32,
61 circle::TensorType::TensorType_INT64}));
63 TEST_P(SliceVariation, Test)
65 auto ¶m = GetParam();
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)
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});
77 uint32_t sizes_buf = cgen.addBuffer(param.sizes);
78 int sizes = cgen.addTensor({{rank}, param.begins_type, sizes_buf});
80 cgen.addOperatorSlice({{in, begins, sizes}, {out}});
82 else if (param.begins_type == circle::TensorType::TensorType_INT64)
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++)
88 begins_64[i] = param.begins[i];
89 sizes_64[i] = param.sizes[i];
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});
96 uint32_t sizes_buf = cgen.addBuffer(sizes_64);
97 int sizes = cgen.addTensor({{rank}, param.begins_type, sizes_buf});
99 cgen.addOperatorSlice({{in, begins, sizes}, {out}});
101 cgen.setInputsAndOutputs({in}, {out});
103 _context = std::make_unique<GenModelTestContext>(cgen.finish());
104 _context->addTestCase(param.tcd);
106 // acl don't support int64 yet
107 if (param.begins_type == circle::TensorType::TensorType_INT64)
109 _context->setBackends({"cpu"});
113 _context->setBackends({"cpu", "acl_cl", "acl_neon"});
119 TEST_F(GenModelTest, neg_OneOp_Slice_Type)
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});
133 _context = std::make_unique<GenModelTestContext>(cgen.finish());
134 _context->expectFailModelLoad();
139 TEST_P(SliceVariation, neg_DiffType)
141 auto ¶m = GetParam();
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)
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++)
153 sizes_64[i] = param.sizes[i];
156 int rank = param.begins.size();
157 int begins = cgen.addTensor({{rank}, param.begins_type, begins_buf});
159 uint32_t sizes_buf = cgen.addBuffer(sizes_64);
160 int sizes = cgen.addTensor({{rank}, circle::TensorType::TensorType_INT64, sizes_buf});
162 cgen.addOperatorSlice({{in, begins, sizes}, {out}});
164 else if (param.begins_type == circle::TensorType::TensorType_INT64)
166 std::vector<int64_t> begins_64(param.begins.size());
167 for (int i = 0; i < param.begins.size(); i++)
169 begins_64[i] = param.begins[i];
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});
176 uint32_t sizes_buf = cgen.addBuffer(param.sizes);
177 int sizes = cgen.addTensor({{rank}, circle::TensorType::TensorType_INT32, sizes_buf});
179 cgen.addOperatorSlice({{in, begins, sizes}, {out}});
181 cgen.setInputsAndOutputs({in}, {out});
183 _context = std::make_unique<GenModelTestContext>(cgen.finish());
184 _context->expectFailModelLoad();