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

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "model/ge_model.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. const uint64_t kModelId = ge::INVALID_MODEL_ID;
  44. const uint16_t kStepStart = 0;
  45. const uint16_t kStepEnd = 1;
  46. } // namespace
  47. bool TransProfConfigToParam(const ProfCommandHandleData &profCommand, vector<string> &prof_config_params) {
  48. prof_config_params.clear();
  49. prof_config_params.emplace_back(kDeviceNums);
  50. prof_config_params.emplace_back(std::to_string(profCommand.devNums));
  51. prof_config_params.emplace_back(kDeviceIdList);
  52. std::string devID = "";
  53. if (profCommand.devNums == 0) {
  54. GELOGW("The device num is invalid.");
  55. return false;
  56. }
  57. for (uint32_t i = 0; i < profCommand.devNums; i++) {
  58. devID.append(std::to_string(profCommand.devIdList[i]));
  59. if (i != profCommand.devNums - 1) {
  60. devID.append(",");
  61. }
  62. }
  63. prof_config_params.push_back(devID);
  64. return true;
  65. }
  66. bool isProfConfigValid(const uint32_t *deviceid_list, uint32_t device_nums) {
  67. if (deviceid_list == nullptr) {
  68. GELOGE(ge::PARAM_INVALID, "[Check][DeviceIDList]Invalid, it is nullptr");
  69. REPORT_INNER_ERROR("E19999", "Device id list is nullptr");
  70. return false;
  71. }
  72. if (device_nums == 0 || device_nums > MAX_DEV_NUM) {
  73. GELOGE(ge::PARAM_INVALID, "[Check][DeviceNums]Invalid, device nums: %u", device_nums);
  74. REPORT_INNER_ERROR("E19999", "DeviceNums %u check invalid", device_nums);
  75. return false;
  76. }
  77. // real device num
  78. int32_t dev_count = 0;
  79. rtError_t rt_err = rtGetDeviceCount(&dev_count);
  80. if (rt_err != RT_ERROR_NONE) {
  81. GELOGE(ge::INTERNAL_ERROR, "[Get][DeviceCount]Failed, error_code %d", rt_err);
  82. REPORT_CALL_ERROR("E19999", "Get device count failed, error_code %d", rt_err);
  83. return false;
  84. }
  85. if (device_nums > static_cast<uint32_t>(dev_count)) {
  86. GELOGE(ge::PARAM_INVALID, "[Check][Param]Device num %u is not in range [1,%d]",
  87. device_nums, dev_count);
  88. REPORT_INNER_ERROR("E19999", "Device num %u check invalid, it is not in range [1,%d]",
  89. device_nums, dev_count);
  90. return false;
  91. }
  92. std::set<uint32_t> record;
  93. for (size_t i = 0; i < device_nums; ++i) {
  94. uint32_t dev_id = deviceid_list[i];
  95. if (dev_id >= static_cast<uint32_t>(dev_count)) {
  96. GELOGE(ge::PARAM_INVALID, "[Check][DeviceId]Device id %u is not in range [0,%d)",
  97. dev_id, dev_count);
  98. REPORT_CALL_ERROR("E19999", "Device id %u is not in range [0,%d)", dev_id, dev_count);
  99. return false;
  100. }
  101. if (record.count(dev_id) > 0) {
  102. GELOGE(ge::PARAM_INVALID, "[Check][DeviceId]Device id %u is duplicatedly set", dev_id);
  103. REPORT_CALL_ERROR("E19999", "Device id %u is not unique, duplicatedly set", dev_id);
  104. return false;
  105. }
  106. record.insert(dev_id);
  107. }
  108. return true;
  109. }
  110. ge::Status RegProfCtrlCallback(MsprofCtrlCallback func) {
  111. if (func == nullptr) {
  112. GELOGE(ge::PARAM_INVALID, "[Check][Param]Msprof ctrl callback is nullptr");
  113. REPORT_INNER_ERROR("E19999", "Msprof ctrl callback is nullptr");
  114. return ge::PARAM_INVALID;
  115. }
  116. if (ge::ProfilingManager::Instance().GetMsprofCallback().msprofCtrlCallback != nullptr) {
  117. GELOGW("Msprof ctrl callback is exist, just ignore it.");
  118. } else {
  119. ge::ProfilingManager::Instance().SetMsprofCtrlCallback(func);
  120. }
  121. return ge::SUCCESS;
  122. }
  123. ge::Status RegProfSetDeviceCallback(MsprofSetDeviceCallback func) {
  124. if (func == nullptr) {
  125. GELOGE(ge::PARAM_INVALID, "[Check][Param]MsprofSetDeviceCallback callback is nullptr");
  126. REPORT_INNER_ERROR("E19999", "MsprofSetDeviceCallback callback is nullptr");
  127. return ge::PARAM_INVALID;
  128. }
  129. // Pass MsprofSetDeviceCallback to runtime
  130. ge::Status rt_ret = rtRegDeviceStateCallback(kRtSetDeviceRegName.c_str(), static_cast<rtDeviceStateCallback>(func));
  131. if (rt_ret != ge::SUCCESS) {
  132. GELOGE(rt_ret, "[Pass][MsprofSetDeviceCallback]To runtime failed, ret 0x%X", rt_ret);
  133. REPORT_CALL_ERROR("E19999", "Pass MsprofSetDeviceCallback to runtime failed, ret 0x%X", rt_ret);
  134. return rt_ret;
  135. }
  136. return ge::SUCCESS;
  137. }
  138. ge::Status RegProfReporterCallback(MsprofReporterCallback func) {
  139. if (func == nullptr) {
  140. GELOGE(ge::PARAM_INVALID, "[Check][Param]MsprofReporterCallback callback is nullptr");
  141. REPORT_INNER_ERROR("E19999", "MsprofReporterCallback callback is nullptr");
  142. return ge::PARAM_INVALID;
  143. }
  144. if (ge::ProfilingManager::Instance().GetMsprofCallback().msprofReporterCallback != nullptr) {
  145. GELOGW("Msprof reporter callback is exist, just ignore it.");
  146. } else {
  147. GELOGI("GE register Msprof reporter callback.");
  148. ge::ProfilingManager::Instance().SetMsprofReporterCallback(func);
  149. // Pass MsprofReporterCallback to runtime
  150. ge::Status rt_ret = rtSetMsprofReporterCallback(func);
  151. if (rt_ret != ge::SUCCESS) {
  152. GELOGE(rt_ret, "[Pass][Param]Pass MsprofReporterCallback to runtime failed, error_code %u",
  153. rt_ret);
  154. REPORT_CALL_ERROR("E19999", "Pass MsprofReporterCallback to runtime failed, error_code %u",
  155. rt_ret);
  156. return rt_ret;
  157. }
  158. // Pass MsprofReporterCallback to hccl
  159. }
  160. return ge::SUCCESS;
  161. }
  162. ge::Status ProfCommandHandle(ProfCommandHandleType type, void *data, uint32_t len) {
  163. if (type != kProfCommandhandleFinalize) {
  164. GE_CHECK_NOTNULL(data);
  165. }
  166. ProfCommandHandleData *prof_config_param = reinterpret_cast<ProfCommandHandleData *>(data);
  167. auto iter = kProfCommandTypeMap.find(type);
  168. if (iter == kProfCommandTypeMap.end()) {
  169. GELOGW("The prof comand type is invalid.");
  170. return ge::PARAM_INVALID;
  171. }
  172. std::vector<string> prof_params;
  173. if (type == kProfCommandhandleStart || type == kProfCommandhandleStop) {
  174. if (!isProfConfigValid(prof_config_param->devIdList, prof_config_param->devNums)) {
  175. return ge::FAILED;
  176. }
  177. if (!TransProfConfigToParam(*prof_config_param, prof_params)) {
  178. GELOGE(ge::PARAM_INVALID, "[Check][Param]Transfer profilerConfig to string vector failed");
  179. REPORT_CALL_ERROR("E19999", "Transfer profilerConfig to string vector failed");
  180. return ge::PARAM_INVALID;
  181. }
  182. }
  183. ge::GraphLoader graph_loader;
  184. ge::Command command;
  185. command.cmd_params.clear();
  186. command.cmd_type = iter->second;
  187. command.cmd_params = prof_params;
  188. if (type != kProfCommandhandleFinalize) {
  189. command.module_index = prof_config_param->profSwitch;
  190. }
  191. GELOGI("GE commandhandle execute, Command Type: %s, data type config: 0x%lx", iter->second.c_str(),
  192. command.module_index);
  193. if (type == kProfCommandhandleStart || type == kProfCommandhandleStop) {
  194. GELOGI("Profiling device nums:%s , deviceID:[%s]", prof_params[0].c_str(), prof_params[kDeviceListIndex].c_str());
  195. }
  196. ge::Status ret = graph_loader.CommandHandle(command);
  197. if (ret != ge::SUCCESS) {
  198. GELOGE(ret, "[Handle][Command]Handle profiling command failed, command type %s, error_code %u",
  199. iter->second.c_str(), ret);
  200. REPORT_CALL_ERROR("E19999", "Handle profiling command failed, command type %s, error_code %u",
  201. iter->second.c_str(), ret);
  202. return ge::FAILED;
  203. }
  204. GELOGI("Successfully execute profiling command type: %d, command 0x%lx.", type, command.module_index);
  205. return ge::SUCCESS;
  206. }
  207. ge::Status ProfSetStepInfo(uint64_t index_id, uint16_t tag_id, rtStream_t stream) {
  208. static bool is_first_run = true;
  209. int32_t device_id = 0;
  210. rtError_t rt_ret = rtGetDevice(&device_id);
  211. if (rt_ret != RT_ERROR_NONE) {
  212. GELOGE(rt_ret, "[Get][LogicDeviceId]Failed, ret 0x%X", rt_ret);
  213. REPORT_CALL_ERROR("E19999", "Get logic device id failed, ret 0x%X", rt_ret);
  214. return ge::FAILED;
  215. }
  216. if (is_first_run && tag_id == kStepStart) {
  217. GE_CHK_STATUS_RET_NOLOG(ge::ProfilingManager::Instance().ProfileStepInfo(index_id,
  218. kModelId,
  219. tag_id,
  220. stream,
  221. device_id));
  222. is_first_run = false;
  223. return ge::SUCCESS;
  224. }
  225. if (!is_first_run && tag_id == kStepEnd) {
  226. GE_CHK_STATUS_RET_NOLOG(ge::ProfilingManager::Instance().ProfileStepInfo(index_id,
  227. kModelId,
  228. tag_id,
  229. stream,
  230. device_id));
  231. is_first_run = true;
  232. return ge::SUCCESS;
  233. }
  234. GELOGE(ge::FAILED, "Param tag_id:%u invalid when is_first_run is %d", tag_id, is_first_run);
  235. REPORT_INPUT_ERROR("E10001", std::vector<std::string>({"value", "parameter", "reason"}),
  236. std::vector<std::string>({std::to_string(tag_id), "tag_id",
  237. "tag id must be 0 when first run, must be 1 when second run"}));
  238. return ge::FAILED;
  239. }

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