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 #include "pipeline/jit/parse/python_adapter.h"
18 #include <memory>
19 #include <string>
20
21 namespace mindspore {
22 namespace parse {
23 namespace python_adapter {
24 // python scoped env, should only have one scoped_ instance
25 static std::shared_ptr<py::scoped_interpreter> scoped_ = nullptr;
26 // true: start process from python, false: start process from c++
27 static bool python_env_ = false;
28 static bool use_signature_in_resolve_ = true;
ResetPythonScope()29 void ResetPythonScope() { scoped_ = nullptr; }
set_use_signature_in_resolve(bool use_signature)30 void set_use_signature_in_resolve(bool use_signature) noexcept { use_signature_in_resolve_ = use_signature; }
UseSignatureInResolve()31 bool UseSignatureInResolve() { return use_signature_in_resolve_; }
set_python_env_flag(bool python_env)32 void set_python_env_flag(bool python_env) noexcept { python_env_ = python_env; }
IsPythonEnv()33 bool IsPythonEnv() { return python_env_; }
SetPythonPath(const std::string & path)34 void SetPythonPath(const std::string &path) {
35 // load the python module path
36 (void)python_adapter::set_python_scoped();
37 py::module sys = py::module::import("sys");
38 py::list sys_path = sys.attr("path");
39
40 // check the path is exist?
41 bool is_exist = false;
42 for (size_t i = 0; i < sys_path.size(); i++) {
43 std::string path_str = py::cast<std::string>(sys_path[i]);
44 if (path_str == path) {
45 is_exist = true;
46 }
47 }
48 if (!is_exist) {
49 (void)sys_path.attr("append")(path.c_str());
50 }
51 }
52
set_python_scoped()53 std::shared_ptr<py::scoped_interpreter> set_python_scoped() {
54 // if start process from python, no need set the python scope.
55 if (!python_env_) {
56 if ((Py_IsInitialized() == 0) && (scoped_ == nullptr)) {
57 scoped_ = std::make_shared<py::scoped_interpreter>();
58 }
59 }
60 return scoped_;
61 }
62
63 // return the module of python
GetPyModule(const std::string & module)64 py::module GetPyModule(const std::string &module) {
65 if (!module.empty()) {
66 return py::module::import(module.c_str());
67 } else {
68 return py::none();
69 }
70 }
71
72 // Get the obj of attr
GetPyObjAttr(const py::object & obj,const std::string & attr)73 py::object GetPyObjAttr(const py::object &obj, const std::string &attr) {
74 if (!attr.empty() && !py::isinstance<py::none>(obj)) {
75 if (py::hasattr(obj, attr.c_str())) {
76 return obj.attr(attr.c_str());
77 }
78 MS_LOG(DEBUG) << "Obj have not the attr: " << attr;
79 }
80 return py::none();
81 }
82
GetPyFn(const std::string & module,const std::string & name)83 py::object GetPyFn(const std::string &module, const std::string &name) {
84 (void)python_adapter::set_python_scoped();
85 if (!module.empty() && !name.empty()) {
86 py::module mod = py::module::import(module.c_str());
87 py::object fn = mod.attr(name.c_str());
88 return fn;
89 }
90 return py::none();
91 }
92
93 } // namespace python_adapter
94 } // namespace parse
95 } // namespace mindspore
96