xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/internal/reference/depthwiseconv_float.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_
17 
18 #include "tensorflow/lite/kernels/internal/common.h"
19 #include "tensorflow/lite/kernels/internal/compatibility.h"
20 #include "tensorflow/lite/kernels/internal/types.h"
21 
22 namespace tflite {
23 namespace reference_ops {
24 
DepthwiseConv(const DepthwiseParams & params,const RuntimeShape & input_shape,const float * input_data,const RuntimeShape & filter_shape,const float * filter_data,const RuntimeShape & bias_shape,const float * bias_data,const RuntimeShape & output_shape,float * output_data)25 inline void DepthwiseConv(
26     const DepthwiseParams& params, const RuntimeShape& input_shape,
27     const float* input_data, const RuntimeShape& filter_shape,
28     const float* filter_data, const RuntimeShape& bias_shape,
29     const float* bias_data, const RuntimeShape& output_shape,
30     float* output_data) {
31   const int stride_width = params.stride_width;
32   const int stride_height = params.stride_height;
33   const int dilation_width_factor = params.dilation_width_factor;
34   const int dilation_height_factor = params.dilation_height_factor;
35   const int pad_width = params.padding_values.width;
36   const int pad_height = params.padding_values.height;
37   const int depth_multiplier = params.depth_multiplier;
38   const float output_activation_min = params.float_activation_min;
39   const float output_activation_max = params.float_activation_max;
40   TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4);
41   TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4);
42   TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4);
43 
44   const int batches = MatchingDim(input_shape, 0, output_shape, 0);
45   const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3);
46   const int input_height = input_shape.Dims(1);
47   const int input_width = input_shape.Dims(2);
48   const int input_depth = input_shape.Dims(3);
49   const int filter_height = filter_shape.Dims(1);
50   const int filter_width = filter_shape.Dims(2);
51   const int output_height = output_shape.Dims(1);
52   const int output_width = output_shape.Dims(2);
53   TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier);
54   TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth);
55 
56   for (int b = 0; b < batches; ++b) {
57     for (int out_y = 0; out_y < output_height; ++out_y) {
58       for (int out_x = 0; out_x < output_width; ++out_x) {
59         for (int ic = 0; ic < input_depth; ++ic) {
60           for (int m = 0; m < depth_multiplier; m++) {
61             const int oc = m + ic * depth_multiplier;
62             const int in_x_origin = (out_x * stride_width) - pad_width;
63             const int in_y_origin = (out_y * stride_height) - pad_height;
64             float total = 0.f;
65             for (int filter_y = 0; filter_y < filter_height; ++filter_y) {
66               for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
67                 const int in_x = in_x_origin + dilation_width_factor * filter_x;
68                 const int in_y =
69                     in_y_origin + dilation_height_factor * filter_y;
70                 // If the location is outside the bounds of the input image,
71                 // use zero as a default value.
72                 if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
73                     (in_y < input_height)) {
74                   float input_value =
75                       input_data[Offset(input_shape, b, in_y, in_x, ic)];
76                   float filter_value = filter_data[Offset(
77                       filter_shape, 0, filter_y, filter_x, oc)];
78                   total += (input_value * filter_value);
79                 }
80               }
81             }
82             float bias_value = 0.0f;
83             if (bias_data) {
84               bias_value = bias_data[oc];
85             }
86             output_data[Offset(output_shape, b, out_y, out_x, oc)] =
87                 ActivationFunctionWithMinMax(total + bias_value,
88                                              output_activation_min,
89                                              output_activation_max);
90           }
91         }
92       }
93     }
94   }
95 }
96 
97 }  // end namespace reference_ops
98 }  // end namespace tflite
99 
100 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_FLOAT_H_
101