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/IR/Attributes.h"  // from @llvm-project
26 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
27 #include "mlir/Support/LogicalResult.h"  // from @llvm-project
28 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_attributes.h"
29 
30 namespace mlir {
31 namespace TFL {
32 
33 // Abstracts the conversion of the padded NMS composite function.
34 class ConvertNMSPaddedFunc {
35  public:
ConvertNMSPaddedFunc(FuncOp func)36   explicit ConvertNMSPaddedFunc(FuncOp func) : func_(func) {}
37 
38   void RewriteFunc();
39 
40   LogicalResult VerifySignature();
41 
42  private:
43   FuncOp func_;
44 };
45 
46 // Abstracts the conversion of the SSD post-processing composite function to
47 // TFLite.
48 class ConvertSSDPostProcessFunc {
49  public:
ConvertSSDPostProcessFunc(FuncOp func,mlir::TF::FuncAttr attr)50   explicit ConvertSSDPostProcessFunc(FuncOp func, mlir::TF::FuncAttr attr)
51       : func_(func), attr_(attr) {}
52 
53   LogicalResult RewriteFunc();
54 
55   LogicalResult VerifySignature();
56 
57  private:
58   LogicalResult CreateNMSCustomOptions(FuncOp func, DictionaryAttr attrs,
59                                        std::string& custom_option_buffer);
60 
61   LogicalResult AddIntAttr(FuncOp func, DictionaryAttr attrs,
62                            const std::string& attribute,
63                            flexbuffers::Builder* builder);
64 
65   LogicalResult AddFloatAttr(FuncOp func, DictionaryAttr attrs,
66                              const std::string& attribute,
67                              flexbuffers::Builder* builder);
68 
69   FuncOp func_;
70   mlir::TF::FuncAttr attr_;
71 };
72 
73 }  // end namespace TFL
74 }  // end namespace mlir
75 
76 #endif  // TENSORFLOW_COMPILER_MLIR_LITE_UTILS_TFTEXT_UTILS_H_
77