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

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