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

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

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