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.

ge_profiling.cc 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * Copyright 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 "common/profiling/ge_profiling.h"
  17. #include "runtime/base.h"
  18. #include "common/profiling/profiling_manager.h"
  19. #include "framework/common/debug/ge_log.h"
  20. #include "framework/common/debug/log.h"
  21. #include "graph/load/graph_loader.h"
  22. #include "init/gelib.h"
  23. #include "framework/common/ge_inner_error_codes.h"
  24. #include "opskernel_manager/ops_kernel_builder_manager.h"
  25. namespace {
  26. const uint32_t kDeviceListIndex = 3;
  27. const std::string kDeviceNums = "devNums";
  28. const std::string kDeviceIdList = "devIdList";
  29. const std::string kProfilingInit = "prof_init";
  30. const std::string kProfilingFinalize = "prof_finalize";
  31. const std::string kProfilingStart = "prof_start";
  32. const std::string kProfilingStop = "prof_stop";
  33. const std::string kProfModelSubscribe = "prof_model_subscribe";
  34. const std::string kProfModelUnsubscribe = "prof_model_cancel_subscribe";
  35. const std::string kRtSetDeviceRegName = "profiling";
  36. const std::map<ProfCommandHandleType, std::string> kProfCommandTypeMap = {
  37. {kProfCommandhandleInit, kProfilingInit},
  38. {kProfCommandhandleStart, kProfilingStart},
  39. {kProfCommandhandleStop, kProfilingStop},
  40. {kProfCommandhandleFinalize, kProfilingFinalize},
  41. {kProfCommandhandleModelSubscribe, kProfModelSubscribe},
  42. {kProfCommandhandleModelUnsubscribe, kProfModelUnsubscribe}};
  43. } // namespace
  44. bool TransProfConfigToParam(const ProfCommandHandleData &profCommand, vector<string> &prof_config_params) {
  45. prof_config_params.clear();
  46. prof_config_params.emplace_back(kDeviceNums);
  47. prof_config_params.emplace_back(std::to_string(profCommand.devNums));
  48. prof_config_params.emplace_back(kDeviceIdList);
  49. std::string devID = "";
  50. if (profCommand.devNums == 0) {
  51. GELOGW("The device num is invalid.");
  52. return false;
  53. }
  54. for (uint32_t i = 0; i < profCommand.devNums; i++) {
  55. devID.append(std::to_string(profCommand.devIdList[i]));
  56. if (i != profCommand.devNums - 1) {
  57. devID.append(",");
  58. }
  59. }
  60. prof_config_params.push_back(devID);
  61. return true;
  62. }
  63. bool isProfConfigValid(const uint32_t *deviceid_list, uint32_t device_nums) {
  64. if (deviceid_list == nullptr) {
  65. GELOGE(ge::PARAM_INVALID, "deviceIdList is nullptr");
  66. return false;
  67. }
  68. if (device_nums == 0 || device_nums > MAX_DEV_NUM) {
  69. GELOGE(ge::PARAM_INVALID, "The device nums: %u is invalid.", device_nums);
  70. return false;
  71. }
  72. // real device num
  73. int32_t dev_count = 0;
  74. rtError_t rt_err = rtGetDeviceCount(&dev_count);
  75. if (rt_err != RT_ERROR_NONE) {
  76. GELOGE(ge::INTERNAL_ERROR, "Get the Device count fail.");
  77. return false;
  78. }
  79. if (device_nums > static_cast<uint32_t>(dev_count)) {
  80. GELOGE(ge::PARAM_INVALID, "Device num(%u) is not in range 1 ~ %d.", device_nums, dev_count);
  81. return false;
  82. }
  83. std::unordered_set<uint32_t> record;
  84. for (size_t i = 0; i < device_nums; ++i) {
  85. uint32_t dev_id = deviceid_list[i];
  86. if (dev_id >= static_cast<uint32_t>(dev_count)) {
  87. GELOGE(ge::PARAM_INVALID, "Device id %u is not in range 0 ~ %d(exclude %d)", dev_id, dev_count, dev_count);
  88. return false;
  89. }
  90. if (record.count(dev_id) > 0) {
  91. GELOGE(ge::PARAM_INVALID, "Device id %u is duplicatedly set", dev_id);
  92. return false;
  93. }
  94. record.insert(dev_id);
  95. }
  96. return true;
  97. }
  98. ge::Status RegProfCtrlCallback(MsprofCtrlCallback func) {
  99. if (func == nullptr) {
  100. GELOGE(ge::PARAM_INVALID, "Msprof ctrl callback is nullptr.");
  101. return ge::PARAM_INVALID;
  102. }
  103. if (ge::ProfilingManager::Instance().GetMsprofCallback().msprofCtrlCallback != nullptr) {
  104. GELOGW("Msprof ctrl callback is exist, just ignore it.");
  105. } else {
  106. GELOGI("GE register Msprof ctrl callback.");
  107. ge::ProfilingManager::Instance().SetMsprofCtrlCallback(func);
  108. }
  109. return ge::SUCCESS;
  110. }
  111. ge::Status RegProfSetDeviceCallback(MsprofSetDeviceCallback func) {
  112. if (func == nullptr) {
  113. GELOGE(ge::PARAM_INVALID, "MsprofSetDeviceCallback callback is nullptr.");
  114. return ge::PARAM_INVALID;
  115. }
  116. // Pass MsprofSetDeviceCallback to runtime
  117. GELOGI("GE pass setdevice callback to runtime.");
  118. ge::Status rt_ret = rtRegDeviceStateCallback(kRtSetDeviceRegName.c_str(), static_cast<rtDeviceStateCallback>(func));
  119. if (rt_ret != ge::SUCCESS) {
  120. GELOGE(rt_ret, "Pass MsprofSetDeviceCallback to runtime failed!");
  121. return rt_ret;
  122. }
  123. return ge::SUCCESS;
  124. }
  125. ge::Status RegProfReporterCallback(MsprofReporterCallback func) {
  126. if (func == nullptr) {
  127. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  128. return ge::PARAM_INVALID;
  129. }
  130. if (ge::ProfilingManager::Instance().GetMsprofCallback().msprofReporterCallback != nullptr) {
  131. GELOGW("Msprof reporter callback is exist, just ignore it.");
  132. } else {
  133. GELOGI("GE register Msprof reporter callback.");
  134. ge::ProfilingManager::Instance().SetMsprofReporterCallback(func);
  135. // Pass MsprofReporterCallback to runtime
  136. ge::Status rt_ret = rtSetMsprofReporterCallback(func);
  137. if (rt_ret != ge::SUCCESS) {
  138. GELOGE(rt_ret, "Pass MsprofReporterCallback to runtime failed!!");
  139. return rt_ret;
  140. }
  141. // Pass MsprofReporterCallback to hccl in opskernel so initialize
  142. rt_ret = OpsKernelBuilderManager::Instance().RegProfReporterCallBack(func);
  143. if (rt_ret != ge::SUCCESS) {
  144. GELOGE(rt_ret, "Pass MsprofReporterCallback to hccl failed.");
  145. return rt_ret;
  146. }
  147. }
  148. return ge::SUCCESS;
  149. }
  150. ge::Status ProfCommandHandle(ProfCommandHandleType type, void *data, uint32_t len) {
  151. if (type != kProfCommandhandleFinalize) {
  152. GE_CHECK_NOTNULL(data);
  153. }
  154. ProfCommandHandleData *prof_config_param = (ProfCommandHandleData *)data;
  155. auto iter = kProfCommandTypeMap.find(type);
  156. if (iter == kProfCommandTypeMap.end()) {
  157. GELOGW("The prof comand type is invalid.");
  158. return ge::PARAM_INVALID;
  159. }
  160. std::vector<string> prof_params;
  161. if (type == kProfCommandhandleStart || type == kProfCommandhandleStop) {
  162. if (!isProfConfigValid(prof_config_param->devIdList, prof_config_param->devNums)) {
  163. return ge::FAILED;
  164. }
  165. if (!TransProfConfigToParam(*prof_config_param, prof_params)) {
  166. GELOGE(ge::PARAM_INVALID, "Transfer profilerConfig to string vector failed");
  167. return ge::PARAM_INVALID;
  168. }
  169. }
  170. ge::GraphLoader graph_loader;
  171. ge::Command command;
  172. command.cmd_params.clear();
  173. command.cmd_type = iter->second;
  174. command.cmd_params = prof_params;
  175. if (type != kProfCommandhandleFinalize) {
  176. command.module_index = prof_config_param->profSwitch;
  177. }
  178. GELOGI("GE commandhandle execute, Command Type: %d, data type config: 0x%llx", type, command.module_index);
  179. if (type == kProfCommandhandleStart || type == kProfCommandhandleStop) {
  180. GELOGI("Profiling device nums:%s , deviceID:[%s]", prof_params[0].c_str(), prof_params[kDeviceListIndex].c_str());
  181. }
  182. ge::Status ret = graph_loader.CommandHandle(command);
  183. if (ret != ge::SUCCESS) {
  184. GELOGE(ret, "Handle profiling command failed");
  185. return ge::FAILED;
  186. }
  187. GELOGI("Successfully execute profiling command type: %d, command 0x%llx.", type, command.module_index);
  188. return ge::SUCCESS;
  189. }

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示