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.

pass_group.cc 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include "frontend/optimizer/pass_group.h"
  17. #include "frontend/optimizer/py_pass_manager.h"
  18. namespace mindspore {
  19. namespace opt {
  20. namespace python_pass {
  21. void PassGroup::AddPass(const PythonPassPtr &pass) {
  22. if (pass != nullptr) {
  23. passes_.push_back(pass);
  24. }
  25. }
  26. bool PassGroup::DeletePass(const std::string &pass_name) {
  27. for (auto iter = passes_.begin(); iter != passes_.end(); iter++) {
  28. if ((*iter)->name() == pass_name) {
  29. *iter = nullptr;
  30. passes_.erase(iter);
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36. bool PassGroup::Run(const FuncGraphPtr &func_graph, const std::vector<PythonPassPtr> &passes,
  37. const MatchResultPtr &res) const {
  38. if (func_graph == nullptr) {
  39. return false;
  40. }
  41. bool changed = false;
  42. for (const auto &pass : passes) {
  43. if (pass != nullptr) {
  44. if (pass->Run(func_graph, res)) {
  45. changed = true;
  46. }
  47. }
  48. }
  49. return changed;
  50. }
  51. bool PassGroup::Run(const FuncGraphPtr &func_graph) const {
  52. bool changed = false;
  53. // run all passes
  54. bool change = true;
  55. auto res = PyPassManager::GetInstance()->GetMatchResult();
  56. while (change) {
  57. change = Run(func_graph, passes_, res);
  58. changed = change || changed;
  59. if (run_only_once_) {
  60. break;
  61. }
  62. }
  63. return changed;
  64. }
  65. } // namespace python_pass
  66. } // namespace opt
  67. } // namespace mindspore