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

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