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.

py_pass.h 1.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * Copyright 2020 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_OPTIMIZER_PASS_H_
  17. #define MINDSPORE_CCSRC_OPTIMIZER_PASS_H_
  18. #include <string>
  19. #include <memory>
  20. #include <unordered_map>
  21. #include "ir/anf.h"
  22. #include "pybind_api/api_register.h"
  23. #include "pybind_api/export_flags.h"
  24. namespace mindspore {
  25. namespace opt {
  26. namespace python_pass {
  27. class PythonPass;
  28. using PythonPassPtr = std::shared_ptr<PythonPass>;
  29. using NodeEquiv = std::unordered_map<std::string, AnfNodePtr>;
  30. using NodeEquivPtr = std::shared_ptr<NodeEquiv>;
  31. class PythonPass {
  32. public:
  33. explicit PythonPass(const std::string &name, const py::function &src, const py::function &dst,
  34. bool run_only_once = false, bool multigraph = true);
  35. ~PythonPass() = default;
  36. bool Run(const FuncGraphPtr &func_graph);
  37. std::string name() const { return name_; }
  38. AnfNodePtr Run(const FuncGraphPtr &func_graph, const AnfNodePtr &node);
  39. private:
  40. void Build(const py::function &src, const py::function &dst);
  41. AnfNodePtr src_node_ = nullptr;
  42. AnfNodePtr dst_node_ = nullptr;
  43. const std::string name_;
  44. bool run_only_once_;
  45. bool multigraph_ = true;
  46. };
  47. using PythonPassPtr = std::shared_ptr<PythonPass>;
  48. } // namespace python_pass
  49. } // namespace opt
  50. } // namespace mindspore
  51. #endif // MINDSPORE_CCSRC_OPTIMIZER_PASS_H_