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

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