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

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