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.4 kB

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