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.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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;
  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) override;
  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 override;
  52. BaseRef RunBpropHookFunction(const py::tuple &py_args) const;
  53. BaseRef RunComputeFunction(const VectorRef &args) const override;
  54. py::object RunPyComputeFunction(const py::tuple &py_args) const;
  55. bool HasComputeFunction() const;
  56. const bool parse_info_ = true;
  57. const py::object &GetPyObj() const { return python_obj_; }
  58. py::dict RunInfer(const py::tuple &args);
  59. void RunCheck(const py::tuple &args);
  60. py::object RunInferValue(const py::tuple &args);
  61. bool ObjHasAttr(const char *attr_name) { return py::hasattr(python_obj_, attr_name); }
  62. bool HasPyObj() { return python_obj_.operator bool(); }
  63. PrimitivePtr Clone() override;
  64. PrimitivePyAdapterPtr adapter() const { return adapter_; }
  65. bool is_tuple_input_ = false;
  66. private:
  67. py::function GetComputeFunction() const;
  68. void ConvertCTensorToPyTensor(const py::tuple &input_args, py::tuple *convert_args) const;
  69. void CheckHookConsistency(const py::object &grad_out, const py::object &expected_grad_out) const;
  70. py::object python_obj_;
  71. PrimitivePyAdapterPtr adapter_;
  72. py::function hook_;
  73. std::vector<Signature> signatures_;
  74. static std::map<std::string, py::object> hook_grad_;
  75. };
  76. class PrimitivePyAdapter {
  77. public:
  78. explicit PrimitivePyAdapter(const py::str &name);
  79. ~PrimitivePyAdapter() = default;
  80. void AddPyAttr(const py::str &name, const py::object &obj);
  81. void DelPyAttr(const py::str &name);
  82. py::dict GetAttrDict();
  83. void set_prim_type(const PrimType t);
  84. void set_const_prim(bool is_const_prim);
  85. void set_const_input_indexes(const std::vector<size_t> &const_input_indexes);
  86. void set_signatures(const std::vector<Signature> &signatures);
  87. void set_hook(const py::function &hook);
  88. void set_instance_name(const std::string &s);
  89. void set_attached_primitive(const PrimitivePyPtr &prim);
  90. PrimitivePyPtr attached_primitive() { return attached_primitive_.lock(); }
  91. void set_name(const std::string &name) { name_ = name; }
  92. const bool parse_info_ = true;
  93. private:
  94. friend PrimitivePy;
  95. std::string name_;
  96. PrimitivePyWeakPtr attached_primitive_;
  97. std::unordered_map<std::string, ValuePtr> attrs_;
  98. PrimType prim_type_{kPrimTypeBuiltIn};
  99. bool is_const_prim_{false};
  100. std::vector<size_t> const_input_indexes_;
  101. std::vector<Signature> signatures_;
  102. py::function hook_;
  103. std::string instance_name_;
  104. };
  105. } // namespace mindspore
  106. #endif // MINDSPORE_CCSRC_UTILS_PRIMITIVE_PY_H_