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

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