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 return UnsupportedType;
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 return status;
83
84 status = utils::checkCondition(input_shape.dimensionsCount() == output_shape.dimensionsCount());
85 if (status != Ok)
86 return status;
87
88 status = utils::checkCondition(input_shape.dimensionsCount() == weight_shape.dimensionsCount());
89 if (status != Ok)
90 return status;
91
92 status = utils::checkCondition(bias == nullptr or weight_shape.dims(0) == bias_shape.flatSize());
93
94 if (input->type() == circle::TensorType_FLOAT32)
95 return status;
96
97 auto input_quant = input->quantization();
98 auto filter_quant = weight->quantization();
99 auto output_quant = output->quantization();
100
101 status = utils::checkCondition(input_quant != nullptr and filter_quant != nullptr and
102 output_quant != nullptr);
103 if (status != Ok)
104 return status;
105
106 auto input_scales = input_quant->scale();
107 auto filter_scales = filter_quant->scale();
108 auto output_scales = output_quant->scale();
109
110 status = utils::checkCondition(input_scales != nullptr and filter_scales != nullptr and
111 output_scales != nullptr);
112 if (status != Ok)
113 return status;
114
115 // Support only per channel
116 status = utils::checkCondition(filter_scales->size() > 1);
117 if (status != Ok)
118 return status;
119
120 return status;
121}
122
123} // namespace import
124} // namespace onert_micro
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