• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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 #include "tensorflow/compiler/mlir/lite/python/graphdef_to_tfl_flatbuffer.h"
17 
18 #include <ostream>
19 #include <utility>
20 
21 #include "llvm/ADT/None.h"
22 #include "llvm/Support/ToolOutputFile.h"
23 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
24 #include "mlir/IR/MLIRContext.h"  // from @llvm-project
25 #include "mlir/Pass/Pass.h"  // from @llvm-project
26 #include "mlir/Support/FileUtilities.h"  // from @llvm-project
27 #include "mlir/Transforms/ViewOpGraph.h"  // from @llvm-project
28 #include "tensorflow/compiler/mlir/lite/common/tfl_pass_config.h"
29 #include "tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.h"
30 #include "tensorflow/compiler/mlir/lite/tf_tfl_passes.h"
31 #include "tensorflow/compiler/mlir/lite/tf_to_tfl_flatbuffer.h"
32 #include "tensorflow/compiler/mlir/lite/transforms/passes.h"
33 #include "tensorflow/compiler/mlir/tensorflow/translate/import_model.h"
34 #include "tensorflow/compiler/mlir/tensorflow/translate/mlir_roundtrip_flags.h"
35 #include "tensorflow/core/framework/graph.pb.h"
36 #include "tensorflow/core/framework/types.pb.h"
37 #include "tensorflow/core/lib/core/errors.h"
38 #include "tensorflow/core/platform/status.h"
39 #include "tensorflow/core/protobuf/graph_debug_info.pb.h"
40 #include "tensorflow/lite/toco/model_flags.pb.h"
41 #include "tensorflow/lite/toco/toco_flags.pb.h"
42 #include "tensorflow/lite/toco/types.pb.h"
43 #include "tensorflow/stream_executor/lib/statusor.h"
44 
45 namespace tensorflow {
ConvertGraphDefToTFLiteFlatBuffer(const toco::ModelFlags & model_flags,const toco::TocoFlags & toco_flags,const GraphDebugInfo & debug_info,const GraphDef & input,string * result)46 Status ConvertGraphDefToTFLiteFlatBuffer(const toco::ModelFlags& model_flags,
47                                          const toco::TocoFlags& toco_flags,
48                                          const GraphDebugInfo& debug_info,
49                                          const GraphDef& input,
50                                          string* result) {
51   using ::tflite::optimize::ReducedPrecisionSupport;
52   mlir::MLIRContext context;
53   GraphImportConfig specs;
54   mlir::TFL::QuantizationSpecs quant_specs;
55 
56   // Parse input arrays.
57   std::vector<string> node_names;
58   std::vector<string> node_dtypes;
59   std::vector<llvm::Optional<std::vector<int>>> node_shapes;
60   std::vector<llvm::Optional<double>> node_mins;
61   std::vector<llvm::Optional<double>> node_maxs;
62 
63   // Populate quantization specs.
64   TF_RETURN_IF_ERROR(internal::PopulateQuantizationSpecs(
65       model_flags, toco_flags, &quant_specs, &node_names, &node_dtypes,
66       &node_shapes, &node_mins, &node_maxs));
67 
68   TF_RETURN_IF_ERROR(tensorflow::ParseInputArrayInfo(
69       node_names, node_dtypes, node_shapes, &specs.inputs));
70 
71   if (toco_flags.quantize_to_float16() || toco_flags.allow_bfloat16()) {
72     ReducedPrecisionSupport mask = ReducedPrecisionSupport::None;
73     if (toco_flags.quantize_to_float16()) {
74       mask |= ReducedPrecisionSupport::Float16Inference;
75     }
76     if (toco_flags.allow_bfloat16()) {
77       mask |= ReducedPrecisionSupport::Bfloat16Inference;
78     }
79     if (toco_flags.accumulation_type() == toco::IODataType::FLOAT16) {
80       mask |= ReducedPrecisionSupport::Float16Accumulation;
81     } else {
82       mask |= ReducedPrecisionSupport::Float32Accumulation;
83     }
84     quant_specs.support_mask = mask;
85   }
86   // Parse output arrays.
87   std::vector<string> output_arrays(model_flags.output_arrays().begin(),
88                                     model_flags.output_arrays().end());
89   TF_RETURN_IF_ERROR(
90       tensorflow::ParseOutputArrayInfo(output_arrays, &specs.outputs));
91 
92   // Parse control output arrays.
93   std::vector<string> control_output_arrays(
94       model_flags.control_output_arrays().begin(),
95       model_flags.control_output_arrays().end());
96   TF_RETURN_IF_ERROR(tensorflow::ParseOutputArrayInfo(control_output_arrays,
97                                                       &specs.control_outputs));
98 
99   specs.prune_unused_nodes = true;
100   specs.convert_legacy_fed_inputs = true;
101   specs.graph_as_function = false;
102   specs.upgrade_legacy = true;
103   internal::WarningUnusedFlags(model_flags, toco_flags);
104 
105   // Register all custom ops, including user-specified custom ops.
106   TF_RETURN_IF_ERROR(internal::RegisterAllCustomOps(toco_flags));
107 
108   TF_ASSIGN_OR_RETURN(
109       auto module, ConvertGraphdefToMlir(input, debug_info, specs, &context));
110 
111   mlir::TFL::PassConfig pass_config(quant_specs);
112   bool emit_builtin_tflite_ops = !toco_flags.force_select_tf_ops();
113   pass_config.emit_builtin_tflite_ops = emit_builtin_tflite_ops;
114   pass_config.unfold_batch_matmul = toco_flags.unfold_batchmatmul();
115   pass_config.lower_tensor_list_ops = toco_flags.lower_tensor_list_ops();
116   // Disable the unfolding of the 16x16 TF::BatchMatMulOp to avoid the
117   // conversion to an unsupported 16x16 TFL::FullyConnectedOp.
118   if (toco_flags.inference_type() == toco::IODataType::QUANTIZED_INT16) {
119     pass_config.unfold_batch_matmul = false;
120   }
121   pass_config.unfold_large_splat_constant =
122       toco_flags.unfold_large_splat_constant();
123 
124   return internal::ConvertMLIRToTFLiteFlatBuffer(
125       model_flags, toco_flags, std::move(module), pass_config,
126       /*saved_model_tags=*/{}, result,
127       /*session=*/llvm::None);
128 }
129 
130 }  // namespace tensorflow
131