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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "common/profiling/profiling_manager.h"
  18. namespace {
  19. const std::string kDeviceNums = "devNums";
  20. const std::string kDeviceIdList = "devIdList";
  21. const std::string kProfilingInit = "prof_init";
  22. const std::string kProfilingFinalize = "prof_finalize";
  23. const std::string kProfilingStart = "prof_start";
  24. const std::string kProfilingStop = "prof_stop";
  25. const std::string kProfModelSubscribe = "prof_model_subscribe";
  26. const std::string kProfModelUnsubscribe = "prof_model_cancel_subscribe";
  27. const std::map<ge::MsprofCommandHandleType, std::string> kProfCommandTypeMap = {
  28. {ge::kProfCommandhandleInit, kProfilingInit},
  29. {ge::kProfCommandhandleStart, kProfilingStart},
  30. {ge::kProfCommandhandleStop, prof_stop},
  31. {ge::kProfCommandhandleFinalize,kProfilingFinalize},
  32. {ge::kProfCommandhandleModelSubscribe, kProfModelSubscribe},
  33. {ge::kProfCommandhandleModelUnsubscribe, kProfModelUnsubscribe}};
  34. } // namespace
  35. namespace ge {
  36. bool TransProfConfigToParam(const ProfCommand &profCommand, vector<string> &prof_config_params) {
  37. prof_config_params.clear();
  38. prof_config_params.emplace_back(kDeviceNums);
  39. prof_config_params.emplace_back(std::to_string(profCommand.dev_nums));
  40. prof_config_params.emplace_back(kDeviceIdList);
  41. std::string devID = "";
  42. if (profCommand.dev_nums == 0) {
  43. GELOGW("The device num is invalid.");
  44. return false;
  45. }
  46. for (uint32_t i = 0; i < profCommand.dev_nums; i++) {
  47. devID.append(std::to_string(profCommand.device_list[i]));
  48. if (i != profCommand.dev_nums - 1) {
  49. devID.append(",");
  50. }
  51. }
  52. prof_config_params.push_back(devID);
  53. return true;
  54. }
  55. bool isProfConfigValid(const uint32_t *deviceid_list, uint32_t device_nums) {
  56. if (deviceid_list == nullptr) {
  57. GELOGE(PARAM_INVALID, "deviceIdList is nullptr");
  58. return false;
  59. }
  60. if (device_nums == 0 || device_nums > kMaxDeviceNum) {
  61. GELOGE(PARAM_INVALID, "The device nums is invalid.");
  62. return false;
  63. }
  64. // real device num
  65. int32_t dev_count = 0;
  66. rtError_t rt_err = rtGetDeviceCount(&dev_count);
  67. if (rt_err != RT_ERROR_NONE) {
  68. GELOGE(INTERNAL_ERROR, "Get the Device count fail.");
  69. return false;
  70. }
  71. if (device_nums > static_cast<uint32_t>(dev_count)) {
  72. GELOGE(PARAM_INVALID, "Device num(%u) is not in range 1 ~ %d.", device_nums, dev_count);
  73. return false;
  74. }
  75. std::unordered_set<uint32_t> record;
  76. for (size_t i = 0; i < device_nums; ++i) {
  77. uint32_t dev_id = deviceid_list[i];
  78. if (dev_id >= static_cast<uint32_t>(dev_count)) {
  79. GELOGE(PARAM_INVALID, "Device id %u is not in range 0 ~ %d(exclude %d)", dev_id, dev_count, dev_count);
  80. return false;
  81. }
  82. if (record.count(dev_id) > 0) {
  83. GELOGE(PARAM_INVALID, "Device id %u is duplicatedly set", dev_id);
  84. return false;
  85. }
  86. record.insert(dev_id);
  87. }
  88. return true;
  89. }
  90. Status GeRegProfCtrlCallback(MsprofCtrlCallback func) {
  91. ProfilingManager::Instance().msprofCtrlCallback = func;
  92. }
  93. Status GeRegProfSetDeviceCallback(MsprofSetDeviceCallback func) {
  94. // pass MsprofSetDeviceCallback to runtime
  95. Status rt_ret = rtRegDeviceStateCallback(func);
  96. }
  97. Status GeRegProfReporterCallback(MsprofReporterCallback func) {
  98. ProfilingManager::Instance().msprofReporterCallback = func;
  99. // pass MsprofReporterCallback to runtime
  100. Status rt_ret = rtSetMsprofReporterCallback(func);
  101. // pass MsprofReporterCallback to hccl
  102. }
  103. Status GeProfCommandHandle(MsprofCommandHandleType type, void *data, uint32_t len) {
  104. GE_CHK_NOT_NULL(data);
  105. ProfCommand *prof_config_param = (ProfCommand *)data;
  106. if (!isProfConfigValid(deviceid_list, device_nums)) {
  107. return FAILED;
  108. }
  109. std::vector<string> prof_params;
  110. if (!TransProfConfigToParam(*prof_config_param, prof_params)) {
  111. GELOGE(PARAM_INVALID, "Transfer profilerConfig to string vector failed");
  112. return PARAM_INVALID;
  113. }
  114. auto iter = kProfCommandTypeMap.find(type);
  115. if (iter == kProfCommandTypeMap.end()) {
  116. GELOGW("The prof comand type is invalid.");
  117. return PARAM_INVALID;
  118. }
  119. GraphLoader graph_loader;
  120. Command command;
  121. command.cmd_params.clear();
  122. command.cmd_type = iter->second;
  123. command.cmd_params = prof_params;
  124. command.module_index = prof_config_param->prof_switch;
  125. GELOGI("GE commandhandle execute, device nums:%s , deviceID:[%s], data type config: 0x%llx", prof_params[0].c_str(),
  126. prof_params[kDeviceListIndex].c_str(), command.module_index);
  127. ret = graph_loader.CommandHandle(command);
  128. if (ret != SUCCESS) {
  129. GELOGE(ret, "Handle profiling command failed");
  130. return FAILED;
  131. }
  132. GELOGI("Successfully execute profiling command 0x%llx.", command.module_index);
  133. }
  134. bool IsGeInitialize() {
  135. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  136. if (instance_ptr == nullptr || instance_ptr->InitFlag() == false) {
  137. return false;
  138. }
  139. return true;
  140. }
  141. } // namespace

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