Imported Upstream version 1.7.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   auto op_params = nnfw::cker::buildStridedSliceParams(
41       reinterpret_cast<uint32_t *>(_begin->buffer()), reinterpret_cast<uint32_t *>(_end->buffer()),
42       reinterpret_cast<uint32_t *>(_strides->buffer()), _begin_mask, _end_mask, _shrink_axis_mask,
43       getTensorShape(_input).DimensionsCount());
44
45   nnfw::cker::checkOutputSize(op_params, getTensorShape(_input), getTensorShape(_output),
46                               getTensorShape(_input).DimensionsCount());
47
48   nnfw::cker::StridedSlice(op_params, getTensorShape(_input),
49                            reinterpret_cast<const T *>(_input->buffer()), getTensorShape(_output),
50                            reinterpret_cast<T *>(_output->buffer()));
51 }
52
53 void StridedSliceLayer::configure(const IPortableTensor *input, const IPortableTensor *begin,
54                                   const IPortableTensor *end, const IPortableTensor *strides,
55                                   IPortableTensor *output, const int32_t begin_mask,
56                                   const int32_t end_mask, const int32_t shrink_axis_mask)
57 {
58   _input = input;
59   _begin = begin;
60   _end = end;
61   _strides = strides;
62   _output = output;
63
64   _begin_mask = begin_mask;
65   _ellipsis_mask = 0;
66   _end_mask = end_mask;
67   _new_axis_mask = 0;
68   _shrink_axis_mask = shrink_axis_mask;
69 }
70
71 void StridedSliceLayer::run()
72 {
73   if (_input->data_type() == OperandType::FLOAT32)
74   {
75     stridedSliceImpl<float>();
76   }
77   else if (_input->data_type() == OperandType::INT32)
78   {
79     stridedSliceImpl<int32_t>();
80   }
81   else if (_input->data_type() == OperandType::INT64)
82   {
83     stridedSliceImpl<int64_t>();
84   }
85   else
86   {
87     throw std::runtime_error{"StridedSlice: unsupported data type"};
88   }
89 }
90
91 } // namespace ops
92 } // namespace cpu
93 } // namespace backend
94 } // namespace onert