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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <string>
  19. #include <memory>
  20. #include "backend/kernel_compiler/tbe/tbe_kernel_build.h"
  21. #include "backend/kernel_compiler/tbe/tbe_kernel_parallel_build.h"
  22. #include "backend/kernel_compiler/tbe/tbe_utils.h"
  23. #include "backend/kernel_compiler/tbe/tbe_convert_utils.h"
  24. #include "utils/ms_context.h"
  25. namespace mindspore::kernel {
  26. using mindspore::kernel::tbe::TbeUtils;
  27. static size_t GenFusionJsonHash(const nlohmann::json &fusion_json) {
  28. // get an copy
  29. nlohmann::json fusion_json_copy = fusion_json;
  30. auto &op_lists = fusion_json_copy["op_list"];
  31. for (auto &op : op_lists) {
  32. op.erase("name");
  33. for (auto &output_desc : op["output_desc"]) {
  34. output_desc.erase("name");
  35. }
  36. if (op["type"] != "Data") {
  37. for (auto &input_desc : op["input_desc"]) {
  38. input_desc.erase("name");
  39. }
  40. for (auto &list_arg : op["prebuild_outs_attrs"]["list_args"]) {
  41. if (list_arg.is_object() && list_arg.find("name") != list_arg.end()) {
  42. list_arg.erase("name");
  43. }
  44. }
  45. }
  46. }
  47. return std::hash<std::string>()(fusion_json_copy.dump());
  48. }
  49. std::map<int64_t, KernelModPtr> KernelFusion(const std::vector<FusionScopeInfo> &fusion_scopes) {
  50. MS_LOG(INFO) << "kernel fusion build start, scope size:" << fusion_scopes.size();
  51. std::map<int64_t, KernelModPtr> kernel_mod_ret;
  52. auto build_manger = std::make_shared<ParallelBuildManager>();
  53. MS_EXCEPTION_IF_NULL(build_manger);
  54. for (const auto &fusion_scope_iter : fusion_scopes) {
  55. string fusion_kernel_name;
  56. nlohmann::json fusion_op;
  57. if (!TbeKernelBuild::GenFusionScopeJson(fusion_scope_iter.input_nodes, fusion_scope_iter.compute_nodes, &fusion_op,
  58. &fusion_kernel_name)) {
  59. continue;
  60. }
  61. // gen kernel_name & check cache
  62. size_t hash_id = GenFusionJsonHash(fusion_op);
  63. MS_LOG(INFO) << "Fusion op hash id: " << hash_id;
  64. auto context_ptr = MsContext::GetInstance();
  65. MS_EXCEPTION_IF_NULL(context_ptr);
  66. auto device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  67. auto json_name =
  68. fusion_kernel_name.append("_").append(std::to_string(hash_id)).append("_").append(std::to_string(device_id));
  69. fusion_op["fusion_op_name"] = json_name;
  70. // get io size
  71. std::vector<size_t> input_size_list;
  72. std::vector<size_t> output_size_list;
  73. if (!TbeKernelBuild::GetIOSize(fusion_op["op_list"], fusion_scope_iter.output_nodes, &input_size_list,
  74. &output_size_list)) {
  75. continue;
  76. }
  77. // search cache
  78. auto kernel_pack = TbeUtils::SearchCache(json_name, tbe::kProcessorAiCore);
  79. if (kernel_pack != nullptr) {
  80. MS_LOG(INFO) << "Use cached kernel, kernel json name: " << json_name;
  81. auto kernel_mod =
  82. build_manger->GenKernelMod(json_name, tbe::kProcessorAiCore, input_size_list, output_size_list, kernel_pack);
  83. if (kernel_mod != nullptr) {
  84. kernel_mod_ret[fusion_scope_iter.scope_id] = kernel_mod;
  85. continue;
  86. }
  87. }
  88. // fusion build
  89. nlohmann::json fusion_json;
  90. fusion_json["fusion_op"] = fusion_op;
  91. auto task_id = build_manger->StartCompileOp(fusion_json);
  92. TbeUtils::SaveJsonInfo(json_name, fusion_json.dump());
  93. if (task_id < 0) {
  94. MS_EXCEPTION(ArgumentError) << "start compile failed.";
  95. }
  96. build_manger->SaveTaskInfo(task_id, nullptr, json_name, input_size_list, output_size_list,
  97. fusion_scope_iter.scope_id);
  98. }
  99. int build_failed_num = 0;
  100. while (!build_manger->IsAllTaskFinish()) {
  101. int task_id = -1;
  102. std::string task_result;
  103. std::string build_result;
  104. auto ret = build_manger->WaitOne(&task_id, &task_result, &build_result);
  105. if (!ret) {
  106. MS_EXCEPTION(ArgumentError) << "Build Failed. wait one ret:" << ret << ", task id:" << task_id;
  107. }
  108. if (task_result != "Success") {
  109. MS_LOG(INFO) << "Fusion warning: Fuison op build failed, err log: " << task_result
  110. << " change to single op build.";
  111. build_failed_num++;
  112. }
  113. auto kernel_mod_item = build_manger->TaskFinishProcess(task_id, build_result, false);
  114. if (kernel_mod_item.second != nullptr) {
  115. (void)kernel_mod_ret.emplace(kernel_mod_item);
  116. }
  117. }
  118. MS_LOG(INFO) << "Build Fusion Kernel Failed Num: " << build_failed_num;
  119. return kernel_mod_ret;
  120. }
  121. } // namespace mindspore::kernel