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 
16 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_COMMON_OPERATION_PARSER_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_OPERATION_PARSER_H_
18 
19 #include "absl/container/flat_hash_map.h"
20 #include "tensorflow/lite/c/common.h"
21 #include "tensorflow/lite/delegates/gpu/common/model.h"
22 #include "tensorflow/lite/delegates/gpu/common/object_reader.h"
23 #include "tensorflow/lite/delegates/gpu/common/operations.h"
24 #include "tensorflow/lite/delegates/gpu/common/status.h"
25 
26 namespace tflite {
27 namespace gpu {
28 
29 // Parses TFLite operation and updates provided GraphFloat32.
30 class TFLiteOperationParser {
31  public:
32   virtual ~TFLiteOperationParser() = default;
33 
34   // Parses TFLite operation. This method allows expanding fused operations
35   // into more than one node.
36   virtual absl::Status Parse(const TfLiteNode* tflite_node,
37                              const TfLiteRegistration* registration,
38                              GraphFloat32* graph, ObjectReader* reader) = 0;
39 
40   // Verifies whether passed tflite node may be built by GPU delegate or not.
41   virtual absl::Status IsSupported(const TfLiteContext* context,
42                                    const TfLiteNode* tflite_node,
43                                    const TfLiteRegistration* registration) = 0;
44 
45   // Returns the value IDs in the graph that correspond to the updated values of
46   // the variable input tensor.
47   virtual absl::flat_hash_map<int, ValueId>
GetNewValueIdsForVariableInputNodes()48   GetNewValueIdsForVariableInputNodes() {
49     return {};
50   }
51 };
52 
53 absl::Status CheckKernelsAndStrides(int kernel_h, int kernel_w, int strides_h,
54                                     int strides_w);
55 absl::Status CheckMaxSupportedOpVersion(const TfLiteRegistration* registration,
56                                         int max_version);
57 absl::Status CheckStrides(int strides_h, int strides_w);
58 absl::Status CheckTensorIsAvailable(const TfLiteContext* context,
59                                     const TfLiteNode* tflite_node, int idx);
60 HW ToHW(int32_t h, int32_t w);
61 absl::Status ParsePoolingAttributes(const TfLitePoolParams* tf_options,
62                                     const BHWC& input_shape,
63                                     Pooling2DAttributes* attr);
64 
65 template <typename AttrT>
UpdatePadding(const TfLitePadding & padding,const BHWC & input_shape,AttrT * attr)66 void UpdatePadding(const TfLitePadding& padding, const BHWC& input_shape,
67                    AttrT* attr) {
68   if (padding == kTfLitePaddingSame) {
69     attr->padding = CalculateSamePadding(input_shape, *attr);
70   } else {
71     attr->padding.prepended = HW(0, 0);
72     attr->padding.appended = HW(0, 0);
73   }
74 }
75 
76 }  // namespace gpu
77 }  // namespace tflite
78 
79 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_OPERATION_PARSER_H_
80