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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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->save_graphs_flag();
  40. auto save_graphs_path = context_ptr->save_graphs_path();
  41. if (save_graphs_path.empty()) {
  42. save_graphs_path = ".";
  43. }
  44. bool changed = false;
  45. size_t num = 0;
  46. for (const auto &pass : passes) {
  47. if (pass != nullptr) {
  48. #if defined(_WIN32) || defined(_WIN64)
  49. auto start_time = std::chrono::steady_clock::now();
  50. #else
  51. struct timeval start_time {};
  52. struct timeval end_time {};
  53. (void)gettimeofday(&start_time, nullptr);
  54. #endif
  55. if (pass->Run(func_graph)) {
  56. changed = true;
  57. }
  58. #if defined(_WIN32) || defined(_WIN64)
  59. auto end_time = std::chrono::steady_clock::now();
  60. std::chrono::duration<double, std::ratio<1, 1000000>> cost = end_time - start_time;
  61. MS_LOG(INFO) << "Run pass hwopt_" + name() + "_" << num << "_" + pass->name() + " in " << cost.count() << " us";
  62. #else
  63. (void)gettimeofday(&end_time, nullptr);
  64. const uint64_t kUSecondInSecond = 1000000;
  65. uint64_t cost = kUSecondInSecond * static_cast<uint64_t>(end_time.tv_sec - start_time.tv_sec);
  66. cost += static_cast<uint64_t>(end_time.tv_usec - start_time.tv_usec);
  67. MS_LOG(INFO) << "Run pass hwopt_" + name() + "_" << num << "_" + pass->name() + " in " << cost << " us";
  68. #endif
  69. if (save_graphs) {
  70. auto dump_file_path =
  71. save_graphs_path + "/" + "hwopt_" + name() + "_" + std::to_string(num) + "_" + pass->name() + ".ir";
  72. DumpIR(dump_file_path, func_graph);
  73. }
  74. num++;
  75. }
  76. }
  77. return changed;
  78. }
  79. bool PassManager::Run(const FuncGraphPtr &func_graph) const {
  80. bool changed = false;
  81. // run all passes
  82. bool change = true;
  83. while (change) {
  84. change = Run(func_graph, passes_);
  85. changed = change || changed;
  86. if (run_only_once_) {
  87. break;
  88. }
  89. }
  90. return changed;
  91. }
  92. } // namespace opt
  93. } // namespace mindspore