1 /* Copyright 2019 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_INTEGER_OPS_DEPTHWISE_CONV_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_DEPTHWISE_CONV_H_
17 
18 #include "tensorflow/lite/kernels/internal/common.h"
19 
20 namespace tflite {
21 namespace reference_integer_ops {
DepthwiseConvPerChannel(const DepthwiseParams & params,const int32 * output_multiplier,const int32 * output_shift,const RuntimeShape & input_shape,const int8 * input_data,const RuntimeShape & filter_shape,const int8 * filter_data,const RuntimeShape & bias_shape,const int32 * bias_data,const RuntimeShape & output_shape,int8 * output_data)22 inline void DepthwiseConvPerChannel(
23     const DepthwiseParams& params, const int32* output_multiplier,
24     const int32* output_shift, const RuntimeShape& input_shape,
25     const int8* input_data, const RuntimeShape& filter_shape,
26     const int8* filter_data, const RuntimeShape& bias_shape,
27     const int32* bias_data, const RuntimeShape& output_shape,
28     int8* output_data) {
29   // Get parameters.
30   const int stride_width = params.stride_width;
31   const int stride_height = params.stride_height;
32   const int dilation_width_factor = params.dilation_width_factor;
33   const int dilation_height_factor = params.dilation_height_factor;
34   const int pad_width = params.padding_values.width;
35   const int pad_height = params.padding_values.height;
36   const int depth_multiplier = params.depth_multiplier;
37   const int32 input_offset = params.input_offset;
38   const int32 output_offset = params.output_offset;
39 
40   // Set min and max value of the output.
41   const int32 output_activation_min = std::numeric_limits<int8_t>::min();
42   const int32 output_activation_max = std::numeric_limits<int8_t>::max();
43 
44   // Check dimensions of the tensors.
45   TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4);
46   TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4);
47   TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4);
48 
49   TFLITE_DCHECK_LE(output_activation_min, output_activation_max);
50   const int batches = MatchingDim(input_shape, 0, output_shape, 0);
51   const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3);
52   const int input_height = input_shape.Dims(1);
53   const int input_width = input_shape.Dims(2);
54   const int input_depth = input_shape.Dims(3);
55   const int filter_height = filter_shape.Dims(1);
56   const int filter_width = filter_shape.Dims(2);
57   const int output_height = output_shape.Dims(1);
58   const int output_width = output_shape.Dims(2);
59   TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier);
60   TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth);
61 
62   for (int batch = 0; batch < batches; ++batch) {
63     for (int out_y = 0; out_y < output_height; ++out_y) {
64       for (int out_x = 0; out_x < output_width; ++out_x) {
65         for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
66           for (int m = 0; m < depth_multiplier; ++m) {
67             const int output_channel = m + in_channel * depth_multiplier;
68             const int in_x_origin = (out_x * stride_width) - pad_width;
69             const int in_y_origin = (out_y * stride_height) - pad_height;
70             int32 acc = 0;
71             for (int filter_y = 0; filter_y < filter_height; ++filter_y) {
72               for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
73                 const int in_x = in_x_origin + dilation_width_factor * filter_x;
74                 const int in_y =
75                     in_y_origin + dilation_height_factor * filter_y;
76                 // Zero padding by omitting the areas outside the image.
77                 const bool is_point_inside_image =
78                     (in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
79                     (in_y < input_height);
80                 if (is_point_inside_image) {
81                   int32 input_val = input_data[Offset(input_shape, batch, in_y,
82                                                       in_x, in_channel)];
83                   int32 filter_val = filter_data[Offset(
84                       filter_shape, 0, filter_y, filter_x, output_channel)];
85                   // Accumulate with 32 bits accumulator.
86                   // In the nudging process during model quantization, we force
87                   // real value of 0.0 be represented by a quantized value. This
88                   // guarentees that the input_offset is a int8, even though it
89                   // is represented using int32.
90                   // int32 += int8 * (int8 - int8) so the highest value we can
91                   // get from each accumulation is [-127, 127] * ([-128, 127] -
92                   // [-128, 127]), which is [-32512, 32512]. log2(32512)
93                   // = 14.98, which means we can accumulate at least 2^16
94                   // multiplications without overflow. The accumulator is
95                   // applied to a filter so the accumulation logic will hold as
96                   // long as the filter size (filter_y * filter_x * in_channel)
97                   // does not exceed 2^16, which is the case in all the models
98                   // we have seen so far.
99                   // TODO(jianlijianli): Add a check to make sure the
100                   // accumulator depth is smaller than 2^16.
101                   acc += filter_val * (input_val - input_offset);
102                 }
103               }
104             }
105             if (bias_data) {
106               acc += bias_data[output_channel];
107             }
108             acc = MultiplyByQuantizedMultiplier(
109                 acc, output_multiplier[output_channel],
110                 output_shift[output_channel]);
111             acc += output_offset;
112             acc = std::max(acc, output_activation_min);
113             acc = std::min(acc, output_activation_max);
114             output_data[Offset(output_shape, batch, out_y, out_x,
115                                output_channel)] = static_cast<int8_t>(acc);
116           }
117         }
118       }
119     }
120   }
121 }
122 }  // namespace reference_integer_ops
123 }  // namespace tflite
124 
125 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_DEPTHWISE_CONV_H_
126