reduce.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright 2022 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/reduce.h"
  13. #include "tensorflow/lite/c/builtin_op_data.h"
  14. #include "tensorflow/lite/c/common.h"
  15. #include "tensorflow/lite/kernels/internal/quantization_util.h"
  16. #include "tensorflow/lite/kernels/internal/reference/integer_ops/mean.h"
  17. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  18. #include "tensorflow/lite/kernels/internal/types.h"
  19. #include "tensorflow/lite/kernels/kernel_util.h"
  20. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  21. #include "tensorflow/lite/micro/kernels/reduce.h"
  22. #include "tensorflow/lite/micro/micro_utils.h"
  23. namespace tflite {
  24. void* InitReduce(TfLiteContext* context, const char* buffer, size_t length) {
  25. return context->AllocatePersistentBuffer(context, sizeof(OpDataReduce));
  26. }
  27. TfLiteStatus PrepareMax(TfLiteContext* context, TfLiteNode* node) {
  28. return PrepareMaxHelper(context, node,
  29. static_cast<OpDataReduce*>(node->user_data));
  30. }
  31. TfLiteStatus PrepareMeanOrSum(TfLiteContext* context, TfLiteNode* node) {
  32. return PrepareMeanOrSumHelper(context, node,
  33. static_cast<OpDataReduce*>(node->user_data));
  34. }
  35. TfLiteStatus EvalMean(TfLiteContext* context, TfLiteNode* node) {
  36. return EvalMeanHelper(context, node,
  37. static_cast<OpDataReduce*>(node->user_data));
  38. }
  39. TfLiteStatus EvalMax(TfLiteContext* context, TfLiteNode* node) {
  40. OpDataReduce* op_data = static_cast<OpDataReduce*>(node->user_data);
  41. return EvalMaxHelper(context, node, op_data);
  42. }
  43. TfLiteStatus EvalSum(TfLiteContext* context, TfLiteNode* node) {
  44. return EvalSumHelper(context, node,
  45. static_cast<OpDataReduce*>(node->user_data));
  46. }
  47. TfLiteRegistration Register_MEAN() {
  48. return tflite::micro::RegisterOp(InitReduce, PrepareMeanOrSum, EvalMean);
  49. }
  50. TfLiteRegistration Register_REDUCE_MAX() {
  51. return tflite::micro::RegisterOp(InitReduce, PrepareMax, EvalMax);
  52. }
  53. TfLiteRegistration Register_SUM() {
  54. return tflite::micro::RegisterOp(InitReduce, PrepareMeanOrSum, EvalSum);
  55. }
  56. } // namespace tflite