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 36 kB

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

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