• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_PYTHON_ADAPTER_H_
18 #define MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_PYTHON_ADAPTER_H_
19 #include <map>
20 #include <memory>
21 #include <string>
22 
23 #include "pybind11/embed.h"
24 #include "pybind11/pybind11.h"
25 #include "pybind11/stl.h"
26 
27 #include "pipeline/jit/parse/parse_base.h"
28 #include "utils/log_adapter.h"
29 
30 namespace mindspore {
31 namespace parse {
32 // A utility to call python interface
33 namespace python_adapter {
34 py::module GetPyModule(const std::string &module);
35 py::object GetPyObjAttr(const py::object &obj, const std::string &attr);
36 template <class... T>
CallPyObjMethod(const py::object & obj,const std::string & method,T...args)37 py::object CallPyObjMethod(const py::object &obj, const std::string &method, T... args) {
38   if (!method.empty() && !py::isinstance<py::none>(obj)) {
39     return obj.attr(method.c_str())(args...);
40   }
41   return py::none();
42 }
43 
44 // call python function of module
45 template <class... T>
CallPyModFn(const py::module & mod,const std::string & function,T...args)46 py::object CallPyModFn(const py::module &mod, const std::string &function, T... args) {
47   if (!function.empty() && !py::isinstance<py::none>(mod)) {
48     return mod.attr(function.c_str())(args...);
49   }
50   return py::none();
51 }
52 
53 // turn off the signature when ut use parser to construct a graph.
54 void set_use_signature_in_resolve(bool use_signature) noexcept;
55 bool UseSignatureInResolve();
56 
57 std::shared_ptr<py::scoped_interpreter> set_python_scoped();
58 void ResetPythonScope();
59 bool IsPythonEnv();
60 void SetPythonPath(const std::string &path);
61 void set_python_env_flag(bool python_env) noexcept;
62 py::object GetPyFn(const std::string &module, const std::string &name);
63 // Call the python function
64 template <class... T>
CallPyFn(const std::string & module,const std::string & name,T...args)65 py::object CallPyFn(const std::string &module, const std::string &name, T... args) {
66   (void)set_python_scoped();
67   if (!module.empty() && !name.empty()) {
68     py::module mod = py::module::import(module.c_str());
69     py::object fn = mod.attr(name.c_str())(args...);
70     return fn;
71   }
72   return py::none();
73 }
74 }  // namespace python_adapter
75 }  // namespace parse
76 }  // namespace mindspore
77 
78 #endif  // MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_PYTHON_ADAPTER_H_
79