xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/internal/reference/integer_ops/conv.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_CONV_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_CONV_H_
17 
18 #include <algorithm>
19 
20 #include "tensorflow/lite/kernels/internal/common.h"
21 
22 namespace tflite {
23 namespace reference_integer_ops {
24 
25 // Fixed-point per-channel-quantization convolution reference kernel.
ConvPerChannel(const ConvParams & params,const int32_t * output_multiplier,const int32_t * output_shift,const RuntimeShape & input_shape,const int8_t * input_data,const RuntimeShape & filter_shape,const int8_t * filter_data,const RuntimeShape & bias_shape,const int32_t * bias_data,const RuntimeShape & output_shape,int8_t * output_data)26 inline void ConvPerChannel(
27     const ConvParams& params, const int32_t* output_multiplier,
28     const int32_t* output_shift, const RuntimeShape& input_shape,
29     const int8_t* input_data, const RuntimeShape& filter_shape,
30     const int8_t* filter_data, const RuntimeShape& bias_shape,
31     const int32_t* bias_data, const RuntimeShape& output_shape,
32     int8_t* output_data) {
33   // Get parameters.
34   const int32_t input_offset = params.input_offset;  // r = s(q - Z)
35   const int stride_width = params.stride_width;
36   const int stride_height = params.stride_height;
37   const int dilation_width_factor = params.dilation_width_factor;
38   const int dilation_height_factor = params.dilation_height_factor;
39   const int pad_width = params.padding_values.width;
40   const int pad_height = params.padding_values.height;
41   const int32_t output_offset = params.output_offset;
42 
43   // Set min and max value of the output.
44   const int32_t output_activation_min = params.quantized_activation_min;
45   const int32_t output_activation_max = params.quantized_activation_max;
46 
47   // Consistency check.
48   TFLITE_DCHECK_LE(output_activation_min, output_activation_max);
49   TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4);
50   TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4);
51   TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4);
52   const int batches = MatchingDim(input_shape, 0, output_shape, 0);
53   const int input_depth = input_shape.Dims(3);
54   const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
55   if (bias_data) {
56     TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth);
57   }
58 
59   // Check dimensions of the tensors.
60   const int input_height = input_shape.Dims(1);
61   const int input_width = input_shape.Dims(2);
62   const int filter_height = filter_shape.Dims(1);
63   const int filter_width = filter_shape.Dims(2);
64   const int filter_input_depth = filter_shape.Dims(3);
65   const int groups = input_depth / filter_input_depth;
66   TFLITE_DCHECK_EQ(input_depth % filter_input_depth, 0);
67   const int filters_per_group = output_depth / groups;
68   const int output_height = output_shape.Dims(1);
69   const int output_width = output_shape.Dims(2);
70   for (int batch = 0; batch < batches; ++batch) {
71     for (int out_y = 0; out_y < output_height; ++out_y) {
72       const int in_y_origin = (out_y * stride_height) - pad_height;
73       for (int out_x = 0; out_x < output_width; ++out_x) {
74         const int in_x_origin = (out_x * stride_width) - pad_width;
75         for (int out_channel = 0; out_channel < output_depth; ++out_channel) {
76           auto group = out_channel / filters_per_group;
77           int32_t acc = 0;
78           for (int filter_y = 0; filter_y < filter_height; ++filter_y) {
79             const int in_y = in_y_origin + dilation_height_factor * filter_y;
80             for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
81               const int in_x = in_x_origin + dilation_width_factor * filter_x;
82 
83               // Zero padding by omitting the areas outside the image.
84               const bool is_point_inside_image =
85                   (in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
86                   (in_y < input_height);
87 
88               if (!is_point_inside_image) {
89                 continue;
90               }
91 
92               for (int in_channel = 0; in_channel < filter_input_depth;
93                    ++in_channel) {
94                 int32_t input_val =
95                     input_data[Offset(input_shape, batch, in_y, in_x,
96                                       in_channel + group * filter_input_depth)];
97                 int32_t filter_val = filter_data[Offset(
98                     filter_shape, out_channel, filter_y, filter_x, in_channel)];
99                 // Accumulate with 32 bits accumulator.
100                 // In the nudging process during model quantization, we force
101                 // real value of 0.0 be represented by a quantized value. This
102                 // guarantees that the input_offset is a int8_t, even though
103                 // it is represented using int32_t. int32_t += int8_t *
104                 // (int8_t - int8_t) so the highest value we can get from each
105                 // accumulation is [-127, 127] * ([-128, 127] -
106                 // [-128, 127]), which is [-32512, 32512]. log2(32512)
107                 // = 14.98, which means we can accumulate at least 2^16
108                 // multiplications without overflow. The accumulator is
109                 // applied to a filter so the accumulation logic will hold as
110                 // long as the filter size (filter_y * filter_x * in_channel)
111                 // does not exceed 2^16, which is the case in all the models
112                 // we have seen so far.
113                 // TODO(b/174275578): Add a check to make sure the
114                 // accumulator depth is smaller than 2^16.
115                 acc += filter_val * (input_val + input_offset);
116               }
117             }
118           }
119 
120           if (bias_data) {
121             acc += bias_data[out_channel];
122           }
123           acc = MultiplyByQuantizedMultiplier(
124               acc, output_multiplier[out_channel], output_shift[out_channel]);
125           acc += output_offset;
126           acc = std::max(acc, output_activation_min);
127           acc = std::min(acc, output_activation_max);
128           output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] =
129               static_cast<int8_t>(acc);
130         }
131       }
132     }
133   }
134 }
135 
136 // Fixed-point per-channel-quantization convolution reference kernel.
137 // 16-bit data and 8-bit filter
138 template <typename AccumScalar>
ConvPerChannel(const ConvParams & params,const int32_t * output_multiplier,const int32_t * output_shift,const RuntimeShape & input_shape,const int16_t * input_data,const RuntimeShape & filter_shape,const int8_t * filter_data,const RuntimeShape & bias_shape,const AccumScalar * bias_data,const RuntimeShape & output_shape,int16_t * output_data)139 inline void ConvPerChannel(
140     const ConvParams& params, const int32_t* output_multiplier,
141     const int32_t* output_shift, const RuntimeShape& input_shape,
142     const int16_t* input_data, const RuntimeShape& filter_shape,
143     const int8_t* filter_data, const RuntimeShape& bias_shape,
144     const AccumScalar* bias_data, const RuntimeShape& output_shape,
145     int16_t* output_data) {
146   // Get parameters.
147   const int stride_width = params.stride_width;
148   const int stride_height = params.stride_height;
149   const int dilation_width_factor = params.dilation_width_factor;
150   const int dilation_height_factor = params.dilation_height_factor;
151   const int pad_width = params.padding_values.width;
152   const int pad_height = params.padding_values.height;
153 
154   // Set min and max value of the output.
155   const int32_t output_activation_min = params.quantized_activation_min;
156   const int32_t output_activation_max = params.quantized_activation_max;
157 
158   // Consistency check.
159   TFLITE_DCHECK_LE(output_activation_min, output_activation_max);
160   TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4);
161   TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4);
162   TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4);
163   const int batches = MatchingDim(input_shape, 0, output_shape, 0);
164   const int input_depth = input_shape.Dims(3);
165   const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
166   if (bias_data) {
167     TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth);
168   }
169 
170   // Check dimensions of the tensors.
171   const int input_height = input_shape.Dims(1);
172   const int input_width = input_shape.Dims(2);
173   const int filter_height = filter_shape.Dims(1);
174   const int filter_width = filter_shape.Dims(2);
175   const int filter_input_depth = filter_shape.Dims(3);
176   const int groups = input_depth / filter_input_depth;
177   TFLITE_DCHECK_EQ(input_depth % filter_input_depth, 0);
178   const int filters_per_group = output_depth / groups;
179   const int output_height = output_shape.Dims(1);
180   const int output_width = output_shape.Dims(2);
181   for (int batch = 0; batch < batches; ++batch) {
182     for (int out_y = 0; out_y < output_height; ++out_y) {
183       const int in_y_origin = (out_y * stride_height) - pad_height;
184       for (int out_x = 0; out_x < output_width; ++out_x) {
185         const int in_x_origin = (out_x * stride_width) - pad_width;
186         for (int out_channel = 0; out_channel < output_depth; ++out_channel) {
187           auto group = out_channel / filters_per_group;
188           AccumScalar acc = 0;
189           for (int filter_y = 0; filter_y < filter_height; ++filter_y) {
190             const int in_y = in_y_origin + dilation_height_factor * filter_y;
191             for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
192               const int in_x = in_x_origin + dilation_width_factor * filter_x;
193 
194               // Zero padding by omitting the areas outside the image.
195               const bool is_point_inside_image =
196                   (in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
197                   (in_y < input_height);
198 
199               if (!is_point_inside_image) {
200                 continue;
201               }
202 
203               for (int in_channel = 0; in_channel < filter_input_depth;
204                    ++in_channel) {
205                 int32_t input_val =
206                     input_data[Offset(input_shape, batch, in_y, in_x,
207                                       in_channel + group * filter_input_depth)];
208                 int32_t filter_val = filter_data[Offset(
209                     filter_shape, out_channel, filter_y, filter_x, in_channel)];
210                 // Accumulate with 64 bits accumulator.
211                 // int64_t += int8_t * int16_t so the highest value we can
212                 // get from each accumulation is [-127, 127] * ([-32768,
213                 // 32767] -
214                 // [-32768, 32767]), which is [-8322945, 8322945].
215                 // log2(8322945) = 22.99.
216                 acc += filter_val * input_val;
217               }
218             }
219           }
220           if (bias_data) {
221             acc += bias_data[out_channel];
222           }
223           int32_t scaled_acc = MultiplyByQuantizedMultiplier(
224               acc, output_multiplier[out_channel], output_shift[out_channel]);
225           scaled_acc = std::max(scaled_acc, output_activation_min);
226           scaled_acc = std::min(scaled_acc, output_activation_max);
227           output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] =
228               static_cast<int16_t>(scaled_acc);
229         }
230       }
231     }
232   }
233 }
234 
235 }  // namespace reference_integer_ops
236 }  // namespace tflite
237 
238 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_CONV_H_
239