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

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

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