ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Conv2D.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
25#include "execute/OMUtils.h"
26
27using namespace onert_micro;
28using namespace onert_micro::core;
29
30namespace
31{
32
33constexpr uint32_t inputTensorIdx = 0;
34constexpr uint32_t weightTensorIdx = 1;
35constexpr uint32_t biasTensorIdx = 2;
36
37constexpr uint32_t outputTensorIdx = 0;
38
39} // namespace
40
41namespace onert_micro
42{
43namespace import
44{
45
47{
48 OMRuntimeContext &runtime_context = config_args.runtime_context;
49 uint16_t op_index = config_args.kernel_index;
50
51 execute::OMRuntimeKernel runtime_kernel;
52 runtime_kernel.readKernel(op_index, runtime_context);
53
54 const circle::Tensor *input = runtime_kernel.inputs[inputTensorIdx];
55 const circle::Tensor *weight = runtime_kernel.inputs[weightTensorIdx];
56 const circle::Tensor *bias = runtime_kernel.inputs[biasTensorIdx];
57
58 const circle::Tensor *output = runtime_kernel.outputs[outputTensorIdx];
59
60 assert(input != nullptr);
61 assert(weight != nullptr);
62 // Bias can be nullptr
63 assert(output != nullptr);
64
65 OMStatus status = Ok;
66
67 if ((input->type() == circle::TensorType_FLOAT32 &&
68 weight->type() != circle::TensorType_FLOAT32) or
69 (input->type() == circle::TensorType_INT8 && weight->type() != circle::TensorType_INT8) or
70 (input->type() == circle::TensorType_INT16 && weight->type() != circle::TensorType_INT16))
71 {
72 OM_LOG_AND_RETURN(UnsupportedType, "Unsupported type encountered");
73 }
74
75 core::OMRuntimeShape input_shape(input);
76 core::OMRuntimeShape weight_shape(weight);
77 core::OMRuntimeShape bias_shape(bias);
79
80 status = utils::checkCondition(input_shape.dimensionsCount() == 4);
81 if (status != Ok)
82 {
83 OM_LOG_ERROR("Status not OK");
84 return status;
85 }
86
87 status = utils::checkCondition(input_shape.dimensionsCount() == output_shape.dimensionsCount());
88 if (status != Ok)
89 {
90 OM_LOG_ERROR("Status not OK");
91 return status;
92 }
93
94 status = utils::checkCondition(input_shape.dimensionsCount() == weight_shape.dimensionsCount());
95 if (status != Ok)
96 {
97 OM_LOG_ERROR("Status not OK");
98 return status;
99 }
100
101 status = utils::checkCondition(bias == nullptr or weight_shape.dims(0) == bias_shape.flatSize());
102
103 if (input->type() == circle::TensorType_FLOAT32)
104 return status;
105
106 auto input_quant = input->quantization();
107 auto filter_quant = weight->quantization();
108 auto output_quant = output->quantization();
109
110 status = utils::checkCondition(input_quant != nullptr and filter_quant != nullptr and
111 output_quant != nullptr);
112 if (status != Ok)
113 {
114 OM_LOG_ERROR("Status not OK");
115 return status;
116 }
117
118 auto input_scales = input_quant->scale();
119 auto filter_scales = filter_quant->scale();
120 auto output_scales = output_quant->scale();
121
122 status = utils::checkCondition(input_scales != nullptr and filter_scales != nullptr and
123 output_scales != nullptr);
124 if (status != Ok)
125 {
126 OM_LOG_ERROR("Status not OK");
127 return status;
128 }
129
130 // Support only per channel
131 status = utils::checkCondition(filter_scales->size() > 1);
132 if (status != Ok)
133 {
134 OM_LOG_ERROR("Status not OK");
135 return status;
136 }
137
138 return status;
139}
140
141} // namespace import
142} // namespace onert_micro
#define OM_LOG_ERROR(msg)
Definition OMLog.h:24
#define OM_LOG_AND_RETURN(err, msg)
Definition OMLog.h:31
int32_t dimensionsCount() const
Definition Tensor.h:106
size_t dimensionsCount() const noexcept
OMStatus readKernel(uint16_t op_index, core::OMRuntimeContext &runtime_context)
const circle::Tensor * outputs[maxOutputSize]
const circle::Tensor * inputs[maxInputSize]
const luci_interpreter::RuntimeShape output_shape
constexpr uint32_t outputTensorIdx
OMStatus configure_kernel_CircleConv2D(const OMConfigureArgs &config_args)
Definition Conv2D.cpp:46
@ UnsupportedType
Definition OMStatus.h:26
core::OMRuntimeContext & runtime_context