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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "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;
  34. using PrimitivePyPtr = std::shared_ptr<PrimitivePy>;
  35. using PrimitivePyWeakPtr = std::weak_ptr<PrimitivePy>;
  36. class PrimitivePyAdapter;
  37. using PrimitivePyAdapterPtr = std::shared_ptr<PrimitivePyAdapter>;
  38. class PrimitivePy : public Primitive {
  39. public:
  40. explicit PrimitivePy(const std::string &name);
  41. PrimitivePy(const py::object &python_obj, const PrimitivePyAdapterPtr &adapter);
  42. ~PrimitivePy() override;
  43. MS_DECLARE_PARENT(PrimitivePy, Primitive);
  44. py::function GetBpropFunction();
  45. void set_signatures(const std::vector<Signature> &signatures);
  46. const std::vector<Signature> &signatures() const { return signatures_; }
  47. void CopyHookFunction(const PrimitivePtr &primitive);
  48. py::dict GetAttrDict();
  49. void set_hook(const py::function &hook) { hook_ = hook; }
  50. py::function hook() const { return hook_; }
  51. BaseRef RunHookFunction(const VectorRef &args) const;
  52. BaseRef RunCellBpropFunction(const py::tuple &py_args) const;
  53. BaseRef RunCellHookFunction(const py::tuple &py_args) const;
  54. BaseRef RunVariableHookFunction(const py::tuple &py_args) const;
  55. BaseRef RunComputeFunction(const VectorRef &args) const override;
  56. py::object RunPyComputeFunction(const py::tuple &py_args) const;
  57. bool HasComputeFunction() const;
  58. const bool parse_info_ = true;
  59. const py::object &GetPyObj() const { return python_obj_; }
  60. py::dict RunInfer(const py::tuple &args);
  61. void RunCheck(const py::tuple &args);
  62. py::object RunInferValue(const py::tuple &args);
  63. bool HasPyObj() { return python_obj_.operator bool(); }
  64. PrimitivePtr Clone() override;
  65. PrimitivePyAdapterPtr adapter() const { return adapter_; }
  66. void set_bprop_cls_name(const std::string &name) { bprop_cls_name_ = name; }
  67. private:
  68. py::function GetComputeFunction() const;
  69. void ConvertCTensorToPyTensor(const py::tuple &input_args, py::tuple *convert_args) const;
  70. void CheckHookConsistency(const py::object &grad_out, const py::object &expected_grad_out) const;
  71. py::object python_obj_;
  72. PrimitivePyAdapterPtr adapter_;
  73. py::function hook_;
  74. std::string bprop_cls_name_;
  75. std::vector<Signature> signatures_;
  76. static std::map<std::string, py::object> hook_grad_;
  77. };
  78. class PrimitivePyAdapter {
  79. public:
  80. explicit PrimitivePyAdapter(const py::str &name);
  81. ~PrimitivePyAdapter() = default;
  82. void AddPyAttr(const py::str &name, const py::object &obj);
  83. void DelPyAttr(const py::str &name);
  84. py::dict GetAttrDict();
  85. void set_prim_type(const PrimType t);
  86. void set_const_prim(bool is_const_prim);
  87. void set_const_input_indexes(const std::vector<size_t> &const_input_indexes);
  88. void set_signatures(const std::vector<Signature> &signatures);
  89. void set_hook(const py::function &hook);
  90. void set_instance_name(const std::string &s);
  91. void set_attached_primitive(const PrimitivePyPtr &prim);
  92. PrimitivePyPtr attached_primitive() { return attached_primitive_.lock(); }
  93. std::string name() const { return name_; }
  94. void set_name(const std::string &name) { name_ = name; }
  95. const bool parse_info_ = true;
  96. private:
  97. friend PrimitivePy;
  98. std::string name_;
  99. PrimitivePyWeakPtr attached_primitive_;
  100. mindspore::HashMap<std::string, ValuePtr> attrs_;
  101. PrimType prim_type_{kPrimTypeBuiltIn};
  102. bool is_const_prim_{false};
  103. std::vector<size_t> const_input_indexes_;
  104. std::vector<Signature> signatures_;
  105. py::function hook_;
  106. std::string instance_name_;
  107. };
  108. } // namespace mindspore
  109. #endif // MINDSPORE_CCSRC_UTILS_PRIMITIVE_PY_H_