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.

kernel_fusion.cc 6.3 kB

5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "backend/kernel_compiler/kernel_fusion.h"
  17. #include <map>
  18. #include <set>
  19. #include <string>
  20. #include <memory>
  21. #include "backend/kernel_compiler/tbe/tbe_kernel_build.h"
  22. #include "backend/kernel_compiler/tbe/tbe_kernel_parallel_build.h"
  23. #include "backend/kernel_compiler/tbe/tbe_utils.h"
  24. #include "backend/kernel_compiler/tbe/tbe_convert_utils.h"
  25. #include "utils/ms_context.h"
  26. namespace mindspore::kernel {
  27. using mindspore::kernel::tbe::TbeUtils;
  28. static size_t GenFusionJsonHash(const nlohmann::json &fusion_json) {
  29. // get an copy
  30. nlohmann::json fusion_json_copy = fusion_json;
  31. auto &op_lists = fusion_json_copy["op_list"];
  32. for (auto &op : op_lists) {
  33. op.erase("name");
  34. for (auto &output_desc : op["output_desc"]) {
  35. output_desc.erase("name");
  36. }
  37. if (op["type"] != "Data") {
  38. for (auto &input_desc : op["input_desc"]) {
  39. input_desc.erase("name");
  40. }
  41. for (auto &list_arg : op["prebuild_outs_attrs"]["list_args"]) {
  42. if (list_arg.is_object() && list_arg.find("name") != list_arg.end()) {
  43. list_arg.erase("name");
  44. }
  45. }
  46. }
  47. }
  48. return std::hash<std::string>()(fusion_json_copy.dump());
  49. }
  50. std::map<int64_t, KernelModPtr> KernelFusion(const std::vector<FusionScopeInfo> &fusion_scopes) {
  51. std::map<int64_t, KernelModPtr> kernel_mod_ret;
  52. static std::set<std::string> processed_fusion_kernel = {};
  53. auto build_manger = std::make_shared<ParallelBuildManager>();
  54. MS_EXCEPTION_IF_NULL(build_manger);
  55. auto context_ptr = MsContext::GetInstance();
  56. MS_EXCEPTION_IF_NULL(context_ptr);
  57. auto device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  58. auto tune_mode = context_ptr->get_param<std::string>(MS_CTX_TUNE_MODE);
  59. std::string offline_tune = common::GetEnv("ENABLE_TUNE_DUMP");
  60. if (!offline_tune.empty()) {
  61. for (size_t j = 0; j < offline_tune.length(); j++) {
  62. offline_tune[j] = tolower(offline_tune[j]);
  63. }
  64. if (!(offline_tune == "true" || offline_tune == "false")) {
  65. MS_LOG(EXCEPTION) << "The value of ENABLE_TUNE_DUMP must be 'true' or 'false'";
  66. }
  67. }
  68. for (const auto &fusion_scope_iter : fusion_scopes) {
  69. string fusion_kernel_name;
  70. nlohmann::json fusion_op;
  71. if (!TbeKernelBuild::GenFusionScopeJson(fusion_scope_iter.input_nodes, fusion_scope_iter.compute_nodes, &fusion_op,
  72. &fusion_kernel_name)) {
  73. continue;
  74. }
  75. // gen kernel_name & check cache
  76. size_t hash_id = GenFusionJsonHash(fusion_op);
  77. auto json_name =
  78. fusion_kernel_name.append("_").append(std::to_string(hash_id)).append("_").append(std::to_string(device_id));
  79. fusion_op["fusion_op_name"] = json_name;
  80. fusion_op["full_name"] = fusion_scope_iter.full_name;
  81. // get io size
  82. std::vector<size_t> input_size_list;
  83. std::vector<size_t> output_size_list;
  84. if (!TbeKernelBuild::GetIOSize(fusion_op["op_list"], fusion_scope_iter.output_nodes, &input_size_list,
  85. &output_size_list)) {
  86. continue;
  87. }
  88. // search cache
  89. auto kernel_pack = TbeUtils::SearchCache(json_name);
  90. if (kernel_pack != nullptr && ((!offline_tune.empty() && offline_tune != "true") || tune_mode == "NO_TUNE")) {
  91. auto kernel_mod = build_manger->GenKernelMod(input_size_list, output_size_list, kernel_pack);
  92. if (kernel_mod != nullptr) {
  93. kernel_mod_ret[fusion_scope_iter.scope_id] = kernel_mod;
  94. continue;
  95. }
  96. }
  97. // same op not need build, but need wait build finish to set kernel mode
  98. if (processed_fusion_kernel.find(json_name) != processed_fusion_kernel.end()) {
  99. build_manger->SaveSameFusionOpInfo(fusion_scope_iter.scope_id, json_name, tbe::kProcessorAiCore, input_size_list,
  100. output_size_list);
  101. continue;
  102. }
  103. (void)processed_fusion_kernel.insert(json_name);
  104. // generate soc info json
  105. nlohmann::json soc_info_json;
  106. TbeUtils::GenSocInfo(&soc_info_json);
  107. soc_info_json["autoTilingMode"] = tune_mode;
  108. auto soc_version = TbeKernelJsonCreator::GetSocVersion();
  109. soc_info_json["socVersion"] = soc_version;
  110. // fusion build
  111. nlohmann::json fusion_json;
  112. fusion_json["fusion_op"] = fusion_op;
  113. fusion_json["SocInfo"] = soc_info_json;
  114. auto task_id = ParallelBuildManager::StartCompileOp(fusion_json);
  115. TbeUtils::SaveJsonInfo(json_name, fusion_json.dump());
  116. if (task_id < 0) {
  117. MS_EXCEPTION(ArgumentError) << "start compile failed.";
  118. }
  119. build_manger->SaveTaskInfo(task_id, nullptr, json_name, input_size_list, output_size_list,
  120. fusion_scope_iter.scope_id);
  121. }
  122. int build_failed_num = 0;
  123. while (!build_manger->IsAllTaskFinish()) {
  124. int task_id = -1;
  125. std::string task_result;
  126. std::string build_result;
  127. auto ret = ParallelBuildManager::WaitOne(&task_id, &task_result, &build_result);
  128. if (!ret) {
  129. MS_EXCEPTION(ArgumentError) << "Build Failed. wait one ret:" << ret << ", task id:" << task_id;
  130. }
  131. if (task_result != "Success") {
  132. MS_LOG(INFO) << "Fusion warning: Fuison op build failed, err log: " << task_result
  133. << " change to single op build.";
  134. build_failed_num++;
  135. }
  136. auto kernel_mod_item = build_manger->TaskFinishProcess(task_id, build_result, false);
  137. if (kernel_mod_item.second != nullptr) {
  138. (void)kernel_mod_ret.emplace(kernel_mod_item);
  139. }
  140. }
  141. bool ret = build_manger->GenSameFusionOpKernelMod(&kernel_mod_ret);
  142. if (!ret) {
  143. MS_LOG(INFO) << "Fusion warning: Fuison op has cache failed.";
  144. }
  145. MS_LOG(INFO) << "Build Fusion Kernel Failed Num: " << build_failed_num;
  146. return kernel_mod_ret;
  147. }
  148. } // namespace mindspore::kernel