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.4 kB

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