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

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