ONE - On-device Neural Engine
Loading...
Searching...
No Matches
PALDepthwiseConv2D.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 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#ifndef ONERT_MICRO_EXECUTE_PAL_DEPTHWISE_CONV_2D_H
19#define ONERT_MICRO_EXECUTE_PAL_DEPTHWISE_CONV_2D_H
20
21#include "PALDepthwiseConv2DCommon.h"
22
23namespace onert_micro
24{
25namespace execute
26{
27namespace pal
28{
29
30OMStatus DepthwiseConvPerChannel(const core::ConvQuant &params,
31 const core::OMRuntimeShape &input_shape, const int8_t *input_data,
32 const core::OMRuntimeShape &filter_shape,
33 const int8_t *filter_data, const int32_t *bias_data,
34 const core::OMRuntimeShape &output_shape, int8_t *output_data)
35{
36 // Get parameters.
37 const int stride_width = params.stride_width;
38 const int stride_height = params.stride_height;
39 const int dilation_width_factor = params.dilation_width_factor;
40 const int dilation_height_factor = params.dilation_height_factor;
41 const int pad_width = params.pad_w;
42 const int pad_height = params.pad_h;
43 const int depth_multiplier = params.depth_multiplier;
44 const int32_t input_offset = params.input_offset;
45 const int32_t output_offset = params.output_offset;
46 const int32_t output_activation_min = params.quantized_activation_min;
47 const int32_t output_activation_max = params.quantized_activation_max;
48
49 const auto &output_multiplier = params.per_channel_output_multiplier;
50 const auto &output_shift = params.per_channel_output_shift;
51
52 // Check dimensions of the tensors.
53 assert(input_shape.dimensionsCount() == 4);
54 assert(filter_shape.dimensionsCount() == 4);
55 assert(output_shape.dimensionsCount() == 4);
56
57 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
58 const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3);
59 const int input_height = input_shape.dims(1);
60 const int input_width = input_shape.dims(2);
61 const int input_depth = input_shape.dims(3);
62 const int filter_height = filter_shape.dims(1);
63 const int filter_width = filter_shape.dims(2);
64 const int output_height = output_shape.dims(1);
65 const int output_width = output_shape.dims(2);
66 assert(output_depth == input_depth * depth_multiplier);
67
68 for (int batch = 0; batch < batches; ++batch)
69 {
70 for (int out_y = 0; out_y < output_height; ++out_y)
71 {
72 for (int out_x = 0; out_x < output_width; ++out_x)
73 {
74 for (int in_channel = 0; in_channel < input_depth; ++in_channel)
75 {
76 for (int m = 0; m < depth_multiplier; ++m)
77 {
78 const int output_channel = m + in_channel * depth_multiplier;
79 const int in_x_origin = (out_x * stride_width) - pad_width;
80 const int in_y_origin = (out_y * stride_height) - pad_height;
81 int32_t acc = 0;
82 for (int filter_y = 0; filter_y < filter_height; ++filter_y)
83 {
84 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
85 {
86 const int in_x = in_x_origin + dilation_width_factor * filter_x;
87 const int in_y = in_y_origin + dilation_height_factor * filter_y;
88 // Zero padding by omitting the areas outside the image.
89 const bool is_point_inside_image =
90 (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height);
91 if (is_point_inside_image)
92 {
93 int32_t input_val =
94 input_data[offset(input_shape.dimsData(), batch, in_y, in_x, in_channel)];
95 int32_t filter_val = filter_data[offset(filter_shape.dimsData(), 0, filter_y,
96 filter_x, output_channel)];
97 // Accumulate with 32 bits accumulator.
98 // In the nudging process during model quantization, we force
99 // real value of 0.0 be represented by a quantized value. This
100 // guarantees that the input_offset is a int8_t, even though
101 // it is represented using int32_t. int32_t += int8_t *
102 // (int8_t - int8_t) so the highest value we can get from each
103 // accumulation is [-127, 127] * ([-128, 127] -
104 // [-128, 127]), which is [-32512, 32512]. log2(32512)
105 // = 14.98, which means we can accumulate at least 2^16
106 // multiplications without overflow. The accumulator is
107 // applied to a filter so the accumulation logic will hold as
108 // long as the filter size (filter_y * filter_x * in_channel)
109 // does not exceed 2^16, which is the case in all the models
110 // we have seen so far.
111 // accumulator depth is smaller than 2^16.
112 acc += filter_val * (input_val + input_offset);
113 }
114 }
115 }
116 if (bias_data)
117 {
118 acc += bias_data[output_channel];
119 }
120 acc = multiplyByQuantizedMultiplier(acc, output_multiplier[output_channel],
121 output_shift[output_channel]);
122 acc += output_offset;
123 acc = std::max(acc, output_activation_min);
124 acc = std::min(acc, output_activation_max);
125 output_data[offset(output_shape.dimsData(), batch, out_y, out_x, output_channel)] =
126 static_cast<int8_t>(acc);
127 }
128 }
129 }
130 }
131 }
132 return Ok;
133}
134
135} // namespace pal
136} // namespace execute
137} // namespace onert_micro
138#endif // ONERT_MICRO_EXECUTE_PAL_ARG_MAX_H
int32_t dimensionsCount() const
Definition Tensor.h:106
int32_t dims(int i) const
Definition Tensor.h:108
const luci_interpreter::RuntimeShape output_shape
list input_data
Definition infer.py:29
int MatchingDim(const core::OMRuntimeShape &shape1, int index1, const core::OMRuntimeShape &shape2, int index2)
Definition PALUtils.h:200
int offset(const int32_t *dims_data, int i0, int i1, int i2, int i3)
Definition PALUtils.h:220
OMStatus DepthwiseConvPerChannel(const core::ConvQuant &params, const core::OMRuntimeShape &input_shape, const int8_t *input_data, const core::OMRuntimeShape &filter_shape, const int8_t *filter_data, const int32_t *bias_data, const core::OMRuntimeShape &output_shape, int8_t *output_data)
int32_t multiplyByQuantizedMultiplier(int32_t x, int32_t quantized_multiplier, int shift)
Definition PALUtils.h:104