xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.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 #ifndef TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_TF_TFL_FLATBUFFER_HELPERS_H_
16 #define TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_TF_TFL_FLATBUFFER_HELPERS_H_
17 
18 #include <ostream>
19 #include <string>
20 #include <unordered_set>
21 #include <utility>
22 
23 #include "llvm/ADT/Optional.h"
24 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
25 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
26 #include "tensorflow/compiler/mlir/lite/common/tfl_pass_config.h"
27 #include "tensorflow/compiler/mlir/lite/transforms/passes.h"
28 #include "tensorflow/core/public/session.h"
29 #include "tensorflow/lite/toco/model_flags.pb.h"
30 #include "tensorflow/lite/toco/toco_flags.pb.h"
31 #include "tensorflow/lite/toco/types.pb.h"
32 #include "tensorflow/stream_executor/lib/statusor.h"
33 
34 namespace tensorflow {
35 namespace internal {
36 
37 // Register all custom ops including user specified custom ops.
38 Status RegisterAllCustomOps(const toco::TocoFlags& toco_flags);
39 
40 // Populate quantization specs (or not) given user specified ranges for each
41 // input arrays.
42 Status PopulateQuantizationSpecs(
43     const toco::ModelFlags& model_flags, const toco::TocoFlags& toco_flags,
44     mlir::quant::QuantizationSpecs* quant_specs,
45     std::vector<string>* node_names, std::vector<string>* node_dtypes,
46     std::vector<llvm::Optional<std::vector<int>>>* node_shapes,
47     std::vector<llvm::Optional<double>>* node_mins,
48     std::vector<llvm::Optional<double>>* node_maxs);
49 
50 // Convert imported MLIR file to TfLite flatbuffer.
51 // This will also run relevant passes as well.
52 Status ConvertMLIRToTFLiteFlatBuffer(
53     const toco::ModelFlags& model_flags, const toco::TocoFlags& toco_flags,
54     mlir::OwningOpRef<mlir::ModuleOp> module,
55     const mlir::TFL::PassConfig& pass_config,
56     const std::unordered_set<std::string>& saved_model_tags, string* result,
57     llvm::Optional<tensorflow::Session*> session);
58 
59 // Give a warning for any unused flags that have been specified.
60 void WarningUnusedFlags(const toco::ModelFlags& model_flags,
61                         const toco::TocoFlags& toco_flags);
62 }  // namespace internal
63 }  // namespace tensorflow
64 
65 #endif  // TENSORFLOW_COMPILER_MLIR_LITE_PYTHON_TF_TFL_FLATBUFFER_HELPERS_H_
66