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 45 kB

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

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