DepthwiseConv2D.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "CpuOperationUtils.h"
  17. #include "Operations.h"
  18. #include "tensorflow/lite/kernels/internal/optimized/depthwiseconv_float.h"
  19. #include "tensorflow/lite/kernels/internal/optimized/depthwiseconv_uint8.h"
  20. #include "Tracing.h"
  21. namespace android {
  22. namespace nn {
  23. bool depthwiseConvFloat16(const _Float16* inputData, const Shape& inputShape,
  24. const _Float16* filterData, const Shape& filterShape,
  25. const _Float16* biasData, const Shape& biasShape, int32_t paddingLeft,
  26. int32_t paddingRight, int32_t paddingTop, int32_t paddingBottom,
  27. int32_t strideWidth, int32_t strideHeight, int32_t dilationWidthFactor,
  28. int32_t dilationHeightFactor, int32_t depthMultiplier, int32_t activation,
  29. _Float16* outputData, const Shape& outputShape) {
  30. NNTRACE_TRANS("depthwiseConvFloat16");
  31. std::vector<float> inputDataFloat32(getNumberOfElements(inputShape));
  32. convertFloat16ToFloat32(inputData, &inputDataFloat32);
  33. std::vector<float> filterDataFloat32(getNumberOfElements(filterShape));
  34. convertFloat16ToFloat32(filterData, &filterDataFloat32);
  35. std::vector<float> biasDataFloat32(getNumberOfElements(biasShape));
  36. convertFloat16ToFloat32(biasData, &biasDataFloat32);
  37. std::vector<float> outputDataFloat32(getNumberOfElements(outputShape));
  38. depthwiseConvFloat32(inputDataFloat32.data(), inputShape, filterDataFloat32.data(), filterShape,
  39. biasDataFloat32.data(), biasShape, paddingLeft, paddingRight, paddingTop,
  40. paddingBottom, strideWidth, strideHeight, dilationWidthFactor,
  41. dilationHeightFactor, depthMultiplier, activation,
  42. outputDataFloat32.data(), outputShape);
  43. convertFloat32ToFloat16(outputDataFloat32, outputData);
  44. return true;
  45. }
  46. #define ANDROID_NN_DEPTHWISE_CONV_PARAMETERS \
  47. uint32_t height = getSizeOfDimension(inputShape, 1); \
  48. uint32_t width = getSizeOfDimension(inputShape, 2); \
  49. uint32_t filterHeight = getSizeOfDimension(filterShape, 1); \
  50. uint32_t filterWidth = getSizeOfDimension(filterShape, 2); \
  51. uint32_t outHeight = getSizeOfDimension(outputShape, 1); \
  52. uint32_t outWidth = getSizeOfDimension(outputShape, 2); \
  53. \
  54. uint32_t paddingHeight = (uint32_t)paddingTop; \
  55. uint32_t paddingWidth = (uint32_t)paddingLeft;
  56. bool depthwiseConvFloat32(const float* inputData, const Shape& inputShape, const float* filterData,
  57. const Shape& filterShape, const float* biasData, const Shape& biasShape,
  58. int32_t paddingLeft, int32_t paddingRight, int32_t paddingTop,
  59. int32_t paddingBottom, int32_t strideWidth, int32_t strideHeight,
  60. int32_t dilationWidthFactor, int32_t dilationHeightFactor,
  61. int32_t depthMultiplier, int32_t activation, float* outputData,
  62. const Shape& outputShape) {
  63. NNTRACE_TRANS("depthwiseConvFloat32");
  64. ANDROID_NN_DEPTHWISE_CONV_PARAMETERS
  65. float output_activation_min, output_activation_max;
  66. CalculateActivationRangeFloat(activation, &output_activation_min, &output_activation_max);
  67. tflite::DepthwiseParams params{
  68. .padding_values = {static_cast<int16>(paddingWidth), static_cast<int16>(paddingHeight)},
  69. .stride_width = static_cast<int16>(strideWidth),
  70. .stride_height = static_cast<int16>(strideHeight),
  71. .depth_multiplier = static_cast<int16>(depthMultiplier),
  72. .float_activation_min = output_activation_min,
  73. .float_activation_max = output_activation_max,
  74. .dilation_width_factor = static_cast<int16>(dilationWidthFactor),
  75. .dilation_height_factor = static_cast<int16>(dilationHeightFactor),
  76. };
  77. NNTRACE_COMP_SWITCH("optimized_ops::DepthwiseConv");
  78. tflite::optimized_ops::DepthwiseConv(params, convertShapeToTflshape(inputShape), inputData,
  79. convertShapeToTflshape(filterShape), filterData,
  80. convertShapeToTflshape(biasShape), biasData,
  81. convertShapeToTflshape(outputShape), outputData);
  82. return true;
  83. }
  84. bool depthwiseConvQuant8(const uint8_t* inputData, const Shape& inputShape,
  85. const uint8_t* filterData, const Shape& filterShape,
  86. const int32_t* biasData, const Shape& biasShape, int32_t paddingLeft,
  87. int32_t paddingRight, int32_t paddingTop, int32_t paddingBottom,
  88. int32_t strideWidth, int32_t strideHeight, int32_t dilationWidthFactor,
  89. int32_t dilationHeightFactor, int32_t depthMultiplier, int32_t activation,
  90. uint8_t* outputData, const Shape& outputShape) {
  91. NNTRACE_TRANS("depthwiseConvQuant8");
  92. ANDROID_NN_DEPTHWISE_CONV_PARAMETERS
  93. double real_multiplier = 0.0;
  94. int32_t output_multiplier = 0;
  95. int32_t output_shift = 0;
  96. int32_t output_activation_min = 0;
  97. int32_t output_activation_max = 0;
  98. NN_RET_CHECK(GetQuantizedConvolutionMultipler(inputShape, filterShape, biasShape, outputShape,
  99. &real_multiplier));
  100. int exponent;
  101. NN_RET_CHECK(QuantizeMultiplier(real_multiplier, &output_multiplier, &exponent));
  102. output_shift = -exponent;
  103. CalculateActivationRangeUint8(activation, outputShape, &output_activation_min,
  104. &output_activation_max);
  105. tflite::DepthwiseParams params{
  106. .padding_values = {static_cast<int16>(paddingWidth), static_cast<int16>(paddingHeight)},
  107. .stride_width = static_cast<int16>(strideWidth),
  108. .stride_height = static_cast<int16>(strideHeight),
  109. .depth_multiplier = static_cast<int16>(depthMultiplier),
  110. .quantized_activation_min = output_activation_min,
  111. .quantized_activation_max = output_activation_max,
  112. .dilation_width_factor = static_cast<int16>(dilationWidthFactor),
  113. .dilation_height_factor = static_cast<int16>(dilationHeightFactor),
  114. .input_offset = -inputShape.offset,
  115. .weights_offset = -filterShape.offset,
  116. .output_offset = outputShape.offset,
  117. .output_shift = -output_shift,
  118. .output_multiplier = output_multiplier,
  119. };
  120. NNTRACE_COMP_SWITCH("optimized_ops::DepthwiseConv");
  121. tflite::optimized_ops::DepthwiseConv(params, convertShapeToTflshape(inputShape), inputData,
  122. convertShapeToTflshape(filterShape), filterData,
  123. convertShapeToTflshape(biasShape), biasData,
  124. convertShapeToTflshape(outputShape), outputData);
  125. return true;
  126. }
  127. bool depthwiseConvQuant8PerChannel(const uint8_t* inputData, const Shape& inputShape,
  128. const int8_t* filterData, const Shape& filterShape,
  129. const float* filterScales, const int32_t* biasData,
  130. const Shape& biasShape, int32_t paddingLeft,
  131. int32_t paddingRight, int32_t paddingTop, int32_t paddingBottom,
  132. int32_t strideWidth, int32_t strideHeight,
  133. int32_t dilationWidthFactor, int32_t dilationHeightFactor,
  134. int32_t depthMultiplier, int32_t activation, uint8_t* outputData,
  135. const Shape& outputShape) {
  136. NNTRACE_TRANS("depthwiseConvQuant8");
  137. uint32_t paddingHeight = (uint32_t)paddingTop;
  138. uint32_t paddingWidth = (uint32_t)paddingLeft;
  139. uint32_t numBatches = getSizeOfDimension(inputShape, 0);
  140. uint32_t inputHeight = getSizeOfDimension(inputShape, 1);
  141. uint32_t inputWidth = getSizeOfDimension(inputShape, 2);
  142. uint32_t inputDepth = getSizeOfDimension(inputShape, 3);
  143. uint32_t filterHeight = getSizeOfDimension(filterShape, 1);
  144. uint32_t filterWidth = getSizeOfDimension(filterShape, 2);
  145. uint32_t filterDepth = getSizeOfDimension(filterShape, 3);
  146. uint32_t outputHeight = getSizeOfDimension(outputShape, 1);
  147. uint32_t outputWidth = getSizeOfDimension(outputShape, 2);
  148. uint32_t outputDepth = getSizeOfDimension(outputShape, 3);
  149. int32_t inputOffset = -inputShape.offset;
  150. int32_t outputOffset = outputShape.offset;
  151. auto realMultiplier = std::vector<double>(outputDepth, .0f);
  152. auto outputMultiplier = std::vector<int32_t>(outputDepth, 0);
  153. auto outputShift = std::vector<int32_t>(outputDepth, .0f);
  154. for (int i = 0; i < outputDepth; ++i) {
  155. Shape filterChannelShape = filterShape;
  156. filterChannelShape.scale = filterScales[i];
  157. Shape biasChannelShape = biasShape;
  158. biasChannelShape.scale = filterScales[i] * inputShape.scale;
  159. NN_RET_CHECK(GetQuantizedConvolutionMultipler(
  160. inputShape, filterChannelShape, biasChannelShape, outputShape, &realMultiplier[i]));
  161. int exponent;
  162. NN_RET_CHECK(QuantizeMultiplier(realMultiplier[i], &outputMultiplier[i], &exponent));
  163. outputShift[i] = -exponent;
  164. }
  165. int32_t output_activation_min = 0, output_activation_max = 0;
  166. CalculateActivationRangeUint8(activation, outputShape, &output_activation_min,
  167. &output_activation_max);
  168. const uint8_t* inputBase = inputData;
  169. uint8_t* outPtr = outputData;
  170. for (uint32_t b = 0; b < numBatches; b++) {
  171. for (uint32_t h = 0; h < outputHeight; h++) {
  172. for (uint32_t w = 0; w < outputWidth; w++) {
  173. for (uint32_t ic = 0; ic < inputDepth; ic++) {
  174. for (uint32_t m = 0; m < depthMultiplier; m++) {
  175. int32_t wInputOrigin = static_cast<int32_t>(w) * strideWidth - paddingLeft;
  176. int32_t hInputOrigin = static_cast<int32_t>(h) * strideHeight - paddingTop;
  177. const int oc = m + ic * depthMultiplier;
  178. int32_t sum = 0.0f;
  179. for (uint32_t i = 0; i < filterHeight; i++) {
  180. for (uint32_t j = 0; j < filterWidth; j++) {
  181. int32_t hInput = hInputOrigin +
  182. dilationHeightFactor * static_cast<int32_t>(i);
  183. int32_t wInput = wInputOrigin +
  184. dilationWidthFactor * static_cast<int32_t>(j);
  185. if (hInput >= 0 && hInput < static_cast<int32_t>(inputHeight) &&
  186. wInput >= 0 && wInput < static_cast<int32_t>(inputWidth)) {
  187. uint32_t filterIndex =
  188. i * filterWidth * filterDepth + j * filterDepth + oc;
  189. uint32_t inputIndex = hInput * inputWidth * inputDepth +
  190. wInput * inputDepth + ic;
  191. sum += (static_cast<int32_t>(filterData[filterIndex])) *
  192. (static_cast<int32_t>(inputBase[inputIndex]) +
  193. inputOffset);
  194. }
  195. }
  196. }
  197. sum += biasData[oc];
  198. sum = tflite::MultiplyByQuantizedMultiplier(sum, outputMultiplier[oc],
  199. -outputShift[oc]);
  200. sum += outputOffset;
  201. sum = std::max(std::min(sum, output_activation_max), output_activation_min);
  202. outPtr[m] = static_cast<uint8_t>(sum);
  203. }
  204. outPtr += depthMultiplier;
  205. }
  206. }
  207. }
  208. inputBase += inputHeight * inputWidth * inputDepth;
  209. }
  210. return true;
  211. }
  212. #undef ANDROID_NN_DEPTHWISE_CONV_PARAMETERS
  213. } // namespace nn
  214. } // namespace android