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 #include "tensorflow/compiler/mlir/lite/sparsity/sparsify_model.h"
17
18 #include "absl/strings/string_view.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "mlir/IR/Location.h" // TF:llvm-project
21 #include "mlir/IR/MLIRContext.h" // TF:llvm-project
22 #include "mlir/IR/Module.h" // TF:llvm-project
23 #include "mlir/Pass/Pass.h" // TF:llvm-project
24 #include "mlir/Pass/PassManager.h" // TF:llvm-project
25 #include "tensorflow/compiler/mlir/lite/common/tfl_pass_config.h"
26 #include "tensorflow/compiler/mlir/lite/flatbuffer_import.h"
27 #include "tensorflow/compiler/mlir/lite/flatbuffer_translate.h"
28 #include "tensorflow/compiler/mlir/lite/utils/convert_type.h"
29 #include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h"
30 #include "tensorflow/core/framework/types.pb.h"
31
32 namespace mlir {
33 namespace lite {
34
SparsifyModel(const tflite::ModelT & input_model,flatbuffers::FlatBufferBuilder * builder,tflite::ErrorReporter * error_reporter)35 TfLiteStatus SparsifyModel(const tflite::ModelT& input_model,
36 flatbuffers::FlatBufferBuilder* builder,
37 tflite::ErrorReporter* error_reporter) {
38 MLIRContext context;
39 StatusScopedDiagnosticHandler statusHandler(&context,
40 /*propagate=*/true);
41
42 // Import input_model to a MLIR module
43 flatbuffers::FlatBufferBuilder input_builder;
44 flatbuffers::Offset<tflite::Model> input_model_location =
45 tflite::Model::Pack(input_builder, &input_model);
46 tflite::FinishModelBuffer(input_builder, input_model_location);
47
48 std::string serialized_model(
49 reinterpret_cast<const char*>(input_builder.GetBufferPointer()),
50 input_builder.GetSize());
51 std::vector<std::string> output_arrays_order;
52
53 OwningModuleRef module =
54 tflite::FlatBufferToMlir(serialized_model, &context,
55 UnknownLoc::get(&context), output_arrays_order);
56 if (!module) {
57 error_reporter->Report("Couldn't import flatbuffer to MLIR.");
58 return kTfLiteError;
59 }
60
61 PassManager pm(module->getContext());
62
63 if (failed(pm.run(module.get()))) {
64 const std::string& err = statusHandler.ConsumeStatus().error_message();
65 error_reporter->Report("Failed to sparsify: %s", err.c_str());
66 return kTfLiteError;
67 }
68
69 // Export the results to the builder
70 std::string result;
71 if (tflite::MlirToFlatBufferTranslateFunction(
72 module.get(), &result, /*emit_builtin_tflite_ops=*/true,
73 /*emit_select_tf_ops=*/true, /*emit_custom_ops=*/true)) {
74 error_reporter->Report("Failed to export MLIR to flatbuffer.");
75 return kTfLiteError;
76 }
77 builder->PushFlatBuffer(reinterpret_cast<const uint8_t*>(result.data()),
78 result.size());
79
80 return kTfLiteOk;
81 }
82
83 } // namespace lite
84 } // namespace mlir
85