You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

python_adapter.cc 3.1 kB

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