pooling.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright 2021 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. #include "tensorflow/lite/kernels/internal/reference/pooling.h"
  13. #include "tensorflow/lite/c/builtin_op_data.h"
  14. #include "tensorflow/lite/kernels/kernel_util.h"
  15. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  16. #include "tensorflow/lite/micro/kernels/pooling.h"
  17. namespace tflite {
  18. namespace {
  19. TfLiteStatus AverageEval(TfLiteContext* context, TfLiteNode* node) {
  20. TFLITE_DCHECK(node->builtin_data != nullptr);
  21. auto* params = reinterpret_cast<TfLitePoolParams*>(node->builtin_data);
  22. TFLITE_DCHECK(node->user_data != nullptr);
  23. const OpDataPooling* data =
  24. static_cast<const OpDataPooling*>(node->user_data);
  25. const TfLiteEvalTensor* input =
  26. micro::GetEvalInput(context, node, kPoolingInputTensor);
  27. TfLiteEvalTensor* output =
  28. micro::GetEvalOutput(context, node, kPoolingOutputTensor);
  29. // Inputs and outputs share the same type, guaranteed by the converter.
  30. switch (input->type) {
  31. case kTfLiteFloat32:
  32. AveragePoolingEvalFloat(context, node, params, data, input, output);
  33. break;
  34. case kTfLiteInt8:
  35. AveragePoolingEvalQuantized(context, node, params, data, input, output);
  36. break;
  37. default:
  38. TF_LITE_KERNEL_LOG(context, "Input type %s is not currently supported",
  39. TfLiteTypeGetName(input->type));
  40. return kTfLiteError;
  41. }
  42. return kTfLiteOk;
  43. }
  44. TfLiteStatus MaxEval(TfLiteContext* context, TfLiteNode* node) {
  45. TFLITE_DCHECK(node->builtin_data != nullptr);
  46. auto* params = reinterpret_cast<TfLitePoolParams*>(node->builtin_data);
  47. TFLITE_DCHECK(node->user_data != nullptr);
  48. const OpDataPooling* data =
  49. static_cast<const OpDataPooling*>(node->user_data);
  50. const TfLiteEvalTensor* input =
  51. micro::GetEvalInput(context, node, kPoolingInputTensor);
  52. TfLiteEvalTensor* output =
  53. micro::GetEvalOutput(context, node, kPoolingOutputTensor);
  54. switch (input->type) {
  55. case kTfLiteFloat32:
  56. MaxPoolingEvalFloat(context, node, params, data, input, output);
  57. break;
  58. case kTfLiteInt8:
  59. MaxPoolingEvalQuantized(context, node, params, data, input, output);
  60. break;
  61. default:
  62. TF_LITE_KERNEL_LOG(context, "Type %s not currently supported.",
  63. TfLiteTypeGetName(input->type));
  64. return kTfLiteError;
  65. }
  66. return kTfLiteOk;
  67. }
  68. void* Init(TfLiteContext* context, const char* buffer, size_t length) {
  69. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  70. return context->AllocatePersistentBuffer(context, sizeof(OpDataPooling));
  71. }
  72. } // namespace
  73. TfLiteRegistration Register_AVERAGE_POOL_2D() {
  74. return {/*init=*/Init,
  75. /*free=*/nullptr,
  76. /*prepare=*/PoolingPrepare,
  77. /*invoke=*/AverageEval,
  78. /*profiling_string=*/nullptr,
  79. /*builtin_code=*/0,
  80. /*custom_name=*/nullptr,
  81. /*version=*/0};
  82. }
  83. TfLiteRegistration Register_MAX_POOL_2D() {
  84. return {/*init=*/Init,
  85. /*free=*/nullptr,
  86. /*prepare=*/PoolingPrepare,
  87. /*invoke=*/MaxEval,
  88. /*profiling_string=*/nullptr,
  89. /*builtin_code=*/0,
  90. /*custom_name=*/nullptr,
  91. /*version=*/0};
  92. }
  93. } // namespace tflite