ONE - On-device Neural Engine
Loading...
Searching...
No Matches
DepthwiseConv2D.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "ConvolutionCommon.h"
19#include "kernels/Utils.h"
20
21#include "PALDepthwiseConv2D.h"
22
23namespace luci_interpreter
24{
25
26namespace
27{
28
29#ifndef DIS_FLOAT
30
31void evalFloat(const circle::Tensor *input, const circle::Tensor *filter,
32 const circle::Tensor *bias, const circle::Tensor *output,
33 const circle::DepthwiseConv2DOptions *options, BaseRuntimeGraph *runtime_graph)
34{
35 float activation_min{};
36 float activation_max{};
37 kernels::calculateActivationRange(luci_actfunc(options->fused_activation_function()),
38 &activation_min, &activation_max);
39
42 input, filter, options->padding(), options->stride_h(), options->dilation_h_factor(), 1);
43 params.padding_values.width = computeConvPadding(
44 input, filter, options->padding(), options->stride_w(), options->dilation_w_factor(), 2);
45 params.stride_height = options->stride_h();
46 params.stride_width = options->stride_w();
47 params.dilation_height_factor = options->dilation_h_factor();
48 params.dilation_width_factor = options->dilation_w_factor();
49 params.float_activation_min = activation_min;
50 params.float_activation_max = activation_max;
51 params.depth_multiplier = options->depth_multiplier();
52
53 auto *input_data = runtime_graph->getDataByTensor(input);
54 auto *output_data = runtime_graph->getDataByTensor(output);
55
56 auto *filter_data = runtime_graph->getConstDataByTensor(filter);
57 auto *bias_data = runtime_graph->getConstDataByTensor(bias);
58
59 int32_t input_shape[kMaxSmallSize];
60 kernels::getTensorDims(input, runtime_graph, input_shape);
61
62 int32_t filter_shape[kMaxSmallSize];
63 kernels::getTensorDims(filter, runtime_graph, filter_shape);
64
65 int32_t output_shape[kMaxSmallSize];
66 kernels::getTensorDims(output, runtime_graph, output_shape);
67
68 luci_interpreter_pal::DepthwiseConv2D(
69 params, input_shape, kernels::getTensorData<float>(input_data), filter_shape,
70 kernels::getTensorData<float>(filter_data), kernels::getTensorData<float>(bias_data),
71 output_shape, kernels::getTensorData<float>(output_data));
72}
73
74#endif // DIS_FLOAT
75
76} // namespace
77
78void configure_kernel_CircleDepthwiseConv2D(const circle::Operator *cur_op,
79 BaseRuntimeGraph *runtime_graph)
80{
81 kernels::DownsamplingConv2DKernel kernel(cur_op, runtime_graph);
82
83 const auto input = kernel.input();
84 const auto filter = kernel.filter();
85 const auto bias = kernel.bias();
86 const auto output = kernel.output();
87
88 auto filter_data = runtime_graph->getConstDataByTensor(filter);
89
90 assert(filter_data != nullptr);
91
92 const auto *options = cur_op->builtin_options_as_DepthwiseConv2DOptions();
93
94 if (Tensor::element_type(input) == DataType::FLOAT32 &&
95 Tensor::element_type(filter) == DataType::FLOAT32)
96 {
97 LUCI_INTERPRETER_CHECK(bias == nullptr || Tensor::element_type(bias) == DataType::FLOAT32);
98 }
99 else
100 {
101 assert(false && "Unsupported type.");
102 }
103 LUCI_INTERPRETER_CHECK(Tensor::element_type(output) == Tensor::element_type(input));
104 LUCI_INTERPRETER_CHECK(Tensor::num_dims(input) == 4 && Tensor::num_dims(filter) == 4);
105
106 const int32_t output_depth = Tensor::dim(output, 3);
107 LUCI_INTERPRETER_CHECK(bias == nullptr ||
108 (Tensor::num_dims(bias) == 1 && Tensor::dim(bias, 0) == output_depth));
109
110 switch (options->fused_activation_function())
111 {
112 case circle::ActivationFunctionType_NONE:
113 case circle::ActivationFunctionType_RELU:
114 case circle::ActivationFunctionType_RELU6:
115 case circle::ActivationFunctionType_RELU_N1_TO_1:
116 break;
117 default:
118 assert(false && "Unsupported fused activation");
119 }
120}
121
122void execute_kernel_CircleDepthwiseConv2D(const circle::Operator *cur_op,
123 BaseRuntimeGraph *runtime_graph)
124{
125 kernels::DownsamplingConv2DKernel kernel(cur_op, runtime_graph);
126
127 const auto input = kernel.input();
128 const auto weights = kernel.filter();
129 const auto bias = kernel.bias();
130 const auto output = kernel.output();
131
132 const auto *options = cur_op->builtin_options_as_DepthwiseConv2DOptions();
133
134 switch (Tensor::element_type(input))
135 {
136#ifndef DIS_FLOAT
137 case DataType::FLOAT32:
138 if (Tensor::element_type(weights) == DataType::FLOAT32)
139 {
140 evalFloat(input, weights, bias, output, options, runtime_graph);
141 break;
142 }
143#endif // DIS_FLOAT
144 default:
145 assert(false && "Unsupported type.");
146 }
147}
148
149} // namespace luci_interpreter
uint8_t * getConstDataByTensor(const circle::Tensor *raw_tensor)
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
const luci_interpreter::RuntimeShape output_shape
list input_data
Definition infer.py:29
void calculateActivationRange(Activation activation, T *activation_min, T *activation_max)
Definition Utils.cpp:52
void getTensorDims(const circle::Tensor *tensor, BaseRuntimeGraph *runtime_graph, int32_t *dims)
Definition Utils.h:121
void configure_kernel_CircleDepthwiseConv2D(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
RuntimeGraph BaseRuntimeGraph
int32_t computeConvPadding(const circle::Tensor *input, const circle::Tensor *filter, circle::Padding padding_type, int32_t stride, int32_t dilation, int axis)
void execute_kernel_CircleDepthwiseConv2D(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
FusedActFunc luci_actfunc(const circle::ActivationFunctionType type)
const loco::Dimension & dim(uint32_t axis) const
Definition Tensor.h:44