2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2019 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_DEPTHWISE_CONV_HYBRID_H__
19 #define __NNFW_CKER_REFERENCE_DEPTHWISE_CONV_HYBRID_H__
21 #include "cker/Shape.h"
22 #include "cker/Types.h"
23 #include "cker/Utils.h"
29 namespace reference_integer_ops
32 inline void DepthwiseConvHybridPerChannel(const DepthwiseConvParams ¶ms,
33 float *scaling_factors_ptr, const Shape &input_shape,
34 const int8_t *input_data, const Shape &filter_shape,
35 const int8_t *filter_data, const Shape &bias_shape,
36 const float *bias_data, const Shape &output_shape,
37 float *output_data, const float *per_channel_scale,
38 int32_t *input_offset)
40 const int stride_width = params.stride_width;
41 const int stride_height = params.stride_height;
42 const int dilation_width_factor = params.dilation_width_factor;
43 const int dilation_height_factor = params.dilation_height_factor;
44 const int pad_width = params.padding_values.width;
45 const int pad_height = params.padding_values.height;
46 const int depth_multiplier = params.depth_multiplier;
47 const float output_activation_min = params.float_activation_min;
48 const float output_activation_max = params.float_activation_max;
50 // Check dimensions of the tensors.
51 assert(input_shape.DimensionsCount() == 4);
52 assert(filter_shape.DimensionsCount() == 4);
53 assert(output_shape.DimensionsCount() == 4);
55 const int batches = MatchingDim(input_shape, 0, output_shape, 0);
56 const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3);
57 const int input_height = input_shape.Dims(1);
58 const int input_width = input_shape.Dims(2);
59 const int input_depth = input_shape.Dims(3);
60 const int filter_height = filter_shape.Dims(1);
61 const int filter_width = filter_shape.Dims(2);
62 const int output_height = output_shape.Dims(1);
63 const int output_width = output_shape.Dims(2);
64 const int bias_depth = bias_shape.FlatSize();
65 UNUSED_RELEASE(output_depth);
66 UNUSED_RELEASE(bias_shape);
67 assert(output_depth == input_depth * depth_multiplier);
68 assert(bias_depth == output_depth);
70 for (int batch = 0; batch < batches; ++batch)
72 for (int out_y = 0; out_y < output_height; ++out_y)
74 for (int out_x = 0; out_x < output_width; ++out_x)
76 for (int in_channel = 0; in_channel < input_depth; ++in_channel)
78 for (int m = 0; m < depth_multiplier; ++m)
80 const int output_channel = m + in_channel * depth_multiplier;
81 const int in_x_origin = (out_x * stride_width) - pad_width;
82 const int in_y_origin = (out_y * stride_height) - pad_height;
84 for (int filter_y = 0; filter_y < filter_height; ++filter_y)
86 for (int filter_x = 0; filter_x < filter_width; ++filter_x)
88 const int in_x = in_x_origin + dilation_width_factor * filter_x;
89 const int in_y = in_y_origin + dilation_height_factor * filter_y;
90 // Zero padding by omitting the areas outside the image.
91 const bool is_point_inside_image =
92 (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height);
93 if (is_point_inside_image)
96 input_data[Offset(input_shape, batch, in_y, in_x, in_channel)];
98 filter_data[Offset(filter_shape, 0, filter_y, filter_x, output_channel)];
99 acc += filter_val * (input_val - input_offset[batch]);
103 float acc_float = static_cast<float>(acc);
104 acc_float *= per_channel_scale[output_channel] * scaling_factors_ptr[batch];
105 if (bias_data && output_channel < bias_depth)
107 acc_float += bias_data[output_channel];
109 output_data[Offset(output_shape, batch, out_y, out_x, output_channel)] =
110 ActivationFunctionWithMinMax(acc_float, output_activation_min, output_activation_max);
118 } // namespace reference_integer_ops
122 #endif // __NNFW_CKER_REFERENCE_DEPTHWISE_CONV_HYBRID_H__