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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. py::function fn = parse::python_adapter::GetPyFn(ad_module, get_bprop_fn)(obj);
  28. return fn;
  29. }
  30. py::function GetBpropFunction(std::string name) {
  31. auto fn = GetBpropFunctionByObj(py::str(name));
  32. return fn;
  33. }
  34. py::function GetComputeFunction(std::string name) {
  35. static const std::string module = "mindspore._extends.builtin_operations";
  36. py::module mod = py::module::import(common::SafeCStr(module));
  37. if (!py::hasattr(mod, common::SafeCStr(name))) {
  38. PyErr_SetString(PyExc_NotImplementedError, common::SafeCStr(name));
  39. // If raise AttributeError, user can't understand. This case need raise NotImplementedError.
  40. throw(py::error_already_set());
  41. }
  42. py::object fn = mod.attr(common::SafeCStr(name));
  43. return fn;
  44. }
  45. py::tuple ConvertDatatoPyTuple(const VectorRef &args) {
  46. auto py_args = py::tuple(args.size());
  47. size_t i = 0;
  48. for (auto &arg : args) {
  49. py_args[i] = BaseRefToPyData(arg);
  50. MS_LOG(DEBUG) << "arg:" << i << ":" << arg.ToString();
  51. i++;
  52. }
  53. return py_args;
  54. }
  55. BaseRef RunComputeFunction(const PrimitivePtr &prim, const VectorRef &args) {
  56. auto func = GetComputeFunction(prim->name());
  57. if (py::isinstance<py::none>(func)) {
  58. MS_LOG(EXCEPTION) << prim->name() << " 's compute function run failed, please check whether it is not implemented";
  59. }
  60. auto py_args = ConvertDatatoPyTuple(args);
  61. py::object obj = func(*py_args);
  62. return std::make_shared<PyObjectRef>(obj);
  63. }
  64. } // namespace mindspore