|
| 1 | +#include "larq_compute_engine/mlir/tf_to_tfl_flatbuffer.h" |
| 2 | + |
| 3 | +#include "llvm/Support/raw_ostream.h" |
| 4 | +#include "mlir/IR/BuiltinOps.h" |
| 5 | +#include "mlir/Pass/PassManager.h" |
| 6 | +#include "tensorflow/compiler/mlir/lite/flatbuffer_export.h" |
| 7 | +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_executor.h" |
| 8 | +#include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h" |
| 9 | +#include "tensorflow/core/framework/op.h" |
| 10 | +#include "tensorflow/stream_executor/lib/statusor.h" |
| 11 | + |
| 12 | +namespace tensorflow { |
| 13 | +namespace { |
| 14 | +using mlir::ModuleOp; |
| 15 | +using mlir::Operation; |
| 16 | + |
| 17 | +bool IsControlFlowV1Op(Operation* op) { |
| 18 | + return mlir::isa<mlir::tf_executor::SwitchOp, mlir::tf_executor::MergeOp, |
| 19 | + mlir::tf_executor::EnterOp, mlir::tf_executor::ExitOp, |
| 20 | + mlir::tf_executor::NextIterationSinkOp, |
| 21 | + mlir::tf_executor::NextIterationSourceOp>(op); |
| 22 | +} |
| 23 | + |
| 24 | +mlir::LogicalResult IsValidGraph(mlir::ModuleOp module) { |
| 25 | + auto result = module.walk([&](Operation* op) { |
| 26 | + return IsControlFlowV1Op(op) ? mlir::WalkResult::interrupt() |
| 27 | + : mlir::WalkResult::advance(); |
| 28 | + }); |
| 29 | + if (result.wasInterrupted()) { |
| 30 | + module.emitError( |
| 31 | + "The graph has Control Flow V1 ops. TFLite converter doesn't support " |
| 32 | + "Control Flow V1 ops. Consider using Control Flow V2 ops instead. See " |
| 33 | + "https://www.tensorflow.org/api_docs/python/tf/compat/v1/" |
| 34 | + "enable_control_flow_v2."); |
| 35 | + return mlir::failure(); |
| 36 | + } |
| 37 | + return mlir::success(); |
| 38 | +} |
| 39 | + |
| 40 | +// Truncates names to a maximum length of ~50 characters since LCE op location |
| 41 | +// names can be very long otherwise. |
| 42 | +class TruncateOpOrArgLocNameMapper : public OpOrArgLocNameMapper { |
| 43 | + protected: |
| 44 | + std::string GetName(OpOrVal op_or_val) override { |
| 45 | + auto name = OpOrArgLocNameMapper::GetName(op_or_val); |
| 46 | + if (name.length() > 50) return name.substr(0, 50); |
| 47 | + return name; |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +} // namespace |
| 52 | + |
| 53 | +Status ConvertTFExecutorToFlatbuffer(mlir::ModuleOp module, bool export_to_mlir, |
| 54 | + std::string* result, |
| 55 | + mlir::PassManager* pass_manager) { |
| 56 | + // Explicitly disable dumping Op details on failures. |
| 57 | + module.getContext()->printOpOnDiagnostic(false); |
| 58 | + |
| 59 | + // Register a warning handler only log to std out. |
| 60 | + mlir::ScopedDiagnosticHandler s( |
| 61 | + module.getContext(), [](mlir::Diagnostic& diag) { |
| 62 | + if (diag.getSeverity() == mlir::DiagnosticSeverity::Warning) { |
| 63 | + for (auto& note : diag.getNotes()) { |
| 64 | + std::cout << note.str() << "\n"; |
| 65 | + LOG(WARNING) << note.str() << "\n"; |
| 66 | + } |
| 67 | + } |
| 68 | + return mlir::failure(); |
| 69 | + }); |
| 70 | + |
| 71 | + mlir::StatusScopedDiagnosticHandler statusHandler(module.getContext(), |
| 72 | + /*propagate=*/true); |
| 73 | + |
| 74 | + if (failed(IsValidGraph(module)) || failed(pass_manager->run(module))) { |
| 75 | + return statusHandler.ConsumeStatus(); |
| 76 | + } |
| 77 | + |
| 78 | + if (export_to_mlir) { |
| 79 | + llvm::raw_string_ostream os(*result); |
| 80 | + module.print(os); |
| 81 | + return Status::OK(); |
| 82 | + } |
| 83 | + |
| 84 | + // This is the only modification compared to the upstream tensorflow file |
| 85 | + TruncateOpOrArgLocNameMapper op_or_arg_name_mapper; |
| 86 | + tflite::FlatbufferExportOptions options; |
| 87 | + options.emit_builtin_tflite_ops = true; |
| 88 | + options.emit_custom_ops = true; |
| 89 | + options.op_or_arg_name_mapper = &op_or_arg_name_mapper; |
| 90 | + if (!tflite::MlirToFlatBufferTranslateFunction(module, options, result)) { |
| 91 | + return statusHandler.ConsumeStatus(); |
| 92 | + } |
| 93 | + |
| 94 | + if (mlir::failed(module.verify())) { |
| 95 | + return tensorflow::errors::Unknown("Final module is invalid"); |
| 96 | + } |
| 97 | + return Status::OK(); |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace tensorflow |
0 commit comments