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.4 kB

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