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_prof.cc 12 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 "ge/ge_prof.h"
  17. #include "ge/ge_api.h"
  18. #include "init/gelib.h"
  19. #include "common/debug/log.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "common/profiling/profiling_manager.h"
  22. #include "graph/load/graph_loader.h"
  23. #include "toolchain/prof_acl_api.h"
  24. using std::map;
  25. using std::string;
  26. using std::vector;
  27. namespace {
  28. const uint32_t kMaxDeviceNum = 64;
  29. const uint32_t kDeviceListIndex = 3;
  30. const std::string kProfilingInit = "prof_init";
  31. const std::string kProfilingFinalize = "prof_finalize";
  32. const std::string kProfilingStart = "prof_start";
  33. const std::string kProfilingStop = "prof_stop";
  34. const std::string kDeviceNums = "devNums";
  35. const std::string kDeviceIdList = "devIdList";
  36. const std::string kAicoreMetrics = "aicoreMetrics";
  37. const std::map<ge::ProfilingAicoreMetrics, std::string> kProfAicoreMetricsToString = {
  38. {ge::kAicoreArithmaticThroughput, "AICORE_ARITHMATIC_THROUGHPUT"},
  39. {ge::kAicorePipeline, "AICORE_PIPELINE"},
  40. {ge::kAicoreSynchronization, "AICORE_SYNCHRONIZATION"},
  41. {ge::kAicoreMemory, "AICORE_MEMORY"},
  42. {ge::kAicoreInternalMemory, "AICORE_INTERNAL_MEMORY"},
  43. {ge::kAicoreStall, "AICORE_STALL"}};
  44. } // namespace
  45. static bool g_graph_prof_init_ = false;
  46. static std::mutex g_prof_mutex_;
  47. namespace ge {
  48. struct aclgrphProfConfig {
  49. ProfConfig config;
  50. };
  51. Status aclgrphProfInit(const char *profiler_path, uint32_t length) {
  52. GELOGT(TRACE_INIT, "Graph prof init start");
  53. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  54. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  55. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "Ge client is not initialized.");
  56. return FAILED;
  57. }
  58. std::lock_guard<std::mutex> lock(g_prof_mutex_);
  59. if (g_graph_prof_init_) {
  60. GELOGW("Multi graph profiling initializations.");
  61. return GE_PROF_MULTI_INIT;
  62. }
  63. Status ret = CheckPath(profiler_path, length);
  64. if (ret != SUCCESS) {
  65. GELOGE(ret, "Profiling config path is invalid.");
  66. return ret;
  67. }
  68. // if command mode is set, just return
  69. if (ProfilingManager::Instance().ProfilingOn()) {
  70. GELOGW("Graph prof init failed, cause profiling command pattern is running.");
  71. return GE_PROF_MODE_CONFLICT;
  72. }
  73. ret = ProfInit(profiler_path);
  74. if (ret != SUCCESS) {
  75. GELOGE(ret, "ProfInit init fail");
  76. return ret;
  77. }
  78. GraphLoader graph_loader;
  79. Command command;
  80. command.cmd_params.clear();
  81. command.cmd_type = kProfilingInit;
  82. command.module_index = PROF_MODEL_LOAD;
  83. ret = graph_loader.CommandHandle(command);
  84. if (ret != SUCCESS) {
  85. GELOGE(ret, "Handle profiling command %s failed, config = %s", kProfilingInit.c_str(), profiler_path);
  86. return ret;
  87. }
  88. if (!g_graph_prof_init_) {
  89. g_graph_prof_init_ = true;
  90. GELOGI("Profiling init successfully.");
  91. }
  92. GELOGI("Successfully execute GraphProfInit.");
  93. return SUCCESS;
  94. }
  95. Status aclgrphProfFinalize() {
  96. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  97. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  98. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "Ge client is not initialized.");
  99. return FAILED;
  100. }
  101. std::lock_guard<std::mutex> lock(g_prof_mutex_);
  102. // if command mode is set, just return
  103. if (ProfilingManager::Instance().ProfilingOn()) {
  104. GELOGW("Graph prof finalize failed, cause profiling command pattern is running.");
  105. return GE_PROF_MODE_CONFLICT;
  106. }
  107. if (!g_graph_prof_init_) {
  108. GELOGE(GE_PROF_NOT_INIT, "Graph not profiling initialize.");
  109. return GE_PROF_NOT_INIT;
  110. }
  111. GraphLoader graph_loader;
  112. Command command;
  113. command.cmd_params.clear();
  114. command.cmd_type = kProfilingFinalize;
  115. Status ret = graph_loader.CommandHandle(command);
  116. if (ret != SUCCESS) {
  117. GELOGE(ret, "Handle profiling command %s failed.", kProfilingFinalize.c_str());
  118. return ret;
  119. }
  120. ret = ProfFinalize();
  121. if (ret != SUCCESS) {
  122. GELOGE(ret, "Finalize profiling failed, result = %d", ret);
  123. }
  124. if (ret == SUCCESS) {
  125. g_graph_prof_init_ = false;
  126. GELOGI("Successfully execute GraphProfFinalize.");
  127. }
  128. return ret;
  129. }
  130. bool TransProfConfigToParam(const aclgrphProfConfig *profiler_config, vector<string> &prof_config_params) {
  131. prof_config_params.clear();
  132. prof_config_params.emplace_back(kDeviceNums);
  133. prof_config_params.emplace_back(std::to_string(profiler_config->config.devNums));
  134. prof_config_params.emplace_back(kDeviceIdList);
  135. std::string devID = "";
  136. if (profiler_config->config.devNums == 0) {
  137. GELOGW("The device num is invalid.");
  138. return false;
  139. }
  140. for (uint32_t i = 0; i < profiler_config->config.devNums; i++) {
  141. devID.append(std::to_string(profiler_config->config.devIdList[i]));
  142. if (i != profiler_config->config.devNums - 1) {
  143. devID.append(",");
  144. }
  145. }
  146. prof_config_params.push_back(devID);
  147. prof_config_params.push_back(kAicoreMetrics);
  148. auto iter =
  149. kProfAicoreMetricsToString.find(static_cast<ProfilingAicoreMetrics>(profiler_config->config.aicoreMetrics));
  150. if (iter == kProfAicoreMetricsToString.end()) {
  151. GELOGW("The prof aicore metrics is invalid.");
  152. return false;
  153. }
  154. prof_config_params.push_back(iter->second);
  155. return true;
  156. }
  157. bool isProfConfigValid(const uint32_t *deviceid_list, uint32_t device_nums) {
  158. if (deviceid_list == nullptr) {
  159. GELOGE(PARAM_INVALID, "deviceIdList is nullptr");
  160. return false;
  161. }
  162. if (device_nums == 0 || device_nums > kMaxDeviceNum) {
  163. GELOGE(PARAM_INVALID, "The device nums is invalid.");
  164. return false;
  165. }
  166. // real device num
  167. int32_t dev_count = 0;
  168. rtError_t rt_err = rtGetDeviceCount(&dev_count);
  169. if (rt_err != RT_ERROR_NONE) {
  170. GELOGE(INTERNAL_ERROR, "Get the Device count fail.");
  171. return false;
  172. }
  173. if (device_nums > static_cast<uint32_t>(dev_count)) {
  174. GELOGE(PARAM_INVALID, "Device num(%u) is not in range 1 ~ %d.", device_nums, dev_count);
  175. return false;
  176. }
  177. std::unordered_set<uint32_t> record;
  178. for (size_t i = 0; i < device_nums; ++i) {
  179. uint32_t dev_id = deviceid_list[i];
  180. if (dev_id >= static_cast<uint32_t>(dev_count)) {
  181. GELOGE(PARAM_INVALID, "Device id %u is not in range 0 ~ %d(exclude %d)", dev_id, dev_count, dev_count);
  182. return false;
  183. }
  184. if (record.count(dev_id) > 0) {
  185. GELOGE(PARAM_INVALID, "Device id %u is duplicatedly set", dev_id);
  186. return false;
  187. }
  188. record.insert(dev_id);
  189. }
  190. return true;
  191. }
  192. aclgrphProfConfig *aclgrphProfCreateConfig(uint32_t *deviceid_list, uint32_t device_nums,
  193. ProfilingAicoreMetrics aicore_metrics, ProfAicoreEvents *aicore_events,
  194. uint64_t data_type_config) {
  195. if (!isProfConfigValid(deviceid_list, device_nums)) {
  196. return nullptr;
  197. }
  198. aclgrphProfConfig *config = new (std::nothrow) aclgrphProfConfig();
  199. if (config == nullptr) {
  200. GELOGE(INTERNAL_ERROR, "new aclgrphProfConfig fail");
  201. return nullptr;
  202. }
  203. config->config.devNums = device_nums;
  204. if (memcpy_s(config->config.devIdList, sizeof(config->config.devIdList), deviceid_list,
  205. device_nums * sizeof(uint32_t)) != EOK) {
  206. GELOGE(INTERNAL_ERROR, "copy devID failed. size = %u", device_nums);
  207. delete config;
  208. return nullptr;
  209. }
  210. config->config.aicoreMetrics = static_cast<ProfAicoreMetrics>(aicore_metrics);
  211. config->config.dataTypeConfig = data_type_config;
  212. GELOGI("Successfully create prof config.");
  213. return config;
  214. }
  215. Status aclgrphProfDestroyConfig(aclgrphProfConfig *profiler_config) {
  216. if (profiler_config == nullptr) {
  217. GELOGE(PARAM_INVALID, "destroy profilerConfig failed, profilerConfig must not be nullptr");
  218. return PARAM_INVALID;
  219. }
  220. delete profiler_config;
  221. GELOGI("Successfully destroy prof config.");
  222. return SUCCESS;
  223. }
  224. Status aclgrphProfStart(aclgrphProfConfig *profiler_config) {
  225. if (profiler_config == nullptr) {
  226. GELOGE(PARAM_INVALID, "aclgrphProfConfig is invalid.");
  227. return FAILED;
  228. }
  229. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  230. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  231. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "Ge client is not initialized.");
  232. return FAILED;
  233. }
  234. std::lock_guard<std::mutex> lock(g_prof_mutex_);
  235. // if command mode is set, just return
  236. if (ProfilingManager::Instance().ProfilingOn()) {
  237. GELOGW("Graph prof finalize failed, cause profiling command pattern is running.");
  238. return GE_PROF_MODE_CONFLICT;
  239. }
  240. if (!g_graph_prof_init_) {
  241. GELOGE(GE_PROF_NOT_INIT, "Graph not profiling initialize.");
  242. return GE_PROF_NOT_INIT;
  243. }
  244. Status ret = ProfStartProfiling(&profiler_config->config);
  245. if (ret != SUCCESS) {
  246. GELOGE(ret, "Start profiling failed, prof result = %d", ret);
  247. return FAILED;
  248. }
  249. std::vector<string> prof_params;
  250. if (!TransProfConfigToParam(profiler_config, prof_params)) {
  251. GELOGE(PARAM_INVALID, "Transfer profilerConfig to string vector failed");
  252. return PARAM_INVALID;
  253. }
  254. GraphLoader graph_loader;
  255. Command command;
  256. command.cmd_params.clear();
  257. command.cmd_type = kProfilingStart;
  258. command.cmd_params = prof_params;
  259. command.module_index = profiler_config->config.dataTypeConfig;
  260. GELOGI("Profiling will start, device nums:%s , deviceID:[%s], data type config: 0x%llx", prof_params[0].c_str(),
  261. prof_params[kDeviceListIndex].c_str(), command.module_index);
  262. ret = graph_loader.CommandHandle(command);
  263. if (ret != SUCCESS) {
  264. GELOGE(ret, "Handle profiling command failed");
  265. return FAILED;
  266. }
  267. GELOGI("Successfully execute GraphProfStartProfiling.");
  268. return SUCCESS;
  269. }
  270. Status aclgrphProfStop(aclgrphProfConfig *profiler_config) {
  271. if (profiler_config == nullptr) {
  272. GELOGE(PARAM_INVALID, "aclgrphProfConfig is invalid.");
  273. return FAILED;
  274. }
  275. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  276. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  277. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "Ge client is not initialized.");
  278. return FAILED;
  279. }
  280. std::lock_guard<std::mutex> lock(g_prof_mutex_);
  281. // if command mode is set, just return
  282. if (ProfilingManager::Instance().ProfilingOn()) {
  283. GELOGW("Graph prof finalize failed, cause profiling command pattern is running.");
  284. return GE_PROF_MODE_CONFLICT;
  285. }
  286. if (!g_graph_prof_init_) {
  287. GELOGE(GE_PROF_NOT_INIT, "Graph not profiling initialize.");
  288. return GE_PROF_NOT_INIT;
  289. }
  290. Status ret = ProfStopProfiling(&profiler_config->config);
  291. if (ret != SUCCESS) {
  292. GELOGE(ret, "Stop profiling failed, prof result = %d", ret);
  293. return ret;
  294. }
  295. std::vector<string> prof_params;
  296. if (!TransProfConfigToParam(profiler_config, prof_params)) {
  297. GELOGE(PARAM_INVALID, "Transfer profilerConfig to string vector failed");
  298. return PARAM_INVALID;
  299. }
  300. GraphLoader graph_loader;
  301. Command command;
  302. command.cmd_params.clear();
  303. command.cmd_type = kProfilingStop;
  304. command.cmd_params = prof_params;
  305. command.module_index = profiler_config->config.dataTypeConfig;
  306. GELOGI("Profiling will stop, device nums:%s , deviceID:[%s], data type config: 0x%llx", prof_params[0].c_str(),
  307. prof_params[kDeviceListIndex].c_str(), command.module_index);
  308. ret = graph_loader.CommandHandle(command);
  309. if (ret != SUCCESS) {
  310. GELOGE(ret, "Handle profiling command failed");
  311. return FAILED;
  312. }
  313. GELOGI("Successfully execute GraphProfStopProfiling.");
  314. return SUCCESS;
  315. }
  316. } // namespace ge

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