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_py.h 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Copyright 2019 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. #ifndef MINDSPORE_CCSRC_UTILS_PRIMITIVE_PY_H_
  17. #define MINDSPORE_CCSRC_UTILS_PRIMITIVE_PY_H_
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <tuple>
  22. #include <unordered_map>
  23. #include <vector>
  24. #include "abstract/abstract_value.h"
  25. #include "frontend/parallel/ops_info/operator_info.h"
  26. #include "ir/primitive.h"
  27. #include "ir/signature.h"
  28. #include "pybind11/pybind11.h"
  29. #include "utils/log_adapter.h"
  30. #include "utils/misc.h"
  31. namespace py = pybind11;
  32. namespace mindspore {
  33. class PrimitivePy : public Primitive {
  34. public:
  35. PrimitivePy(const py::str &name, const py::object &python_obj)
  36. : Primitive(name, false), python_obj_(python_obj), signatures_() {}
  37. ~PrimitivePy() override = default;
  38. MS_DECLARE_PARENT(PrimitivePy, Primitive);
  39. py::function GetBpropFunction();
  40. void set_signatures(const std::vector<Signature> &signatures);
  41. const std::vector<Signature> &signatures() const { return signatures_; }
  42. void CopyHookFunction(const PrimitivePtr &primitive) override;
  43. void AddPyAttr(const py::str &name, const py::object &obj);
  44. py::dict GetAttrDict();
  45. void set_hook(const py::function &hook) { hook_ = hook; }
  46. py::function hook() const { return hook_; }
  47. BaseRef RunHookFunction(const VectorRef &args) const override;
  48. BaseRef RunComputeFunction(const VectorRef &args) const override;
  49. py::object RunPyComputeFunction(const py::tuple &py_args) const;
  50. bool HasComputeFunction() const;
  51. const bool parse_info_ = true;
  52. const py::object &GetPyObj() const { return python_obj_; }
  53. py::dict RunInfer(const py::tuple &args);
  54. void RunCheck(const py::tuple &args);
  55. py::object RunInferValue(const py::tuple &args);
  56. bool ObjHasAttr(const char *attr_name) { return py::hasattr(python_obj_, attr_name); }
  57. bool HasPyObj() { return python_obj_.operator bool(); }
  58. PrimitivePtr Clone() override;
  59. bool is_tuple_input_ = false;
  60. private:
  61. py::function GetComputeFunction() const;
  62. void CheckHookConsistency(const py::object &grad_out, const py::object &expected_grad_out) const;
  63. py::object python_obj_;
  64. py::function hook_;
  65. std::vector<Signature> signatures_;
  66. static std::map<std::string, py::object> hook_grad_;
  67. };
  68. using PrimitivePyPtr = std::shared_ptr<PrimitivePy>;
  69. } // namespace mindspore
  70. #endif // MINDSPORE_CCSRC_UTILS_PRIMITIVE_PY_H_