1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/kernels/internal/reference/pooling.h"
16 
17 #include "tensorflow/lite/c/builtin_op_data.h"
18 #include "tensorflow/lite/kernels/kernel_util.h"
19 #include "tensorflow/lite/micro/kernels/kernel_util.h"
20 #include "tensorflow/lite/micro/kernels/pooling.h"
21 
22 namespace tflite {
23 
24 namespace {
25 
AverageEval(TfLiteContext * context,TfLiteNode * node)26 TfLiteStatus AverageEval(TfLiteContext* context, TfLiteNode* node) {
27   TFLITE_DCHECK(node->builtin_data != nullptr);
28   auto* params = reinterpret_cast<TfLitePoolParams*>(node->builtin_data);
29 
30   TFLITE_DCHECK(node->user_data != nullptr);
31   const OpDataPooling* data =
32       static_cast<const OpDataPooling*>(node->user_data);
33 
34   const TfLiteEvalTensor* input =
35       micro::GetEvalInput(context, node, kPoolingInputTensor);
36   TfLiteEvalTensor* output =
37       micro::GetEvalOutput(context, node, kPoolingOutputTensor);
38 
39   // Inputs and outputs share the same type, guaranteed by the converter.
40   switch (input->type) {
41     case kTfLiteFloat32:
42       AveragePoolingEvalFloat(context, node, params, data, input, output);
43       break;
44     case kTfLiteInt8:
45       AveragePoolingEvalQuantized(context, node, params, data, input, output);
46       break;
47     default:
48       TF_LITE_KERNEL_LOG(context, "Input type %s is not currently supported",
49                          TfLiteTypeGetName(input->type));
50       return kTfLiteError;
51   }
52   return kTfLiteOk;
53 }
54 
MaxEval(TfLiteContext * context,TfLiteNode * node)55 TfLiteStatus MaxEval(TfLiteContext* context, TfLiteNode* node) {
56   TFLITE_DCHECK(node->builtin_data != nullptr);
57   auto* params = reinterpret_cast<TfLitePoolParams*>(node->builtin_data);
58 
59   TFLITE_DCHECK(node->user_data != nullptr);
60   const OpDataPooling* data =
61       static_cast<const OpDataPooling*>(node->user_data);
62 
63   const TfLiteEvalTensor* input =
64       micro::GetEvalInput(context, node, kPoolingInputTensor);
65   TfLiteEvalTensor* output =
66       micro::GetEvalOutput(context, node, kPoolingOutputTensor);
67 
68   switch (input->type) {
69     case kTfLiteFloat32:
70       MaxPoolingEvalFloat(context, node, params, data, input, output);
71       break;
72     case kTfLiteInt8:
73       MaxPoolingEvalQuantized(context, node, params, data, input, output);
74       break;
75     default:
76       TF_LITE_KERNEL_LOG(context, "Type %s not currently supported.",
77                          TfLiteTypeGetName(input->type));
78       return kTfLiteError;
79   }
80   return kTfLiteOk;
81 }
82 
Init(TfLiteContext * context,const char * buffer,size_t length)83 void* Init(TfLiteContext* context, const char* buffer, size_t length) {
84   TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
85   return context->AllocatePersistentBuffer(context, sizeof(OpDataPooling));
86 }
87 
88 }  // namespace
89 
Register_AVERAGE_POOL_2D()90 TfLiteRegistration Register_AVERAGE_POOL_2D() {
91   return {/*init=*/Init,
92           /*free=*/nullptr,
93           /*prepare=*/PoolingPrepare,
94           /*invoke=*/AverageEval,
95           /*profiling_string=*/nullptr,
96           /*builtin_code=*/0,
97           /*custom_name=*/nullptr,
98           /*version=*/0};
99 }
100 
Register_MAX_POOL_2D()101 TfLiteRegistration Register_MAX_POOL_2D() {
102   return {/*init=*/Init,
103           /*free=*/nullptr,
104           /*prepare=*/PoolingPrepare,
105           /*invoke=*/MaxEval,
106           /*profiling_string=*/nullptr,
107           /*builtin_code=*/0,
108           /*custom_name=*/nullptr,
109           /*version=*/0};
110 }
111 
112 }  // namespace tflite
113