maximum_minimum.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Copyright 2018 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/maximum_minimum.h"
  13. #include "tensorflow/lite/c/builtin_op_data.h"
  14. #include "tensorflow/lite/c/common.h"
  15. #include "tensorflow/lite/kernels/internal/common.h"
  16. #include "tensorflow/lite/kernels/internal/quantization_util.h"
  17. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  18. #include "tensorflow/lite/kernels/kernel_util.h"
  19. #include "tensorflow/lite/kernels/op_macros.h"
  20. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  21. namespace tflite {
  22. namespace ops {
  23. namespace micro {
  24. namespace maximum_minimum {
  25. namespace {
  26. // This file has a reference implementation of TFMaximum/TFMinimum.
  27. enum KernelType {
  28. kReference,
  29. };
  30. constexpr int kInputTensor1 = 0;
  31. constexpr int kInputTensor2 = 1;
  32. constexpr int kOutputTensor = 0;
  33. struct OpContext {
  34. OpContext(TfLiteContext* context, TfLiteNode* node) {
  35. input1 = tflite::micro::GetEvalInput(context, node, kInputTensor1);
  36. input2 = tflite::micro::GetEvalInput(context, node, kInputTensor2);
  37. output = tflite::micro::GetEvalOutput(context, node, kOutputTensor);
  38. }
  39. const TfLiteEvalTensor* input1;
  40. const TfLiteEvalTensor* input2;
  41. TfLiteEvalTensor* output;
  42. };
  43. struct MaximumOp {
  44. template <typename data_type>
  45. static data_type op(data_type el1, data_type el2) {
  46. return el1 > el2 ? el1 : el2;
  47. }
  48. };
  49. struct MinimumOp {
  50. template <typename data_type>
  51. static data_type op(data_type el1, data_type el2) {
  52. return el1 < el2 ? el1 : el2;
  53. }
  54. };
  55. } // namespace
  56. template <typename data_type, typename op_type>
  57. void TFLiteOperation(TfLiteContext* context, TfLiteNode* node,
  58. const OpContext& op_context) {
  59. reference_ops::MaximumMinimumBroadcastSlow(
  60. tflite::micro::GetTensorShape(op_context.input1),
  61. tflite::micro::GetTensorData<data_type>(op_context.input1),
  62. tflite::micro::GetTensorShape(op_context.input2),
  63. tflite::micro::GetTensorData<data_type>(op_context.input2),
  64. tflite::micro::GetTensorShape(op_context.output),
  65. tflite::micro::GetTensorData<data_type>(op_context.output),
  66. op_type::template op<data_type>);
  67. }
  68. template <KernelType kernel_type, typename OpType>
  69. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  70. OpContext op_context(context, node);
  71. if (kernel_type == kReference) {
  72. switch (op_context.output->type) {
  73. case kTfLiteFloat32:
  74. TFLiteOperation<float, OpType>(context, node, op_context);
  75. break;
  76. case kTfLiteUInt8:
  77. TFLiteOperation<uint8_t, OpType>(context, node, op_context);
  78. break;
  79. case kTfLiteInt8:
  80. TFLiteOperation<int8_t, OpType>(context, node, op_context);
  81. break;
  82. case kTfLiteInt32:
  83. TFLiteOperation<int32_t, OpType>(context, node, op_context);
  84. break;
  85. case kTfLiteInt64:
  86. TFLiteOperation<int64_t, OpType>(context, node, op_context);
  87. break;
  88. default:
  89. TF_LITE_KERNEL_LOG(context,
  90. "Type %s (%d) is not supported by Maximum/Minimum.",
  91. TfLiteTypeGetName(op_context.output->type),
  92. op_context.output->type);
  93. return kTfLiteError;
  94. }
  95. } else {
  96. TF_LITE_KERNEL_LOG(context,
  97. "Kernel type not supported by Maximum/Minimum.");
  98. return kTfLiteError;
  99. }
  100. return kTfLiteOk;
  101. }
  102. } // namespace maximum_minimum
  103. TfLiteRegistration Register_MAXIMUM() {
  104. return {/*init=*/nullptr,
  105. /*free=*/nullptr,
  106. /*prepare=*/nullptr,
  107. /*invoke=*/
  108. maximum_minimum::Eval<maximum_minimum::kReference,
  109. maximum_minimum::MaximumOp>,
  110. /*profiling_string=*/nullptr,
  111. /*builtin_code=*/0,
  112. /*custom_name=*/nullptr,
  113. /*version=*/0};
  114. }
  115. TfLiteRegistration Register_MINIMUM() {
  116. return {/*init=*/nullptr,
  117. /*free=*/nullptr,
  118. /*prepare=*/nullptr,
  119. /*invoke=*/
  120. maximum_minimum::Eval<maximum_minimum::kReference,
  121. maximum_minimum::MinimumOp>,
  122. /*profiling_string=*/nullptr,
  123. /*builtin_code=*/0,
  124. /*custom_name=*/nullptr,
  125. /*version=*/0};
  126. }
  127. } // namespace micro
  128. } // namespace ops
  129. } // namespace tflite