Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / StridedSlice.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "Builders.h"
19 #include "kernels/Utils.h"
20 #include "MISOKernel.h"
21
22 #include "PALStridedSlice.h"
23
24 namespace luci_interpreter
25 {
26 namespace
27 {
28
29 luci_interpreter_pal::StridedSliceParams
30 buildStridedSliceParams(int32_t dims, const int32_t *begin, const int32_t *end,
31                         const int32_t *strides, const circle::StridedSliceOptions *options)
32 {
33   luci_interpreter_pal::StridedSliceParams op_params;
34   op_params.start_indices_count = dims;
35   op_params.stop_indices_count = dims;
36   op_params.strides_count = dims;
37
38   for (int i = 0; i < dims; ++i)
39   {
40     op_params.start_indices[i] = begin[i];
41     op_params.stop_indices[i] = end[i];
42     op_params.strides[i] = strides[i];
43   }
44
45   op_params.begin_mask = options->begin_mask();
46   op_params.ellipsis_mask = 0;
47   op_params.end_mask = options->end_mask();
48   op_params.new_axis_mask = 0;
49   op_params.shrink_axis_mask = options->shrink_axis_mask();
50   return op_params;
51 }
52
53 } // namespace
54
55 void configure_kernel_CircleStridedSlice(const circle::Operator *cur_op,
56                                          BaseRuntimeGraph *runtime_graph)
57 {
58   kernels::MISOKernel miso_kernel(cur_op, runtime_graph);
59
60   const circle::Tensor *input = miso_kernel.input1();
61   const circle::Tensor *begin = miso_kernel.input2();
62   const circle::Tensor *end = miso_kernel.input3();
63   const circle::Tensor *strides = miso_kernel.input4();
64
65   LUCI_INTERPRETER_CHECK(strides != nullptr);
66
67   const circle::Tensor *output = miso_kernel.output();
68
69   LUCI_INTERPRETER_CHECK(Tensor::element_type(begin) == DataType::S32);
70   LUCI_INTERPRETER_CHECK(Tensor::element_type(end) == DataType::S32);
71   LUCI_INTERPRETER_CHECK(Tensor::element_type(strides) == DataType::S32);
72   LUCI_INTERPRETER_CHECK(Tensor::element_type(input) == Tensor::element_type(output));
73 }
74
75 void execute_kernel_CircleStridedSlice(const circle::Operator *cur_op,
76                                        BaseRuntimeGraph *runtime_graph)
77 {
78   kernels::MISOKernel miso_kernel(cur_op, runtime_graph);
79
80   const circle::Tensor *input = miso_kernel.input1();
81   const circle::Tensor *begin = miso_kernel.input2();
82   const circle::Tensor *end = miso_kernel.input3();
83   const circle::Tensor *strides = miso_kernel.input4();
84   const circle::Tensor *output = miso_kernel.output();
85
86   const int32_t dims = Tensor::num_dims(input);
87
88   const uint8_t *input_data = runtime_graph->getDataByTensor(input);
89   const int32_t *begin_data =
90     kernels::getTensorData<int32_t>(runtime_graph->getConstDataByTensor(begin));
91   const int32_t *end_data =
92     kernels::getTensorData<int32_t>(runtime_graph->getConstDataByTensor(end));
93   const int32_t *strides_data =
94     kernels::getTensorData<int32_t>(runtime_graph->getConstDataByTensor(strides));
95   uint8_t *output_data = runtime_graph->getDataByTensor(output);
96
97   LUCI_INTERPRETER_CHECK(input_data != nullptr);
98   LUCI_INTERPRETER_CHECK(begin_data != nullptr);
99   LUCI_INTERPRETER_CHECK(end_data != nullptr);
100   LUCI_INTERPRETER_CHECK(strides_data != nullptr);
101   LUCI_INTERPRETER_CHECK(output_data != nullptr);
102
103   const auto *options = cur_op->builtin_options_as_StridedSliceOptions();
104
105   auto op_params = buildStridedSliceParams(dims, begin_data, end_data, strides_data, options);
106
107   switch (Tensor::element_type(input))
108   {
109 #ifndef DIS_FLOAT
110     case DataType::FLOAT32:
111       luci_interpreter_pal::StridedSlice(op_params, kernels::getTensorShape(input),
112                                          kernels::getTensorData<float>(input_data),
113                                          kernels::getTensorData<float>(output_data));
114       break;
115 #endif // DIS_FLOAT
116 #ifndef DIS_QUANT
117     case DataType::U8:
118       luci_interpreter_pal::StridedSlice(op_params, kernels::getTensorShape(input), input_data,
119                                          output_data);
120       break;
121     case DataType::S8:
122       luci_interpreter_pal::StridedSlice(op_params, kernels::getTensorShape(input), input_data,
123                                          output_data);
124       break;
125 #endif
126     case DataType::S32:
127       luci_interpreter_pal::StridedSlice(op_params, kernels::getTensorShape(input),
128                                          kernels::getTensorData<int32_t>(input_data),
129                                          kernels::getTensorData<int32_t>(output_data));
130       break;
131     default:
132       assert(false && "Unsupported type");
133   }
134 }
135
136 } // namespace luci_interpreter