ONE - On-device Neural Engine
Loading...
Searching...
No Matches
StridedSlice.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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 "OMStatus.h"
18
19#include "core/OMUtils.h"
20#include "core/OMKernelData.h"
21
23#include "execute/OMUtils.h"
25
26#include "PALStridedSlice.h"
27
28using namespace onert_micro;
29using namespace onert_micro::core;
30using namespace onert_micro::execute;
31
32namespace
33{
34
35constexpr uint32_t inputTensorIdx = 0;
36constexpr uint32_t beginTensorIdx = 1;
37constexpr uint32_t endTensorIdx = 2;
38constexpr uint32_t stridesTensorIdx = 3;
39
40constexpr uint32_t outputTensorIdx = 0;
41
42core::StridedSliceParams buildStridedSliceParams(int32_t dims, const int32_t *begin,
43 const int32_t *end, const int32_t *strides,
44 const circle::StridedSliceOptions *options)
45{
47 op_params.start_indices_count = dims;
48 op_params.stop_indices_count = dims;
49 op_params.strides_count = dims;
50
51 for (int i = 0; i < dims; ++i)
52 {
53 op_params.start_indices[i] = begin[i];
54 op_params.stop_indices[i] = end[i];
55 op_params.strides[i] = strides[i];
56 }
57
58 op_params.begin_mask = options->begin_mask();
59 op_params.ellipsis_mask = 0;
60 op_params.end_mask = options->end_mask();
61 op_params.new_axis_mask = 0;
62 op_params.shrink_axis_mask = options->shrink_axis_mask();
63 return op_params;
64}
65
66} // namespace
67
68// NOTE: doesnt currently support dynamic shapes
69OMStatus onert_micro::execute::execute_kernel_CircleStridedSlice(const OMExecuteArgs &execute_args)
70{
71 core::OMRuntimeContext &runtime_context = execute_args.runtime_context;
72 core::OMRuntimeStorage &runtime_storage = execute_args.runtime_storage;
73 uint16_t op_index = execute_args.kernel_index;
74
75 const circle::Tensor *input = nullptr;
76 const circle::Tensor *begin = nullptr;
77 const circle::Tensor *end = nullptr;
78 const circle::Tensor *strides = nullptr;
79
80 const circle::Tensor *output = nullptr;
81
82 uint8_t *input_data;
83 const int32_t *begin_data;
84 const int32_t *end_data;
85 const int32_t *strides_data;
86 uint8_t *output_data;
87
88 OMStatus status = Ok;
89 const circle::StridedSliceOptions *options;
90 // Read kernel
91 {
92 execute::OMRuntimeKernel runtime_kernel;
93 runtime_kernel.readKernel(op_index, runtime_context);
94
95 input = runtime_kernel.inputs[inputTensorIdx];
96 begin = runtime_kernel.inputs[beginTensorIdx];
97 end = runtime_kernel.inputs[endTensorIdx];
98 strides = runtime_kernel.inputs[stridesTensorIdx];
99
100 output = runtime_kernel.outputs[outputTensorIdx];
101 assert(input != nullptr);
102 assert(begin != nullptr);
103 assert(end != nullptr);
104 assert(strides != nullptr);
105 assert(output != nullptr);
106
107 status = runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context);
108 if (status != Ok)
109 return status;
110
111 input_data = runtime_kernel.inputs_data[inputTensorIdx];
112 begin_data = utils::castInputData<int32_t>(runtime_kernel.inputs_data[beginTensorIdx]);
113 end_data = utils::castInputData<int32_t>(runtime_kernel.inputs_data[endTensorIdx]);
114 strides_data = utils::castInputData<int32_t>(runtime_kernel.inputs_data[stridesTensorIdx]);
115 output_data = runtime_kernel.outputs_data[outputTensorIdx];
116
117 assert(input_data != nullptr);
118 assert(begin_data != nullptr);
119 assert(end_data != nullptr);
120 assert(strides_data != nullptr);
121 assert(output_data != nullptr);
122
123 options = runtime_kernel.first_operator->builtin_options_as_StridedSliceOptions();
124 }
125
126 core::OMRuntimeShape input_shape(input);
127
128 auto op_params = buildStridedSliceParams(input_shape.dimensionsCount(), begin_data, end_data,
129 strides_data, options);
130
131 switch (input->type())
132 {
133#ifndef DIS_FLOAT
134 case circle::TensorType_FLOAT32:
135 {
136 status = pal::StridedSlice(op_params, input_shape, utils::castInputData<float>(input_data),
137 utils::castOutputData<float>(output_data));
138 }
139 break;
140#endif // DIS_FLOAT
141#ifndef DIS_QUANT
142 case circle::TensorType_INT8:
143 {
144 status = pal::StridedSlice(op_params, input_shape, utils::castInputData<int8_t>(input_data),
145 utils::castOutputData<int8_t>(output_data));
146 }
147 break;
148#endif // DIS_QUANT
149 case circle::TensorType_INT32:
150 {
151 status = pal::StridedSlice(op_params, input_shape, utils::castInputData<int32_t>(input_data),
152 utils::castOutputData<int32_t>(output_data));
153 }
154 break;
155 default:
156 {
157 status = UnsupportedActivation;
158 assert(false && "Unsupported type.");
159 }
160 }
161
162 return status;
163}
uint8_t * outputs_data[maxOutputSize]
const circle::Operator * first_operator
OMStatus getDataFromStorage(uint16_t op_index, core::OMRuntimeStorage &storage, core::OMRuntimeContext &context)
OMStatus readKernel(uint16_t op_index, core::OMRuntimeContext &runtime_context)
const circle::Tensor * outputs[maxOutputSize]
const circle::Tensor * inputs[maxInputSize]
constexpr uint32_t outputTensorIdx
list input_data
Definition infer.py:29
ShapeIterator end(const Shape &s)
StridedSliceParams buildStridedSliceParams(const T *begin, const T *end, const T *strides, const uint32_t begin_mask, const uint32_t end_mask, const uint32_t shrink_axis_mask, const uint8_t rank)
OMStatus StridedSlice(core::StridedSliceParams &op_params, const core::OMRuntimeShape &unextended_input_shape, const T *input_data, T *output_data)
@ UnsupportedActivation
Definition OMStatus.h:28
int32_t begin[5]
Definition Slice.cpp:33
core::OMRuntimeContext & runtime_context
core::OMRuntimeStorage & runtime_storage