ONE - On-device Neural Engine
Loading...
Searching...
No Matches
DepthwiseConv2D.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
42onert_micro::import::configure_kernel_CircleDepthwiseConv2D(const OMConfigureArgs &config_args)
43{
44 OMRuntimeContext &runtime_context = config_args.runtime_context;
45 uint16_t op_index = config_args.kernel_index;
46
47 execute::OMRuntimeKernel runtime_kernel;
48 runtime_kernel.readKernel(op_index, runtime_context);
49
50 const circle::Tensor *input = runtime_kernel.inputs[inputTensorIdx];
51 const circle::Tensor *weight = runtime_kernel.inputs[weightTensorIdx];
52 const circle::Tensor *bias = runtime_kernel.inputs[biasTensorIdx];
53
54 const circle::Tensor *output = runtime_kernel.outputs[outputTensorIdx];
55
56 assert(input != nullptr);
57 assert(weight != nullptr);
58 // Bias can be nullptr
59 assert(output != nullptr);
60
61 OMStatus status = Ok;
62 const auto *options = runtime_kernel.first_operator->builtin_options_as_DepthwiseConv2DOptions();
63
64 core::OMRuntimeShape input_shape(input);
65 core::OMRuntimeShape weight_shape(weight);
66 core::OMRuntimeShape bias_shape(bias);
68
69 status = utils::checkCondition(input->type() == output->type());
70 if (status != Ok)
71 return status;
72
73 status = utils::checkCondition(input->type() == weight->type());
74 if (status != Ok)
75 return status;
76
77 status = utils::checkCondition(input_shape.dimensionsCount() == 4);
78 if (status != Ok)
79 return status;
80
81 status = utils::checkCondition(input_shape.dimensionsCount() == output_shape.dimensionsCount());
82 if (status != Ok)
83 return status;
84
85 status = utils::checkCondition(input_shape.dimensionsCount() == weight_shape.dimensionsCount());
86 if (status != Ok)
87 return status;
88
89 const auto output_depth = output_shape.dims(3);
90
91 status = utils::checkCondition(
92 bias == nullptr or (bias_shape.dimensionsCount() == 1 && bias_shape.dims(0) == output_depth));
93 if (status != Ok)
94 return status;
95
96 switch (options->fused_activation_function())
97 {
98 case circle::ActivationFunctionType_NONE:
99 case circle::ActivationFunctionType_RELU:
100 case circle::ActivationFunctionType_RELU6:
101 case circle::ActivationFunctionType_RELU_N1_TO_1:
102 break;
103 default:
105 }
106
107 if (input->type() == circle::TensorType_FLOAT32)
108 {
109 status = utils::checkCondition(bias == nullptr or input->type() == bias->type());
110 return status;
111 }
112
113 auto input_quant = input->quantization();
114 auto filter_quant = weight->quantization();
115 auto output_quant = output->quantization();
116
117 status = utils::checkCondition(input_quant != nullptr and filter_quant != nullptr and
118 output_quant != nullptr);
119 if (status != Ok)
120 return status;
121
122 auto input_scales = input_quant->scale();
123 auto filter_scales = filter_quant->scale();
124 auto output_scales = output_quant->scale();
125
126 status = utils::checkCondition(input_scales != nullptr and filter_scales != nullptr and
127 output_scales != nullptr);
128 if (status != Ok)
129 return status;
130
131 // Support only per channel
132 status = utils::checkCondition(filter_scales->size() > 1);
133 if (status != Ok)
134 return status;
135
136 return status;
137}
int32_t dimensionsCount() const
Definition Tensor.h:106
int32_t dims(int i) const
Definition Tensor.h:108
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]
const luci_interpreter::RuntimeShape output_shape
constexpr uint32_t outputTensorIdx
@ UnsupportedActivation
Definition OMStatus.h:28