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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <utility>
  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. namespace mindspore {
  26. namespace kernel {
  27. using mindspore::kernel::tbe::TbeUtils;
  28. static bool GenPreBuildKernelJson(const std::vector<AnfNodePtr> &compute_nodes,
  29. std::vector<nlohmann::json> *prebuild_op_list) {
  30. MS_EXCEPTION_IF_NULL(prebuild_op_list);
  31. TbeKernelJsonCreator creator(PREBUILD);
  32. for (const auto &anf_node : compute_nodes) {
  33. nlohmann::json prebuild;
  34. if (!creator.GenTbeSingleKernelJson(anf_node, &prebuild)) {
  35. MS_LOG(ERROR) << "GenTbeSingleKernelJson failed";
  36. return false;
  37. }
  38. (*prebuild_op_list).push_back(prebuild);
  39. }
  40. return true;
  41. }
  42. std::map<int32_t, KernelModPtr> KernelFusion(const std::vector<FusionScopeInfo> &fusion_scopes) {
  43. MS_LOG(INFO) << "kernel fusion build start, scope size:" << fusion_scopes.size();
  44. std::map<int32_t, KernelModPtr> kernel_mod_ret;
  45. auto build_manger = std::make_shared<ParallelBuildManager>();
  46. MS_EXCEPTION_IF_NULL(build_manger);
  47. for (const auto &fusion_scope_iter : fusion_scopes) {
  48. auto scope_id = fusion_scope_iter.scope_id;
  49. nlohmann::json fusion_op;
  50. string fusion_kernel = "te_fusion";
  51. if (!TbeKernelBuild::GenFusionScopeJson(fusion_scope_iter.input_nodes, fusion_scope_iter.compute_nodes, &fusion_op,
  52. &fusion_kernel)) {
  53. continue;
  54. }
  55. // gen kernel_name & check cache
  56. std::string json_str = fusion_op.dump();
  57. size_t hash_id = std::hash<std::string>()(json_str);
  58. auto json_name = fusion_kernel.append("_").append(std::to_string(hash_id));
  59. fusion_op["fusion_op_name"] = json_name;
  60. // gen json for prebuild
  61. std::vector<nlohmann::json> prebuild_op_list;
  62. if (!GenPreBuildKernelJson(fusion_scope_iter.compute_nodes, &prebuild_op_list)) {
  63. continue;
  64. }
  65. // get io size
  66. std::vector<size_t> input_size_list;
  67. std::vector<size_t> output_size_list;
  68. if (!TbeKernelBuild::GetIOSize(fusion_op["op_list"], fusion_scope_iter.output_nodes, &input_size_list,
  69. &output_size_list)) {
  70. continue;
  71. }
  72. // search cache
  73. auto kernel_pack = TbeUtils::SearchCache(json_name, tbe::kProcessorAiCore);
  74. if (kernel_pack != nullptr) {
  75. MS_LOG(INFO) << "Use cached kernel, kernel json name: " << json_name;
  76. auto kernel_mod =
  77. build_manger->GenKernelMod(json_name, tbe::kProcessorAiCore, input_size_list, output_size_list, kernel_pack);
  78. if (kernel_mod != nullptr) {
  79. kernel_mod_ret[scope_id] = kernel_mod;
  80. continue;
  81. }
  82. }
  83. // fusion build
  84. nlohmann::json fusion_json;
  85. fusion_json["fusion_op"] = fusion_op;
  86. fusion_json["prebuild_ops"] = prebuild_op_list;
  87. auto task_id = build_manger->StartCompileOp(fusion_json);
  88. TbeUtils::SaveJsonInfo(json_name, fusion_json.dump());
  89. if (task_id < 0) {
  90. MS_EXCEPTION(ArgumentError) << "start compile failed.";
  91. }
  92. build_manger->SaveTaskInfo(task_id, nullptr, json_name, input_size_list, output_size_list, scope_id);
  93. }
  94. int build_failed_num = 0;
  95. while (!build_manger->IsAllTaskFinish()) {
  96. int task_id = -1;
  97. std::string task_result;
  98. std::string pre_build_result;
  99. auto ret = build_manger->WaitOne(&task_id, &task_result, &pre_build_result);
  100. if (!ret) {
  101. MS_EXCEPTION(ArgumentError) << "Build Failed. wait one ret:" << ret << ", task id:" << task_id;
  102. }
  103. if (task_result != "Success") {
  104. MS_LOG(INFO) << "Fusion warning: Fuison op build failed, err log: " << task_result
  105. << " change to single op build.";
  106. build_failed_num++;
  107. }
  108. auto kernel_mod_item = build_manger->TaskFinishProcess(task_id, false);
  109. if (kernel_mod_item.second != nullptr) {
  110. (void)kernel_mod_ret.emplace(kernel_mod_item);
  111. }
  112. }
  113. MS_LOG(INFO) << "Build Fusion Kernel Failed Num: " << build_failed_num;
  114. return kernel_mod_ret;
  115. }
  116. } // namespace kernel
  117. } // namespace mindspore