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