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

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