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.

primitive_utils.cc 3.5 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright 2020 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 "utils/primitive_utils.h"
  17. #include <memory>
  18. #include "pipeline/jit/parse/python_adapter.h"
  19. #include "utils/log_adapter.h"
  20. #include "utils/ms_utils.h"
  21. #include "utils/convert_utils_py.h"
  22. #include "pybind_api/ir/base_ref_py.h"
  23. namespace mindspore {
  24. py::function GetBpropFunctionByObj(py::object obj) {
  25. static const std::string get_bprop_fn = "get_bprop_fn";
  26. static const std::string ad_module = "mindspore.ops._grad";
  27. static const std::string ad_experimental_module = "mindspore.ops._grad_experimental";
  28. py::function fn = parse::python_adapter::GetPyFn(ad_module, get_bprop_fn)(obj);
  29. if (!fn || py::isinstance<py::none>(fn)) {
  30. fn = parse::python_adapter::GetPyFn(ad_experimental_module, get_bprop_fn)(obj);
  31. }
  32. return fn;
  33. }
  34. py::function GetBpropFunction(std::string name) {
  35. auto fn = GetBpropFunctionByObj(py::str(name));
  36. return fn;
  37. }
  38. py::function GetComputeFunction(std::string name) {
  39. static const std::string module = "mindspore._extends.builtin_operations";
  40. py::module mod = py::module::import(common::SafeCStr(module));
  41. if (!py::hasattr(mod, common::SafeCStr(name))) {
  42. PyErr_SetString(PyExc_NotImplementedError, common::SafeCStr(name));
  43. // If raise AttributeError, user can't understand. This case need raise NotImplementedError.
  44. throw(py::error_already_set());
  45. }
  46. py::object fn = mod.attr(common::SafeCStr(name));
  47. return fn;
  48. }
  49. py::tuple ConvertDatatoPyTuple(const VectorRef &args) {
  50. auto py_args = py::tuple(args.size());
  51. size_t i = 0;
  52. for (auto &arg : args) {
  53. py_args[i] = BaseRefToPyData(arg);
  54. MS_LOG(DEBUG) << "arg:" << i << ":" << arg.ToString();
  55. i++;
  56. }
  57. return py_args;
  58. }
  59. py::function GetComputeFunctionWithoutPyObj(const std::string &name) {
  60. static const std::string module = "tests.vm_impl.vm_impl_function";
  61. py::module mod = py::module::import(common::SafeCStr(module));
  62. if (!py::hasattr(mod, common::SafeCStr(name))) {
  63. return py::none();
  64. }
  65. py::object fn = mod.attr(common::SafeCStr(name));
  66. return fn;
  67. }
  68. BaseRef RunComputeFunctionWithoutPyObj(const PrimitivePtr &prim, const VectorRef &args) {
  69. auto func = GetComputeFunctionWithoutPyObj(prim->name());
  70. if (py::isinstance<py::none>(func)) {
  71. return nullptr;
  72. }
  73. auto py_args = ConvertDatatoPyTuple(args);
  74. py::object obj = func(*py_args);
  75. if (py::isinstance<py::none>(obj)) {
  76. return nullptr;
  77. }
  78. return std::make_shared<PyObjectRef>(obj);
  79. }
  80. BaseRef RunComputeFunction(const PrimitivePtr &prim, const VectorRef &args) {
  81. auto func = GetComputeFunction(prim->name());
  82. if (py::isinstance<py::none>(func)) {
  83. MS_LOG(EXCEPTION) << prim->name() << " 's compute function run failed, please check whether it is not implemented";
  84. }
  85. auto py_args = ConvertDatatoPyTuple(args);
  86. py::object obj = func(*py_args);
  87. return std::make_shared<PyObjectRef>(obj);
  88. }
  89. } // namespace mindspore