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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 <unordered_map>
  19. #include <vector>
  20. #include <memory>
  21. #include <string>
  22. #include <tuple>
  23. #include <map>
  24. #include "abstract/abstract_value.h"
  25. #include "utils/misc.h"
  26. #include "pybind11/pybind11.h"
  27. #include "utils/log_adapter.h"
  28. #include "ir/primitive.h"
  29. #include "ir/signature.h"
  30. #include "frontend/parallel/ops_info/operator_info.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(
  41. std::vector<std::tuple<std::string, SignatureEnumRW, SignatureEnumKind, py::object, SignatureEnumDType>>
  42. signatures);
  43. const std::vector<Signature> &signatures() const { return signatures_; }
  44. void CopyHookFunction(const PrimitivePtr &primitive) override;
  45. void AddPyAttr(const py::str &name, const py::object &obj);
  46. py::dict GetAttrDict();
  47. void set_hook(const py::function &hook) { hook_ = hook; }
  48. py::function hook() const { return hook_; }
  49. BaseRef RunHookFunction(const VectorRef &args) const override;
  50. BaseRef RunComputeFunction(const VectorRef &args) const override;
  51. py::object RunPyComputeFunction(const py::tuple &py_args) const;
  52. bool HasComputeFunction() const;
  53. const bool parse_info_ = true;
  54. const py::object &GetPyObj() const { return python_obj_; }
  55. py::dict RunInfer(const py::tuple &args);
  56. bool ObjHasAttr(const char *attr_name) { return py::hasattr(python_obj_, attr_name); }
  57. bool HasPyObj() { return python_obj_ != nullptr; }
  58. PrimitivePtr Clone() override;
  59. bool is_tuple_input_ = false;
  60. private:
  61. py::function GetComputeFunction() const;
  62. py::object python_obj_;
  63. py::function hook_;
  64. std::vector<Signature> signatures_;
  65. static std::map<std::string, py::object> hook_grad_;
  66. };
  67. using PrimitivePyPtr = std::shared_ptr<PrimitivePy>;
  68. } // namespace mindspore
  69. #endif // MINDSPORE_CCSRC_UTILS_PRIMITIVE_PY_H_