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

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * Copyright 2019-2021 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 <vector>
  23. #include "utils/hash_map.h"
  24. #include "abstract/abstract_value.h"
  25. #include "ir/primitive.h"
  26. #include "ir/signature.h"
  27. #include "pybind11/pybind11.h"
  28. #include "utils/log_adapter.h"
  29. #include "utils/misc.h"
  30. namespace py = pybind11;
  31. namespace mindspore {
  32. class PrimitivePy;
  33. using PrimitivePyPtr = std::shared_ptr<PrimitivePy>;
  34. using PrimitivePyWeakPtr = std::weak_ptr<PrimitivePy>;
  35. class PrimitivePyAdapter;
  36. using PrimitivePyAdapterPtr = std::shared_ptr<PrimitivePyAdapter>;
  37. class PrimitivePy : public Primitive {
  38. public:
  39. explicit PrimitivePy(const std::string &name);
  40. PrimitivePy(const py::object &python_obj, const PrimitivePyAdapterPtr &adapter);
  41. ~PrimitivePy() override;
  42. MS_DECLARE_PARENT(PrimitivePy, Primitive);
  43. const bool parse_info_ = true;
  44. py::function GetVmapRuleFunction(const bool is_side_effect = false, int axis_size = 0);
  45. py::function GetBpropFunction();
  46. py::function GetTaylorRuleFunction();
  47. void set_signatures(const std::vector<Signature> &signatures);
  48. const std::vector<Signature> &signatures() const { return signatures_; }
  49. const std::map<int, py::function> &backward_hook_fn() const { return backward_hook_fn_; }
  50. void CopyHookFunction(const PrimitivePyPtr &primitive_py);
  51. void AddBpropCutPrim(const PrimitivePyPtr &bprop_cut_prim);
  52. void AddBackwardHookFn(const int &key, const py::function &backward_hook_fn);
  53. void RemoveBackwardHookFn(const int &key);
  54. BaseRef RunHookFunction(const VectorRef &args) const;
  55. BaseRef RunCellBpropFunction(const py::tuple &py_args) const;
  56. BaseRef RunCellHookFunction(const py::tuple &py_args) const;
  57. BaseRef RunVariableHookFunction(const py::tuple &py_args) const;
  58. BaseRef RunComputeFunction(const VectorRef &args) const override;
  59. py::object RunPyComputeFunction(const py::tuple &py_args) const;
  60. bool HasComputeFunction() const;
  61. py::dict GetAttrDict();
  62. const py::object &GetPyObj() const { return python_obj_; }
  63. bool HasPyObj() const { return python_obj_.operator bool(); }
  64. void RunCheck(const py::tuple &args);
  65. py::dict RunInfer(const py::tuple &args);
  66. py::object RunInferValue(const py::tuple &args);
  67. PrimitivePtr Clone() override;
  68. PrimitivePyAdapterPtr adapter() const { return adapter_; }
  69. void set_bprop_cls_name(const std::string &name) { bprop_cls_name_ = name; }
  70. static void ClearHookRes();
  71. private:
  72. py::function GetComputeFunction() const;
  73. void CheckHookConsistency(const py::object &grad_out, const py::object &expected_grad_out, const py::object &code_obj,
  74. const py::object &co_name) const;
  75. py::object python_obj_;
  76. std::string bprop_cls_name_;
  77. PrimitivePyAdapterPtr adapter_;
  78. std::vector<Signature> signatures_;
  79. std::vector<PrimitivePyWeakPtr> bprop_cut_prims_;
  80. std::map<int, py::function> backward_hook_fn_;
  81. static std::map<std::string, py::object> hook_grad_;
  82. };
  83. class PrimitivePyAdapter {
  84. public:
  85. explicit PrimitivePyAdapter(const py::str &name);
  86. ~PrimitivePyAdapter() = default;
  87. void AddPyAttr(const py::str &name, const py::object &obj);
  88. void DelPyAttr(const py::str &name);
  89. py::dict GetAttrDict();
  90. int AddBackwardHookFn(const py::function &backward_hook_fn);
  91. void RemoveBackwardHookFn(int key);
  92. void set_prim_type(const PrimType t);
  93. void set_const_prim(bool is_const_prim);
  94. void set_const_input_indexes(const std::vector<size_t> &const_input_indexes);
  95. void set_signatures(const std::vector<Signature> &signatures);
  96. void set_instance_name(const std::string &s);
  97. void set_attached_primitive(const PrimitivePyPtr &prim);
  98. PrimitivePyPtr attached_primitive() const { return attached_primitive_.lock(); }
  99. std::string name() const { return name_; }
  100. void set_name(const std::string &name) { name_ = name; }
  101. const bool parse_info_ = true;
  102. private:
  103. friend PrimitivePy;
  104. bool is_const_prim_{false};
  105. int backward_hook_fn_key_{-1};
  106. std::string name_;
  107. std::string instance_name_;
  108. PrimType prim_type_{kPrimTypeBuiltIn};
  109. PrimitivePyWeakPtr attached_primitive_;
  110. mindspore::HashMap<std::string, ValuePtr> attrs_;
  111. std::vector<size_t> const_input_indexes_;
  112. std::vector<Signature> signatures_;
  113. std::map<int, py::function> backward_hook_fn_;
  114. };
  115. } // namespace mindspore
  116. #endif // MINDSPORE_CCSRC_UTILS_PRIMITIVE_PY_H_