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
41OMStatus onert_micro::import::configure_kernel_CircleConv2D(const OMConfigureArgs &config_args)
42{
43 OMRuntimeContext &runtime_context = config_args.runtime_context;
44 uint16_t op_index = config_args.kernel_index;
45
46 execute::OMRuntimeKernel runtime_kernel;
47 runtime_kernel.readKernel(op_index, runtime_context);
48
49 const circle::Tensor *input = runtime_kernel.inputs[inputTensorIdx];
50 const circle::Tensor *weight = runtime_kernel.inputs[weightTensorIdx];
51 const circle::Tensor *bias = runtime_kernel.inputs[biasTensorIdx];
52
53 const circle::Tensor *output = runtime_kernel.outputs[outputTensorIdx];
54
55 assert(input != nullptr);
56 assert(weight != nullptr);
57 // Bias can be nullptr
58 assert(output != nullptr);
59
60 OMStatus status = Ok;
61
62 if ((input->type() == circle::TensorType_FLOAT32 &&
63 weight->type() != circle::TensorType_FLOAT32) or
64 (input->type() == circle::TensorType_INT8 && weight->type() != circle::TensorType_INT8) or
65 (input->type() == circle::TensorType_INT16 && weight->type() != circle::TensorType_INT16))
66 {
67 return UnsupportedType;
68 }
69
70 core::OMRuntimeShape input_shape(input);
71 core::OMRuntimeShape weight_shape(weight);
72 core::OMRuntimeShape bias_shape(bias);
74
75 status = utils::checkCondition(input_shape.dimensionsCount() == 4);
76 if (status != Ok)
77 return status;
78
79 status = utils::checkCondition(input_shape.dimensionsCount() == output_shape.dimensionsCount());
80 if (status != Ok)
81 return status;
82
83 status = utils::checkCondition(input_shape.dimensionsCount() == weight_shape.dimensionsCount());
84 if (status != Ok)
85 return status;
86
87 status = utils::checkCondition(bias == nullptr or weight_shape.dims(0) == bias_shape.flatSize());
88
89 if (input->type() == circle::TensorType_FLOAT32)
90 return status;
91
92 auto input_quant = input->quantization();
93 auto filter_quant = weight->quantization();
94 auto output_quant = output->quantization();
95
96 status = utils::checkCondition(input_quant != nullptr and filter_quant != nullptr and
97 output_quant != nullptr);
98 if (status != Ok)
99 return status;
100
101 auto input_scales = input_quant->scale();
102 auto filter_scales = filter_quant->scale();
103 auto output_scales = output_quant->scale();
104
105 status = utils::checkCondition(input_scales != nullptr and filter_scales != nullptr and
106 output_scales != nullptr);
107 if (status != Ok)
108 return status;
109
110 // Support only per channel
111 status = utils::checkCondition(filter_scales->size() > 1);
112 if (status != Ok)
113 return status;
114
115 return status;
116}
int32_t dimensionsCount() const
Definition Tensor.h:106
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
@ UnsupportedType
Definition OMStatus.h:26