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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #include "backend/session/anf_runtime_algorithm.h"
  26. namespace mindspore {
  27. namespace opt {
  28. void CacheManager::Update(const AnfNodePtr &node) {
  29. MS_EXCEPTION_IF_NULL(node);
  30. auto type_iter = type_map_.find(node);
  31. auto shape_iter = shape_map_.find(node);
  32. if (type_iter != type_map_.end()) {
  33. (void)type_map_.erase(type_iter);
  34. }
  35. if (shape_iter != shape_map_.end()) {
  36. (void)shape_map_.erase(shape_iter);
  37. }
  38. }
  39. TypeId CacheManager::GetOutputType(const AnfNodePtr &node, size_t index) {
  40. MS_EXCEPTION_IF_NULL(node);
  41. auto iter = type_map_.find(node);
  42. if (iter != type_map_.end()) {
  43. auto types = iter->second;
  44. auto type_iter = types.find(index);
  45. if (type_iter != types.end()) {
  46. return type_iter->second;
  47. }
  48. return kTypeUnknown;
  49. }
  50. auto output_nums = AnfAlgo::GetOutputTensorNum(node);
  51. std::map<size_t, TypeId> index_to_types;
  52. TypeId result = kTypeUnknown;
  53. for (size_t i = 0; i < output_nums; i++) {
  54. auto output_type = AnfAlgo::GetOutputInferDataType(node, i);
  55. (void)index_to_types.emplace(i, output_type);
  56. if (index == i) {
  57. result = output_type;
  58. }
  59. }
  60. (void)type_map_.emplace(node, index_to_types);
  61. return result;
  62. }
  63. std::vector<size_t> CacheManager::GetOutputShape(const AnfNodePtr &node, size_t index) {
  64. MS_EXCEPTION_IF_NULL(node);
  65. auto iter = shape_map_.find(node);
  66. if (iter != shape_map_.end()) {
  67. auto shapes = iter->second;
  68. auto shape_iter = shapes.find(index);
  69. if (shape_iter != shapes.end()) {
  70. return shape_iter->second;
  71. }
  72. return {};
  73. }
  74. auto output_nums = AnfAlgo::GetOutputTensorNum(node);
  75. std::map<size_t, std::vector<size_t>> index_to_shapes;
  76. std::vector<size_t> result = {};
  77. for (size_t i = 0; i < output_nums; i++) {
  78. auto output_shape = AnfAlgo::GetOutputInferShape(node, i);
  79. (void)index_to_shapes.emplace(i, output_shape);
  80. if (index == i) {
  81. result = output_shape;
  82. }
  83. }
  84. (void)shape_map_.emplace(node, index_to_shapes);
  85. return result;
  86. }
  87. const std::vector<PassPtr> &PassManager::Passes() const { return passes_; }
  88. void PassManager::AddPass(const PassPtr &pass) {
  89. if (pass != nullptr) {
  90. passes_.push_back(pass);
  91. }
  92. }
  93. bool PassManager::RunPass(const FuncGraphPtr &func_graph, size_t pass_id, const PassPtr &pass) const {
  94. #if defined(_WIN32) || defined(_WIN64)
  95. auto start_time = std::chrono::steady_clock::now();
  96. #else
  97. struct timeval start_time {};
  98. struct timeval end_time {};
  99. (void)gettimeofday(&start_time, nullptr);
  100. #endif
  101. bool changed = pass->Run(func_graph);
  102. constexpr auto kMicroSendUnit = 1000000;
  103. #if defined(_WIN32) || defined(_WIN64)
  104. auto end_time = std::chrono::steady_clock::now();
  105. std::chrono::duration<double, std::ratio<1, kMicroSendUnit>> cost = end_time - start_time;
  106. MS_LOG(INFO) << "Run pass " + GetPassFullname(pass_id, pass) + " in " << cost.count() << " us";
  107. #else
  108. (void)gettimeofday(&end_time, nullptr);
  109. // time unit: us
  110. uint64_t cost = kMicroSendUnit * static_cast<uint64_t>(end_time.tv_sec - start_time.tv_sec);
  111. cost += static_cast<uint64_t>(end_time.tv_usec - start_time.tv_usec);
  112. MS_LOG(INFO) << "Run pass " + GetPassFullname(pass_id, pass) + " in " << cost << " us";
  113. #endif
  114. return changed;
  115. }
  116. std::string PassManager::GetPassFullname(size_t pass_id, const PassPtr &pass) const {
  117. return std::string("hwopt_") + name() + "_" + std::to_string(pass_id) + "_" + pass->name();
  118. }
  119. void PassManager::DumpPassIR(const FuncGraphPtr &func_graph, const std::string &pass_fullname) const {
  120. #ifdef ENABLE_DUMP_IR
  121. auto context_ptr = MsContext::GetInstance();
  122. MS_EXCEPTION_IF_NULL(context_ptr);
  123. bool save_graphs = context_ptr->get_param<bool>(MS_CTX_SAVE_GRAPHS_FLAG);
  124. static const auto enable_dump = (common::GetEnv("ENV_NO_DUMP_BE_PASS_IR") != "1");
  125. if (save_graphs && enable_dump) {
  126. std::ostringstream oss;
  127. oss << "verbose_ir_files"
  128. << "/";
  129. oss << (pass_fullname + ".ir");
  130. DumpIR(oss.str(), func_graph, true);
  131. }
  132. #endif
  133. }
  134. bool PassManager::Run(const FuncGraphPtr &func_graph, const std::vector<PassPtr> &passes) const {
  135. if (func_graph == nullptr) {
  136. return false;
  137. }
  138. bool changed = false;
  139. size_t num = 0;
  140. for (const auto &pass : passes) {
  141. if (pass != nullptr) {
  142. pass->SetCacheManager(cache_manager_);
  143. changed = RunPass(func_graph, num, pass) || changed;
  144. #ifdef ENABLE_DUMP_IR
  145. DumpPassIR(func_graph, GetPassFullname(num, pass));
  146. #endif
  147. num++;
  148. }
  149. }
  150. return changed;
  151. }
  152. bool PassManager::Run(const FuncGraphPtr &func_graph) const {
  153. bool changed = false;
  154. // run all passes
  155. bool change = true;
  156. while (change) {
  157. change = Run(func_graph, passes_);
  158. changed = change || changed;
  159. if (run_only_once_) {
  160. break;
  161. }
  162. }
  163. return changed;
  164. }
  165. } // namespace opt
  166. } // namespace mindspore