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