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

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