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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. std::lock_guard<std::mutex> lock(mutex_report_);
  331. for (size_t i = 0; i < index; ++i) {
  332. reporter_data.data = (unsigned char *)data.c_str() + report_max_len * i;
  333. reporter_data.dataLen = report_max_len;
  334. cb_ret = CallMsprofReport(reporter_data);
  335. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data [%s] failed, ret:%d", tag_name.c_str(), cb_ret);
  336. return;);
  337. }
  338. reporter_data.dataLen = data.size() - report_max_len * index;
  339. if (reporter_data.dataLen != 0) {
  340. reporter_data.data = (unsigned char *)data.c_str() + report_max_len * index;
  341. cb_ret = CallMsprofReport(reporter_data);
  342. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data [%s] failed, ret:%d", tag_name.c_str(), cb_ret);
  343. return;);
  344. }
  345. } else {
  346. reporter_data.deviceId = device_id;
  347. reporter_data.data = (unsigned char *)data.c_str();
  348. reporter_data.dataLen = data.size();
  349. ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, tag_name.c_str(), tag_name.size());
  350. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag [%s] memcpy error!", tag_name.c_str()); return;);
  351. std::lock_guard<std::mutex> lock(mutex_report_);
  352. cb_ret = CallMsprofReport(reporter_data);
  353. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data [%s] failed, ret:%d", tag_name.c_str(), cb_ret);
  354. return;);
  355. }
  356. #endif
  357. }
  358. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ReportProfilingData(
  359. uint32_t model_id, const std::vector<TaskDescInfo> &task_desc_info) {
  360. #ifdef DAVINCI_SUPPORT_PROFILING
  361. int32_t logic_device_id = 0;
  362. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  363. if (rt_ret != RT_ERROR_NONE) {
  364. GELOGE(rt_ret, "runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  365. return;
  366. }
  367. GELOGD("current logic_device_id:%d", logic_device_id);
  368. GELOGD("start ProfilingTaskDescInfo.");
  369. ProfilingTaskDescInfo(model_id, task_desc_info, logic_device_id);
  370. GELOGD("Report profiling data for GE end.");
  371. #endif
  372. }
  373. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint64_t ProfilingManager::GetProfilingModule() {
  374. uint64_t module = PROF_MODEL_EXECUTE_MASK |
  375. PROF_RUNTIME_API_MASK |
  376. PROF_RUNTIME_TRACE_MASK |
  377. PROF_SCHEDULE_TIMELINE_MASK |
  378. PROF_SCHEDULE_TRACE_MASK |
  379. PROF_TASK_TIME_MASK |
  380. PROF_SUBTASK_TIME_MASK |
  381. PROF_AICPU_TRACE_MASK |
  382. PROF_AICORE_METRICS_MASK |
  383. PROF_AIVECTORCORE_METRICS_MASK |
  384. PROF_MODEL_LOAD_MASK;
  385. return module;
  386. }
  387. void ProfilingManager::UpdateSubscribeDeviceModuleMap(std::string prof_type, uint32_t device_id, uint64_t module) {
  388. #ifdef DAVINCI_SUPPORT_PROFILING
  389. if (prof_type == kProfModelSubscribe) {
  390. if (subs_dev_module_.find(device_id) != subs_dev_module_.end()) {
  391. subs_dev_module_[device_id].subscribe_count++;
  392. } else {
  393. DeviceSubsInfo dev_info;
  394. dev_info.module = module;
  395. dev_info.subscribe_count = 1;
  396. subs_dev_module_[device_id] = dev_info;
  397. }
  398. } else if (prof_type == kProfModelUnsubscribe) {
  399. auto iter = subs_dev_module_.find(device_id);
  400. if (iter != subs_dev_module_.end()) {
  401. if (iter->second.subscribe_count > 0) {
  402. iter->second.subscribe_count--;
  403. }
  404. if (iter->second.subscribe_count == 0) {
  405. subs_dev_module_.erase(iter);
  406. }
  407. }
  408. } else {
  409. GELOGI("No need to update device_id module map.");
  410. }
  411. #endif
  412. }
  413. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfModelSubscribe(
  414. uint64_t module, void *model) {
  415. #ifdef DAVINCI_SUPPORT_PROFILING
  416. std::lock_guard<std::mutex> lock(mutex_);
  417. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  418. if ((subscribe_count_ == 0) && (model_load_mask == PROF_MODEL_LOAD_MASK)) {
  419. // register framework to profiling
  420. // register Framework to profiling
  421. int32_t cb_ret = PluginInit();
  422. if (cb_ret != 0) {
  423. GELOGE(cb_ret, "profiling plugin init failed, ret:%d", cb_ret);
  424. return cb_ret;
  425. }
  426. GELOGI("Prof subscribe: model load profiling on.");
  427. }
  428. subscribe_count_++;
  429. auto davinci_model = static_cast<DavinciModel *>(model);
  430. int32_t device_num = 1;
  431. uint32_t device[1];
  432. device[0] = davinci_model->GetDeviceId();
  433. rtError_t rt_ret = rtProfilerStart(module, device_num, device);
  434. if (rt_ret != RT_ERROR_NONE) {
  435. GELOGE(FAILED, "Runtime profiler start failed.");
  436. return FAILED;
  437. }
  438. UpdateSubscribeDeviceModuleMap(kProfModelSubscribe, device[0], module);
  439. // Report profiling data
  440. Status p_ret = davinci_model->ReportProfilingData();
  441. if (p_ret != SUCCESS) {
  442. GELOGE(p_ret, "Report profiling data failed.");
  443. return p_ret;
  444. }
  445. #endif
  446. return SUCCESS;
  447. }
  448. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfModelUnsubscribe(
  449. void *model) {
  450. #ifdef DAVINCI_SUPPORT_PROFILING
  451. std::lock_guard<std::mutex> lock(mutex_);
  452. if (subscribe_count_ == 0) {
  453. GELOGW("The profiler has not been subscribed, you do not need to cannel the subscription.");
  454. return SUCCESS;
  455. }
  456. auto davinci_model = static_cast<DavinciModel *>(model);
  457. int32_t dev_num = 1;
  458. uint32_t device[1];
  459. device[0] = davinci_model->GetDeviceId();
  460. auto iter = subs_dev_module_.find(device[0]);
  461. if (iter != subs_dev_module_.end()) {
  462. if (subs_dev_module_[device[0]].subscribe_count == 1) {
  463. // The same device_id, only stop at last time
  464. rtError_t rt_ret = rtProfilerStop(subs_dev_module_[device[0]].module, dev_num, device);
  465. if (rt_ret != RT_ERROR_NONE) {
  466. GELOGE(FAILED, "Runtime profiler stop failed.");
  467. return FAILED;
  468. }
  469. }
  470. UpdateSubscribeDeviceModuleMap(kProfModelUnsubscribe, device[0], subs_dev_module_[device[0]].module);
  471. } else {
  472. GELOGE(FAILED, "The device_id:%u has not been subscribed, do not need to cancel.", device[0]);
  473. return FAILED;
  474. }
  475. subscribe_count_--;
  476. if (subscribe_count_ == 0) {
  477. // profiling plugin uninit at last subscription
  478. PluginUnInit();
  479. }
  480. #endif
  481. return SUCCESS;
  482. }
  483. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfInit(uint64_t module) {
  484. #ifdef DAVINCI_SUPPORT_PROFILING
  485. std::lock_guard<std::mutex> lock(mutex_);
  486. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  487. if (model_load_mask == PROF_MODEL_LOAD_MASK) {
  488. // register Framework to profiling
  489. int32_t cb_ret = PluginInit();
  490. if (cb_ret != 0) {
  491. GELOGE(cb_ret, "profiling plugin init failed, ret:%d", cb_ret);
  492. return cb_ret;
  493. }
  494. int32_t device_num = -1;
  495. rtError_t rt_ret = rtProfilerStart(model_load_mask, device_num, nullptr);
  496. if (rt_ret != RT_ERROR_NONE) {
  497. GELOGE(FAILED, "Runtime profiler start failed.");
  498. return FAILED;
  499. }
  500. is_load_profiling_ = true;
  501. GELOGI("Prof init: model load profiling on.");
  502. }
  503. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE_MASK;
  504. if (training_trace_mask == PROF_TRAINING_TRACE_MASK) {
  505. is_training_trace_ = true;
  506. }
  507. GELOGI("Prof init success.");
  508. #endif
  509. return SUCCESS;
  510. }
  511. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfFinalize() {
  512. #ifdef DAVINCI_SUPPORT_PROFILING
  513. std::lock_guard<std::mutex> lock(mutex_);
  514. is_load_profiling_ = false;
  515. is_training_trace_ = false;
  516. is_execute_profiling_ = false;
  517. // profiling plugin uninit
  518. PluginUnInit();
  519. int32_t dev_num = -1;
  520. rtError_t rt_ret = rtProfilerStop(PROF_MODEL_LOAD_MASK, dev_num, nullptr);
  521. if (rt_ret != RT_ERROR_NONE) {
  522. GELOGE(FAILED, "Runtime profiler stop failed.");
  523. return FAILED;
  524. }
  525. for (auto device_id_module : device_id_module_map_) {
  526. if (device_id_module.second != 0) {
  527. uint32_t device_id = static_cast<uint32_t>(device_id_module.first);
  528. GELOGI("Prof finalize: device_id: %u, module: 0x%lx.", device_id, device_id_module.second);
  529. rt_ret = rtProfilerStop(device_id_module.second, 1, &device_id);
  530. if (rt_ret != RT_ERROR_NONE) {
  531. GELOGE(FAILED, "Runtime profiler stop failed.");
  532. return FAILED;
  533. }
  534. }
  535. }
  536. device_id_module_map_.clear();
  537. device_id_.clear();
  538. GELOGI("Prof finalize success.");
  539. #endif
  540. return SUCCESS;
  541. }
  542. Status ProfilingManager::ProfParseDeviceId(const std::map<std::string, std::string> &config_para,
  543. vector<int32_t> &device_list) {
  544. #ifdef DAVINCI_SUPPORT_PROFILING
  545. auto iter = config_para.find(kConfigDevIdList);
  546. if (iter != config_para.end()) {
  547. std::string device_id_list = iter->second;
  548. std::string temp;
  549. vector<std::string> decvice_id;
  550. for (uint32_t i = 0; i < device_id_list.size(); i++) {
  551. if (isdigit(device_id_list[i])) {
  552. temp.append(1, device_id_list[i]);
  553. } else {
  554. if (!temp.empty()) {
  555. decvice_id.emplace_back(temp);
  556. }
  557. temp.clear();
  558. }
  559. }
  560. if (!temp.empty()) {
  561. decvice_id.emplace_back(temp);
  562. }
  563. for (uint32_t i = 0; i < decvice_id.size(); i++) {
  564. try {
  565. int32_t dev_id = std::stoi(decvice_id[i]);
  566. device_list.push_back(dev_id);
  567. } catch (std::invalid_argument &) {
  568. GELOGE(FAILED, "Device id: %s is invalid.", decvice_id[i].c_str());
  569. return FAILED;
  570. } catch (std::out_of_range &) {
  571. GELOGE(FAILED, "Device id: %s is out of range.", decvice_id[i].c_str());
  572. return FAILED;
  573. } catch (...) {
  574. GELOGE(FAILED, "Device id: %s cannot change to int.", decvice_id[i].c_str());
  575. return FAILED;
  576. }
  577. }
  578. } else {
  579. GELOGE(FAILED, "Config para not contain device id list.");
  580. return FAILED;
  581. }
  582. #endif
  583. return SUCCESS;
  584. }
  585. Status ProfilingManager::ProfParseParam(const std::map<std::string, std::string> &config_para,
  586. int32_t &device_num, vector<int32_t> &device_list) {
  587. #ifdef DAVINCI_SUPPORT_PROFILING
  588. // device num
  589. auto iter = config_para.find(kConfigNumsdev);
  590. if (iter != config_para.end()) {
  591. try {
  592. device_num = std::stoi(iter->second);
  593. } catch (std::invalid_argument &) {
  594. GELOGE(FAILED, "Device nun: %s is invalid.", iter->second.c_str());
  595. return FAILED;
  596. } catch (std::out_of_range &) {
  597. GELOGE(FAILED, "Device num: %s is out of range.", iter->second.c_str());
  598. return FAILED;
  599. } catch (...) {
  600. GELOGE(FAILED, "Device num: %s cannot change to int.", iter->second.c_str());
  601. return FAILED;
  602. }
  603. } else {
  604. GELOGE(FAILED, "Config para not contain device num.");
  605. return FAILED;
  606. }
  607. // device id
  608. if (ProfParseDeviceId(config_para, device_list) != SUCCESS) {
  609. GELOGE(FAILED, "Parse config para device id failed.");
  610. return FAILED;
  611. }
  612. if (device_num == 0 || device_num > kMaxDeviceNum || device_num != static_cast<int32_t>(device_list.size())) {
  613. GELOGE(FAILED, "Config para device num: %d not equal to device list size: %zu.", device_num, device_list.size());
  614. return FAILED;
  615. }
  616. #endif
  617. return SUCCESS;
  618. }
  619. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfStartProfiling(
  620. uint64_t module, const std::map<std::string, std::string> &config_para) {
  621. #ifdef DAVINCI_SUPPORT_PROFILING
  622. std::lock_guard<std::mutex> lock(mutex_);
  623. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE_MASK;
  624. if (training_trace_mask == PROF_TRAINING_TRACE_MASK) {
  625. is_training_trace_ = true;
  626. }
  627. int32_t device_num = 0;
  628. vector<int32_t> device_list;
  629. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  630. GELOGE(FAILED, "Prof start parse param failed.");
  631. return FAILED;
  632. }
  633. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  634. if (device_id_ptr == nullptr) {
  635. GELOGE(FAILED, "Prof start: device id ptr is null.");
  636. return FAILED;
  637. }
  638. for (int32_t i = 0; i < device_num; i++) {
  639. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  640. }
  641. GELOGI("Runtime config param: 0x%lx, device num: %d.", module, device_num);
  642. rtError_t rt_ret = rtProfilerStart(module, device_num, device_id_ptr.get());
  643. if (rt_ret != RT_ERROR_NONE) {
  644. GELOGE(FAILED, "Runtime profiler config proc failed.");
  645. return FAILED;
  646. }
  647. if ((module & PROF_MODEL_EXECUTE_MASK) == PROF_MODEL_EXECUTE_MASK) {
  648. for (int32_t i = 0; i < device_num; i++) {
  649. if (std::find(device_id_.begin(), device_id_.end(), device_list[i]) == device_id_.end()) {
  650. device_id_.push_back(device_list[i]);
  651. }
  652. }
  653. GELOGI("Prof start: ge execute model start profiling.");
  654. }
  655. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  656. GELOGW("Prof start: load model module is invalid.");
  657. }
  658. UpdateDeviceIdModuleMap(kProfStart, module, device_list);
  659. GELOGI("Prof start profiling success.");
  660. #endif
  661. return SUCCESS;
  662. }
  663. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfStopProfiling(uint64_t module,
  664. const std::map<std::string, std::string> &config_para) {
  665. #ifdef DAVINCI_SUPPORT_PROFILING
  666. std::lock_guard<std::mutex> lock(mutex_);
  667. int32_t device_num = 0;
  668. vector<int32_t> device_list;
  669. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  670. GELOGE(FAILED, "Prof stop parse param failed.");
  671. return FAILED;
  672. }
  673. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  674. if (device_id_ptr == nullptr) {
  675. GELOGE(FAILED, "Prof stop: device id ptr is null.");
  676. return FAILED;
  677. }
  678. for (int32_t i = 0; i < device_num; i++) {
  679. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  680. }
  681. GELOGI("Prof stop: runtime config param: 0x%lx, device num: %d", module, device_num);
  682. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  683. if (rt_ret != RT_ERROR_NONE) {
  684. GELOGE(FAILED, "Prof stop: runtime profiler config proc failed.");
  685. return FAILED;
  686. }
  687. uint64_t execute_model_mask = module & PROF_MODEL_EXECUTE_MASK;
  688. if (execute_model_mask == PROF_MODEL_EXECUTE_MASK) {
  689. for (int32_t i = 0; i < device_num; i++) {
  690. auto iter = std::find(device_id_.begin(), device_id_.end(), device_list[i]);
  691. if (iter != device_id_.end()) {
  692. device_id_.erase(iter);
  693. }
  694. }
  695. GELOGI("Prof stop: ge execute model stop profiling.");
  696. }
  697. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  698. GELOGW("Prof stop: load model module is invalid.");
  699. }
  700. UpdateDeviceIdModuleMap(kProfStop, module, device_list);
  701. GELOGI("Prof stop profiling success.");
  702. #endif
  703. return SUCCESS;
  704. }
  705. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::UpdateDeviceIdModuleMap(string prof_type,
  706. uint64_t module, const vector<int32_t> &device_list) {
  707. #ifdef DAVINCI_SUPPORT_PROFILING
  708. if (prof_type == kProfStart) {
  709. for (uint32_t i = 0; i < device_list.size(); i++) {
  710. auto iter = device_id_module_map_.find(device_list[i]);
  711. if (iter != device_id_module_map_.end()) {
  712. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  713. // save all profiling on module of device
  714. device_id_module_map_[device_list[i]] = prof_on_module | module;
  715. } else {
  716. device_id_module_map_[device_list[i]] = module;
  717. }
  718. }
  719. } else if (prof_type == kProfStop) {
  720. for (uint32_t i = 0; i < device_list.size(); i++) {
  721. auto iter = device_id_module_map_.find(device_list[i]);
  722. if (iter != device_id_module_map_.end()) {
  723. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  724. uint64_t prof_off_module = prof_on_module & module;
  725. uint64_t prof_on_left_module = prof_on_module & (~prof_off_module);
  726. // stop profiling on module of device
  727. device_id_module_map_[device_list[i]] = prof_on_left_module;
  728. }
  729. }
  730. } else {
  731. GELOGI("No need to update device_id module map.");
  732. }
  733. #endif
  734. }
  735. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ProfilingManager::ProfilingModelExecuteOn() const {
  736. int32_t logic_device_id = 0;
  737. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  738. if (rt_ret != RT_ERROR_NONE) {
  739. GELOGE(rt_ret, "Runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  740. }
  741. GELOGI("Current logic_device_id:%d", logic_device_id);
  742. bool execute_model_prof_on = false;
  743. auto iter = std::find(device_id_.begin(), device_id_.end(), logic_device_id);
  744. if (iter != device_id_.end()) {
  745. execute_model_prof_on = true;
  746. }
  747. GELOGI("Flag is_execute_profiling: %d, execute_model_prof_on: %d", is_execute_profiling_, execute_model_prof_on);
  748. return execute_model_prof_on;
  749. }
  750. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::PluginInit() {
  751. if (prof_cb_.msprofReporterCallback == nullptr) {
  752. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  753. return ge::PARAM_INVALID;
  754. }
  755. int32_t cb_ret = prof_cb_.msprofReporterCallback(
  756. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  757. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_INIT),
  758. nullptr, 0);
  759. if (cb_ret != MSPROF_ERROR_NONE) {
  760. REPORT_CALL_ERROR("E19999", "Profiling reporter init failed, ret = %d.", cb_ret);
  761. GELOGE(INTERNAL_ERROR, "[Init][ProfilingReporter] profiling init failed, ret = %d.", cb_ret);
  762. return INTERNAL_ERROR;
  763. }
  764. cb_ret = prof_cb_.msprofReporterCallback(
  765. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  766. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_DATA_MAX_LEN),
  767. &reporter_max_len_, sizeof(uint32_t));
  768. if (cb_ret != MSPROF_ERROR_NONE) {
  769. REPORT_CALL_ERROR("E19999", "Get profiling reporter data max len failed, ret = %d.", cb_ret);
  770. GELOGE(INTERNAL_ERROR, "[Init][ProfilingReporter] Get profiling reporter data max len failed, ret = %d.", cb_ret);
  771. return INTERNAL_ERROR;
  772. }
  773. return SUCCESS;
  774. }
  775. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::PluginUnInit() const {
  776. #ifdef DAVINCI_SUPPORT_PROFILING
  777. if (prof_cb_.msprofReporterCallback == nullptr) {
  778. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  779. return;
  780. }
  781. int32_t cb_ret = prof_cb_.msprofReporterCallback(
  782. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  783. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_UNINIT),
  784. nullptr, 0);
  785. if (cb_ret != 0) {
  786. GELOGW("profiling plugin uninit failed, ret:%d", cb_ret);
  787. }
  788. #endif
  789. }
  790. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::CallMsprofReport(
  791. ReporterData &reporter_data) const {
  792. if (prof_cb_.msprofReporterCallback == nullptr) {
  793. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  794. return ge::PARAM_INVALID;
  795. }
  796. return prof_cb_.msprofReporterCallback(
  797. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  798. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_REPORT),
  799. static_cast<void *>(&reporter_data), sizeof(ReporterData));
  800. }
  801. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::GetOpInputOutputInfo(
  802. const OpDescPtr &op, TaskDescInfo &task_desc_info) const {
  803. std::vector<Format> input_format;
  804. std::vector<std::vector<int64_t>> input_shape;
  805. std::vector<DataType> input_data_type;
  806. for (size_t i = 0; i < op->GetAllInputsSize(); ++i) {
  807. GeTensorDescPtr input_tensor_desc = op->MutableInputDesc(i);
  808. if (input_tensor_desc == nullptr) {
  809. continue;
  810. }
  811. input_format.emplace_back(input_tensor_desc->GetFormat());
  812. input_shape.emplace_back(input_tensor_desc->GetShape().GetDims());
  813. input_data_type.emplace_back(input_tensor_desc->GetDataType());
  814. }
  815. std::vector<Format> output_format;
  816. std::vector<std::vector<int64_t>> output_shape;
  817. std::vector<DataType> output_data_type;
  818. for (size_t j = 0; j < op->GetOutputsSize(); ++j) {
  819. GeTensorDescPtr output_tensor_desc = op->MutableOutputDesc(j);
  820. if (output_tensor_desc == nullptr) {
  821. continue;
  822. }
  823. output_format.emplace_back(output_tensor_desc->GetFormat());
  824. output_shape.emplace_back(output_tensor_desc->GetShape().GetDims());
  825. output_data_type.emplace_back(output_tensor_desc->GetDataType());
  826. }
  827. std::vector<Format> format_default = { FORMAT_NULL };
  828. std::vector<std::vector<int64_t>> shape_default = { {0} };
  829. std::vector<DataType> data_type_default = { DT_UNDEFINED };
  830. task_desc_info.input_format = input_format.empty() ? format_default : input_format;
  831. task_desc_info.input_shape = input_shape.empty() ? shape_default : input_shape;
  832. task_desc_info.input_data_type = input_data_type.empty() ? data_type_default : input_data_type;
  833. task_desc_info.output_format = output_format.empty() ? format_default : output_format;
  834. task_desc_info.output_shape = output_shape.empty() ? shape_default : output_shape;
  835. task_desc_info.output_data_type = output_data_type.empty() ? data_type_default : output_data_type;
  836. }
  837. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::GetFpBpPoint(
  838. std::string &fp_point, std::string &bp_point) {
  839. // Env or options mode, fp_point_/bp_point_ have initiliazed on profiling init
  840. if (!fp_point_.empty() && !bp_point_.empty()) {
  841. fp_point = fp_point_;
  842. bp_point = bp_point_;
  843. GELOGI("Bp Fp have been initialized in env or options. bp_point: %s, fp_point: %s", bp_point.c_str(),
  844. fp_point.c_str());
  845. return;
  846. }
  847. // ProfApi mode and training trace is set
  848. // Parse options first
  849. char env_profiling_options[MSPROF_OPTIONS_DEF_LEN_MAX] = { 0x00 };
  850. bool is_profiling_valid = false;
  851. std::string profiling_options;
  852. if (ge::GetContext().GetOption(OPTION_EXEC_PROFILING_OPTIONS, profiling_options) == SUCCESS &&
  853. !profiling_options.empty()) {
  854. is_profiling_valid = true;
  855. } else {
  856. INT32 ret = mmGetEnv("PROFILING_OPTIONS", env_profiling_options, MSPROF_OPTIONS_DEF_LEN_MAX);
  857. if (ret != EN_OK) {
  858. GELOGI("PROFILING_OPTIONS env is not exist.");
  859. return;
  860. }
  861. GELOGI("Parse env PROFILING_OPTIONS:%s.", env_profiling_options);
  862. profiling_options = env_profiling_options;
  863. is_profiling_valid = true;
  864. }
  865. if (is_profiling_valid) {
  866. try {
  867. Json prof_options = Json::parse(profiling_options);
  868. fp_point_ = prof_options[kFpPoint];
  869. bp_point_ = prof_options[kBpPoint];
  870. fp_point = fp_point_;
  871. bp_point = bp_point_;
  872. if (!fp_point_.empty() && !bp_point_.empty()) {
  873. GELOGI("Training trace bp fp is set, bp_point:%s, fp_point:%s.", bp_point_.c_str(), fp_point_.c_str());
  874. }
  875. } catch (...) {
  876. GELOGW("Json prof options is invalid.");
  877. return;
  878. }
  879. }
  880. return;
  881. }
  882. } // namespace ge

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