ONE - On-device Neural Engine
Loading...
Searching...
No Matches
PALConv2D.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_CONV_2D_H
19#define ONERT_MICRO_EXECUTE_PAL_CONV_2D_H
20
21#include "PALConv2DCommon.h"
22#include "core/OMKernelData.h"
23#include "core/OMRuntimeShape.h"
24#include "PALUtils.h"
25
26namespace onert_micro
27{
28namespace execute
29{
30namespace pal
31{
32
33// Fixed-point per-channel-quantization convolution reference kernel.
34OMStatus ConvPerChannel(const core::ConvQuant &params, const core::OMRuntimeShape &input_shape,
35 const int8_t *input_data, const core::OMRuntimeShape &filter_shape,
36 const int8_t *filter_data, const int32_t *bias_data,
37 const core::OMRuntimeShape &output_shape, int8_t *output_data)
38{
39 // Get parameters.
40 const int32_t input_offset = params.input_offset; // r = s(q - Z)
41 const int stride_width = params.stride_width;
42 const int stride_height = params.stride_height;
43 const int dilation_width_factor = params.dilation_width_factor;
44 const int dilation_height_factor = params.dilation_height_factor;
45 const int pad_width = params.pad_w;
46 const int pad_height = params.pad_h;
47 const int32_t output_offset = params.output_offset;
48
49 const auto &output_multiplier = params.per_channel_output_multiplier;
50 const auto &output_shift = params.per_channel_output_shift;
51
52 // Set min and max value of the output.
53 const int32_t output_activation_min = params.quantized_activation_min;
54 const int32_t output_activation_max = params.quantized_activation_max;
55
56 // Consistency check.
57 assert(output_activation_max >= output_activation_min);
58 assert(input_shape.dimensionsCount() == 4);
59 assert(filter_shape.dimensionsCount() == 4);
60 assert(output_shape.dimensionsCount() == 4);
61
62 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
63 const int input_depth = input_shape.dims(3);
64 const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
65
66 // Check dimensions of the tensors.
67 const int input_height = input_shape.dims(1);
68 const int input_width = input_shape.dims(2);
69 const int filter_height = filter_shape.dims(1);
70 const int filter_width = filter_shape.dims(2);
71 const int filter_input_depth = filter_shape.dims(3);
72 const int groups = input_depth / filter_input_depth;
73 assert(groups != 0);
74 assert(input_depth % filter_input_depth == 0);
75 const int filters_per_group = output_depth / groups;
76 assert(filters_per_group != 0);
77 const int output_height = output_shape.dims(1);
78 const int output_width = output_shape.dims(2);
79 for (int batch = 0; batch < batches; ++batch)
80 {
81 for (int out_y = 0; out_y < output_height; ++out_y)
82 {
83 const int in_y_origin = (out_y * stride_height) - pad_height;
84 for (int out_x = 0; out_x < output_width; ++out_x)
85 {
86 const int in_x_origin = (out_x * stride_width) - pad_width;
87 for (int out_channel = 0; out_channel < output_depth; ++out_channel)
88 {
89 auto group = out_channel / filters_per_group;
90 int32_t acc = 0;
91 for (int filter_y = 0; filter_y < filter_height; ++filter_y)
92 {
93 const int in_y = in_y_origin + dilation_height_factor * filter_y;
94 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
95 {
96 const int in_x = in_x_origin + dilation_width_factor * filter_x;
97
98 // Zero padding by omitting the areas outside the image.
99 const bool is_point_inside_image =
100 (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height);
101
102 if (!is_point_inside_image)
103 {
104 continue;
105 }
106
107 for (int in_channel = 0; in_channel < filter_input_depth; ++in_channel)
108 {
109 int32_t input_val = input_data[offset(input_shape.dimsData(), batch, in_y, in_x,
110 in_channel + group * filter_input_depth)];
111 int32_t filter_val = filter_data[offset(filter_shape.dimsData(), out_channel,
112 filter_y, filter_x, in_channel)];
113 // Accumulate with 32 bits accumulator.
114 // In the nudging process during model quantization, we force
115 // real value of 0.0 be represented by a quantized value. This
116 // guarantees that the input_offset is a int8_t, even though
117 // it is represented using int32_t. int32_t += int8_t *
118 // (int8_t - int8_t) so the highest value we can get from each
119 // accumulation is [-127, 127] * ([-128, 127] -
120 // [-128, 127]), which is [-32512, 32512]. log2(32512)
121 // = 14.98, which means we can accumulate at least 2^16
122 // multiplications without overflow. The accumulator is
123 // applied to a filter so the accumulation logic will hold as
124 // long as the filter size (filter_y * filter_x * in_channel)
125 // does not exceed 2^16, which is the case in all the models
126 // we have seen so far.
127 // accumulator depth is smaller than 2^16.
128 acc += filter_val * (input_val + input_offset);
129 }
130 }
131 }
132
133 if (bias_data)
134 {
135 acc += bias_data[out_channel];
136 }
137 acc = multiplyByQuantizedMultiplier(acc, output_multiplier[out_channel],
138 output_shift[out_channel]);
139 acc += output_offset;
140 acc = std::max(acc, output_activation_min);
141 acc = std::min(acc, output_activation_max);
142 output_data[offset(output_shape.dimsData(), batch, out_y, out_x, out_channel)] =
143 static_cast<int8_t>(acc);
144 }
145 }
146 }
147 }
148 return Ok;
149}
150
151} // namespace pal
152} // namespace execute
153} // namespace onert_micro
154
155#endif // ONERT_MICRO_EXECUTE_PAL_CONV_2D_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
OMStatus ConvPerChannel(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)
Definition PALConv2D.h:36
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
int32_t multiplyByQuantizedMultiplier(int32_t x, int32_t quantized_multiplier, int shift)
Definition PALUtils.h:104