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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_FRONTEND_OPTIMIZER_PASS_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_PASS_H_
  18. #include <string>
  19. #include <memory>
  20. #include <unordered_map>
  21. #include "ir/anf.h"
  22. #include "frontend/optimizer/pattern.h"
  23. #include "pybind_api/api_register.h"
  24. #include "pybind_api/export_flags.h"
  25. namespace mindspore {
  26. namespace opt {
  27. namespace python_pass {
  28. class PythonPass;
  29. using PythonPassPtr = std::shared_ptr<PythonPass>;
  30. using NodeEquiv = std::unordered_map<std::string, AnfNodePtr>;
  31. using NodeEquivPtr = std::shared_ptr<NodeEquiv>;
  32. class PythonPass {
  33. public:
  34. explicit PythonPass(const std::string &name, const PatternPtr &src, const PatternPtr &dst, bool run_only_once = false)
  35. : src_pattern_(src), dst_pattern_(dst), name_(name), run_only_once_(run_only_once) {}
  36. ~PythonPass() = default;
  37. bool Run(const FuncGraphPtr &func_graph, const MatchResultPtr &res);
  38. std::string name() const { return name_; }
  39. AnfNodePtr Run(const FuncGraphPtr &func_graph, const FuncGraphPtr &top_graph, const AnfNodePtr &node,
  40. const MatchResultPtr &res);
  41. PatternPtr src_pattern() { return src_pattern_; }
  42. PatternPtr dst_pattern() { return dst_pattern_; }
  43. private:
  44. PatternPtr src_pattern_;
  45. PatternPtr dst_pattern_;
  46. const std::string name_;
  47. bool run_only_once_;
  48. };
  49. using PythonPassPtr = std::shared_ptr<PythonPass>;
  50. } // namespace python_pass
  51. } // namespace opt
  52. } // namespace mindspore
  53. #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_PASS_H_