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

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