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

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