ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Gather.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
26using namespace onert_micro;
27using namespace onert_micro::core;
28using namespace onert_micro::execute;
29
30namespace
31{
32
33constexpr uint32_t inputTensorIdx = 0;
34constexpr uint32_t positionsTensorIdx = 1;
35
36constexpr uint32_t outputTensorIdx = 0;
37
38template <typename InputT, typename CoordsT = int32_t>
39void gather(const InputT *input_data, const CoordsT *coords_data, InputT *output_data,
40 int32_t axis_size, int32_t batch_size, int32_t outer_size, int32_t inner_size,
41 int32_t coord_size)
42{
43
44 for (int batch = 0; batch < batch_size; ++batch)
45 {
46 for (int outer = 0; outer < outer_size; ++outer)
47 {
48 for (int coord = 0; coord < coord_size; ++coord)
49 {
50 auto x = coords_data[coord];
51 std::memcpy(
52 output_data + (((batch * outer_size) + outer) * coord_size + coord) * inner_size,
53 input_data +
54 (((batch * outer_size) + outer) * axis_size + coords_data[batch * coord_size + coord]) *
55 inner_size,
56 sizeof(InputT) * inner_size);
57 }
58 }
59 }
60}
61
62} // namespace
63
64// NOTE: doesn't currently support dynamic shapes
65OMStatus onert_micro::execute::execute_kernel_CircleGather(const OMExecuteArgs &execute_args)
66{
67 core::OMRuntimeContext &runtime_context = execute_args.runtime_context;
68 core::OMRuntimeStorage &runtime_storage = execute_args.runtime_storage;
69 uint16_t op_index = execute_args.kernel_index;
70
71 const circle::Tensor *input;
72 const circle::Tensor *position;
73 const circle::Tensor *output;
74
75 uint8_t *input_data;
76 uint8_t *position_data;
77 uint8_t *output_data;
78
79 const circle::GatherOptions *options;
80 // Read kernel
81 {
82 execute::OMRuntimeKernel runtime_kernel;
83 OMStatus status = runtime_kernel.readKernel(op_index, runtime_context);
84 if (status != Ok)
85 return status;
86
87 input = runtime_kernel.inputs[inputTensorIdx];
88 position = runtime_kernel.inputs[positionsTensorIdx];
89 output = runtime_kernel.outputs[outputTensorIdx];
90 assert(input != nullptr);
91 assert(position != nullptr);
92 assert(output != nullptr);
93
94 status = runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context);
95 if (status != Ok)
96 return status;
97
98 input_data = runtime_kernel.inputs_data[inputTensorIdx];
99 position_data = runtime_kernel.inputs_data[positionsTensorIdx];
100 output_data = runtime_kernel.outputs_data[outputTensorIdx];
101 assert(input_data != nullptr);
102 assert(position_data != nullptr);
103 assert(output_data != nullptr);
104
105 options = runtime_kernel.first_operator->builtin_options_as_GatherOptions();
106 }
107
108 OMStatus status = Ok;
109
110 OMRuntimeShape position_shape(position);
111 OMRuntimeShape input_shape(input);
112
113 const int input_dims_size = input_shape.dimensionsCount();
114 int axis = options->axis();
115 if (axis < 0)
116 {
117 axis += input_dims_size;
118 }
119
120 int batch_dims = options->batch_dims();
121 // batch_dims should be in range: [-rank(coords), rank(coords)].
122 // Negative batch_dims is added with rank of coords.
123 const int coords_dims_size = position_shape.dimensionsCount();
124 if (batch_dims < 0)
125 {
126 batch_dims += coords_dims_size;
127 }
128
129 const int axis_size = input_shape.dims(axis);
130
131 int batch_size = 1;
132 for (int i = 0; i < batch_dims; ++i)
133 {
134 batch_size *= input_shape.dims(i);
135 }
136 int outer_size = 1;
137 for (int i = batch_dims; i < axis; ++i)
138 {
139 outer_size *= input_shape.dims(i);
140 }
141 int inner_size = 1;
142 for (int i = axis + 1; i < input_dims_size; ++i)
143 {
144 inner_size *= input_shape.dims(i);
145 }
146 int coord_size = 1;
147 for (int i = batch_dims; i < coords_dims_size; ++i)
148 {
149 coord_size *= position_shape.dims(i);
150 }
151
152 switch (input->type())
153 {
154#ifndef DIS_FLOAT
155 case circle::TensorType_FLOAT32:
156 {
157 gather<float, int32_t>(utils::castInputData<float>(input_data),
158 utils::castInputData<int32_t>(position_data),
159 utils::castOutputData<float>(output_data), axis_size, batch_size,
160 outer_size, inner_size, coord_size);
161 }
162 break;
163#endif // DIS_FLOAT
164#ifndef DIS_QUANT
165 case circle::TensorType_INT8:
166 {
167 gather<int8_t, int32_t>(utils::castInputData<int8_t>(input_data),
168 utils::castInputData<int32_t>(position_data),
169 utils::castOutputData<int8_t>(output_data), axis_size, batch_size,
170 outer_size, inner_size, coord_size);
171 }
172 break;
173#endif // DIS_QUANT
174 case circle::TensorType_INT32:
175 {
176 gather<int32_t, int32_t>(utils::castInputData<int32_t>(input_data),
177 utils::castInputData<int32_t>(position_data),
178 utils::castOutputData<int32_t>(output_data), axis_size, batch_size,
179 outer_size, inner_size, coord_size);
180 }
181 break;
182 default:
183 {
184 status = UnsupportedActivation;
185 assert(false && "Unsupported type.");
186 }
187 }
188
189 return status;
190}
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
@ UnsupportedActivation
Definition OMStatus.h:28
core::OMRuntimeContext & runtime_context
core::OMRuntimeStorage & runtime_storage