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

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 "pipeline/parse/python_adapter.h"
  18. #include "utils/log_adapter.h"
  19. #include "common/utils.h"
  20. namespace mindspore {
  21. py::function GetBpropFunctionByObj(py::object obj) {
  22. static const std::string get_bprop_fn = "get_bprop_fn";
  23. static const std::string ad_module = "mindspore.ops._grad";
  24. py::function fn = parse::python_adapter::GetPyFn(ad_module, get_bprop_fn)(obj);
  25. return fn;
  26. }
  27. py::function GetBpropFunction(std::string name) {
  28. auto fn = GetBpropFunctionByObj(py::str(name));
  29. return fn;
  30. }
  31. py::function GetComputeFunction(std::string name) {
  32. static const std::string module = "mindspore._extends.builtin_operations";
  33. py::module mod = py::module::import(common::SafeCStr(module));
  34. if (!py::hasattr(mod, common::SafeCStr(name))) {
  35. PyErr_SetString(PyExc_NotImplementedError, common::SafeCStr(name));
  36. // If raise AttributeError, user can't understand. This case need raise NotImplementedError.
  37. throw(py::error_already_set());
  38. }
  39. py::object fn = mod.attr(common::SafeCStr(name));
  40. return fn;
  41. }
  42. } // namespace mindspore