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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. ~PrimitivePy() override;
  37. MS_DECLARE_PARENT(PrimitivePy, Primitive);
  38. py::function GetBpropFunction();
  39. void set_signatures(const std::vector<Signature> &signatures);
  40. const std::vector<Signature> &signatures() const { return signatures_; }
  41. void CopyHookFunction(const PrimitivePtr &primitive) override;
  42. void AddPyAttr(const py::str &name, const py::object &obj);
  43. void DelPyAttr(const py::str &name);
  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. void SetPyObj(const py::object &obj);
  54. py::dict RunInfer(const py::tuple &args);
  55. void RunCheck(const py::tuple &args);
  56. py::object RunInferValue(const py::tuple &args);
  57. bool ObjHasAttr(const char *attr_name) { return py::hasattr(python_obj_, attr_name); }
  58. bool HasPyObj() { return python_obj_.operator bool(); }
  59. PrimitivePtr Clone() override;
  60. bool is_tuple_input_ = false;
  61. private:
  62. py::function GetComputeFunction() const;
  63. void ConvertCTensorToPyTensor(const py::tuple &input_args, py::tuple *convert_args) const;
  64. void CheckHookConsistency(const py::object &grad_out, const py::object &expected_grad_out) const;
  65. py::object python_obj_;
  66. py::function hook_;
  67. std::vector<Signature> signatures_;
  68. static std::map<std::string, py::object> hook_grad_;
  69. };
  70. using PrimitivePyPtr = std::shared_ptr<PrimitivePy>;
  71. } // namespace mindspore
  72. #endif // MINDSPORE_CCSRC_UTILS_PRIMITIVE_PY_H_