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"
22
23using namespace onert_micro;
24using namespace onert_micro::core;
25
26namespace
27{
28
29constexpr uint32_t inputTensorIdx = 0;
30constexpr uint32_t positionsTensorIdx = 1;
31constexpr uint32_t outputTensorIdx = 0;
32
33} // namespace
34
35namespace onert_micro
36{
37namespace import
38{
39
41{
42 OMRuntimeContext &runtime_context = config_args.runtime_context;
43 uint16_t op_index = config_args.kernel_index;
44
46
47 OMStatus status = runtime_kernel.readKernel(op_index, runtime_context);
48 if (status != Ok)
49 return status;
50
51 const circle::Tensor *input = runtime_kernel.inputs[inputTensorIdx];
52 const circle::Tensor *positions = runtime_kernel.inputs[positionsTensorIdx];
53 const circle::Tensor *output = runtime_kernel.outputs[outputTensorIdx];
54
55 assert(input != nullptr);
56 assert(positions != nullptr);
57 assert(output != nullptr);
58
59 status = utils::checkCondition(input->type() == output->type());
60 if (status != Ok)
61 return status;
62
63 const auto *options = runtime_kernel.first_operator->builtin_options_as_GatherOptions();
64
65 if (options == nullptr)
66 return UnknownError;
67
68 status = utils::checkCondition(positions->type() == circle::TensorType_INT32);
69 if (status != Ok)
70 return status;
71
72 auto input_type = input->type();
73 status = utils::checkCondition(input_type == circle::TensorType_INT32 or
74 input_type == circle::TensorType_FLOAT32 or
75 input_type == circle::TensorType_INT8);
76 if (status != Ok)
77 return status;
78
79#ifndef DIS_QUANT
80 if (input_type == circle::TensorType_INT8)
81 {
82 status = utils::checkCondition(*output->quantization()->scale()->begin() ==
83 *input->quantization()->scale()->begin());
84 if (status != Ok)
85 return status;
86 status = utils::checkCondition(*output->quantization()->zero_point()->begin() ==
87 *input->quantization()->zero_point()->begin());
88 if (status != Ok)
89 return status;
90 }
91#endif // DIS_QUANT
92
93 int32_t axis = options->axis();
94
95 core::OMRuntimeShape input_shape(input);
96 core::OMRuntimeShape positions_shape(positions);
97
98 int32_t num_dims = input_shape.dimensionsCount();
99 if (axis < 0)
100 {
101 axis += num_dims;
102 }
103
104 status = utils::checkCondition(axis >= 0 and axis < num_dims);
105 if (status != Ok)
106 return status;
107
108 int32_t batch_dims = options->batch_dims();
109 int32_t coords_num_dims = positions_shape.dimensionsCount();
110 // batch_dims should be in range: [-rank(coords), rank(coords)].
111 // Negative batch_dims is added with rank of coords.
112 if (batch_dims < 0)
113 {
114 batch_dims += coords_num_dims;
115 }
116
117 status = utils::checkCondition(batch_dims <= axis and batch_dims >= 0 and
118 batch_dims < num_dims and batch_dims <= coords_num_dims);
119
120 if (status != Ok)
121 return status;
122
123 for (int i = 0; i < batch_dims; ++i)
124 {
125 status = utils::checkCondition(input_shape.dims(i) == positions_shape.dims(i));
126 }
127
128 return status;
129}
130
131} // namespace import
132} // namespace onert_micro
size_t dimensionsCount() const noexcept
const circle::Operator * first_operator
OMStatus readKernel(uint16_t op_index, core::OMRuntimeContext &runtime_context)
const circle::Tensor * outputs[maxOutputSize]
const circle::Tensor * inputs[maxInputSize]
constexpr uint32_t outputTensorIdx
OMStatus configure_kernel_CircleGather(const OMConfigureArgs &config_args)
Definition Gather.cpp:40
core::OMRuntimeContext & runtime_context