2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef __NNFW_CKER_REFERENCE_CONV_H__
19 #define __NNFW_CKER_REFERENCE_CONV_H__
21 #include "cker/Shape.h"
22 #include "cker/Types.h"
33 inline void Conv(const ConvParams ¶ms, const Shape &input_shape, const float *input_data,
34 const Shape &filter_shape, const float *filter_data, const Shape &bias_shape,
35 const float *bias_data, const Shape &output_shape, float *output_data)
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.padding_values.width;
42 const int pad_height = params.padding_values.height;
43 const float output_activation_min = params.float_activation_min;
44 const float output_activation_max = params.float_activation_max;
45 assert(input_shape.DimensionsCount() == 4);
46 assert(filter_shape.DimensionsCount() == 4);
47 assert(output_shape.DimensionsCount() == 4);
48 UNUSED_RELEASE(bias_shape);
50 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
51 const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3);
52 const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
55 assert(bias_shape.FlatSize() == output_depth);
57 const int input_height = input_shape.Dims(1);
58 const int input_width = input_shape.Dims(2);
59 const int filter_height = filter_shape.Dims(1);
60 const int filter_width = filter_shape.Dims(2);
61 const int output_height = output_shape.Dims(1);
62 const int output_width = output_shape.Dims(2);
63 for (int batch = 0; batch < batches; ++batch)
65 for (int out_y = 0; out_y < output_height; ++out_y)
67 for (int out_x = 0; out_x < output_width; ++out_x)
69 for (int out_channel = 0; out_channel < output_depth; ++out_channel)
71 const int in_x_origin = (out_x * stride_width) - pad_width;
72 const int in_y_origin = (out_y * stride_height) - pad_height;
74 for (int filter_y = 0; filter_y < filter_height; ++filter_y)
76 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
78 const int in_x = in_x_origin + dilation_width_factor * filter_x;
79 const int in_y = in_y_origin + dilation_height_factor * filter_y;
80 // If the location is outside the bounds of the input image,
81 // use zero as a default value.
82 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height))
84 const int in_offset = Offset(input_shape, batch, in_y, in_x, 0);
85 const int filter_offset = Offset(filter_shape, out_channel, filter_y, filter_x, 0);
86 for (int in_channel = 0; in_channel < input_depth; ++in_channel)
88 float input_value = input_data[in_offset + in_channel];
89 float filter_value = filter_data[filter_offset + in_channel];
90 total += (input_value * filter_value);
95 float bias_value = 0.0f;
98 bias_value = bias_data[out_channel];
100 output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] =
101 ActivationFunctionWithMinMax(total + bias_value, output_activation_min,
102 output_activation_max);
109 inline void Conv(const ConvParams ¶ms, const Shape &input_shape, const uint8_t *input_data,
110 const Shape &filter_shape, const uint8_t *filter_data, const Shape &bias_shape,
111 const int32_t *bias_data, const Shape &output_shape, uint8_t *output_data)
113 const int stride_width = params.stride_width;
114 const int stride_height = params.stride_height;
115 const int dilation_width_factor = params.dilation_width_factor;
116 const int dilation_height_factor = params.dilation_height_factor;
117 const int pad_width = params.padding_values.width;
118 const int pad_height = params.padding_values.height;
119 const int32_t input_offset = params.input_offset;
120 const int32_t filter_offset = params.weights_offset;
121 const int32_t output_offset = params.output_offset;
122 const int32_t output_multiplier = params.output_multiplier;
123 const int output_shift = params.output_shift;
124 const int32_t output_activation_min = params.quantized_activation_min;
125 const int32_t output_activation_max = params.quantized_activation_max;
126 assert(output_activation_min <= output_activation_max);
128 assert(input_shape.DimensionsCount() == 4);
129 assert(filter_shape.DimensionsCount() == 4);
130 assert(output_shape.DimensionsCount() == 4);
131 UNUSED_RELEASE(bias_shape);
132 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
133 const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3);
134 const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
137 assert(bias_shape.FlatSize() == output_depth);
139 const int input_height = input_shape.Dims(1);
140 const int input_width = input_shape.Dims(2);
141 const int filter_height = filter_shape.Dims(1);
142 const int filter_width = filter_shape.Dims(2);
143 const int output_height = output_shape.Dims(1);
144 const int output_width = output_shape.Dims(2);
145 for (int batch = 0; batch < batches; ++batch)
147 for (int out_y = 0; out_y < output_height; ++out_y)
149 for (int out_x = 0; out_x < output_width; ++out_x)
151 for (int out_channel = 0; out_channel < output_depth; ++out_channel)
153 const int in_x_origin = (out_x * stride_width) - pad_width;
154 const int in_y_origin = (out_y * stride_height) - pad_height;
156 for (int filter_y = 0; filter_y < filter_height; ++filter_y)
158 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
160 const int in_x = in_x_origin + dilation_width_factor * filter_x;
161 const int in_y = in_y_origin + dilation_height_factor * filter_y;
162 // If the location is outside the bounds of the input image,
163 // use zero as a default value.
164 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height))
166 const int in_base = Offset(input_shape, batch, in_y, in_x, 0);
167 const int filter_base = Offset(filter_shape, out_channel, filter_y, filter_x, 0);
168 for (int in_channel = 0; in_channel < input_depth; in_channel++)
170 int32_t input_val = input_data[in_channel + in_base];
171 int32_t filter_val = filter_data[in_channel + filter_base];
172 acc += (filter_val + filter_offset) * (input_val + input_offset);
179 acc += bias_data[out_channel];
181 acc = MultiplyByQuantizedMultiplier(acc, output_multiplier, output_shift);
182 acc += output_offset;
183 acc = std::max(acc, output_activation_min);
184 acc = std::min(acc, output_activation_max);
185 output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] =
186 static_cast<uint8_t>(acc);
193 } // namespace reference
197 #endif // __NNFW_CKER_REFERENCE_CONV_H__