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 "pybind11/pybind11.h"
17 #include "pybind11/pytypes.h"
18 #include "tensorflow/c/tf_status.h"
19 #include "tensorflow/compiler/mlir/python/mlir.h"
20 #include "tensorflow/python/lib/core/pybind11_lib.h"
21 #include "tensorflow/python/lib/core/pybind11_status.h"
22 #include "tensorflow/python/lib/core/safe_ptr.h"
23 
PYBIND11_MODULE(_pywrap_mlir,m)24 PYBIND11_MODULE(_pywrap_mlir, m) {
25   m.def("ImportGraphDef",
26         [](const std::string &graphdef, const std::string &pass_pipeline,
27            bool show_debug_info) {
28           tensorflow::Safe_TF_StatusPtr status =
29               tensorflow::make_safe(TF_NewStatus());
30           std::string output = tensorflow::ImportGraphDef(
31               graphdef, pass_pipeline, show_debug_info, status.get());
32           tensorflow::MaybeRaiseRegisteredFromTFStatus(status.get());
33           return output;
34         });
35 
36   m.def("ImportFunction",
37         [](const py::handle &context, const std::string &functiondef,
38            const std::string &pass_pipeline, bool show_debug_info) {
39           tensorflow::Safe_TF_StatusPtr status =
40               tensorflow::make_safe(TF_NewStatus());
41           auto *ctxt = static_cast<TFE_Context *>(
42               PyCapsule_GetPointer(context.ptr(), nullptr));
43           if (!ctxt) throw py::error_already_set();
44           std::string output = tensorflow::ImportFunction(
45               functiondef, pass_pipeline, show_debug_info, ctxt, status.get());
46           tensorflow::MaybeRaiseRegisteredFromTFStatus(status.get());
47           return output;
48         });
49 
50   m.def("ExperimentalConvertSavedModelToMlir",
51         [](const std::string &saved_model_path,
52            const std::string &exported_names, bool show_debug_info) {
53           tensorflow::Safe_TF_StatusPtr status =
54               tensorflow::make_safe(TF_NewStatus());
55           std::string output = tensorflow::ExperimentalConvertSavedModelToMlir(
56               saved_model_path, exported_names, show_debug_info, status.get());
57           tensorflow::MaybeRaiseRegisteredFromTFStatus(status.get());
58           return output;
59         });
60 
61   m.def("ExperimentalConvertSavedModelV1ToMlirLite",
62         [](const std::string &saved_model_path, const std::string &tags,
63            bool upgrade_legacy, bool show_debug_info) {
64           tensorflow::Safe_TF_StatusPtr status =
65               tensorflow::make_safe(TF_NewStatus());
66           std::string output =
67               tensorflow::ExperimentalConvertSavedModelV1ToMlirLite(
68                   saved_model_path, tags, upgrade_legacy, show_debug_info,
69                   status.get());
70           tensorflow::MaybeRaiseRegisteredFromTFStatus(status.get());
71           return output;
72         });
73 
74   m.def("ExperimentalConvertSavedModelV1ToMlir",
75         [](const std::string &saved_model_path, const std::string &tags,
76            bool lift_variables, bool upgrade_legacy, bool show_debug_info) {
77           tensorflow::Safe_TF_StatusPtr status =
78               tensorflow::make_safe(TF_NewStatus());
79           std::string output =
80               tensorflow::ExperimentalConvertSavedModelV1ToMlir(
81                   saved_model_path, tags, lift_variables, upgrade_legacy,
82                   show_debug_info, status.get());
83           tensorflow::MaybeRaiseRegisteredFromTFStatus(status.get());
84           return output;
85         });
86 
87   m.def("ExperimentalRunPassPipeline",
88         [](const std::string &mlir_txt, const std::string &pass_pipeline,
89            bool show_debug_info) {
90           tensorflow::Safe_TF_StatusPtr status =
91               tensorflow::make_safe(TF_NewStatus());
92           std::string output = tensorflow::ExperimentalRunPassPipeline(
93               mlir_txt, pass_pipeline, show_debug_info, status.get());
94           tensorflow::MaybeRaiseRegisteredFromTFStatus(status.get());
95           return output;
96         });
97 };
98