conv.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_CONV_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_CONV_H_
  14. #include "tensorflow/lite/kernels/internal/common.h"
  15. namespace tflite {
  16. namespace reference_integer_ops {
  17. // Fixed-point per-channel-quantization convolution reference kernel.
  18. inline void ConvPerChannel(
  19. const ConvParams& params, const int32_t* output_multiplier,
  20. const int32_t* output_shift, const RuntimeShape& input_shape,
  21. const int8_t* input_data, const RuntimeShape& filter_shape,
  22. const int8_t* filter_data, const RuntimeShape& bias_shape,
  23. const int32_t* bias_data, const RuntimeShape& output_shape,
  24. int8_t* output_data) {
  25. // Get parameters.
  26. const int32_t input_offset = params.input_offset; // r = s(q - Z)
  27. const int stride_width = params.stride_width;
  28. const int stride_height = params.stride_height;
  29. const int dilation_width_factor = params.dilation_width_factor;
  30. const int dilation_height_factor = params.dilation_height_factor;
  31. const int pad_width = params.padding_values.width;
  32. const int pad_height = params.padding_values.height;
  33. const int32_t output_offset = params.output_offset;
  34. // Set min and max value of the output.
  35. const int32_t output_activation_min = params.quantized_activation_min;
  36. const int32_t output_activation_max = params.quantized_activation_max;
  37. // Consistency check.
  38. TFLITE_DCHECK_LE(output_activation_min, output_activation_max);
  39. TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4);
  40. TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4);
  41. TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4);
  42. const int batches = MatchingDim(input_shape, 0, output_shape, 0);
  43. const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3);
  44. const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
  45. if (bias_data) {
  46. TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth);
  47. }
  48. // Check dimensions of the tensors.
  49. const int input_height = input_shape.Dims(1);
  50. const int input_width = input_shape.Dims(2);
  51. const int filter_height = filter_shape.Dims(1);
  52. const int filter_width = filter_shape.Dims(2);
  53. const int output_height = output_shape.Dims(1);
  54. const int output_width = output_shape.Dims(2);
  55. for (int batch = 0; batch < batches; ++batch) {
  56. for (int out_y = 0; out_y < output_height; ++out_y) {
  57. for (int out_x = 0; out_x < output_width; ++out_x) {
  58. for (int out_channel = 0; out_channel < output_depth; ++out_channel) {
  59. const int in_x_origin = (out_x * stride_width) - pad_width;
  60. const int in_y_origin = (out_y * stride_height) - pad_height;
  61. int32_t acc = 0;
  62. for (int filter_y = 0; filter_y < filter_height; ++filter_y) {
  63. for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
  64. for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
  65. const int in_x = in_x_origin + dilation_width_factor * filter_x;
  66. const int in_y =
  67. in_y_origin + dilation_height_factor * filter_y;
  68. // Zero padding by omitting the areas outside the image.
  69. const bool is_point_inside_image =
  70. (in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
  71. (in_y < input_height);
  72. if (is_point_inside_image) {
  73. int32_t input_val = input_data[Offset(
  74. input_shape, batch, in_y, in_x, in_channel)];
  75. int32_t filter_val =
  76. filter_data[Offset(filter_shape, out_channel, filter_y,
  77. filter_x, in_channel)];
  78. // Accumulate with 32 bits accumulator.
  79. // In the nudging process during model quantization, we force
  80. // real value of 0.0 be represented by a quantized value. This
  81. // guarantees that the input_offset is a int8_t, even though
  82. // it is represented using int32_t. int32_t += int8_t *
  83. // (int8_t - int8_t) so the highest value we can get from each
  84. // accumulation is [-127, 127] * ([-128, 127] -
  85. // [-128, 127]), which is [-32512, 32512]. log2(32512)
  86. // = 14.98, which means we can accumulate at least 2^16
  87. // multiplications without overflow. The accumulator is
  88. // applied to a filter so the accumulation logic will hold as
  89. // long as the filter size (filter_y * filter_x * in_channel)
  90. // does not exceed 2^16, which is the case in all the models
  91. // we have seen so far.
  92. // TODO(jianlijianli): Add a check to make sure the
  93. // accumulator depth is smaller than 2^16.
  94. acc += filter_val * (input_val + input_offset);
  95. }
  96. }
  97. }
  98. }
  99. if (bias_data) {
  100. acc += bias_data[out_channel];
  101. }
  102. acc = MultiplyByQuantizedMultiplier(
  103. acc, output_multiplier[out_channel], output_shift[out_channel]);
  104. acc += output_offset;
  105. acc = std::max(acc, output_activation_min);
  106. acc = std::min(acc, output_activation_max);
  107. output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] =
  108. static_cast<int8_t>(acc);
  109. }
  110. }
  111. }
  112. }
  113. }
  114. // Fixed-point per-channel-quantization convolution reference kernel.
  115. // 16-bit data and 8-bit filter
  116. inline void ConvPerChannel(
  117. const ConvParams& params, const int32_t* output_multiplier,
  118. const int32_t* output_shift, const RuntimeShape& input_shape,
  119. const int16_t* input_data, const RuntimeShape& filter_shape,
  120. const int8_t* filter_data, const RuntimeShape& bias_shape,
  121. const std::int64_t* bias_data, const RuntimeShape& output_shape,
  122. int16_t* output_data) {
  123. // Get parameters.
  124. const int stride_width = params.stride_width;
  125. const int stride_height = params.stride_height;
  126. const int dilation_width_factor = params.dilation_width_factor;
  127. const int dilation_height_factor = params.dilation_height_factor;
  128. const int pad_width = params.padding_values.width;
  129. const int pad_height = params.padding_values.height;
  130. // Set min and max value of the output.
  131. const int32_t output_activation_min = params.quantized_activation_min;
  132. const int32_t output_activation_max = params.quantized_activation_max;
  133. // Consistency check.
  134. TFLITE_DCHECK_LE(output_activation_min, output_activation_max);
  135. TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4);
  136. TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4);
  137. TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4);
  138. const int batches = MatchingDim(input_shape, 0, output_shape, 0);
  139. const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3);
  140. const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3);
  141. if (bias_data) {
  142. TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth);
  143. }
  144. // Check dimensions of the tensors.
  145. const int input_height = input_shape.Dims(1);
  146. const int input_width = input_shape.Dims(2);
  147. const int filter_height = filter_shape.Dims(1);
  148. const int filter_width = filter_shape.Dims(2);
  149. const int output_height = output_shape.Dims(1);
  150. const int output_width = output_shape.Dims(2);
  151. for (int batch = 0; batch < batches; ++batch) {
  152. for (int out_y = 0; out_y < output_height; ++out_y) {
  153. for (int out_x = 0; out_x < output_width; ++out_x) {
  154. for (int out_channel = 0; out_channel < output_depth; ++out_channel) {
  155. const int in_x_origin = (out_x * stride_width) - pad_width;
  156. const int in_y_origin = (out_y * stride_height) - pad_height;
  157. std::int64_t acc = 0;
  158. for (int filter_y = 0; filter_y < filter_height; ++filter_y) {
  159. for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
  160. for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
  161. const int in_x = in_x_origin + dilation_width_factor * filter_x;
  162. const int in_y =
  163. in_y_origin + dilation_height_factor * filter_y;
  164. // Zero padding by omitting the areas outside the image.
  165. const bool is_point_inside_image =
  166. (in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
  167. (in_y < input_height);
  168. if (is_point_inside_image) {
  169. int32_t input_val = input_data[Offset(
  170. input_shape, batch, in_y, in_x, in_channel)];
  171. int32_t filter_val =
  172. filter_data[Offset(filter_shape, out_channel, filter_y,
  173. filter_x, in_channel)];
  174. // Accumulate with 64 bits accumulator.
  175. // int64_t += int8_t * int16_t so the highest value we can
  176. // get from each accumulation is [-127, 127] * ([-32768,
  177. // 32767] -
  178. // [-32768, 32767]), which is [-8322945, 8322945].
  179. // log2(8322945) = 22.99.
  180. acc += filter_val * input_val;
  181. }
  182. }
  183. }
  184. }
  185. if (bias_data) {
  186. acc += bias_data[out_channel];
  187. }
  188. int32_t scaled_acc = MultiplyByQuantizedMultiplier(
  189. acc, output_multiplier[out_channel], output_shift[out_channel]);
  190. scaled_acc = std::max(scaled_acc, output_activation_min);
  191. scaled_acc = std::min(scaled_acc, output_activation_max);
  192. output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] =
  193. static_cast<int16_t>(scaled_acc);
  194. }
  195. }
  196. }
  197. }
  198. }
  199. } // namespace reference_integer_ops
  200. } // namespace tflite
  201. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_CONV_H_