depthwise_conv.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_DEPTHWISE_CONV_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_DEPTHWISE_CONV_H_
  14. #include "tflite/kernels/internal/common.h"
  15. namespace tflite {
  16. namespace reference_integer_ops {
  17. inline void DepthwiseConvPerChannel(
  18. const DepthwiseParams& params, const int32* output_multiplier,
  19. const int32* output_shift, const RuntimeShape& input_shape,
  20. const int8* input_data, const RuntimeShape& filter_shape,
  21. const int8* filter_data, const RuntimeShape& bias_shape,
  22. const int32* bias_data, const RuntimeShape& output_shape,
  23. int8* output_data) {
  24. // Get parameters.
  25. // TODO(b/141565753): Re-introduce ScopedProfilingLabel on Micro.
  26. const int stride_width = params.stride_width;
  27. const int stride_height = params.stride_height;
  28. const int dilation_width_factor = params.dilation_width_factor;
  29. const int dilation_height_factor = params.dilation_height_factor;
  30. const int pad_width = params.padding_values.width;
  31. const int pad_height = params.padding_values.height;
  32. const int depth_multiplier = params.depth_multiplier;
  33. const int32 input_offset = params.input_offset;
  34. const int32 output_offset = params.output_offset;
  35. const int32 output_activation_min = params.quantized_activation_min;
  36. const int32 output_activation_max = params.quantized_activation_max;
  37. // Check dimensions of the tensors.
  38. TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4);
  39. TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4);
  40. TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4);
  41. TFLITE_DCHECK_LE(output_activation_min, output_activation_max);
  42. const int batches = MatchingDim(input_shape, 0, output_shape, 0);
  43. const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3);
  44. const int input_height = input_shape.Dims(1);
  45. const int input_width = input_shape.Dims(2);
  46. const int input_depth = input_shape.Dims(3);
  47. const int filter_height = filter_shape.Dims(1);
  48. const int filter_width = filter_shape.Dims(2);
  49. const int output_height = output_shape.Dims(1);
  50. const int output_width = output_shape.Dims(2);
  51. TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier);
  52. TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth);
  53. for (int batch = 0; batch < batches; ++batch) {
  54. for (int out_y = 0; out_y < output_height; ++out_y) {
  55. for (int out_x = 0; out_x < output_width; ++out_x) {
  56. for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
  57. for (int m = 0; m < depth_multiplier; ++m) {
  58. const int output_channel = m + in_channel * depth_multiplier;
  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 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. const int in_x = in_x_origin + dilation_width_factor * filter_x;
  65. const int in_y =
  66. in_y_origin + dilation_height_factor * filter_y;
  67. // Zero padding by omitting the areas outside the image.
  68. const bool is_point_inside_image =
  69. (in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
  70. (in_y < input_height);
  71. if (is_point_inside_image) {
  72. int32 input_val = input_data[Offset(input_shape, batch, in_y,
  73. in_x, in_channel)];
  74. int32 filter_val = filter_data[Offset(
  75. filter_shape, 0, filter_y, filter_x, output_channel)];
  76. // Accumulate with 32 bits accumulator.
  77. // In the nudging process during model quantization, we force
  78. // real value of 0.0 be represented by a quantized value. This
  79. // guarantees that the input_offset is a int8, even though it
  80. // is represented using int32.
  81. // int32 += int8 * (int8 - int8) so the highest value we can
  82. // get from each accumulation is [-127, 127] * ([-128, 127] -
  83. // [-128, 127]), which is [-32512, 32512]. log2(32512)
  84. // = 14.98, which means we can accumulate at least 2^16
  85. // multiplications without overflow. The accumulator is
  86. // applied to a filter so the accumulation logic will hold as
  87. // long as the filter size (filter_y * filter_x * in_channel)
  88. // does not exceed 2^16, which is the case in all the models
  89. // we have seen so far.
  90. // TODO(jianlijianli): Add a check to make sure the
  91. // accumulator depth is smaller than 2^16.
  92. acc += filter_val * (input_val + input_offset);
  93. }
  94. }
  95. }
  96. if (bias_data) {
  97. acc += bias_data[output_channel];
  98. }
  99. acc = MultiplyByQuantizedMultiplier(
  100. acc, output_multiplier[output_channel],
  101. output_shift[output_channel]);
  102. acc += output_offset;
  103. acc = std::max(acc, output_activation_min);
  104. acc = std::min(acc, output_activation_max);
  105. output_data[Offset(output_shape, batch, out_y, out_x,
  106. output_channel)] = static_cast<int8_t>(acc);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. inline void DepthwiseConvPerChannel(
  114. const DepthwiseParams& params, const int32* output_multiplier,
  115. const int32* output_shift, const RuntimeShape& input_shape,
  116. const int16* input_data, const RuntimeShape& filter_shape,
  117. const int8* filter_data, const RuntimeShape& bias_shape,
  118. const std::int64_t* bias_data, const RuntimeShape& output_shape,
  119. int16* output_data) {
  120. // Get parameters.
  121. const int stride_width = params.stride_width;
  122. const int stride_height = params.stride_height;
  123. const int dilation_width_factor = params.dilation_width_factor;
  124. const int dilation_height_factor = params.dilation_height_factor;
  125. const int pad_width = params.padding_values.width;
  126. const int pad_height = params.padding_values.height;
  127. const int depth_multiplier = params.depth_multiplier;
  128. const int32 output_activation_min = params.quantized_activation_min;
  129. const int32 output_activation_max = params.quantized_activation_max;
  130. // Check dimensions of the tensors.
  131. TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4);
  132. TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4);
  133. TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4);
  134. TFLITE_DCHECK_LE(output_activation_min, output_activation_max);
  135. const int batches = MatchingDim(input_shape, 0, output_shape, 0);
  136. const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3);
  137. const int input_height = input_shape.Dims(1);
  138. const int input_width = input_shape.Dims(2);
  139. const int input_depth = input_shape.Dims(3);
  140. const int filter_height = filter_shape.Dims(1);
  141. const int filter_width = filter_shape.Dims(2);
  142. const int output_height = output_shape.Dims(1);
  143. const int output_width = output_shape.Dims(2);
  144. TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier);
  145. TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth);
  146. for (int batch = 0; batch < batches; ++batch) {
  147. for (int out_y = 0; out_y < output_height; ++out_y) {
  148. for (int out_x = 0; out_x < output_width; ++out_x) {
  149. for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
  150. for (int m = 0; m < depth_multiplier; ++m) {
  151. const int output_channel = m + in_channel * depth_multiplier;
  152. const int in_x_origin = (out_x * stride_width) - pad_width;
  153. const int in_y_origin = (out_y * stride_height) - pad_height;
  154. std::int64_t acc = 0;
  155. for (int filter_y = 0; filter_y < filter_height; ++filter_y) {
  156. for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
  157. const int in_x = in_x_origin + dilation_width_factor * filter_x;
  158. const int in_y =
  159. in_y_origin + dilation_height_factor * filter_y;
  160. // Zero padding by omitting the areas outside the image.
  161. const bool is_point_inside_image =
  162. (in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
  163. (in_y < input_height);
  164. if (is_point_inside_image) {
  165. int32 input_val = input_data[Offset(input_shape, batch, in_y,
  166. in_x, in_channel)];
  167. int32 filter_val = filter_data[Offset(
  168. filter_shape, 0, filter_y, filter_x, output_channel)];
  169. // Accumulate with 64 bits accumulator.
  170. // We assume maximum of 2^16 accumulations as with the 8-bit
  171. // case so actually the value in the accumulator should not
  172. // exceed 40 bits
  173. acc += static_cast<int64_t>(filter_val) *
  174. static_cast<int64_t>(input_val);
  175. }
  176. }
  177. }
  178. if (bias_data) {
  179. acc += bias_data[output_channel];
  180. }
  181. int32 scaled_acc = MultiplyByQuantizedMultiplier(
  182. acc, output_multiplier[output_channel],
  183. output_shift[output_channel]);
  184. scaled_acc = std::max(scaled_acc, output_activation_min);
  185. scaled_acc = std::min(scaled_acc, output_activation_max);
  186. output_data[Offset(output_shape, batch, out_y, out_x,
  187. output_channel)] =
  188. static_cast<int16_t>(scaled_acc);
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. inline void DepthwiseConvHybridPerChannel(
  196. const DepthwiseParams& params, float* scaling_factors_ptr,
  197. const RuntimeShape& input_shape, const int8* input_data,
  198. const RuntimeShape& filter_shape, const int8* filter_data,
  199. const RuntimeShape& bias_shape, const float* bias_data,
  200. const RuntimeShape& output_shape, float* output_data,
  201. const float* per_channel_scale, int32_t* input_offset) {
  202. const int stride_width = params.stride_width;
  203. const int stride_height = params.stride_height;
  204. const int dilation_width_factor = params.dilation_width_factor;
  205. const int dilation_height_factor = params.dilation_height_factor;
  206. const int pad_width = params.padding_values.width;
  207. const int pad_height = params.padding_values.height;
  208. const int depth_multiplier = params.depth_multiplier;
  209. const float output_activation_min = params.float_activation_min;
  210. const float output_activation_max = params.float_activation_max;
  211. // Check dimensions of the tensors.
  212. TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4);
  213. TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4);
  214. TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4);
  215. const int batches = MatchingDim(input_shape, 0, output_shape, 0);
  216. const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3);
  217. const int input_height = input_shape.Dims(1);
  218. const int input_width = input_shape.Dims(2);
  219. const int input_depth = input_shape.Dims(3);
  220. const int filter_height = filter_shape.Dims(1);
  221. const int filter_width = filter_shape.Dims(2);
  222. const int output_height = output_shape.Dims(1);
  223. const int output_width = output_shape.Dims(2);
  224. const int bias_depth = bias_shape.FlatSize();
  225. TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier);
  226. TFLITE_DCHECK_EQ(bias_depth, output_depth);
  227. for (int batch = 0; batch < batches; ++batch) {
  228. for (int out_y = 0; out_y < output_height; ++out_y) {
  229. for (int out_x = 0; out_x < output_width; ++out_x) {
  230. for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
  231. for (int m = 0; m < depth_multiplier; ++m) {
  232. const int output_channel = m + in_channel * depth_multiplier;
  233. const int in_x_origin = (out_x * stride_width) - pad_width;
  234. const int in_y_origin = (out_y * stride_height) - pad_height;
  235. int32 acc = 0;
  236. for (int filter_y = 0; filter_y < filter_height; ++filter_y) {
  237. for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
  238. const int in_x = in_x_origin + dilation_width_factor * filter_x;
  239. const int in_y =
  240. in_y_origin + dilation_height_factor * filter_y;
  241. // Zero padding by omitting the areas outside the image.
  242. const bool is_point_inside_image =
  243. (in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
  244. (in_y < input_height);
  245. if (is_point_inside_image) {
  246. int32 input_val = input_data[Offset(input_shape, batch, in_y,
  247. in_x, in_channel)];
  248. int32 filter_val = filter_data[Offset(
  249. filter_shape, 0, filter_y, filter_x, output_channel)];
  250. acc += filter_val * (input_val - input_offset[batch]);
  251. }
  252. }
  253. }
  254. float acc_float = static_cast<float>(acc);
  255. acc_float *=
  256. per_channel_scale[output_channel] * scaling_factors_ptr[batch];
  257. if (bias_data && output_channel < bias_depth) {
  258. acc_float += bias_data[output_channel];
  259. }
  260. output_data[Offset(output_shape, batch, out_y, out_x,
  261. output_channel)] =
  262. ActivationFunctionWithMinMax(acc_float, output_activation_min,
  263. output_activation_max);
  264. }
  265. }
  266. }
  267. }
  268. }
  269. }
  270. } // namespace reference_integer_ops
  271. } // namespace tflite
  272. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_DEPTHWISE_CONV_H_