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 32 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "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. #include "graph/load/new_model_manager/davinci_model.h"
  23. namespace {
  24. const char *const kTrainingTrace = "training_trace";
  25. const char *const kFpPoint = "fp_point";
  26. const char *const kBpPoint = "bp_point";
  27. const size_t kReportMaxLen = 2048;
  28. const int32_t kMaxDeviceNum = 256;
  29. const std::string kConfigNumsdev = "devNums";
  30. const std::string kConfigDevIdList = "devIdList";
  31. const std::string kProfStart = "prof_start";
  32. const std::string kProfStop = "prof_stop";
  33. const std::string kProfModelSubscribe = "prof_model_subscribe";
  34. const std::string kProfModelUnsubscribe = "prof_model_cancel_subscribe";
  35. } // namespace
  36. namespace ge {
  37. ProfilingManager::ProfilingManager()
  38. : is_load_profiling_(false), is_execute_profiling_(false), is_training_trace_(false), subscribe_count_(0) {
  39. prof_cb_.msprofCtrlCallback = nullptr;
  40. prof_cb_.msprofReporterCallback = nullptr;
  41. }
  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. subscribe_count_ = 0;
  51. GELOGI("ProfilingManager::Init job_id:%s", options.job_id.c_str());
  52. struct MsprofGeOptions prof_conf = {{ 0 }};
  53. Status ret = InitFromOptions(options, prof_conf);
  54. if (ret != SUCCESS) {
  55. GELOGE(ret, "Failed to init profiling.");
  56. return ret;
  57. }
  58. if (is_execute_profiling_) {
  59. if (prof_cb_.msprofCtrlCallback == nullptr) {
  60. GELOGE(ge::PARAM_INVALID, "MsprofCtrlCallback callback is nullptr.");
  61. return ge::PARAM_INVALID;
  62. }
  63. int32_t cb_ret = prof_cb_.msprofCtrlCallback(
  64. static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_INIT_GE_OPTIONS),
  65. static_cast<void *>(&prof_conf), sizeof(MsprofGeOptions));
  66. if (cb_ret != 0) {
  67. GELOGE(FAILED, "Call msprofCtrlCallback failed, type:%u, return:%d",
  68. static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_INIT_GE_OPTIONS), cb_ret);
  69. return FAILED;
  70. }
  71. GELOGI("Profiling init success");
  72. } else {
  73. GELOGI("The profiling is off, skip the initialization");
  74. }
  75. #endif
  76. return SUCCESS;
  77. }
  78. ge::Status ProfilingManager::InitFromOptions(const Options &options, MsprofGeOptions &prof_conf) {
  79. #ifdef DAVINCI_SUPPORT_PROFILING
  80. // enable profiling by env
  81. char env_profiling_mode[MMPA_MAX_PATH] = { 0x00 };
  82. is_load_profiling_ = false; // Change in ProfInit
  83. is_execute_profiling_ = false;
  84. if (options.profiling_mode == "1" && !options.profiling_options.empty()) {
  85. // enable profiling by ge option
  86. if (strncpy_s(prof_conf.options, MSPROF_OPTIONS_DEF_LEN_MAX, options.profiling_options.c_str(),
  87. MSPROF_OPTIONS_DEF_LEN_MAX - 1) != EOK) {
  88. GELOGE(INTERNAL_ERROR, "copy profiling_options failed.");
  89. return INTERNAL_ERROR;
  90. }
  91. is_execute_profiling_ = true;
  92. GELOGI("The profiling in options is %s, %s. origin option: %s", options.profiling_mode.c_str(), prof_conf.options,
  93. options.profiling_options.c_str());
  94. } else {
  95. (void)mmGetEnv("PROFILING_MODE", env_profiling_mode, MMPA_MAX_PATH);
  96. (void)mmGetEnv("PROFILING_OPTIONS", prof_conf.options, MSPROF_OPTIONS_DEF_LEN_MAX);
  97. // The env is invalid
  98. if ((strcmp("true", env_profiling_mode) != 0) || (strcmp(prof_conf.options, "\0") == 0)) {
  99. return SUCCESS;
  100. }
  101. // enable profiling by env
  102. is_execute_profiling_ = true;
  103. GELOGI("The profiling in env is %s, %s", env_profiling_mode, prof_conf.options);
  104. }
  105. if (!is_execute_profiling_) {
  106. return SUCCESS;
  107. }
  108. // Parse json str for bp fp
  109. Status ret = ParseOptions(prof_conf.options);
  110. if (ret != ge::SUCCESS) {
  111. GELOGE(ge::PARAM_INVALID, "Parse training trace param failed.");
  112. return ge::PARAM_INVALID;
  113. }
  114. if (strncpy_s(prof_conf.jobId, MSPROF_OPTIONS_DEF_LEN_MAX, options.job_id.c_str(),
  115. MSPROF_OPTIONS_DEF_LEN_MAX - 1) != EOK) {
  116. GELOGE(INTERNAL_ERROR, "copy job_id failed.");
  117. return INTERNAL_ERROR;
  118. }
  119. GELOGI("Job id: %s, original job id: %s.", prof_conf.jobId, options.job_id.c_str());
  120. #endif
  121. return ge::SUCCESS;
  122. }
  123. ge::Status ProfilingManager::ParseOptions(const std::string &options) {
  124. if (options.empty()) {
  125. GELOGE(ge::PARAM_INVALID, "Profiling options is empty.");
  126. return ge::PARAM_INVALID;
  127. }
  128. try {
  129. Json prof_options = Json::parse(options);
  130. if (options.find(kTrainingTrace) == std::string::npos) {
  131. return ge::SUCCESS;
  132. }
  133. const std::string training_trace = prof_options[kTrainingTrace];
  134. if (training_trace.empty()) {
  135. GELOGI("Training trace will not take effect.");
  136. return ge::SUCCESS;
  137. }
  138. GELOGI("GE profiling training trace:%s", training_trace.c_str());
  139. if (training_trace != "on") {
  140. GELOGE(ge::PARAM_INVALID, "Training trace param:%s is invalid.", training_trace.c_str());
  141. return ge::PARAM_INVALID;
  142. }
  143. fp_point_ = prof_options[kFpPoint];
  144. bp_point_ = prof_options[kBpPoint];
  145. if (!fp_point_.empty() && !bp_point_.empty()) {
  146. GELOGI("Training trace bp fp is set, bp_point:%s, fp_point:%s.", bp_point_.c_str(), fp_point_.c_str());
  147. }
  148. is_training_trace_ = true;
  149. } catch (...) {
  150. GELOGE(FAILED, "Json prof_conf options is invalid.");
  151. return ge::PARAM_INVALID;
  152. }
  153. return ge::SUCCESS;
  154. }
  155. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::StopProfiling() {
  156. #ifdef DAVINCI_SUPPORT_PROFILING
  157. uint64_t module = GetProfilingModule();
  158. // The following if case will not be executed in normal case, inc case of ProfStopProfiling is abnormal
  159. int32_t device_num = static_cast<int32_t>(device_id_.size());
  160. if (device_num != 0) {
  161. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  162. if (device_id_ptr == nullptr) {
  163. GELOGE(FAILED, "Stop profiling: device id ptr is null.");
  164. return;
  165. }
  166. for (int32_t i = 0; i < device_num; i++) {
  167. device_id_ptr[i] = static_cast<uint32_t>(device_id_[i]);
  168. }
  169. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  170. if (rt_ret != RT_ERROR_NONE) {
  171. GELOGW("Call rtProfilerStop failed, ret:%d", rt_ret);
  172. }
  173. }
  174. // stop profiling
  175. if (prof_cb_.msprofCtrlCallback == nullptr) {
  176. GELOGE(ge::PARAM_INVALID, "MsprofCtrlCallback callback is nullptr.");
  177. return;
  178. }
  179. int32_t cb_ret = prof_cb_.msprofCtrlCallback(static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_FINALIZE),
  180. nullptr, 0);
  181. if (cb_ret != 0) {
  182. GELOGW("call msprofCtrlCallback failed, type:%u, return:%d",
  183. static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_FINALIZE), cb_ret);
  184. return;
  185. }
  186. GELOGI("Stop Profiling success.");
  187. #endif
  188. }
  189. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingTaskDescInfo(
  190. uint32_t model_id, const std::vector<TaskDescInfo> &task_desc_info, const int32_t &device_id) {
  191. #ifdef DAVINCI_SUPPORT_PROFILING
  192. std::string data;
  193. for (const auto &task : task_desc_info) {
  194. std::string model_name = task.model_name;
  195. std::string op_name = task.op_name;
  196. uint32_t block_dim = task.block_dim;
  197. uint32_t task_id = task.task_id;
  198. uint32_t stream_id = task.stream_id;
  199. std::string shape_type = task.shape_type;
  200. int64_t cur_iter_num = task.cur_iter_num;
  201. data = model_name.append(" ")
  202. .append(op_name).append(" ")
  203. .append(std::to_string(block_dim)).append(" ")
  204. .append(std::to_string(task_id)).append(" ")
  205. .append(std::to_string(stream_id)).append(" ")
  206. .append(std::to_string(model_id)).append(" ")
  207. .append(shape_type).append(" ")
  208. .append(std::to_string(cur_iter_num)).append("\n");
  209. ReporterData reporter_data{};
  210. reporter_data.deviceId = device_id;
  211. reporter_data.data = (unsigned char *)data.c_str();
  212. reporter_data.dataLen = data.size();
  213. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "task_desc_info", sizeof("task_desc_info"));
  214. if (ret != EOK) {
  215. GELOGE(ret, "Report data tag of task_desc_info memcpy error!");
  216. return;
  217. }
  218. int32_t cb_ret = CallMsprofReport(reporter_data);
  219. if (cb_ret != 0) {
  220. GELOGE(cb_ret, "Reporter data of task_desc_info failed, ret:%d", cb_ret);
  221. return;
  222. }
  223. }
  224. data.clear();
  225. #endif
  226. }
  227. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingGraphDescInfo(
  228. uint32_t model_id, const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info, const int32_t &device_id) {
  229. #ifdef DAVINCI_SUPPORT_PROFILING
  230. std::string data;
  231. for (const auto &graph : compute_graph_desc_info) {
  232. data.append("model_name:")
  233. .append(graph.model_name)
  234. .append(" op_name:")
  235. .append(graph.op_name)
  236. .append(" op_type:")
  237. .append(graph.op_type);
  238. for (size_t i = 0; i < graph.input_format.size(); ++i) {
  239. data.append(" input_id:")
  240. .append(std::to_string(i))
  241. .append(" input_format:")
  242. .append(std::to_string(graph.input_format.at(i)))
  243. .append(" input_data_type:")
  244. .append(std::to_string(graph.input_data_type.at(i)))
  245. .append(" input_shape:\"");
  246. size_t input_shape_len = graph.input_shape.at(i).size();
  247. if (input_shape_len == 0) {
  248. data.append("");
  249. } else if (input_shape_len == 1) {
  250. data.append(std::to_string(graph.input_shape.at(i).at(0)));
  251. } else {
  252. for (size_t j = 0; j < input_shape_len - 1; ++j) {
  253. data.append(std::to_string(graph.input_shape.at(i).at(j))).append(",");
  254. }
  255. data.append(std::to_string(graph.input_shape.at(i).at(input_shape_len - 1)));
  256. }
  257. data.append("\"");
  258. }
  259. for (size_t i = 0; i < graph.output_format.size(); ++i) {
  260. data.append(" output_id:")
  261. .append(std::to_string(i))
  262. .append(" output_format:")
  263. .append(std::to_string(graph.output_format.at(i)))
  264. .append(" output_data_type:")
  265. .append(std::to_string(graph.output_data_type.at(i)))
  266. .append(" output_shape:\"");
  267. size_t output_shape_len = graph.output_shape.at(i).size();
  268. if (output_shape_len == 0) {
  269. data.append("");
  270. } else if (output_shape_len == 1) {
  271. data.append(std::to_string(graph.output_shape.at(i).at(0)));
  272. } else {
  273. for (size_t j = 0; j < output_shape_len - 1; ++j) {
  274. data.append(std::to_string(graph.output_shape.at(i).at(j))).append(",");
  275. }
  276. data.append(std::to_string(graph.output_shape.at(i).at(output_shape_len - 1)));
  277. }
  278. data.append("\"");
  279. }
  280. data.append(" model_id:").append(std::to_string(model_id));
  281. data.append("\n");
  282. GraphDescReport(device_id, data);
  283. data.clear();
  284. }
  285. #endif
  286. }
  287. void ProfilingManager::GraphDescReport(const int32_t &device_id, const string &data) {
  288. #ifdef DAVINCI_SUPPORT_PROFILING
  289. ReporterData reporter_data{};
  290. int ret = -1;
  291. int32_t cb_ret = -1;
  292. size_t index = data.size() / kReportMaxLen;
  293. if (index >= 1) {
  294. reporter_data.deviceId = device_id;
  295. ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  296. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  297. for (size_t i = 0; i < index; ++i) {
  298. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * i;
  299. reporter_data.dataLen = kReportMaxLen;
  300. cb_ret = CallMsprofReport(reporter_data);
  301. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data of graph_desc_info failed, ret:%d", cb_ret); return;);
  302. }
  303. reporter_data.dataLen = data.size() - kReportMaxLen * index;
  304. if (reporter_data.dataLen != 0) {
  305. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * index;
  306. cb_ret = CallMsprofReport(reporter_data);
  307. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data of graph_desc_info failed, ret:%d", cb_ret); return;);
  308. }
  309. } else {
  310. reporter_data.deviceId = device_id;
  311. reporter_data.data = (unsigned char *)data.c_str();
  312. reporter_data.dataLen = data.size();
  313. ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  314. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  315. cb_ret = CallMsprofReport(reporter_data);
  316. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data of graph_desc_info failed, ret:%d", cb_ret); return;);
  317. }
  318. #endif
  319. }
  320. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ReportProfilingData(
  321. uint32_t model_id, const std::vector<TaskDescInfo> &task_desc_info,
  322. const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info) {
  323. #ifdef DAVINCI_SUPPORT_PROFILING
  324. int32_t logic_device_id = 0;
  325. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  326. if (rt_ret != RT_ERROR_NONE) {
  327. GELOGE(rt_ret, "runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  328. return;
  329. }
  330. GELOGD("current logic_device_id:%d", logic_device_id);
  331. GELOGD("start ProfilingTaskDescInfo.");
  332. ProfilingTaskDescInfo(model_id, task_desc_info, logic_device_id);
  333. GELOGD("start ProfilingGraphDescInfo.");
  334. ProfilingGraphDescInfo(model_id, compute_graph_desc_info, logic_device_id);
  335. GELOGD("Report profiling data for GE end.");
  336. #endif
  337. }
  338. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint64_t ProfilingManager::GetProfilingModule() {
  339. uint64_t module = PROF_MODEL_EXECUTE_MASK |
  340. PROF_RUNTIME_API_MASK |
  341. PROF_RUNTIME_TRACE_MASK |
  342. PROF_SCHEDULE_TIMELINE_MASK |
  343. PROF_SCHEDULE_TRACE_MASK |
  344. PROF_TASK_TIME_MASK |
  345. PROF_SUBTASK_TIME_MASK |
  346. PROF_AICPU_TRACE_MASK |
  347. PROF_AICORE_METRICS_MASK |
  348. PROF_AIVECTORCORE_METRICS_MASK |
  349. PROF_MODEL_LOAD_MASK;
  350. return module;
  351. }
  352. void ProfilingManager::UpdateSubscribeDeviceModuleMap(std::string prof_type, uint32_t device_id, uint64_t module) {
  353. #ifdef DAVINCI_SUPPORT_PROFILING
  354. if (prof_type == kProfModelSubscribe) {
  355. if (subs_dev_module_.find(device_id) != subs_dev_module_.end()) {
  356. subs_dev_module_[device_id].subscribe_count++;
  357. } else {
  358. DeviceSubsInfo dev_info;
  359. dev_info.module = module;
  360. dev_info.subscribe_count = 1;
  361. subs_dev_module_[device_id] = dev_info;
  362. }
  363. } else if (prof_type == kProfModelUnsubscribe) {
  364. auto iter = subs_dev_module_.find(device_id);
  365. if (iter != subs_dev_module_.end()) {
  366. if (iter->second.subscribe_count > 0) {
  367. iter->second.subscribe_count--;
  368. }
  369. if (iter->second.subscribe_count == 0) {
  370. subs_dev_module_.erase(iter);
  371. }
  372. }
  373. } else {
  374. GELOGI("No need to update device_id module map.");
  375. }
  376. #endif
  377. }
  378. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfModelSubscribe(
  379. uint64_t module, void *model) {
  380. #ifdef DAVINCI_SUPPORT_PROFILING
  381. std::lock_guard<std::mutex> lock(mutex_);
  382. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  383. if ((subscribe_count_ == 0) && (model_load_mask == PROF_MODEL_LOAD_MASK)) {
  384. // register framework to profiling
  385. // register Framework to profiling
  386. int32_t cb_ret = PluginInit();
  387. if (cb_ret != 0) {
  388. GELOGE(cb_ret, "profiling plugin init failed, ret:%d", cb_ret);
  389. return cb_ret;
  390. }
  391. GELOGI("Prof subscribe: model load profiling on.");
  392. }
  393. subscribe_count_++;
  394. auto davinci_model = static_cast<DavinciModel *>(model);
  395. int32_t device_num = 1;
  396. uint32_t device[1];
  397. device[0] = davinci_model->GetDeviceId();
  398. rtError_t rt_ret = rtProfilerStart(module, device_num, device);
  399. if (rt_ret != RT_ERROR_NONE) {
  400. GELOGE(FAILED, "Runtime profiler start failed.");
  401. return FAILED;
  402. }
  403. UpdateSubscribeDeviceModuleMap(kProfModelSubscribe, device[0], module);
  404. // Report profiling data
  405. Status p_ret = davinci_model->ReportProfilingData();
  406. if (p_ret != SUCCESS) {
  407. GELOGE(p_ret, "Report profiling data failed.");
  408. return p_ret;
  409. }
  410. #endif
  411. return SUCCESS;
  412. }
  413. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfModelUnsubscribe(
  414. void *model) {
  415. #ifdef DAVINCI_SUPPORT_PROFILING
  416. std::lock_guard<std::mutex> lock(mutex_);
  417. if (subscribe_count_ == 0) {
  418. GELOGW("The profiler has not been subscribed, you do not need to cannel the subscription.");
  419. return SUCCESS;
  420. }
  421. auto davinci_model = static_cast<DavinciModel *>(model);
  422. int32_t dev_num = 1;
  423. uint32_t device[1];
  424. device[0] = davinci_model->GetDeviceId();
  425. auto iter = subs_dev_module_.find(device[0]);
  426. if (iter != subs_dev_module_.end()) {
  427. if (subs_dev_module_[device[0]].subscribe_count == 1) {
  428. // The same device_id, only stop at last time
  429. rtError_t rt_ret = rtProfilerStop(subs_dev_module_[device[0]].module, dev_num, device);
  430. if (rt_ret != RT_ERROR_NONE) {
  431. GELOGE(FAILED, "Runtime profiler stop failed.");
  432. return FAILED;
  433. }
  434. }
  435. UpdateSubscribeDeviceModuleMap(kProfModelUnsubscribe, device[0], subs_dev_module_[device[0]].module);
  436. } else {
  437. GELOGE(FAILED, "The device_id:%u has not been subscribed, do not need to cancel.", device[0]);
  438. return FAILED;
  439. }
  440. subscribe_count_--;
  441. if (subscribe_count_ == 0) {
  442. // profiling plugin uninit at last subscription
  443. PluginUnInit();
  444. }
  445. #endif
  446. return SUCCESS;
  447. }
  448. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfInit(uint64_t module) {
  449. #ifdef DAVINCI_SUPPORT_PROFILING
  450. std::lock_guard<std::mutex> lock(mutex_);
  451. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  452. if (model_load_mask == PROF_MODEL_LOAD_MASK) {
  453. // register Framework to profiling
  454. int32_t cb_ret = PluginInit();
  455. if (cb_ret != 0) {
  456. GELOGE(cb_ret, "profiling plugin init failed, ret:%d", cb_ret);
  457. return cb_ret;
  458. }
  459. int32_t device_num = -1;
  460. rtError_t rt_ret = rtProfilerStart(model_load_mask, device_num, nullptr);
  461. if (rt_ret != RT_ERROR_NONE) {
  462. GELOGE(FAILED, "Runtime profiler start failed.");
  463. return FAILED;
  464. }
  465. is_load_profiling_ = true;
  466. GELOGI("Prof init: model load profiling on.");
  467. }
  468. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE_MASK;
  469. if (training_trace_mask == PROF_TRAINING_TRACE_MASK) {
  470. is_training_trace_ = true;
  471. }
  472. GELOGI("Prof init success.");
  473. #endif
  474. return SUCCESS;
  475. }
  476. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfFinalize() {
  477. #ifdef DAVINCI_SUPPORT_PROFILING
  478. std::lock_guard<std::mutex> lock(mutex_);
  479. is_load_profiling_ = false;
  480. is_training_trace_ = false;
  481. is_execute_profiling_ = false;
  482. // profiling plugin uninit
  483. PluginUnInit();
  484. int32_t dev_num = -1;
  485. rtError_t rt_ret = rtProfilerStop(PROF_MODEL_LOAD_MASK, dev_num, nullptr);
  486. if (rt_ret != RT_ERROR_NONE) {
  487. GELOGE(FAILED, "Runtime profiler stop failed.");
  488. return FAILED;
  489. }
  490. for (auto device_id_module : device_id_module_map_) {
  491. if (device_id_module.second != 0) {
  492. uint32_t device_id = static_cast<uint32_t>(device_id_module.first);
  493. GELOGI("Prof finalize: device_id: %u, module: 0x%llx.", device_id, device_id_module.second);
  494. rt_ret = rtProfilerStop(device_id_module.second, 1, &device_id);
  495. if (rt_ret != RT_ERROR_NONE) {
  496. GELOGE(FAILED, "Runtime profiler stop failed.");
  497. return FAILED;
  498. }
  499. }
  500. }
  501. device_id_module_map_.clear();
  502. device_id_.clear();
  503. GELOGI("Prof finalize success.");
  504. #endif
  505. return SUCCESS;
  506. }
  507. Status ProfilingManager::ProfParseDeviceId(const std::map<std::string, std::string> &config_para,
  508. vector<int32_t> &device_list) {
  509. #ifdef DAVINCI_SUPPORT_PROFILING
  510. auto iter = config_para.find(kConfigDevIdList);
  511. if (iter != config_para.end()) {
  512. std::string device_id_list = iter->second;
  513. std::string temp;
  514. vector<std::string> decvice_id;
  515. for (uint32_t i = 0; i < device_id_list.size(); i++) {
  516. if (isdigit(device_id_list[i])) {
  517. temp.append(1, device_id_list[i]);
  518. } else {
  519. if (!temp.empty()) {
  520. decvice_id.emplace_back(temp);
  521. }
  522. temp.clear();
  523. }
  524. }
  525. if (!temp.empty()) {
  526. decvice_id.emplace_back(temp);
  527. }
  528. for (uint32_t i = 0; i < decvice_id.size(); i++) {
  529. try {
  530. int32_t dev_id = std::stoi(decvice_id[i]);
  531. device_list.push_back(dev_id);
  532. } catch (std::invalid_argument &) {
  533. GELOGE(FAILED, "Device id: %s is invalid.", decvice_id[i].c_str());
  534. return FAILED;
  535. } catch (std::out_of_range &) {
  536. GELOGE(FAILED, "Device id: %s is out of range.", decvice_id[i].c_str());
  537. return FAILED;
  538. } catch (...) {
  539. GELOGE(FAILED, "Device id: %s cannot change to int.", decvice_id[i].c_str());
  540. return FAILED;
  541. }
  542. }
  543. } else {
  544. GELOGE(FAILED, "Config para not contain device id list.");
  545. return FAILED;
  546. }
  547. #endif
  548. return SUCCESS;
  549. }
  550. Status ProfilingManager::ProfParseParam(const std::map<std::string, std::string> &config_para,
  551. int32_t &device_num, vector<int32_t> &device_list) {
  552. #ifdef DAVINCI_SUPPORT_PROFILING
  553. // device num
  554. auto iter = config_para.find(kConfigNumsdev);
  555. if (iter != config_para.end()) {
  556. try {
  557. device_num = std::stoi(iter->second);
  558. } catch (std::invalid_argument &) {
  559. GELOGE(FAILED, "Device nun: %s is invalid.", iter->second.c_str());
  560. return FAILED;
  561. } catch (std::out_of_range &) {
  562. GELOGE(FAILED, "Device num: %s is out of range.", iter->second.c_str());
  563. return FAILED;
  564. } catch (...) {
  565. GELOGE(FAILED, "Device num: %s cannot change to int.", iter->second.c_str());
  566. return FAILED;
  567. }
  568. } else {
  569. GELOGE(FAILED, "Config para not contain device num.");
  570. return FAILED;
  571. }
  572. // device id
  573. if (ProfParseDeviceId(config_para, device_list) != SUCCESS) {
  574. GELOGE(FAILED, "Parse config para device id failed.");
  575. return FAILED;
  576. }
  577. if (device_num == 0 || device_num > kMaxDeviceNum || device_num != static_cast<int32_t>(device_list.size())) {
  578. GELOGE(FAILED, "Config para device num: %d not equal to device list size: %d.", device_num, device_list.size());
  579. return FAILED;
  580. }
  581. #endif
  582. return SUCCESS;
  583. }
  584. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfStartProfiling(
  585. uint64_t module, const std::map<std::string, std::string> &config_para) {
  586. #ifdef DAVINCI_SUPPORT_PROFILING
  587. std::lock_guard<std::mutex> lock(mutex_);
  588. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE_MASK;
  589. if (training_trace_mask == PROF_TRAINING_TRACE_MASK) {
  590. is_training_trace_ = true;
  591. }
  592. int32_t device_num = 0;
  593. vector<int32_t> device_list;
  594. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  595. GELOGE(FAILED, "Prof start parse param failed.");
  596. return FAILED;
  597. }
  598. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  599. if (device_id_ptr == nullptr) {
  600. GELOGE(FAILED, "Prof start: device id ptr is null.");
  601. return FAILED;
  602. }
  603. for (int32_t i = 0; i < device_num; i++) {
  604. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  605. }
  606. GELOGI("Runtime config param: 0x%llx, device num: %d.", module, device_num);
  607. rtError_t rt_ret = rtProfilerStart(module, device_num, device_id_ptr.get());
  608. if (rt_ret != RT_ERROR_NONE) {
  609. GELOGE(FAILED, "Runtime profiler config proc failed.");
  610. return FAILED;
  611. }
  612. if ((module & PROF_MODEL_EXECUTE_MASK) == PROF_MODEL_EXECUTE_MASK) {
  613. for (int32_t i = 0; i < device_num; i++) {
  614. if (std::find(device_id_.begin(), device_id_.end(), device_list[i]) == device_id_.end()) {
  615. device_id_.push_back(device_list[i]);
  616. }
  617. }
  618. GELOGI("Prof start: ge execute model start profiling.");
  619. }
  620. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  621. GELOGW("Prof start: load model module is invalid.");
  622. }
  623. UpdateDeviceIdModuleMap(kProfStart, module, device_list);
  624. GELOGI("Prof start profiling success.");
  625. #endif
  626. return SUCCESS;
  627. }
  628. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfStopProfiling(uint64_t module,
  629. const std::map<std::string, std::string> &config_para) {
  630. #ifdef DAVINCI_SUPPORT_PROFILING
  631. std::lock_guard<std::mutex> lock(mutex_);
  632. int32_t device_num = 0;
  633. vector<int32_t> device_list;
  634. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  635. GELOGE(FAILED, "Prof stop parse param failed.");
  636. return FAILED;
  637. }
  638. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  639. if (device_id_ptr == nullptr) {
  640. GELOGE(FAILED, "Prof stop: device id ptr is null.");
  641. return FAILED;
  642. }
  643. for (int32_t i = 0; i < device_num; i++) {
  644. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  645. }
  646. GELOGI("Prof stop: runtime config param: 0x%llx, device num: %d", module, device_num);
  647. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  648. if (rt_ret != RT_ERROR_NONE) {
  649. GELOGE(FAILED, "Prof stop: runtime profiler config proc failed.");
  650. return FAILED;
  651. }
  652. uint64_t execute_model_mask = module & PROF_MODEL_EXECUTE_MASK;
  653. if (execute_model_mask == PROF_MODEL_EXECUTE_MASK) {
  654. for (int32_t i = 0; i < device_num; i++) {
  655. auto iter = std::find(device_id_.begin(), device_id_.end(), device_list[i]);
  656. if (iter != device_id_.end()) {
  657. device_id_.erase(iter);
  658. }
  659. }
  660. GELOGI("Prof stop: ge execute model stop profiling.");
  661. }
  662. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  663. GELOGW("Prof stop: load model module is invalid.");
  664. }
  665. UpdateDeviceIdModuleMap(kProfStop, module, device_list);
  666. GELOGI("Prof stop profiling success.");
  667. #endif
  668. return SUCCESS;
  669. }
  670. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::UpdateDeviceIdModuleMap(string prof_type,
  671. uint64_t module, const vector<int32_t> &device_list) {
  672. #ifdef DAVINCI_SUPPORT_PROFILING
  673. if (prof_type == kProfStart) {
  674. for (uint32_t i = 0; i < device_list.size(); i++) {
  675. auto iter = device_id_module_map_.find(device_list[i]);
  676. if (iter != device_id_module_map_.end()) {
  677. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  678. // save all profiling on module of device
  679. device_id_module_map_[device_list[i]] = prof_on_module | module;
  680. } else {
  681. device_id_module_map_[device_list[i]] = module;
  682. }
  683. }
  684. } else if (prof_type == kProfStop) {
  685. for (uint32_t i = 0; i < device_list.size(); i++) {
  686. auto iter = device_id_module_map_.find(device_list[i]);
  687. if (iter != device_id_module_map_.end()) {
  688. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  689. uint64_t prof_off_module = prof_on_module & module;
  690. uint64_t prof_on_left_module = prof_on_module & (~prof_off_module);
  691. // stop profiling on module of device
  692. device_id_module_map_[device_list[i]] = prof_on_left_module;
  693. }
  694. }
  695. } else {
  696. GELOGI("No need to update device_id module map.");
  697. }
  698. #endif
  699. }
  700. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ProfilingManager::ProfilingModelExecuteOn() const {
  701. int32_t logic_device_id = 0;
  702. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  703. if (rt_ret != RT_ERROR_NONE) {
  704. GELOGE(rt_ret, "Runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  705. }
  706. GELOGI("Current logic_device_id:%d", logic_device_id);
  707. bool execute_model_prof_on = false;
  708. auto iter = std::find(device_id_.begin(), device_id_.end(), logic_device_id);
  709. if (iter != device_id_.end()) {
  710. execute_model_prof_on = true;
  711. }
  712. GELOGI("Flag is_execute_profiling: %d, execute_model_prof_on: %d", is_execute_profiling_, execute_model_prof_on);
  713. return execute_model_prof_on;
  714. }
  715. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::PluginInit() const {
  716. if (prof_cb_.msprofReporterCallback == nullptr) {
  717. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  718. return ge::PARAM_INVALID;
  719. }
  720. return prof_cb_.msprofReporterCallback(
  721. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  722. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_INIT),
  723. nullptr, 0);
  724. }
  725. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::PluginUnInit() const {
  726. #ifdef DAVINCI_SUPPORT_PROFILING
  727. if (prof_cb_.msprofReporterCallback == nullptr) {
  728. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  729. return;
  730. }
  731. int32_t cb_ret = prof_cb_.msprofReporterCallback(
  732. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  733. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_UNINIT),
  734. nullptr, 0);
  735. if (cb_ret != 0) {
  736. GELOGW("profiling plugin uninit failed, ret:%d", cb_ret);
  737. }
  738. #endif
  739. }
  740. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::CallMsprofReport(
  741. ReporterData &reporter_data) const {
  742. if (prof_cb_.msprofReporterCallback == nullptr) {
  743. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  744. return ge::PARAM_INVALID;
  745. }
  746. return prof_cb_.msprofReporterCallback(
  747. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  748. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_REPORT),
  749. static_cast<void *>(&reporter_data), sizeof(ReporterData));
  750. }
  751. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::GetFpBpPoint(
  752. std::string &fp_point, std::string &bp_point) {
  753. // Env or options mode, fp_point_/bp_point_ have initiliazed on profiling init
  754. if (!fp_point_.empty() && !bp_point_.empty()) {
  755. fp_point = fp_point_;
  756. bp_point = bp_point_;
  757. GELOGI("Bp Fp have been initialized in env or options. bp_point: %s, fp_point: %s", bp_point.c_str(),
  758. fp_point.c_str());
  759. return;
  760. }
  761. // ProfApi mode and training trace is set
  762. // Parse options first
  763. char env_profiling_options[MSPROF_OPTIONS_DEF_LEN_MAX] = { 0x00 };
  764. bool is_profiling_valid = false;
  765. std::string profiling_options;
  766. if (ge::GetContext().GetOption(OPTION_EXEC_PROFILING_OPTIONS, profiling_options) == SUCCESS &&
  767. !profiling_options.empty()) {
  768. is_profiling_valid = true;
  769. } else {
  770. INT32 ret = mmGetEnv("PROFILING_OPTIONS", env_profiling_options, MSPROF_OPTIONS_DEF_LEN_MAX);
  771. if (ret != EN_OK) {
  772. GELOGI("PROFILING_OPTIONS env is not exist.");
  773. return;
  774. }
  775. GELOGI("Parse env PROFILING_OPTIONS:%s.", env_profiling_options);
  776. profiling_options = env_profiling_options;
  777. is_profiling_valid = true;
  778. }
  779. if (is_profiling_valid) {
  780. try {
  781. Json prof_options = Json::parse(profiling_options);
  782. fp_point_ = prof_options[kFpPoint];
  783. bp_point_ = prof_options[kBpPoint];
  784. fp_point = fp_point_;
  785. bp_point = bp_point_;
  786. if (!fp_point_.empty() && !bp_point_.empty()) {
  787. GELOGI("Training trace bp fp is set, bp_point:%s, fp_point:%s.", bp_point_.c_str(), fp_point_.c_str());
  788. }
  789. } catch (...) {
  790. GELOGW("Json prof options is invalid.");
  791. return;
  792. }
  793. }
  794. return;
  795. }
  796. } // namespace ge

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