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 21 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
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. GELOGW("Profiling start failed on device %d.", device_id_[i]);
  71. continue;
  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 on device %d ", device_id);
  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 model_name = task.model_name;
  341. std::string op_name = task.op_name;
  342. uint32_t block_dim = task.block_dim;
  343. uint32_t task_id = task.task_id;
  344. uint32_t stream_id = task.stream_id;
  345. data = model_name.append(" ").append(op_name).append(" ").append(std::to_string(block_dim)
  346. .append(" ")
  347. .append(std::to_string(task_id))
  348. .append(" ")
  349. .append(std::to_string(stream_id))
  350. .append("\n"));
  351. Msprof::Engine::ReporterData reporter_data{};
  352. reporter_data.deviceId = device_id;
  353. reporter_data.data = (unsigned char *)data.c_str();
  354. reporter_data.dataLen = data.size();
  355. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "task_desc_info", sizeof("task_desc_info"));
  356. if (ret != EOK) {
  357. GELOGE(ret, "Report data tag of task_desc_info memcpy error!");
  358. return;
  359. }
  360. ret = reporter->Report(&reporter_data);
  361. if (ret != SUCCESS) {
  362. GELOGE(ret, "Reporter data of task_desc_info fail!");
  363. return;
  364. }
  365. }
  366. data.clear();
  367. #endif
  368. }
  369. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingGraphDescInfo(
  370. const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info, const int32_t &device_id) {
  371. #ifdef DAVINCI_SUPPORT_PROFILING
  372. Msprof::Engine::Reporter *reporter = PluginImpl::GetPluginReporter();
  373. GE_IF_BOOL_EXEC(reporter == nullptr, GELOGI("Profiling report is nullptr!"); return;);
  374. std::string data;
  375. for (const auto &graph : compute_graph_desc_info) {
  376. data.append("model_name:")
  377. .append(graph.model_name)
  378. .append(" op_name:")
  379. .append(graph.op_name)
  380. .append(" op_type:")
  381. .append(graph.op_type);
  382. for (size_t i = 0; i < graph.input_format.size(); ++i) {
  383. data.append(" input_id:")
  384. .append(std::to_string(i))
  385. .append(" input_format:")
  386. .append(std::to_string(graph.input_format.at(i)))
  387. .append(" input_data_type:")
  388. .append(std::to_string(graph.input_data_type.at(i)))
  389. .append(" input_shape:\"");
  390. size_t input_shape_len = graph.input_shape.at(i).size();
  391. if (input_shape_len == 0) {
  392. data.append("");
  393. } else if (input_shape_len == 1) {
  394. data.append(std::to_string(graph.input_shape.at(i).at(0)));
  395. } else {
  396. for (size_t j = 0; j < input_shape_len - 1; ++j) {
  397. data.append(std::to_string(graph.input_shape.at(i).at(j))).append(",");
  398. }
  399. data.append(std::to_string(graph.input_shape.at(i).at(input_shape_len - 1)));
  400. }
  401. data.append("\"");
  402. }
  403. for (size_t i = 0; i < graph.output_format.size(); ++i) {
  404. data.append(" output_id:")
  405. .append(std::to_string(i))
  406. .append(" output_format:")
  407. .append(std::to_string(graph.output_format.at(i)))
  408. .append(" output_data_type:")
  409. .append(std::to_string(graph.output_data_type.at(i)))
  410. .append(" output_shape:\"");
  411. size_t output_shape_len = graph.output_shape.at(i).size();
  412. if (output_shape_len == 0) {
  413. data.append("");
  414. } else if (output_shape_len == 1) {
  415. data.append(std::to_string(graph.output_shape.at(i).at(0)));
  416. } else {
  417. for (size_t j = 0; j < output_shape_len - 1; ++j) {
  418. data.append(std::to_string(graph.output_shape.at(i).at(j))).append(",");
  419. }
  420. data.append(std::to_string(graph.output_shape.at(i).at(output_shape_len - 1)));
  421. }
  422. data.append("\"");
  423. }
  424. data.append("\n");
  425. Msprof::Engine::ReporterData reporter_data{};
  426. Report(device_id, data, *reporter, reporter_data);
  427. data.clear();
  428. }
  429. #endif
  430. }
  431. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::Report(
  432. const int32_t &device_id, const string &data, Msprof::Engine::Reporter &reporter,
  433. Msprof::Engine::ReporterData &reporter_data) {
  434. #ifdef DAVINCI_SUPPORT_PROFILING
  435. size_t index = data.size() / kReportMaxLen;
  436. if (index >= 1) {
  437. reporter_data.deviceId = device_id;
  438. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  439. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  440. for (size_t i = 0; i < index; ++i) {
  441. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * i;
  442. reporter_data.dataLen = kReportMaxLen;
  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. reporter_data.dataLen = data.size() - kReportMaxLen * index;
  447. if (reporter_data.dataLen != 0) {
  448. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * index;
  449. ret = reporter.Report(&reporter_data);
  450. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "Reporter data of graph_desc_info fail!"); return;);
  451. }
  452. } else {
  453. reporter_data.deviceId = device_id;
  454. reporter_data.data = (unsigned char *)data.c_str();
  455. reporter_data.dataLen = data.size();
  456. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  457. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  458. ret = reporter.Report(&reporter_data);
  459. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "Reporter data of graph_desc_info fail!"); return;);
  460. }
  461. #endif
  462. }
  463. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::PluginUnInit(const std::string &module) const {
  464. #ifdef DAVINCI_SUPPORT_PROFILING
  465. int ret = Msprof::Engine::UnInit(module);
  466. if (ret != SUCCESS) {
  467. GELOGE(ret, "profiling plugin uninit failed, ret:%d", ret);
  468. }
  469. #endif
  470. }
  471. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ReportProfilingData(
  472. const std::vector<TaskDescInfo> &task_desc_info, const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info) {
  473. #ifdef DAVINCI_SUPPORT_PROFILING
  474. int32_t logic_device_id = 0;
  475. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  476. if (rt_ret != RT_ERROR_NONE) {
  477. GELOGE(rt_ret, "runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  478. return;
  479. }
  480. GELOGI("current logic_device_id:%d", logic_device_id);
  481. uint32_t phy_device_id = 0;
  482. rt_ret = rtGetDevicePhyIdByIndex((uint32_t)logic_device_id, &phy_device_id);
  483. if (rt_ret != RT_ERROR_NONE) {
  484. GELOGE(rt_ret, "runtime get phy_device_id failed, current phy_device_id:%d", phy_device_id);
  485. return;
  486. }
  487. GELOGI("current phy_device_id:%d", phy_device_id);
  488. auto ret = std::find(device_id_.begin(), device_id_.end(), phy_device_id);
  489. if (ret == device_id_.end()) {
  490. GELOGE(FAILED, "get valid phy_device_id failed, profiling report failed.");
  491. return;
  492. }
  493. GELOGI("start ProfilingTaskDescInfo.");
  494. ProfilingTaskDescInfo(task_desc_info, phy_device_id);
  495. GELOGI("start ProfilingGraphDescInfo.");
  496. ProfilingGraphDescInfo(compute_graph_desc_info, phy_device_id);
  497. GELOGI("Report profiling data for GE end.");
  498. #endif
  499. }
  500. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::SetProfilingConfig(
  501. const std::string &profiling_cfg) {
  502. recv_profiling_config_ = profiling_cfg;
  503. }
  504. /**
  505. * @brief Profiling PluginImpl
  506. */
  507. // PluginImpl static variable init
  508. Msprof::Engine::Reporter *PluginImpl::reporter_ = nullptr;
  509. PluginImpl::PluginImpl(const std::string &module) : module_(module) { GELOGI("Create PluginImpl\n"); }
  510. int PluginImpl::Init(const Msprof::Engine::Reporter *reporter) {
  511. GELOGI("PluginImpl init");
  512. reporter_ = const_cast<Msprof::Engine::Reporter *>(reporter);
  513. return 0;
  514. }
  515. int PluginImpl::UnInit() {
  516. GELOGI("PluginImpl Uninit");
  517. reporter_ = nullptr;
  518. return 0;
  519. }
  520. Msprof::Engine::PluginIntf *ProfilingEngineImpl::CreatePlugin() {
  521. GELOGI(" Create Plugin");
  522. return new (std::nothrow) PluginImpl(GE_PROFILING_MODULE);
  523. }
  524. int ProfilingEngineImpl::ReleasePlugin(Msprof::Engine::PluginIntf *plugin) {
  525. if (plugin != nullptr) {
  526. delete plugin;
  527. plugin = nullptr;
  528. }
  529. return 0;
  530. }
  531. } // namespace ge

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