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