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.

profiling_manager.cc 20 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /**
  2. * Copyright 2019-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/profiling_manager.h"
  17. #include "framework/common/debug/ge_log.h"
  18. #include "framework/common/debug/log.h"
  19. #include "framework/common/string_util.h"
  20. #include "graph/ge_context.h"
  21. #include "runtime/base.h"
  22. namespace {
  23. const char *const kJobID = "jobID";
  24. const char *const kDeviceID = "deviceID";
  25. const char *const kStartCfg = "startCfg";
  26. const char *const kFeatures = "features";
  27. const char *const kConf = "conf";
  28. const char *const kEvents = "events";
  29. const char *const kAiCoreEvents = "ai_core_events";
  30. const char *const kName = "name";
  31. const char *const kTraceID = "traceId";
  32. const char *const kProfDir = "resultPath";
  33. const size_t kReportMaxLen = 2048;
  34. } // namespace
  35. namespace ge {
  36. ProfilingManager::ProfilingManager() {}
  37. ProfilingManager::~ProfilingManager() {}
  38. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ProfilingManager &ProfilingManager::Instance() {
  39. static ProfilingManager profiling_manager;
  40. return profiling_manager;
  41. }
  42. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::Init(const Options &options) {
  43. #ifdef DAVINCI_SUPPORT_PROFILING
  44. vector<int32_t>().swap(device_id_);
  45. device_id_.push_back(options.device_id);
  46. job_id_ = options.job_id;
  47. Status ret;
  48. if (!recv_profiling_config_.empty()) {
  49. GELOGI("Profiling json config from acl:%s", recv_profiling_config_.c_str());
  50. ret = InitFromAclCfg(recv_profiling_config_);
  51. } else {
  52. ret = InitFromOptions(options);
  53. }
  54. if (ret != SUCCESS) {
  55. GELOGE(ret, "Failed to init profiling.");
  56. return ret;
  57. }
  58. if (is_profiling_) {
  59. // register Framework to profiling
  60. int result = Msprof::Engine::Init(GE_PROFILING_MODULE, &engine_);
  61. if (result != 0) {
  62. GELOGE(FAILED, "Register profiling engine failed.");
  63. return FAILED;
  64. }
  65. // profiling startup first time
  66. GELOGI("Begin to init profiling, device num %zu", device_id_.size());
  67. for (size_t i = 0; i < device_id_.size(); ++i) {
  68. ret = StartProfiling(0, device_id_[i]);
  69. if (ret != SUCCESS) {
  70. GELOGE(ret, "Profiling start failed on device %d.", device_id_[i]);
  71. return FAILED;
  72. }
  73. GELOGI("Profiling init succ on device %d.", device_id_[i]);
  74. }
  75. } else {
  76. GELOGI("The profiling is off, skip the initialization");
  77. }
  78. #endif
  79. return SUCCESS;
  80. }
  81. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::InitFromAclCfg(
  82. const std::string &config) {
  83. #ifdef DAVINCI_SUPPORT_PROFILING
  84. try {
  85. is_profiling_ = false;
  86. profiling_opts_.clear();
  87. op_trace_conf_.clear();
  88. Json start_prof_conf = Json::parse(config);
  89. Json &prof_conf = start_prof_conf[kStartCfg][0];
  90. job_id_ = prof_conf[kJobID];
  91. auto iter = prof_conf.find(kProfDir);
  92. if (iter != prof_conf.end()) {
  93. prof_dir_ = prof_conf[kProfDir];
  94. }
  95. Json &device_id = prof_conf[kDeviceID];
  96. if (device_id.size() != 0) {
  97. vector<int32_t>().swap(device_id_);
  98. bool is_all = false;
  99. for (size_t i = 0; i < device_id.size(); i++) {
  100. std::string device_id_str = device_id[i].get<std::string>();
  101. if (device_id_str == "all") {
  102. is_all = true;
  103. break;
  104. }
  105. device_id_.push_back(std::stoi(device_id_str));
  106. }
  107. if (is_all == true) {
  108. int32_t count = 0;
  109. rtError_t rt_err = rtGetDeviceCount(&count);
  110. if (rt_err != RT_ERROR_NONE) {
  111. GELOGE(FAILED, "Call rtGetDeviceCount to get device failed.");
  112. }
  113. vector<int32_t>().swap(device_id_);
  114. for (int32_t i = 0; i < count; ++i) {
  115. device_id_.push_back(i);
  116. }
  117. }
  118. }
  119. Json &features = prof_conf[kFeatures];
  120. if (ParseFeaturesFromAclCfg(features) != SUCCESS) {
  121. GELOGE(FAILED, "Parse feature from acl cfg failed.");
  122. return FAILED;
  123. }
  124. is_profiling_ = true;
  125. } catch (...) {
  126. GELOGE(FAILED, "Json conf is not invalid !");
  127. return ge::PARAM_INVALID;
  128. }
  129. #endif
  130. return ge::SUCCESS;
  131. }
  132. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::ParseFeaturesFromAclCfg(
  133. const Json &features) {
  134. #ifdef DAVINCI_SUPPORT_PROFILING
  135. try {
  136. for (size_t i = 0; i < features.size(); ++i) {
  137. const Json &feature = features[i];
  138. if ((feature.find(kName) == feature.end()) || feature[kName].is_null()) {
  139. continue;
  140. }
  141. const std::string &name = feature[kName];
  142. if (name == "op_trace") {
  143. const Json &conf = feature[kConf];
  144. const Json &events = conf[0][kEvents];
  145. const std::string &ai_core_events = events[0][kAiCoreEvents];
  146. GELOGI("Op trace config from acl ai_core_events:%s", ai_core_events.c_str());
  147. is_op_trace_ = true;
  148. ProfMgrConf prof_mgr_conf;
  149. int result = ProfMgrGetConf(ai_core_events, &prof_mgr_conf);
  150. if (result != 0) {
  151. GELOGE(FAILED, "ProfMgrGetConf failed.");
  152. return FAILED;
  153. }
  154. op_trace_conf_ = prof_mgr_conf.conf;
  155. op_trace_iter_num_ = static_cast<int32_t>(op_trace_conf_.size());
  156. GELOGI("Op trace profiling iter num %d,", op_trace_iter_num_);
  157. } else if (name == "task_trace") {
  158. is_op_trace_ = false;
  159. if (feature.find(kConf) != feature.end()) {
  160. const Json &conf = feature[kConf];
  161. std::stringstream task_trace_conf;
  162. task_trace_conf << conf;
  163. task_trace_conf_ = task_trace_conf.str();
  164. }
  165. GELOGI("Task trace config from acl");
  166. } else if (name == "system_trace") {
  167. is_op_trace_ = false;
  168. const Json &conf = feature[kConf];
  169. std::stringstream system_trace_conf;
  170. system_trace_conf << conf;
  171. system_trace_conf_ = system_trace_conf.str();
  172. GELOGI("System trace config from acl");
  173. }
  174. profiling_opts_.push_back(name);
  175. }
  176. } catch (...) {
  177. GELOGE(ge::PARAM_INVALID, "Json conf feature is not invalid !");
  178. return ge::PARAM_INVALID;
  179. }
  180. #endif
  181. return ge::SUCCESS;
  182. }
  183. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::InitFromOptions(const Options &options) {
  184. #ifdef DAVINCI_SUPPORT_PROFILING
  185. // enable profiling support two ways: env and front end
  186. const char *profiling_mode = std::getenv("PROFILING_MODE");
  187. const char *prof_options = std::getenv("PROFILING_OPTIONS");
  188. if ((profiling_mode == nullptr) || (strcmp("true", profiling_mode) != 0) || (prof_options == nullptr)) {
  189. is_profiling_ = false;
  190. } else {
  191. std::string prof_options_str = std::string(prof_options);
  192. profiling_opts_ = StringUtils::Split(prof_options_str, ':');
  193. is_profiling_ = true;
  194. GELOGI("The profiling in env is %s, %s", profiling_mode, prof_options);
  195. }
  196. if (!is_profiling_) {
  197. const std::string enable_profiling = "1";
  198. if (options.profiling_mode != enable_profiling || options.profiling_options.empty()) {
  199. is_profiling_ = false;
  200. return SUCCESS;
  201. } else {
  202. profiling_opts_ = StringUtils::Split(options.profiling_options, ':');
  203. is_profiling_ = true;
  204. GELOGI("The profiling in options is %s, %s", options.profiling_mode.c_str(), options.profiling_options.c_str());
  205. }
  206. }
  207. // features:'training_trace', 'task_trace' or 'op_trace' etc
  208. if (!profiling_opts_.empty()) {
  209. if (profiling_opts_[0] == "op_trace") {
  210. is_op_trace_ = true;
  211. // op trace get conf
  212. ProfMgrConf prof_mgr_conf;
  213. int result = ProfMgrGetConf("", &prof_mgr_conf);
  214. if (result != 0) {
  215. GELOGE(FAILED, "ProfMgrGetConf failed.");
  216. return FAILED;
  217. }
  218. op_trace_conf_ = prof_mgr_conf.conf;
  219. op_trace_iter_num_ = static_cast<int32_t>(op_trace_conf_.size());
  220. GELOGI("op trace profiling iter num %d,", op_trace_iter_num_);
  221. } else {
  222. is_op_trace_ = false;
  223. op_trace_iter_num_ = 1;
  224. }
  225. }
  226. #endif
  227. return ge::SUCCESS;
  228. }
  229. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::StartProfiling(int32_t iter_num,
  230. int32_t device_id) {
  231. #ifdef DAVINCI_SUPPORT_PROFILING
  232. if (!profiling_opts_.empty()) {
  233. GELOGI("Start profiling index is %d", iter_num);
  234. // current one docker only use one device
  235. Json p_device;
  236. try {
  237. // profiling need physical_device_id
  238. p_device[kDeviceID] = std::to_string(device_id);
  239. p_device[kJobID] = job_id_;
  240. p_device[kTraceID] = std::to_string(GetContext().TraceId());
  241. if (!prof_dir_.empty()) {
  242. p_device[kProfDir] = prof_dir_;
  243. GELOGI("Prof dir: %s.", prof_dir_.c_str());
  244. }
  245. Json features;
  246. if (is_op_trace_) {
  247. Json f;
  248. f[kName] = "op_trace";
  249. Json conf;
  250. if (op_trace_conf_.size() <= static_cast<size_t>(iter_num)) {
  251. GELOGE(FAILED, "Op trace iter num is invalid!");
  252. return FAILED;
  253. }
  254. Json events;
  255. events[0] = nlohmann::json::parse(op_trace_conf_[iter_num]);
  256. conf[0][kEvents] = events;
  257. f[kConf] = conf;
  258. features[0] = f;
  259. if (iter_num == 0) {
  260. is_load_ = true;
  261. }
  262. } else {
  263. for (std::vector<std::string>::size_type i = 0; i < profiling_opts_.size(); i++) {
  264. Json f;
  265. if (profiling_opts_[i] == "system_trace") {
  266. f[kConf] = nlohmann::json::parse(system_trace_conf_);
  267. } else if (profiling_opts_[i] == "task_trace") {
  268. if (!task_trace_conf_.empty()) {
  269. f[kConf] = nlohmann::json::parse(task_trace_conf_);
  270. }
  271. }
  272. f[kName] = profiling_opts_[i];
  273. features[i] = f;
  274. }
  275. is_load_ = true;
  276. }
  277. p_device[kFeatures] = features;
  278. // only one device, but sProfMgrStartUp API require for device list
  279. Json devices;
  280. devices[0] = p_device;
  281. Json start_cfg;
  282. start_cfg[kStartCfg] = devices;
  283. // convert json to string
  284. std::stringstream ss;
  285. ss << start_cfg;
  286. send_profiling_config_ = ss.str();
  287. GELOGI("Profiling config %s\n", send_profiling_config_.c_str());
  288. } catch (...) {
  289. GELOGE(FAILED, "Op trace json conf is not invalid !");
  290. return FAILED;
  291. }
  292. // runtime startup for profiling
  293. GE_CHK_RT_RET(rtProfilerStart());
  294. // call profiling startup API
  295. ProfMgrCfg prof_cfg = {send_profiling_config_};
  296. void *prof_handle = ProfMgrStartUp(&prof_cfg);
  297. if (prof_handle == nullptr) {
  298. GELOGW("ProfMgrStartUp failed.");
  299. return FAILED;
  300. }
  301. GELOGD("StartProfiling, prof_handle: %p", prof_handle);
  302. prof_handle_vec_.push_back(prof_handle);
  303. }
  304. #endif
  305. return SUCCESS;
  306. }
  307. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::StopProfiling() {
  308. #ifdef DAVINCI_SUPPORT_PROFILING
  309. Msprof::Engine::Reporter *reporter = PluginImpl::GetPluginReporter();
  310. if (reporter != nullptr) {
  311. int ret = reporter->Flush();
  312. GELOGI("Report data end, ret is %d", ret);
  313. }
  314. rtError_t rt_ret = rtProfilerStop();
  315. if (rt_ret != RT_ERROR_NONE) {
  316. GELOGI("Call rtProfilerStop ret:%d", rt_ret);
  317. }
  318. for (size_t i = 0; i < prof_handle_vec_.size(); ++i) {
  319. int result = ProfMgrStop(prof_handle_vec_[i]);
  320. if (result != 0) {
  321. GELOGW("ProfMgr stop return fail:%d, handle:%p", result, prof_handle_vec_[i]);
  322. }
  323. }
  324. vector<void *>().swap(prof_handle_vec_);
  325. is_load_ = false;
  326. recv_profiling_config_ = "";
  327. GELOGI("Stop Profiling success.");
  328. #endif
  329. }
  330. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingTaskDescInfo(
  331. const std::vector<TaskDescInfo> &task_desc_info, const int32_t &device_id) {
  332. #ifdef DAVINCI_SUPPORT_PROFILING
  333. Msprof::Engine::Reporter *reporter = PluginImpl::GetPluginReporter();
  334. if (reporter == nullptr) {
  335. GELOGI("Profiling report is nullptr!");
  336. return;
  337. }
  338. std::string data;
  339. for (const auto &task : task_desc_info) {
  340. std::string op_name = task.op_name;
  341. uint32_t block_dim = task.block_dim;
  342. uint32_t task_id = task.task_id;
  343. uint32_t stream_id = task.stream_id;
  344. data = op_name.append(" ").append(std::to_string(block_dim)
  345. .append(" ")
  346. .append(std::to_string(task_id))
  347. .append(" ")
  348. .append(std::to_string(stream_id))
  349. .append("\n"));
  350. Msprof::Engine::ReporterData reporter_data{};
  351. reporter_data.deviceId = device_id;
  352. reporter_data.data = (unsigned char *)data.c_str();
  353. reporter_data.dataLen = data.size();
  354. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "task_desc_info", sizeof("task_desc_info"));
  355. if (ret != EOK) {
  356. GELOGE(ret, "Report data tag of task_desc_info memcpy error!");
  357. return;
  358. }
  359. ret = reporter->Report(&reporter_data);
  360. if (ret != SUCCESS) {
  361. GELOGE(ret, "Reporter data of task_desc_info fail!");
  362. return;
  363. }
  364. }
  365. data.clear();
  366. #endif
  367. }
  368. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingGraphDescInfo(
  369. const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info, const int32_t &device_id) {
  370. #ifdef DAVINCI_SUPPORT_PROFILING
  371. Msprof::Engine::Reporter *reporter = PluginImpl::GetPluginReporter();
  372. GE_IF_BOOL_EXEC(reporter == nullptr, GELOGI("Profiling report is nullptr!"); return;);
  373. std::string data;
  374. for (const auto &graph : compute_graph_desc_info) {
  375. data.append("op_name:").append(graph.op_name).append(" op_type:").append(graph.op_type);
  376. for (size_t i = 0; i < graph.input_format.size(); ++i) {
  377. data.append(" input_id:")
  378. .append(std::to_string(i))
  379. .append(" input_format:")
  380. .append(std::to_string(graph.input_format.at(i)))
  381. .append(" input_data_type:")
  382. .append(std::to_string(graph.input_data_type.at(i)))
  383. .append(" input_shape:\"");
  384. size_t input_shape_len = graph.input_shape.at(i).size();
  385. if (input_shape_len == 0) {
  386. data.append("");
  387. } else if (input_shape_len == 1) {
  388. data.append(std::to_string(graph.input_shape.at(i).at(0)));
  389. } else {
  390. for (size_t j = 0; j < input_shape_len - 1; ++j) {
  391. data.append(std::to_string(graph.input_shape.at(i).at(j))).append(",");
  392. }
  393. data.append(std::to_string(graph.input_shape.at(i).at(input_shape_len - 1)));
  394. }
  395. data.append("\"");
  396. }
  397. for (size_t i = 0; i < graph.output_format.size(); ++i) {
  398. data.append(" output_id:")
  399. .append(std::to_string(i))
  400. .append(" output_format:")
  401. .append(std::to_string(graph.output_format.at(i)))
  402. .append(" output_data_type:")
  403. .append(std::to_string(graph.output_data_type.at(i)))
  404. .append(" output_shape:\"");
  405. size_t output_shape_len = graph.output_shape.at(i).size();
  406. if (output_shape_len == 0) {
  407. data.append("");
  408. } else if (output_shape_len == 1) {
  409. data.append(std::to_string(graph.output_shape.at(i).at(0)));
  410. } else {
  411. for (size_t j = 0; j < output_shape_len - 1; ++j) {
  412. data.append(std::to_string(graph.output_shape.at(i).at(j))).append(",");
  413. }
  414. data.append(std::to_string(graph.output_shape.at(i).at(output_shape_len - 1)));
  415. }
  416. data.append("\"");
  417. }
  418. data.append("\n");
  419. Msprof::Engine::ReporterData reporter_data{};
  420. Report(device_id, data, *reporter, reporter_data);
  421. data.clear();
  422. }
  423. #endif
  424. }
  425. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::Report(
  426. const int32_t &device_id, const string &data, Msprof::Engine::Reporter &reporter,
  427. Msprof::Engine::ReporterData &reporter_data) {
  428. #ifdef DAVINCI_SUPPORT_PROFILING
  429. size_t index = data.size() / kReportMaxLen;
  430. if (index >= 1) {
  431. reporter_data.deviceId = device_id;
  432. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  433. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  434. for (size_t i = 0; i < index; ++i) {
  435. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * i;
  436. reporter_data.dataLen = kReportMaxLen;
  437. ret = reporter.Report(&reporter_data);
  438. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "Reporter data of graph_desc_info fail!"); return;);
  439. }
  440. reporter_data.dataLen = data.size() - kReportMaxLen * index;
  441. if (reporter_data.dataLen != 0) {
  442. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * index;
  443. ret = reporter.Report(&reporter_data);
  444. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "Reporter data of graph_desc_info fail!"); return;);
  445. }
  446. } else {
  447. reporter_data.deviceId = device_id;
  448. reporter_data.data = (unsigned char *)data.c_str();
  449. reporter_data.dataLen = data.size();
  450. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  451. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  452. ret = reporter.Report(&reporter_data);
  453. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "Reporter data of graph_desc_info fail!"); return;);
  454. }
  455. #endif
  456. }
  457. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::PluginUnInit(const std::string &module) const {
  458. #ifdef DAVINCI_SUPPORT_PROFILING
  459. int ret = Msprof::Engine::UnInit(module);
  460. if (ret != SUCCESS) {
  461. GELOGE(ret, "profiling plugin uninit failed, ret:%d", ret);
  462. }
  463. #endif
  464. }
  465. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ReportProfilingData(
  466. const std::vector<TaskDescInfo> &task_desc_info, const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info) {
  467. #ifdef DAVINCI_SUPPORT_PROFILING
  468. int32_t logic_device_id = 0;
  469. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  470. if (rt_ret != RT_ERROR_NONE) {
  471. GELOGE(rt_ret, "runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  472. return;
  473. }
  474. GELOGI("current logic_device_id:%d", logic_device_id);
  475. uint32_t phy_device_id = 0;
  476. rt_ret = rtGetDevicePhyIdByIndex((uint32_t)logic_device_id, &phy_device_id);
  477. if (rt_ret != RT_ERROR_NONE) {
  478. GELOGE(rt_ret, "runtime get phy_device_id failed, current phy_device_id:%d", phy_device_id);
  479. return;
  480. }
  481. GELOGI("current phy_device_id:%d", phy_device_id);
  482. auto ret = std::find(device_id_.begin(), device_id_.end(), phy_device_id);
  483. if (ret == device_id_.end()) {
  484. GELOGE(FAILED, "get valid phy_device_id failed, profiling report failed.");
  485. return;
  486. }
  487. GELOGI("start ProfilingTaskDescInfo.");
  488. ProfilingTaskDescInfo(task_desc_info, phy_device_id);
  489. GELOGI("start ProfilingGraphDescInfo.");
  490. ProfilingGraphDescInfo(compute_graph_desc_info, phy_device_id);
  491. GELOGI("Report profiling data for GE end.");
  492. #endif
  493. }
  494. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::SetProfilingConfig(
  495. const std::string &profiling_cfg) {
  496. recv_profiling_config_ = profiling_cfg;
  497. }
  498. /**
  499. * @brief Profiling PluginImpl
  500. */
  501. // PluginImpl static variable init
  502. Msprof::Engine::Reporter *PluginImpl::reporter_ = nullptr;
  503. PluginImpl::PluginImpl(const std::string &module) : module_(module) { GELOGI("Create PluginImpl\n"); }
  504. int PluginImpl::Init(const Msprof::Engine::Reporter *reporter) {
  505. GELOGI("PluginImpl init");
  506. reporter_ = const_cast<Msprof::Engine::Reporter *>(reporter);
  507. return 0;
  508. }
  509. int PluginImpl::UnInit() {
  510. GELOGI("PluginImpl Uninit");
  511. reporter_ = nullptr;
  512. return 0;
  513. }
  514. Msprof::Engine::PluginIntf *ProfilingEngineImpl::CreatePlugin() {
  515. GELOGI(" Create Plugin");
  516. return new (std::nothrow) PluginImpl(GE_PROFILING_MODULE);
  517. }
  518. int ProfilingEngineImpl::ReleasePlugin(Msprof::Engine::PluginIntf *plugin) {
  519. if (plugin != nullptr) {
  520. delete plugin;
  521. plugin = nullptr;
  522. }
  523. return 0;
  524. }
  525. } // namespace ge

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