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 4.5 kB

4 years ago
4 years ago
4 years ago
6 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "include/common/utils/primitive_utils.h"
  17. #include <memory>
  18. #include "include/common/utils/python_adapter.h"
  19. #include "utils/log_adapter.h"
  20. #include "utils/ms_utils.h"
  21. #include "include/common/utils/convert_utils_py.h"
  22. #include "pybind_api/ir/base_ref_py.h"
  23. namespace mindspore {
  24. py::function GetBpropFunctionByObj(const 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 = python_adapter::GetPyFn(ad_module, get_bprop_fn)(obj);
  29. if (!fn || py::isinstance<py::none>(fn)) {
  30. fn = python_adapter::GetPyFn(ad_experimental_module, get_bprop_fn)(obj);
  31. }
  32. return fn;
  33. }
  34. py::function GetBpropFunction(const std::string &name) {
  35. auto fn = GetBpropFunctionByObj(py::str(name));
  36. return fn;
  37. }
  38. py::function GetTaylorRuleFunctionByObj(const py::object &obj) {
  39. static const std::string get_taylor_fprop_fn = "get_taylor_fprop_fn";
  40. static const std::string ad_module = "mindspore.ops._grad";
  41. py::function fn = python_adapter::GetPyFn(ad_module, get_taylor_fprop_fn)(obj);
  42. return fn;
  43. }
  44. py::function GetTaylorRuleFunction(const std::string &name) {
  45. auto fn = GetTaylorRuleFunctionByObj(py::str(name));
  46. return fn;
  47. }
  48. py::function GetComputeFunction(const std::string &name) {
  49. static const std::string module = "mindspore._extends.builtin_operations";
  50. py::module mod = py::module::import(common::SafeCStr(module));
  51. if (!py::hasattr(mod, common::SafeCStr(name))) {
  52. PyErr_SetString(PyExc_NotImplementedError, common::SafeCStr(name));
  53. // If raise AttributeError, user can't understand. This case need raise NotImplementedError.
  54. throw(py::error_already_set());
  55. }
  56. py::object fn = mod.attr(common::SafeCStr(name));
  57. return fn;
  58. }
  59. py::tuple ConvertDatatoPyTuple(const VectorRef &args) {
  60. auto py_args = py::tuple(args.size());
  61. size_t i = 0;
  62. for (auto &arg : args) {
  63. py_args[i] = BaseRefToPyData(arg);
  64. MS_LOG(DEBUG) << "arg:" << i << ":" << arg.ToString();
  65. i++;
  66. }
  67. return py_args;
  68. }
  69. py::function GetComputeFunctionWithoutPyObj(const std::string &name) {
  70. static const std::string vm_module = "mindspore.ops.vm_impl_registry";
  71. static const std::string get_vm_impl_fn = "get_vm_impl_fn";
  72. py::function get_fn = python_adapter::GetPyFn(vm_module, get_vm_impl_fn);
  73. if (py::isinstance<py::none>(get_fn)) {
  74. MS_LOG(DEBUG) << "Failed to get the function 'get_vm_impl_fn'";
  75. return py::none();
  76. }
  77. py::function vm_fn = get_fn(py::str(name));
  78. return vm_fn;
  79. }
  80. BaseRef RunComputeFunctionWithoutPyObj(const PrimitivePtr &prim, const VectorRef &args) {
  81. auto func = GetComputeFunctionWithoutPyObj(prim->name());
  82. if (py::isinstance<py::none>(func)) {
  83. return nullptr;
  84. }
  85. auto py_args = ConvertDatatoPyTuple(args);
  86. py::object obj = func(*py_args);
  87. if (py::isinstance<py::none>(obj)) {
  88. return nullptr;
  89. }
  90. return std::make_shared<PyObjectRef>(obj);
  91. }
  92. BaseRef RunComputeFunction(const PrimitivePtr &prim, const VectorRef &args) {
  93. auto func = GetComputeFunction(prim->name());
  94. if (py::isinstance<py::none>(func)) {
  95. MS_LOG(EXCEPTION) << prim->name() << " 's compute function run failed, please check whether it is not implemented";
  96. }
  97. auto py_args = ConvertDatatoPyTuple(args);
  98. py::object obj = func(*py_args);
  99. return std::make_shared<PyObjectRef>(obj);
  100. }
  101. py::function GetVmapRuleFunctionByObj(const py::object &obj, int axis_size) {
  102. constexpr char get_vmap_rule_fn[] = "get_vmap_rule";
  103. constexpr char vmap_module[] = "mindspore.ops._vmap";
  104. py::function fn = python_adapter::GetPyFn(vmap_module, get_vmap_rule_fn)(obj, axis_size);
  105. return fn;
  106. }
  107. py::function GetVmapRuleFunction(const std::string &name, int axis_size) {
  108. auto fn = GetVmapRuleFunctionByObj(py::str(name), axis_size);
  109. return fn;
  110. }
  111. } // namespace mindspore