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_manager.cc 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Copyright 2019-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 "backend/optimizer/common/pass_manager.h"
  17. #include <sys/time.h>
  18. #include <deque>
  19. #include <string>
  20. #include "ir/anf.h"
  21. #include "ir/func_graph.h"
  22. #include "ir/manager.h"
  23. #include "utils/ms_context.h"
  24. #include "debug/anf_ir_dump.h"
  25. namespace mindspore {
  26. namespace opt {
  27. const std::vector<PassPtr> &PassManager::Passes() const { return passes_; }
  28. void PassManager::AddPass(const PassPtr &pass) {
  29. if (pass != nullptr) {
  30. passes_.push_back(pass);
  31. }
  32. }
  33. bool PassManager::Run(const FuncGraphPtr &func_graph, const std::vector<PassPtr> &passes) const {
  34. if (func_graph == nullptr) {
  35. return false;
  36. }
  37. auto context_ptr = MsContext::GetInstance();
  38. MS_EXCEPTION_IF_NULL(context_ptr);
  39. bool save_graphs = context_ptr->get_param<bool>(MS_CTX_SAVE_GRAPHS_FLAG);
  40. bool changed = false;
  41. size_t num = 0;
  42. for (const auto &pass : passes) {
  43. if (pass != nullptr) {
  44. #if defined(_WIN32) || defined(_WIN64)
  45. auto start_time = std::chrono::steady_clock::now();
  46. #else
  47. struct timeval start_time {};
  48. struct timeval end_time {};
  49. (void)gettimeofday(&start_time, nullptr);
  50. #endif
  51. if (pass->Run(func_graph)) {
  52. changed = true;
  53. }
  54. #if defined(_WIN32) || defined(_WIN64)
  55. auto end_time = std::chrono::steady_clock::now();
  56. std::chrono::duration<double, std::ratio<1, 1000000>> cost = end_time - start_time;
  57. MS_LOG(INFO) << "Run pass hwopt_" + name() + "_" << num << "_" + pass->name() + " in " << cost.count() << " us";
  58. #else
  59. (void)gettimeofday(&end_time, nullptr);
  60. const uint64_t kUSecondInSecond = 1000000;
  61. uint64_t cost = kUSecondInSecond * static_cast<uint64_t>(end_time.tv_sec - start_time.tv_sec);
  62. cost += static_cast<uint64_t>(end_time.tv_usec - start_time.tv_usec);
  63. MS_LOG(INFO) << "Run pass hwopt_" + name() + "_" << num << "_" + pass->name() + " in " << cost << " us";
  64. #endif
  65. if (save_graphs) {
  66. std::ostringstream oss;
  67. oss << "verbose_ir_files"
  68. << "/";
  69. oss << "hwopt_" + name() + "_" + std::to_string(num) + "_" + pass->name() + ".ir";
  70. DumpIR(oss.str(), func_graph, true);
  71. }
  72. num++;
  73. }
  74. }
  75. return changed;
  76. }
  77. bool PassManager::Run(const FuncGraphPtr &func_graph) const {
  78. bool changed = false;
  79. // run all passes
  80. bool change = true;
  81. while (change) {
  82. change = Run(func_graph, passes_);
  83. changed = change || changed;
  84. if (run_only_once_) {
  85. break;
  86. }
  87. }
  88. return changed;
  89. }
  90. } // namespace opt
  91. } // namespace mindspore