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 33 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. const int32_t kMaxDeviceNum = 256;
  35. const std::string kConfigNumsdev = "devNums";
  36. const std::string kConfigDevIdList = "devIdList";
  37. const std::string kProfStart = "prof_start";
  38. const std::string kProfStop = "prof_stop";
  39. } // namespace
  40. namespace ge {
  41. ProfilingManager::ProfilingManager() {}
  42. ProfilingManager::~ProfilingManager() {}
  43. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ProfilingManager &ProfilingManager::Instance() {
  44. static ProfilingManager profiling_manager;
  45. return profiling_manager;
  46. }
  47. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::Init(const Options &options) {
  48. #ifdef DAVINCI_SUPPORT_PROFILING
  49. vector<int32_t>().swap(device_id_);
  50. job_id_ = options.job_id;
  51. GELOGI("ProfilingManager::Init job_id:%s", job_id_.c_str());
  52. Status ret;
  53. if (!recv_profiling_config_.empty()) {
  54. GELOGI("Profiling json config from acl:%s", recv_profiling_config_.c_str());
  55. ret = InitFromAclCfg(recv_profiling_config_);
  56. } else {
  57. ret = InitFromOptions(options);
  58. if (ret == SUCCESS && is_load_profiling_) {
  59. device_id_.push_back(options.device_id);
  60. }
  61. }
  62. if (ret != SUCCESS) {
  63. GELOGE(ret, "Failed to init profiling.");
  64. return ret;
  65. }
  66. if (is_load_profiling_) {
  67. // register Framework to profiling
  68. int result = Msprof::Engine::Init(GE_PROFILING_MODULE, &engine_);
  69. if (result != 0) {
  70. GELOGE(FAILED, "Register profiling engine failed.");
  71. return FAILED;
  72. }
  73. // profiling startup first time
  74. GELOGI("Begin to init profiling, device num %zu", device_id_.size());
  75. for (size_t i = 0; i < device_id_.size(); ++i) {
  76. ret = StartProfiling(0, device_id_[i]);
  77. if (ret != SUCCESS) {
  78. GELOGW("Profiling start failed on device %d.", device_id_[i]);
  79. continue;
  80. }
  81. GELOGI("Profiling init succ on device %d.", device_id_[i]);
  82. }
  83. } else {
  84. GELOGI("The profiling is off, skip the initialization");
  85. }
  86. #endif
  87. return SUCCESS;
  88. }
  89. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::InitFromAclCfg(
  90. const std::string &config) {
  91. #ifdef DAVINCI_SUPPORT_PROFILING
  92. try {
  93. is_load_profiling_ = false;
  94. is_execute_profiling_ = false;
  95. profiling_opts_.clear();
  96. op_trace_conf_.clear();
  97. Json start_prof_conf = Json::parse(config);
  98. Json &prof_conf = start_prof_conf[kStartCfg][0];
  99. job_id_ = prof_conf[kJobID];
  100. auto iter = prof_conf.find(kProfDir);
  101. if (iter != prof_conf.end()) {
  102. prof_dir_ = prof_conf[kProfDir];
  103. }
  104. Json &device_id = prof_conf[kDeviceID];
  105. if (device_id.size() != 0) {
  106. vector<int32_t>().swap(device_id_);
  107. bool is_all = false;
  108. for (size_t i = 0; i < device_id.size(); i++) {
  109. std::string device_id_str = device_id[i].get<std::string>();
  110. if (device_id_str == "all") {
  111. is_all = true;
  112. break;
  113. }
  114. device_id_.push_back(std::stoi(device_id_str));
  115. }
  116. if (is_all) {
  117. int32_t count = 0;
  118. rtError_t rt_err = rtGetDeviceCount(&count);
  119. if (rt_err != RT_ERROR_NONE) {
  120. GELOGE(FAILED, "Call rtGetDeviceCount to get device failed.");
  121. }
  122. vector<int32_t>().swap(device_id_);
  123. for (int32_t i = 0; i < count; ++i) {
  124. device_id_.push_back(i);
  125. }
  126. }
  127. }
  128. Json &features = prof_conf[kFeatures];
  129. if (ParseFeaturesFromAclCfg(features) != SUCCESS) {
  130. GELOGE(FAILED, "Parse feature from acl cfg failed.");
  131. return FAILED;
  132. }
  133. is_load_profiling_ = true;
  134. is_execute_profiling_ = true;
  135. } catch (...) {
  136. GELOGE(FAILED, "Json conf is not invalid !");
  137. return ge::PARAM_INVALID;
  138. }
  139. #endif
  140. return ge::SUCCESS;
  141. }
  142. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::ParseFeaturesFromAclCfg(
  143. const Json &features) {
  144. #ifdef DAVINCI_SUPPORT_PROFILING
  145. try {
  146. for (size_t i = 0; i < features.size(); ++i) {
  147. const Json &feature = features[i];
  148. if ((feature.find(kName) == feature.end()) || feature[kName].is_null()) {
  149. continue;
  150. }
  151. const std::string &name = feature[kName];
  152. if (name == "op_trace") {
  153. const Json &conf = feature[kConf];
  154. const Json &events = conf[0][kEvents];
  155. const std::string &ai_core_events = events[0][kAiCoreEvents];
  156. GELOGI("Op trace config from acl ai_core_events:%s", ai_core_events.c_str());
  157. is_op_trace_ = true;
  158. ProfMgrConf prof_mgr_conf;
  159. int result = ProfMgrGetConf(ai_core_events, &prof_mgr_conf);
  160. if (result != 0) {
  161. GELOGE(FAILED, "ProfMgrGetConf failed.");
  162. return FAILED;
  163. }
  164. op_trace_conf_ = prof_mgr_conf.conf;
  165. op_trace_iter_num_ = static_cast<int32_t>(op_trace_conf_.size());
  166. GELOGI("Op trace profiling iter num %d,", op_trace_iter_num_);
  167. } else if (name == "task_trace") {
  168. is_op_trace_ = false;
  169. if (feature.find(kConf) != feature.end()) {
  170. const Json &conf = feature[kConf];
  171. std::stringstream task_trace_conf;
  172. task_trace_conf << conf;
  173. task_trace_conf_ = task_trace_conf.str();
  174. }
  175. GELOGI("Task trace config from acl");
  176. } else if (name == "system_trace") {
  177. is_op_trace_ = false;
  178. const Json &conf = feature[kConf];
  179. std::stringstream system_trace_conf;
  180. system_trace_conf << conf;
  181. system_trace_conf_ = system_trace_conf.str();
  182. GELOGI("System trace config from acl");
  183. }
  184. profiling_opts_.push_back(name);
  185. }
  186. } catch (...) {
  187. GELOGE(ge::PARAM_INVALID, "Json conf feature is not invalid !");
  188. return ge::PARAM_INVALID;
  189. }
  190. #endif
  191. return ge::SUCCESS;
  192. }
  193. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::InitFromOptions(const Options &options) {
  194. #ifdef DAVINCI_SUPPORT_PROFILING
  195. // enable profiling support two ways: env and front end
  196. const char *profiling_mode = std::getenv("PROFILING_MODE");
  197. const char *prof_options = std::getenv("PROFILING_OPTIONS");
  198. if ((profiling_mode == nullptr) || (strcmp("true", profiling_mode) != 0) || (prof_options == nullptr)) {
  199. is_load_profiling_ = false;
  200. is_execute_profiling_ = false;
  201. } else {
  202. std::string prof_options_str = std::string(prof_options);
  203. profiling_opts_ = StringUtils::Split(prof_options_str, ':');
  204. is_load_profiling_ = true;
  205. is_execute_profiling_ = true;
  206. GELOGI("The profiling in env is %s, %s", profiling_mode, prof_options);
  207. }
  208. if (!is_load_profiling_) {
  209. const std::string enable_profiling = "1";
  210. if (options.profiling_mode != enable_profiling || options.profiling_options.empty()) {
  211. is_load_profiling_ = false;
  212. is_execute_profiling_ = false;
  213. return SUCCESS;
  214. } else {
  215. profiling_opts_ = StringUtils::Split(options.profiling_options, ':');
  216. is_load_profiling_ = true;
  217. is_execute_profiling_ = true;
  218. GELOGI("The profiling in options is %s, %s", options.profiling_mode.c_str(), options.profiling_options.c_str());
  219. }
  220. }
  221. // features:'training_trace', 'task_trace' or 'op_trace' etc
  222. if (!profiling_opts_.empty()) {
  223. if (profiling_opts_[0] == "op_trace") {
  224. is_op_trace_ = true;
  225. // op trace get conf
  226. ProfMgrConf prof_mgr_conf;
  227. int result = ProfMgrGetConf("", &prof_mgr_conf);
  228. if (result != 0) {
  229. GELOGE(FAILED, "ProfMgrGetConf failed.");
  230. return FAILED;
  231. }
  232. op_trace_conf_ = prof_mgr_conf.conf;
  233. op_trace_iter_num_ = static_cast<int32_t>(op_trace_conf_.size());
  234. GELOGI("op trace profiling iter num %d,", op_trace_iter_num_);
  235. } else {
  236. is_op_trace_ = false;
  237. op_trace_iter_num_ = 1;
  238. }
  239. }
  240. #endif
  241. return ge::SUCCESS;
  242. }
  243. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::StartProfiling(int32_t iter_num,
  244. int32_t device_id) {
  245. #ifdef DAVINCI_SUPPORT_PROFILING
  246. if (!profiling_opts_.empty()) {
  247. GELOGI("Start profiling index is %d", iter_num);
  248. // current one docker only use one device
  249. Json p_device;
  250. try {
  251. // profiling need physical_device_id
  252. p_device[kDeviceID] = std::to_string(device_id);
  253. p_device[kJobID] = job_id_;
  254. p_device[kTraceID] = std::to_string(GetContext().TraceId());
  255. if (!prof_dir_.empty()) {
  256. p_device[kProfDir] = prof_dir_;
  257. GELOGI("Prof dir: %s.", prof_dir_.c_str());
  258. }
  259. Json features;
  260. if (is_op_trace_) {
  261. Json f;
  262. f[kName] = "op_trace";
  263. Json conf;
  264. if (op_trace_conf_.size() <= static_cast<size_t>(iter_num)) {
  265. GELOGE(FAILED, "Op trace iter num is invalid!");
  266. return FAILED;
  267. }
  268. Json events;
  269. events[0] = nlohmann::json::parse(op_trace_conf_[iter_num]);
  270. conf[0][kEvents] = events;
  271. f[kConf] = conf;
  272. features[0] = f;
  273. if (iter_num == 0) {
  274. is_load_ = true;
  275. }
  276. } else {
  277. for (std::vector<std::string>::size_type i = 0; i < profiling_opts_.size(); i++) {
  278. Json f;
  279. if (profiling_opts_[i] == "system_trace") {
  280. f[kConf] = nlohmann::json::parse(system_trace_conf_);
  281. } else if (profiling_opts_[i] == "task_trace") {
  282. if (!task_trace_conf_.empty()) {
  283. f[kConf] = nlohmann::json::parse(task_trace_conf_);
  284. }
  285. }
  286. f[kName] = profiling_opts_[i];
  287. features[i] = f;
  288. }
  289. is_load_ = true;
  290. }
  291. p_device[kFeatures] = features;
  292. // only one device, but sProfMgrStartUp API require for device list
  293. Json devices;
  294. devices[0] = p_device;
  295. Json start_cfg;
  296. start_cfg[kStartCfg] = devices;
  297. // convert json to string
  298. std::stringstream ss;
  299. ss << start_cfg;
  300. send_profiling_config_ = ss.str();
  301. GELOGI("Profiling config %s\n", send_profiling_config_.c_str());
  302. } catch (...) {
  303. GELOGE(FAILED, "Op trace json conf is not invalid !");
  304. return FAILED;
  305. }
  306. // runtime startup for profiling
  307. uint64_t module = GetProfilingModule();
  308. int32_t device_num = 1;
  309. uint32_t device_id_rt = static_cast<uint32_t>(device_id);
  310. GE_CHK_RT_RET(rtProfilerStart(module, device_num, &device_id_rt));
  311. // call profiling startup API
  312. ProfMgrCfg prof_cfg = {send_profiling_config_};
  313. void *prof_handle = ProfMgrStartUp(&prof_cfg);
  314. if (prof_handle == nullptr) {
  315. GELOGW("ProfMgrStartUp failed on device %d ", device_id);
  316. return FAILED;
  317. }
  318. GELOGD("StartProfiling, prof_handle: %p", prof_handle);
  319. prof_handle_vec_.push_back(prof_handle);
  320. }
  321. #endif
  322. return SUCCESS;
  323. }
  324. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::StopProfiling() {
  325. #ifdef DAVINCI_SUPPORT_PROFILING
  326. Msprof::Engine::Reporter *reporter = PluginImpl::GetPluginReporter();
  327. if (reporter != nullptr) {
  328. int ret = reporter->Flush();
  329. GELOGI("Report data end, ret is %d", ret);
  330. }
  331. uint64_t module = GetProfilingModule();
  332. int32_t device_num = static_cast<int32_t>(device_id_.size());
  333. uint32_t *device_id_ptr = new (std::nothrow) uint32_t[device_num];
  334. if (device_id_ptr == nullptr) {
  335. GELOGE(FAILED, "Stop profiling device id ptr is null.");
  336. return;
  337. }
  338. for (int32_t i = 0; i < device_num; i++) {
  339. device_id_ptr[i] = static_cast<uint32_t>(device_id_[i]);
  340. }
  341. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr);
  342. if (rt_ret != RT_ERROR_NONE) {
  343. GELOGW("Call rtProfilerStop failed, ret:%d", rt_ret);
  344. }
  345. delete[] device_id_ptr;
  346. device_id_ptr = nullptr;
  347. for (size_t i = 0; i < prof_handle_vec_.size(); ++i) {
  348. int result = ProfMgrStop(prof_handle_vec_[i]);
  349. if (result != 0) {
  350. GELOGW("ProfMgr stop return fail:%d, handle:%p", result, prof_handle_vec_[i]);
  351. }
  352. }
  353. vector<void *>().swap(prof_handle_vec_);
  354. is_load_ = false;
  355. recv_profiling_config_ = "";
  356. GELOGI("Stop Profiling success.");
  357. #endif
  358. }
  359. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingTaskDescInfo(
  360. const std::vector<TaskDescInfo> &task_desc_info, const int32_t &device_id) {
  361. #ifdef DAVINCI_SUPPORT_PROFILING
  362. Msprof::Engine::Reporter *reporter = PluginImpl::GetPluginReporter();
  363. if (reporter == nullptr) {
  364. GELOGI("Profiling report is nullptr!");
  365. return;
  366. }
  367. std::string data;
  368. for (const auto &task : task_desc_info) {
  369. std::string model_name = task.model_name;
  370. std::string op_name = task.op_name;
  371. uint32_t block_dim = task.block_dim;
  372. uint32_t task_id = task.task_id;
  373. uint32_t stream_id = task.stream_id;
  374. data = model_name.append(" ").append(op_name).append(" ").append(std::to_string(block_dim)
  375. .append(" ")
  376. .append(std::to_string(task_id))
  377. .append(" ")
  378. .append(std::to_string(stream_id))
  379. .append("\n"));
  380. Msprof::Engine::ReporterData reporter_data{};
  381. reporter_data.deviceId = device_id;
  382. reporter_data.data = (unsigned char *)data.c_str();
  383. reporter_data.dataLen = data.size();
  384. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "task_desc_info", sizeof("task_desc_info"));
  385. if (ret != EOK) {
  386. GELOGE(ret, "Report data tag of task_desc_info memcpy error!");
  387. return;
  388. }
  389. ret = reporter->Report(&reporter_data);
  390. if (ret != SUCCESS) {
  391. GELOGE(ret, "Reporter data of task_desc_info fail!");
  392. return;
  393. }
  394. }
  395. data.clear();
  396. #endif
  397. }
  398. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingGraphDescInfo(
  399. const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info, const int32_t &device_id) {
  400. #ifdef DAVINCI_SUPPORT_PROFILING
  401. Msprof::Engine::Reporter *reporter = PluginImpl::GetPluginReporter();
  402. GE_IF_BOOL_EXEC(reporter == nullptr, GELOGI("Profiling report is nullptr!"); return;);
  403. std::string data;
  404. for (const auto &graph : compute_graph_desc_info) {
  405. data.append("model_name:")
  406. .append(graph.model_name)
  407. .append(" op_name:")
  408. .append(graph.op_name)
  409. .append(" op_type:")
  410. .append(graph.op_type);
  411. for (size_t i = 0; i < graph.input_format.size(); ++i) {
  412. data.append(" input_id:")
  413. .append(std::to_string(i))
  414. .append(" input_format:")
  415. .append(std::to_string(graph.input_format.at(i)))
  416. .append(" input_data_type:")
  417. .append(std::to_string(graph.input_data_type.at(i)))
  418. .append(" input_shape:\"");
  419. size_t input_shape_len = graph.input_shape.at(i).size();
  420. if (input_shape_len == 0) {
  421. data.append("");
  422. } else if (input_shape_len == 1) {
  423. data.append(std::to_string(graph.input_shape.at(i).at(0)));
  424. } else {
  425. for (size_t j = 0; j < input_shape_len - 1; ++j) {
  426. data.append(std::to_string(graph.input_shape.at(i).at(j))).append(",");
  427. }
  428. data.append(std::to_string(graph.input_shape.at(i).at(input_shape_len - 1)));
  429. }
  430. data.append("\"");
  431. }
  432. for (size_t i = 0; i < graph.output_format.size(); ++i) {
  433. data.append(" output_id:")
  434. .append(std::to_string(i))
  435. .append(" output_format:")
  436. .append(std::to_string(graph.output_format.at(i)))
  437. .append(" output_data_type:")
  438. .append(std::to_string(graph.output_data_type.at(i)))
  439. .append(" output_shape:\"");
  440. size_t output_shape_len = graph.output_shape.at(i).size();
  441. if (output_shape_len == 0) {
  442. data.append("");
  443. } else if (output_shape_len == 1) {
  444. data.append(std::to_string(graph.output_shape.at(i).at(0)));
  445. } else {
  446. for (size_t j = 0; j < output_shape_len - 1; ++j) {
  447. data.append(std::to_string(graph.output_shape.at(i).at(j))).append(",");
  448. }
  449. data.append(std::to_string(graph.output_shape.at(i).at(output_shape_len - 1)));
  450. }
  451. data.append("\"");
  452. }
  453. data.append("\n");
  454. Msprof::Engine::ReporterData reporter_data{};
  455. Report(device_id, data, *reporter, reporter_data);
  456. data.clear();
  457. }
  458. #endif
  459. }
  460. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::Report(
  461. const int32_t &device_id, const string &data, Msprof::Engine::Reporter &reporter,
  462. Msprof::Engine::ReporterData &reporter_data) {
  463. #ifdef DAVINCI_SUPPORT_PROFILING
  464. size_t index = data.size() / kReportMaxLen;
  465. if (index >= 1) {
  466. reporter_data.deviceId = device_id;
  467. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  468. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  469. for (size_t i = 0; i < index; ++i) {
  470. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * i;
  471. reporter_data.dataLen = kReportMaxLen;
  472. ret = reporter.Report(&reporter_data);
  473. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "Reporter data of graph_desc_info fail!"); return;);
  474. }
  475. reporter_data.dataLen = data.size() - kReportMaxLen * index;
  476. if (reporter_data.dataLen != 0) {
  477. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * index;
  478. ret = reporter.Report(&reporter_data);
  479. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "Reporter data of graph_desc_info fail!"); return;);
  480. }
  481. } else {
  482. reporter_data.deviceId = device_id;
  483. reporter_data.data = (unsigned char *)data.c_str();
  484. reporter_data.dataLen = data.size();
  485. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  486. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  487. ret = reporter.Report(&reporter_data);
  488. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "Reporter data of graph_desc_info fail!"); return;);
  489. }
  490. #endif
  491. }
  492. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::PluginUnInit(const std::string &module) const {
  493. #ifdef DAVINCI_SUPPORT_PROFILING
  494. int ret = Msprof::Engine::UnInit(module);
  495. if (ret != SUCCESS) {
  496. GELOGE(ret, "profiling plugin uninit failed, ret:%d", ret);
  497. }
  498. #endif
  499. }
  500. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ReportProfilingData(
  501. const std::vector<TaskDescInfo> &task_desc_info, const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info) {
  502. #ifdef DAVINCI_SUPPORT_PROFILING
  503. int32_t logic_device_id = 0;
  504. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  505. if (rt_ret != RT_ERROR_NONE) {
  506. GELOGE(rt_ret, "runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  507. return;
  508. }
  509. GELOGI("current logic_device_id:%d", logic_device_id);
  510. if (!is_acl_api_mode_) {
  511. auto ret = std::find(device_id_.begin(), device_id_.end(), logic_device_id);
  512. if (ret == device_id_.end()) {
  513. GELOGE(FAILED, "get valid phy_device_id failed, profiling report failed.");
  514. return;
  515. }
  516. }
  517. GELOGI("start ProfilingTaskDescInfo.");
  518. ProfilingTaskDescInfo(task_desc_info, logic_device_id);
  519. GELOGI("start ProfilingGraphDescInfo.");
  520. ProfilingGraphDescInfo(compute_graph_desc_info, logic_device_id);
  521. GELOGI("Report profiling data for GE end.");
  522. #endif
  523. }
  524. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::SetProfilingConfig(
  525. const std::string &profiling_cfg) {
  526. recv_profiling_config_ = profiling_cfg;
  527. }
  528. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint64_t ProfilingManager::GetProfilingModule() {
  529. uint64_t module = PROF_MODEL_EXECUTE_MASK | PROF_RUNTIME_API_MASK | PROF_RUNTIME_TRACE_MASK |
  530. PROF_SCHEDULE_TIMELINE_MASK | PROF_SCHEDULE_TRACE_MASK | PROF_TASK_TIME_MASK |
  531. PROF_SUBTASK_TIME_MASK | PROF_AICPU_TRACE_MASK | PROF_AICORE_METRICS_MASK |
  532. PROF_AIVECTORCORE_METRICS_MASK | PROF_MODEL_LOAD_MASK;
  533. return module;
  534. }
  535. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfInit(uint64_t module) {
  536. #ifdef DAVINCI_SUPPORT_PROFILING
  537. std::lock_guard<std::mutex> lock(mutex_);
  538. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  539. if (model_load_mask == PROF_MODEL_LOAD_MASK) {
  540. // register Framework to profiling
  541. int32_t result = Msprof::Engine::Init(GE_PROFILING_MODULE, &engine_);
  542. if (result != SUCCESS) {
  543. GELOGE(FAILED, "Register profiling engine failed.");
  544. return FAILED;
  545. }
  546. int32_t device_num = -1;
  547. rtError_t rt_ret = rtProfilerStart(model_load_mask, device_num, nullptr);
  548. if (rt_ret != RT_ERROR_NONE) {
  549. GELOGE(FAILED, "Runtime profiler start failed.");
  550. return FAILED;
  551. }
  552. is_load_profiling_ = true;
  553. GELOGI("Prof init: model load profiling on.");
  554. }
  555. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE_MASK;
  556. if (training_trace_mask == PROF_TRAINING_TRACE_MASK) {
  557. is_training_trace_ = true;
  558. }
  559. is_acl_api_mode_ = true;
  560. GELOGI("Prof init success.");
  561. #endif
  562. return SUCCESS;
  563. }
  564. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfFinalize() {
  565. #ifdef DAVINCI_SUPPORT_PROFILING
  566. std::lock_guard<std::mutex> lock(mutex_);
  567. is_load_profiling_ = false;
  568. is_training_trace_ = false;
  569. is_acl_api_mode_ = false;
  570. int32_t ret = Msprof::Engine::UnInit(GE_PROFILING_MODULE);
  571. if (ret != SUCCESS) {
  572. GELOGE(ret, "Profiling plugin uninit failed, ret:%d", ret);
  573. }
  574. int32_t dev_num = -1;
  575. rtError_t rt_ret = rtProfilerStop(PROF_MODEL_LOAD_MASK, dev_num, nullptr);
  576. if (rt_ret != RT_ERROR_NONE) {
  577. GELOGE(FAILED, "Runtime profiler stop failed.");
  578. return FAILED;
  579. }
  580. for (auto device_id_module : device_id_module_map_) {
  581. if (device_id_module.second != 0) {
  582. uint32_t device_id = static_cast<uint32_t>(device_id_module.first);
  583. GELOGI("Prof finalize: device_id: %u, module: 0x%llx.", device_id, device_id_module.second);
  584. rt_ret = rtProfilerStop(device_id_module.second, 1, &device_id);
  585. if (rt_ret != RT_ERROR_NONE) {
  586. GELOGE(FAILED, "Runtime profiler stop failed.");
  587. return FAILED;
  588. }
  589. }
  590. }
  591. device_id_module_map_.clear();
  592. device_id_.clear();
  593. GELOGI("Prof finalize success.");
  594. #endif
  595. return SUCCESS;
  596. }
  597. Status ProfilingManager::ProfParseDeviceId(const std::map<std::string, std::string> &config_para,
  598. vector<int32_t> &device_list) {
  599. #ifdef DAVINCI_SUPPORT_PROFILING
  600. auto iter = config_para.find(kConfigDevIdList);
  601. if (iter != config_para.end()) {
  602. std::string device_id_list = iter->second;
  603. std::string temp;
  604. vector<std::string> decvice_id;
  605. for (uint32_t i = 0; i < device_id_list.size(); i++) {
  606. if (isdigit(device_id_list[i])) {
  607. temp.append(1, device_id_list[i]);
  608. } else {
  609. if (!temp.empty()) {
  610. decvice_id.emplace_back(temp);
  611. }
  612. temp.clear();
  613. }
  614. }
  615. if (!temp.empty()) {
  616. decvice_id.emplace_back(temp);
  617. }
  618. for (uint32_t i = 0; i < decvice_id.size(); i++) {
  619. try {
  620. int32_t dev_id = std::stoi(decvice_id[i]);
  621. device_list.push_back(dev_id);
  622. } catch (std::invalid_argument &) {
  623. GELOGE(FAILED, "Device id: %s is invalid.", decvice_id[i].c_str());
  624. return FAILED;
  625. } catch (std::out_of_range &) {
  626. GELOGE(FAILED, "Device id: %s is out of range.", decvice_id[i].c_str());
  627. } catch (...) {
  628. GELOGE(FAILED, "Device id: %s cannot change to int.", decvice_id[i].c_str());
  629. return FAILED;
  630. }
  631. }
  632. } else {
  633. GELOGE(FAILED, "Config para not contain device id list.");
  634. return FAILED;
  635. }
  636. #endif
  637. return SUCCESS;
  638. }
  639. Status ProfilingManager::ProfParseParam(const std::map<std::string, std::string> &config_para, int32_t &device_num,
  640. vector<int32_t> &device_list) {
  641. #ifdef DAVINCI_SUPPORT_PROFILING
  642. // device num
  643. auto iter = config_para.find(kConfigNumsdev);
  644. if (iter != config_para.end()) {
  645. try {
  646. device_num = std::stoi(iter->second);
  647. } catch (std::invalid_argument &) {
  648. GELOGE(FAILED, "Device nun: %s is invalid.", iter->second.c_str());
  649. return FAILED;
  650. } catch (std::out_of_range &) {
  651. GELOGE(FAILED, "Device num: %s is out of range.", iter->second.c_str());
  652. } catch (...) {
  653. GELOGE(FAILED, "Device num: %s cannot change to int.", iter->second.c_str());
  654. return FAILED;
  655. }
  656. } else {
  657. GELOGE(FAILED, "Config para not contain device num.");
  658. return FAILED;
  659. }
  660. // device id
  661. if (ProfParseDeviceId(config_para, device_list) != SUCCESS) {
  662. GELOGE(FAILED, "Parse config para device id failed.");
  663. return FAILED;
  664. }
  665. if (device_num == 0 || device_num > kMaxDeviceNum || device_num != static_cast<int32_t>(device_list.size())) {
  666. GELOGE(FAILED, "Config para device num: %d not equal to device list size: %d.", device_num, device_list.size());
  667. return FAILED;
  668. }
  669. #endif
  670. return SUCCESS;
  671. }
  672. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status
  673. ProfilingManager::ProfStartProfiling(uint64_t module, const std::map<std::string, std::string> &config_para) {
  674. #ifdef DAVINCI_SUPPORT_PROFILING
  675. std::lock_guard<std::mutex> lock(mutex_);
  676. int32_t device_num = 0;
  677. vector<int32_t> device_list;
  678. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  679. GELOGE(FAILED, "Prof start parse param failed.");
  680. return FAILED;
  681. }
  682. auto *device_id = new (std::nothrow) uint32_t[device_num];
  683. if (device_id == nullptr) {
  684. GELOGE(FAILED, "Prof start parse param failed.");
  685. return FAILED;
  686. }
  687. for (int32_t i = 0; i < device_num; i++) {
  688. device_id[i] = static_cast<uint32_t>(device_list[i]);
  689. }
  690. GELOGI("Runtime config param: 0x%llx, device num: %d.", module, device_num);
  691. rtError_t rt_ret = rtProfilerStart(module, device_num, device_id);
  692. if (rt_ret != RT_ERROR_NONE) {
  693. delete[] device_id;
  694. GELOGE(FAILED, "Runtime profiler config proc failed.");
  695. return FAILED;
  696. }
  697. delete[] device_id;
  698. device_id = nullptr;
  699. if ((module & PROF_MODEL_EXECUTE_MASK) == PROF_MODEL_EXECUTE_MASK) {
  700. for (int32_t i = 0; i < device_num; i++) {
  701. if (std::find(device_id_.begin(), device_id_.end(), device_list[i]) == device_id_.end()) {
  702. device_id_.push_back(device_list[i]);
  703. }
  704. }
  705. GELOGI("Prof start: ge execute model start profiling.");
  706. }
  707. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  708. GELOGW("Prof start: load model module is invalid.");
  709. }
  710. UpdateDeviceIdModuleMap(kProfStart, module, device_list);
  711. GELOGI("Prof start profiling success.");
  712. #endif
  713. return SUCCESS;
  714. }
  715. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status
  716. ProfilingManager::ProfStopProfiling(uint64_t module, const std::map<std::string, std::string> &config_para) {
  717. #ifdef DAVINCI_SUPPORT_PROFILING
  718. std::lock_guard<std::mutex> lock(mutex_);
  719. int32_t device_num = 0;
  720. vector<int32_t> device_list;
  721. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  722. GELOGE(FAILED, "Prof stop parse param failed.");
  723. return FAILED;
  724. }
  725. auto *device_id = new (std::nothrow) uint32_t[device_num];
  726. if (device_id == nullptr) {
  727. GELOGE(FAILED, "Prof stop parse param failed.");
  728. return FAILED;
  729. }
  730. for (int32_t i = 0; i < device_num; i++) {
  731. device_id[i] = static_cast<uint32_t>(device_list[i]);
  732. }
  733. GELOGI("Prof stop: runtime config param: 0x%llx, device num: %d", module, device_num);
  734. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id);
  735. if (rt_ret != RT_ERROR_NONE) {
  736. delete[] device_id;
  737. GELOGE(FAILED, "Prof stop: runtime profiler config proc failed.");
  738. return FAILED;
  739. }
  740. delete[] device_id;
  741. device_id = nullptr;
  742. uint64_t execute_model_mask = module & PROF_MODEL_EXECUTE_MASK;
  743. if (execute_model_mask == PROF_MODEL_EXECUTE_MASK) {
  744. for (int32_t i = 0; i < device_num; i++) {
  745. auto iter = std::find(device_id_.begin(), device_id_.end(), device_list[i]);
  746. if (iter != device_id_.end()) {
  747. device_id_.erase(iter);
  748. }
  749. }
  750. GELOGI("Prof stop: ge execute model stop profiling.");
  751. }
  752. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  753. GELOGW("Prof stop: load model module is invalid.");
  754. }
  755. UpdateDeviceIdModuleMap(kProfStop, module, device_list);
  756. GELOGI("Prof stop profiling success.");
  757. #endif
  758. return SUCCESS;
  759. }
  760. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::UpdateDeviceIdModuleMap(
  761. string prof_type, uint64_t module, const vector<int32_t> &device_list) {
  762. #ifdef DAVINCI_SUPPORT_PROFILING
  763. if (prof_type == kProfStart) {
  764. for (uint32_t i = 0; i < device_list.size(); i++) {
  765. auto iter = device_id_module_map_.find(device_list[i]);
  766. if (iter != device_id_module_map_.end()) {
  767. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  768. // save all profiling on module of device
  769. device_id_module_map_[device_list[i]] = prof_on_module | module;
  770. } else {
  771. device_id_module_map_[device_list[i]] = module;
  772. }
  773. }
  774. } else if (prof_type == kProfStop) {
  775. for (uint32_t i = 0; i < device_list.size(); i++) {
  776. auto iter = device_id_module_map_.find(device_list[i]);
  777. if (iter != device_id_module_map_.end()) {
  778. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  779. uint64_t prof_off_module = prof_on_module & module;
  780. uint64_t prof_on_left_module = prof_on_module & (~prof_off_module);
  781. // stop profiling on module of device
  782. device_id_module_map_[device_list[i]] = prof_on_left_module;
  783. }
  784. }
  785. } else {
  786. GELOGI("No need to update device_id module map.");
  787. }
  788. #endif
  789. }
  790. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ProfilingManager::ProfilingModelExecuteOn() const {
  791. int32_t logic_device_id = 0;
  792. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  793. if (rt_ret != RT_ERROR_NONE) {
  794. GELOGE(rt_ret, "Runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  795. }
  796. GELOGI("Current logic_device_id:%d", logic_device_id);
  797. bool execute_model_prof_on = false;
  798. auto iter = std::find(device_id_.begin(), device_id_.end(), logic_device_id);
  799. if (iter != device_id_.end()) {
  800. execute_model_prof_on = true;
  801. }
  802. GELOGI("Flag is_execute_profiling: %d, execute_model_prof_on: %d", is_execute_profiling_, execute_model_prof_on);
  803. return is_execute_profiling_ || execute_model_prof_on;
  804. }
  805. /**
  806. * @brief Profiling PluginImpl
  807. */
  808. // PluginImpl static variable init
  809. Msprof::Engine::Reporter *PluginImpl::reporter_ = nullptr;
  810. PluginImpl::PluginImpl(const std::string &module) : module_(module) { GELOGI("Create PluginImpl\n"); }
  811. int PluginImpl::Init(const Msprof::Engine::Reporter *reporter) {
  812. GELOGI("PluginImpl init");
  813. reporter_ = const_cast<Msprof::Engine::Reporter *>(reporter);
  814. return 0;
  815. }
  816. int PluginImpl::UnInit() {
  817. GELOGI("PluginImpl Uninit");
  818. reporter_ = nullptr;
  819. return 0;
  820. }
  821. Msprof::Engine::PluginIntf *ProfilingEngineImpl::CreatePlugin() {
  822. GELOGI(" Create Plugin");
  823. return new (std::nothrow) PluginImpl(GE_PROFILING_MODULE);
  824. }
  825. int ProfilingEngineImpl::ReleasePlugin(Msprof::Engine::PluginIntf *plugin) {
  826. if (plugin != nullptr) {
  827. delete plugin;
  828. plugin = nullptr;
  829. }
  830. return 0;
  831. }
  832. } // namespace ge

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