Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / StridedSliceLayer.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 "StridedSliceLayer.h"
18
19 #include "OperationUtils.h"
20
21 #include <cker/operation/StridedSlice.h>
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace cpu
28 {
29 namespace ops
30 {
31
32 StridedSliceLayer::StridedSliceLayer()
33   : _input(nullptr), _begin(nullptr), _end(nullptr), _strides(nullptr), _output(nullptr),
34     _begin_mask(0), _ellipsis_mask(0), _end_mask(0), _new_axis_mask(0), _shrink_axis_mask(0)
35 {
36 }
37
38 template <typename T> void StridedSliceLayer::stridedSliceImpl()
39 {
40   const auto input_shape = getShape(_input);
41   const auto output_shape = getShape(_output);
42   auto op_params = nnfw::cker::buildStridedSliceParams(
43     getBuffer<uint32_t>(_begin), getBuffer<uint32_t>(_end), getBuffer<uint32_t>(_strides),
44     _begin_mask, _end_mask, _shrink_axis_mask, input_shape.DimensionsCount());
45
46   nnfw::cker::checkOutputSize(op_params, input_shape, output_shape, input_shape.DimensionsCount());
47
48   nnfw::cker::StridedSlice(op_params, input_shape, getBuffer<T>(_input), output_shape,
49                            getBuffer<T>(_output));
50 }
51
52 void StridedSliceLayer::configure(const IPortableTensor *input, const IPortableTensor *begin,
53                                   const IPortableTensor *end, const IPortableTensor *strides,
54                                   IPortableTensor *output, const int32_t begin_mask,
55                                   const int32_t end_mask, const int32_t shrink_axis_mask)
56 {
57   _input = input;
58   _begin = begin;
59   _end = end;
60   _strides = strides;
61   _output = output;
62
63   _begin_mask = begin_mask;
64   _ellipsis_mask = 0;
65   _end_mask = end_mask;
66   _new_axis_mask = 0;
67   _shrink_axis_mask = shrink_axis_mask;
68 }
69
70 void StridedSliceLayer::run()
71 {
72   if (_input->data_type() == OperandType::FLOAT32)
73   {
74     stridedSliceImpl<float>();
75   }
76   else if (_input->data_type() == OperandType::INT32)
77   {
78     stridedSliceImpl<int32_t>();
79   }
80   else if (_input->data_type() == OperandType::INT64)
81   {
82     stridedSliceImpl<int64_t>();
83   }
84   else
85   {
86     throw std::runtime_error{"StridedSlice: unsupported data type"};
87   }
88 }
89
90 } // namespace ops
91 } // namespace cpu
92 } // namespace backend
93 } // namespace onert