xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/lite/utils/nms_utils.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 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 // This header file defines common utils used by TFLite transformation
17 // passes to work with NMS ops in TFLite.
18 
19 #ifndef TENSORFLOW_COMPILER_MLIR_LITE_UTILS_NMS_UTILS_H_
20 #define TENSORFLOW_COMPILER_MLIR_LITE_UTILS_NMS_UTILS_H_
21 
22 #include <string>
23 
24 #include "flatbuffers/flexbuffers.h"  // from @flatbuffers
25 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
26 #include "mlir/IR/Attributes.h"  // from @llvm-project
27 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
28 #include "mlir/Support/LogicalResult.h"  // from @llvm-project
29 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_attributes.h"
30 
31 namespace mlir {
32 namespace TFL {
33 
34 // Abstracts the conversion of the padded NMS composite function.
35 class ConvertNMSPaddedFunc {
36  public:
ConvertNMSPaddedFunc(func::FuncOp func)37   explicit ConvertNMSPaddedFunc(func::FuncOp func) : func_(func) {}
38 
39   void RewriteFunc();
40 
41   LogicalResult VerifySignature();
42 
43  private:
44   func::FuncOp func_;
45 };
46 
47 // Abstracts the conversion of the SSD post-processing composite function to
48 // TFLite.
49 class ConvertSSDPostProcessFunc {
50  public:
ConvertSSDPostProcessFunc(func::FuncOp func,mlir::TF::FuncAttr attr)51   explicit ConvertSSDPostProcessFunc(func::FuncOp func, mlir::TF::FuncAttr attr)
52       : func_(func), attr_(attr) {}
53 
54   LogicalResult RewriteFunc();
55 
56   LogicalResult VerifySignature();
57 
58  private:
59   LogicalResult CreateNMSCustomOptions(func::FuncOp func, DictionaryAttr attrs,
60                                        std::string& custom_option_buffer);
61 
62   LogicalResult AddIntAttr(func::FuncOp func, DictionaryAttr attrs,
63                            const std::string& attribute,
64                            flexbuffers::Builder* builder);
65 
66   LogicalResult AddFloatAttr(func::FuncOp func, DictionaryAttr attrs,
67                              const std::string& attribute,
68                              flexbuffers::Builder* builder);
69 
70   LogicalResult HasIntAttr(func::FuncOp func, DictionaryAttr attrs,
71                            const std::string& attribute);
72 
73   LogicalResult HasFloatAttr(func::FuncOp func, DictionaryAttr attrs,
74                              const std::string& attribute);
75 
76   func::FuncOp func_;
77   mlir::TF::FuncAttr attr_;
78 };
79 
80 }  // end namespace TFL
81 }  // end namespace mlir
82 
83 #endif  // TENSORFLOW_COMPILER_MLIR_LITE_UTILS_TFTEXT_UTILS_H_
84