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

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