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

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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. #include "graph/load/graph_loader.h"
  27. #include "toolchain/prof_acl_api.h"
  28. #include "common/profiling/profiling_properties.h"
  29. namespace {
  30. const char *const kTrainingTrace = "training_trace";
  31. const char *const kFpPoint = "fp_point";
  32. const char *const kBpPoint = "bp_point";
  33. const int32_t kMaxDeviceNum = 256;
  34. const uint32_t kInteval = 2;
  35. const std::string kConfigNumsdev = "devNums";
  36. const std::string kConfigDevIdList = "devIdList";
  37. const std::string kProfStart = "prof_start";
  38. const std::string kProfStop = "prof_stop";
  39. const std::string kProfModelSubscribe = "prof_model_subscribe";
  40. const std::string kProfModelUnsubscribe = "prof_model_cancel_subscribe";
  41. const std::string kModelName = "model_name";
  42. const std::string kModelId = "model_id";
  43. const std::string kOpNmae = "op_name";
  44. const std::string kOptype = "op_type";
  45. const std::string kBlockDim = "block_dims";
  46. const std::string kTaskId = "task_id";
  47. const std::string kStreamId = "stream_id";
  48. const std::string kThreadId = "thread_id";
  49. const std::string kIndexId = "index_id";
  50. const std::string kTimeStamp = "time_stamp";
  51. const std::string kTagId = "tag_id";
  52. const std::string kShapeType = "shape_type";
  53. const std::string kCurIterNum = "cur_iter_num";
  54. const std::string kTaskType = "task_type";
  55. const std::string kInput = "input";
  56. const std::string kOutput = "output";
  57. const std::string kFormat = "format";
  58. const std::string kDataType = "data_type";
  59. const std::string kShape = "shape";
  60. const std::string kIdx = "idx";
  61. } // namespace
  62. namespace ge {
  63. ProfilingManager::ProfilingManager()
  64. : subscribe_count_(0),
  65. index_id_(UINT64_MAX),
  66. subscribe_info_({false, 0, 0}),
  67. reporter_callback_(nullptr) {
  68. }
  69. ProfilingManager::~ProfilingManager() {}
  70. ProfilingManager &ProfilingManager::Instance() {
  71. static ProfilingManager profiling_manager;
  72. return profiling_manager;
  73. }
  74. void ProfilingManager::ProfilingOpInputOutInfo(const TaskDescInfo &task, Json &task_json) {
  75. for (size_t i = 0; i < task.input_format.size(); i++) {
  76. Json tmp_input;
  77. tmp_input[kIdx] = i;
  78. Format format = task.input_format[i];
  79. tmp_input[kFormat] = TypeUtils::FormatToSerialString(format);
  80. DataType data_type = task.input_data_type[i];
  81. tmp_input[kDataType] = TypeUtils::DataTypeToSerialString(data_type);
  82. tmp_input[kShape] = task.input_shape[i];
  83. task_json[kInput] += tmp_input;
  84. }
  85. for (size_t i = 0; i < task.output_format.size(); i++) {
  86. Json tmp_output;
  87. tmp_output[kIdx] = i;
  88. Format format = task.output_format[i];
  89. tmp_output[kFormat] = TypeUtils::FormatToSerialString(format);
  90. DataType data_type = task.output_data_type[i];
  91. tmp_output[kDataType] = TypeUtils::DataTypeToSerialString(data_type);
  92. tmp_output[kShape] = task.output_shape[i];
  93. task_json[kOutput] += tmp_output;
  94. }
  95. }
  96. void ProfilingManager::ProfilingTaskDescInfo(uint32_t model_id, const std::vector<TaskDescInfo> &task_desc_info,
  97. const int32_t &device_id) {
  98. for (const auto &task : task_desc_info) {
  99. Json task_info;
  100. task_info[kModelName] = task.model_name;
  101. task_info[kModelId] = model_id;
  102. task_info[kOpNmae] = task.op_name;
  103. task_info[kOptype] = task.op_type;
  104. task_info[kBlockDim] = task.block_dim;
  105. task_info[kTaskType] = task.task_type;
  106. task_info[kTaskId] = task.task_id;
  107. task_info[kStreamId] = task.stream_id;
  108. task_info[kCurIterNum] = task.cur_iter_num;
  109. task_info[kShapeType] = task.shape_type;
  110. ProfilingOpInputOutInfo(task, task_info);
  111. std::string reported_data;
  112. try {
  113. reported_data = task_info.dump(kInteval, ' ', false, Json::error_handler_t::ignore);
  114. } catch (std::exception &e) {
  115. GELOGE(FAILED, "[Convert][ReportData]Failed to convert json to string, reason %s.",
  116. e.what());
  117. REPORT_CALL_ERROR("E19999", "Failed to convert reported_data from json to string, reason %s",
  118. e.what());
  119. return ;
  120. } catch (...) {
  121. GELOGE(FAILED, "[Convert][ReportedData]Failed to convert JSON to string");
  122. REPORT_CALL_ERROR("E19999", "Failed to convert reported data from json to string");
  123. return;
  124. }
  125. reported_data.append(",")
  126. .append("\n");
  127. ReportData(device_id, reported_data, "task_desc_info");
  128. }
  129. }
  130. Status ProfilingManager::ProfileStepInfo(uint64_t index_id, uint64_t model_id, uint16_t tag_id, rtStream_t stream,
  131. int32_t device_id) {
  132. auto is_load_profiling = ProfilingProperties::Instance().IsLoadProfiling();
  133. if (!is_load_profiling && subscribe_count_ == 0) {
  134. GELOGD("Profiling is not turned on, no need to profile step info.");
  135. return SUCCESS;
  136. }
  137. GELOGD("Profiling Step Info TraceTask execute async start, index_id = %lu, model_id = %lu, tag_id = %u",
  138. index_id, model_id, tag_id);
  139. rtError_t rt_ret = rtProfilerTraceEx(index_id, model_id, tag_id, stream);
  140. if (rt_ret != RT_ERROR_NONE) {
  141. GELOGE(RT_FAILED, "[Call][rtProfilerTraceEx]Failed, ret 0x%X", rt_ret);
  142. REPORT_CALL_ERROR("E19999", "Call rtProfilerTraceEx failed, ret 0x%X", rt_ret);
  143. return RT_ERROR_TO_GE_STATUS(rt_ret);
  144. }
  145. GELOGD("Profiling Step Info TraceTask execute async success, index_id = %lu, model_id = %lu, tag_id = %u",
  146. index_id, model_id, tag_id);
  147. mmTimespec timespec = mmGetTickCount();
  148. // 1000 ^ 3 converts second to nanosecond
  149. int64_t time = timespec.tv_sec * 1000 * 1000 * 1000 + timespec.tv_nsec;
  150. uint32_t task_id = 0;
  151. uint32_t stream_id = 0;
  152. rt_ret = rtGetTaskIdAndStreamID(&task_id, &stream_id);
  153. if (rt_ret != RT_ERROR_NONE) {
  154. GELOGE(RT_FAILED, "[Get][RtsInfo]Task_id and stream_id failed, ret 0x%X", rt_ret);
  155. REPORT_CALL_ERROR("E19999", "Get task_id and stream_id failed, ret 0x%X", rt_ret);
  156. return RT_ERROR_TO_GE_STATUS(rt_ret);
  157. }
  158. GELOGD("Get profiling args, task_id[%u], stream_id[%u]", task_id, stream_id);
  159. Json step_info;
  160. step_info[kIndexId] = index_id;
  161. step_info[kModelId] = model_id;
  162. step_info[kTimeStamp] = time;
  163. step_info[kTagId] = tag_id;
  164. step_info[kTaskId] = task_id;
  165. step_info[kStreamId] = stream_id;
  166. step_info[kThreadId] = mmGetTid();
  167. std::string reported_data;
  168. try {
  169. reported_data = step_info.dump(kInteval, ' ', false, Json::error_handler_t::ignore);
  170. } catch (std::exception &e) {
  171. GELOGE(FAILED, "Failed to convert JSON to string, reason: %s.", e.what());
  172. GELOGE(FAILED, "[Convert][ReportedData]Failed to convert from json to string, reason: %s",
  173. e.what());
  174. REPORT_CALL_ERROR("E19999", "Failed to convert reported data from json to string, reason: %s",
  175. e.what());
  176. } catch (...) {
  177. GELOGE(FAILED, "[Convert][ReportedData]Failed to convert from json to string");
  178. REPORT_CALL_ERROR("E19999", "Failed to convert reported data from json to string");
  179. }
  180. reported_data.append(",")
  181. .append("\n");
  182. ReportData(device_id, reported_data, "step_info");
  183. return SUCCESS;
  184. }
  185. void ProfilingManager::ReportData(const int32_t &device_id, const string &data, const string &tag_name) {
  186. ReporterData reporter_data{};
  187. int ret = -1;
  188. int32_t cb_ret = -1;
  189. size_t report_max_len = reporter_max_len_;
  190. size_t index = data.size() / report_max_len;
  191. if (index >= 1) {
  192. reporter_data.deviceId = device_id;
  193. ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, tag_name.c_str(), tag_name.size());
  194. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag [%s] memcpy error!", tag_name.c_str()); return;);
  195. std::lock_guard<std::mutex> lock(mutex_report_);
  196. for (size_t i = 0; i < index; ++i) {
  197. reporter_data.data = (unsigned char *)data.c_str() + report_max_len * i;
  198. reporter_data.dataLen = report_max_len;
  199. cb_ret = CallMsprofReport(reporter_data);
  200. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data [%s] failed, ret:%d", tag_name.c_str(), cb_ret);
  201. return;);
  202. }
  203. reporter_data.dataLen = data.size() - report_max_len * index;
  204. if (reporter_data.dataLen != 0) {
  205. reporter_data.data = (unsigned char *)data.c_str() + report_max_len * index;
  206. cb_ret = CallMsprofReport(reporter_data);
  207. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data [%s] failed, ret:%d", tag_name.c_str(), cb_ret);
  208. return;);
  209. }
  210. } else {
  211. reporter_data.deviceId = device_id;
  212. reporter_data.data = (unsigned char *)data.c_str();
  213. reporter_data.dataLen = data.size();
  214. ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, tag_name.c_str(), tag_name.size());
  215. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag [%s] memcpy error!", tag_name.c_str()); return;);
  216. std::lock_guard<std::mutex> lock(mutex_report_);
  217. cb_ret = CallMsprofReport(reporter_data);
  218. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data [%s] failed, ret:%d", tag_name.c_str(), cb_ret);
  219. return;);
  220. }
  221. }
  222. void ProfilingManager::ReportProfilingData(uint32_t model_id, const std::vector<TaskDescInfo> &task_desc_info) {
  223. int32_t logic_device_id = 0;
  224. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  225. if (rt_ret != RT_ERROR_NONE) {
  226. GELOGE(rt_ret, "[Get][LogicDeviceId]Failed, ret 0x%X", rt_ret);
  227. REPORT_CALL_ERROR("E19999", "Get logic device id failed, ret 0x%X", rt_ret);
  228. return;
  229. }
  230. GELOGD("current logic_device_id:%d", logic_device_id);
  231. GELOGD("start ProfilingTaskDescInfo.");
  232. ProfilingTaskDescInfo(model_id, task_desc_info, logic_device_id);
  233. GELOGD("Report profiling data for GE end.");
  234. }
  235. void ProfilingManager::UpdateSubscribeDeviceModuleMap(std::string prof_type, uint32_t device_id, uint64_t module) {
  236. if (prof_type == kProfModelSubscribe) {
  237. if (subs_dev_module_.find(device_id) != subs_dev_module_.end()) {
  238. subs_dev_module_[device_id].subscribe_count++;
  239. } else {
  240. DeviceSubsInfo dev_info;
  241. dev_info.module = module;
  242. dev_info.subscribe_count = 1;
  243. subs_dev_module_[device_id] = dev_info;
  244. }
  245. } else if (prof_type == kProfModelUnsubscribe) {
  246. auto iter = subs_dev_module_.find(device_id);
  247. if (iter != subs_dev_module_.end()) {
  248. if (iter->second.subscribe_count > 0) {
  249. iter->second.subscribe_count--;
  250. }
  251. if (iter->second.subscribe_count == 0) {
  252. subs_dev_module_.erase(iter);
  253. }
  254. }
  255. } else {
  256. GELOGI("No need to update device_id module map.");
  257. }
  258. }
  259. Status ProfilingManager::ProfModelSubscribe(uint64_t module, void *model) {
  260. std::lock_guard<std::mutex> lock(mutex_);
  261. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  262. if ((subscribe_count_ == 0) && (model_load_mask == PROF_MODEL_LOAD_MASK)) {
  263. // register framework to profiling
  264. // register Framework to profiling
  265. int32_t cb_ret = PluginInit();
  266. if (cb_ret != 0) {
  267. GELOGE(cb_ret, "[Init][ProfilingPlugin]Failed, ret %d", cb_ret);
  268. REPORT_CALL_ERROR("E19999", "Init profiling plugin failed, ret %d", cb_ret);
  269. return cb_ret;
  270. }
  271. GELOGI("Prof subscribe: model load profiling on.");
  272. }
  273. subscribe_count_++;
  274. auto davinci_model = static_cast<DavinciModel *>(model);
  275. int32_t device_num = 1;
  276. uint32_t device[1];
  277. device[0] = davinci_model->GetDeviceId();
  278. rtError_t rt_ret = rtProfilerStart(module, device_num, device);
  279. if (rt_ret != RT_ERROR_NONE) {
  280. GELOGE(FAILED, "[Start][Profiler]Malloc buffer failed, ret 0x%X", rt_ret);
  281. REPORT_CALL_ERROR("E19999", "Malloc buffer failed when start profiling, ret 0x%X", rt_ret);
  282. return FAILED;
  283. }
  284. UpdateSubscribeDeviceModuleMap(kProfModelSubscribe, device[0], module);
  285. // Report profiling data
  286. Status p_ret = davinci_model->ReportProfilingData();
  287. if (p_ret != SUCCESS) {
  288. GELOGE(p_ret, "[Report][ProfilingData]Failed, ret %u", p_ret);
  289. REPORT_CALL_ERROR("E19999", "Report profiling data failed, ret %u", p_ret);
  290. return p_ret;
  291. }
  292. return SUCCESS;
  293. }
  294. Status ProfilingManager::ProfModelUnsubscribe(void *model) {
  295. std::lock_guard<std::mutex> lock(mutex_);
  296. if (subscribe_count_ == 0) {
  297. GELOGW("The profiler has not been subscribed, you do not need to cannel the subscription.");
  298. return SUCCESS;
  299. }
  300. auto davinci_model = static_cast<DavinciModel *>(model);
  301. int32_t dev_num = 1;
  302. uint32_t device[1];
  303. device[0] = davinci_model->GetDeviceId();
  304. auto iter = subs_dev_module_.find(device[0]);
  305. if (iter != subs_dev_module_.end()) {
  306. if (subs_dev_module_[device[0]].subscribe_count == 1) {
  307. // The same device_id, only stop at last time
  308. rtError_t rt_ret = rtProfilerStop(subs_dev_module_[device[0]].module, dev_num, device);
  309. if (rt_ret != RT_ERROR_NONE) {
  310. GELOGE(FAILED, "[Stop][Profiler]Malloc buffer Failed, ret %d", rt_ret);
  311. REPORT_CALL_ERROR("E19999", "Malloc buffer failed when stop profiling, ret %d", rt_ret);
  312. return FAILED;
  313. }
  314. }
  315. UpdateSubscribeDeviceModuleMap(kProfModelUnsubscribe, device[0], subs_dev_module_[device[0]].module);
  316. } else {
  317. GELOGE(FAILED, "[Cancel][DeviceId]The device_id %u has not been subscribed, "
  318. "do not need to cancel", device[0]);
  319. REPORT_CALL_ERROR("E19999", "The device_id %u has not been subscribed, do not need to cancel",
  320. device[0]);
  321. return FAILED;
  322. }
  323. subscribe_count_--;
  324. if (subscribe_count_ == 0) {
  325. // profiling plugin uninit at last subscription
  326. PluginUnInit();
  327. }
  328. return SUCCESS;
  329. }
  330. Status ProfilingManager::ProfInit(uint64_t module) {
  331. std::lock_guard<std::mutex> lock(mutex_);
  332. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  333. if (model_load_mask == PROF_MODEL_LOAD_MASK) {
  334. // register Framework to profiling
  335. int32_t cb_ret = PluginInit();
  336. if (cb_ret != 0) {
  337. GELOGE(cb_ret, "[Init][ProfilingPlugin]Failed, ret %d", cb_ret);
  338. REPORT_CALL_ERROR("E19999", "Init profiling plugin failed, ret %d", cb_ret);
  339. return cb_ret;
  340. }
  341. int32_t device_num = -1;
  342. rtError_t rt_ret = rtProfilerStart(model_load_mask, device_num, nullptr);
  343. if (rt_ret != RT_ERROR_NONE) {
  344. GELOGE(FAILED, "[Start][Profiler]Malloc buffer failed, ret 0x%X", rt_ret);
  345. REPORT_CALL_ERROR("E19999", "Malloc buffer failed when start profiling, ret 0x%X", rt_ret);
  346. return FAILED;
  347. }
  348. ProfilingProperties::Instance().SetLoadProfiling(true);
  349. GELOGI("Prof init: model load profiling on.");
  350. }
  351. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE;
  352. if (training_trace_mask == PROF_TRAINING_TRACE) {
  353. ProfilingProperties::Instance().SetTrainingTrace(true);
  354. }
  355. GELOGI("Prof init success.");
  356. return SUCCESS;
  357. }
  358. Status ProfilingManager::ProfFinalize() {
  359. std::lock_guard<std::mutex> lock(mutex_);
  360. index_id_ = UINT64_MAX;
  361. ProfilingProperties::Instance().ClearProperties();
  362. // profiling plugin uninit
  363. PluginUnInit();
  364. CleanSubscribeInfo();
  365. int32_t dev_num = -1;
  366. rtError_t rt_ret = rtProfilerStop(PROF_MODEL_LOAD_MASK, dev_num, nullptr);
  367. if (rt_ret != RT_ERROR_NONE) {
  368. GELOGE(FAILED, "[Stop][Profiler]Malloc buffer failed, ret 0x%X", rt_ret);
  369. REPORT_CALL_ERROR("E19999", "Malloc buffer failed when stop profiling, ret 0x%X", rt_ret);
  370. return FAILED;
  371. }
  372. for (auto device_id_module : device_id_module_map_) {
  373. if (device_id_module.second != 0) {
  374. uint32_t device_id = static_cast<uint32_t>(device_id_module.first);
  375. GELOGI("Prof finalize: device_id: %u, module: 0x%lx.", device_id, device_id_module.second);
  376. rt_ret = rtProfilerStop(device_id_module.second, 1, &device_id);
  377. if (rt_ret != RT_ERROR_NONE) {
  378. GELOGE(FAILED, "[Stop][Profiler]Failed, device_id %d, ret 0x%X", device_id, rt_ret);
  379. REPORT_CALL_ERROR("E19999", "Stop runtime profiler failed, device_id %d, ret 0x%X",
  380. device_id,rt_ret);
  381. return FAILED;
  382. }
  383. }
  384. }
  385. device_id_module_map_.clear();
  386. device_id_.clear();
  387. device_id_map_.clear();
  388. model_id_map_.clear();
  389. GELOGI("Prof finalize success.");
  390. return SUCCESS;
  391. }
  392. Status ProfilingManager::ProfParseDeviceId(const std::map<std::string, std::string> &config_para,
  393. vector<int32_t> &device_list) {
  394. auto iter = config_para.find(kConfigDevIdList);
  395. if (iter != config_para.end()) {
  396. std::string device_id_list = iter->second;
  397. std::string temp;
  398. vector<std::string> decvice_id;
  399. for (uint32_t i = 0; i < device_id_list.size(); i++) {
  400. if (isdigit(device_id_list[i])) {
  401. temp.append(1, device_id_list[i]);
  402. } else {
  403. if (!temp.empty()) {
  404. decvice_id.emplace_back(temp);
  405. }
  406. temp.clear();
  407. }
  408. }
  409. if (!temp.empty()) {
  410. decvice_id.emplace_back(temp);
  411. }
  412. for (uint32_t i = 0; i < decvice_id.size(); i++) {
  413. try {
  414. int32_t dev_id = std::stoi(decvice_id[i]);
  415. device_list.push_back(dev_id);
  416. } catch (std::invalid_argument &) {
  417. GELOGE(FAILED, "[Parse][DeviceId]Failed, it is invalid, %s", decvice_id[i].c_str());
  418. REPORT_CALL_ERROR("E19999", "Parse device id %s failed, it is invalid",
  419. decvice_id[i].c_str());
  420. return FAILED;
  421. } catch (std::out_of_range &) {
  422. GELOGE(FAILED, "[Parse][DeviceId]Failed, it is out of range, %s", decvice_id[i].c_str());
  423. REPORT_CALL_ERROR("E19999", "Parse device id %s failed, it is out of range",
  424. decvice_id[i].c_str());
  425. return FAILED;
  426. } catch (...) {
  427. GELOGE(FAILED, "[Parse][DeviceId]Faield, it cannot change to int, %s",
  428. decvice_id[i].c_str());
  429. REPORT_CALL_ERROR("E19999", "Parse device id %s failed, it cannot change to int",
  430. decvice_id[i].c_str());
  431. return FAILED;
  432. }
  433. }
  434. } else {
  435. GELOGE(FAILED, "[Parse][DeviceId]Config para not contain device id list");
  436. REPORT_CALL_ERROR("E19999", "Parse device id failed, config para not contain device id list");
  437. return FAILED;
  438. }
  439. return SUCCESS;
  440. }
  441. Status ProfilingManager::ProfParseParam(const std::map<std::string, std::string> &config_para, int32_t &device_num,
  442. vector<int32_t> &device_list) {
  443. // device num
  444. auto iter = config_para.find(kConfigNumsdev);
  445. if (iter != config_para.end()) {
  446. try {
  447. device_num = std::stoi(iter->second);
  448. } catch (std::invalid_argument &) {
  449. GELOGE(FAILED, "[Parse][Param]Failed, device num %s is invalid", iter->second.c_str());
  450. REPORT_CALL_ERROR("E19999", "Parse param failed, device num %s is invalid",
  451. iter->second.c_str());
  452. return FAILED;
  453. } catch (std::out_of_range &) {
  454. GELOGE(FAILED, "[Parse][Param]Failed, device num %s cannot change to int",
  455. iter->second.c_str());
  456. REPORT_CALL_ERROR("E19999", "Parse param failed, device num %s cannot change to int",
  457. iter->second.c_str());
  458. return FAILED;
  459. } catch (...) {
  460. GELOGE(FAILED, "[Parse][Param]Failed, device num %s cannot change to int",
  461. iter->second.c_str());
  462. REPORT_CALL_ERROR("E19999", "Parse param failed, device num %s cannot change to int",
  463. iter->second.c_str());
  464. return FAILED;
  465. }
  466. } else {
  467. GELOGE(FAILED, "[Parse][Param]Config para not contain device num %s", iter->second.c_str());
  468. REPORT_CALL_ERROR("E19999", "Parse param failed, config para not contain device num %s",
  469. iter->second.c_str());
  470. return FAILED;
  471. }
  472. // device id
  473. if (ProfParseDeviceId(config_para, device_list) != SUCCESS) {
  474. GELOGE(FAILED, "[Parse][DeviceId]Failed");
  475. REPORT_CALL_ERROR("E19999", "Parse device id failed");
  476. return FAILED;
  477. }
  478. if (device_num == 0 || device_num > kMaxDeviceNum || device_num != static_cast<int32_t>(device_list.size())) {
  479. GELOGE(FAILED, "[Parse][Param]Failed, config para device num %d not equal to "
  480. "device list size %zu", device_num, device_list.size());
  481. REPORT_INNER_ERROR("E19999", "[Parse][Param]Failed, config para device num %d "
  482. "not equal to device list size %zu", device_num, device_list.size());
  483. return FAILED;
  484. }
  485. return SUCCESS;
  486. }
  487. Status ProfilingManager::ProfStartProfiling(uint64_t module, const std::map<std::string, std::string> &config_para) {
  488. std::lock_guard<std::mutex> lock(mutex_);
  489. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE;
  490. if (training_trace_mask == PROF_TRAINING_TRACE) {
  491. ProfilingProperties::Instance().SetTrainingTrace(true);
  492. }
  493. int32_t device_num = 0;
  494. vector<int32_t> device_list;
  495. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  496. GELOGE(FAILED, "[Parse][Param]Prof start parse param failed, device num %d, "
  497. "device list size %zu", device_num, device_list.size());
  498. REPORT_CALL_ERROR("E19999", "Prof start parse param failed, device num %d, "
  499. "device list size %zu", device_num, device_list.size());
  500. return FAILED;
  501. }
  502. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  503. if (device_id_ptr == nullptr) {
  504. GELOGE(FAILED, "[Start][Profiling]Malloc buffer failed when start profiling, device num %d",
  505. device_num);
  506. REPORT_CALL_ERROR("E19999", "Malloc buffer failed when start profiling, device num %d",
  507. device_num);
  508. return FAILED;
  509. }
  510. for (int32_t i = 0; i < device_num; i++) {
  511. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  512. }
  513. GELOGI("Runtime config param: 0x%lx, device num: %d.", module, device_num);
  514. rtError_t rt_ret = rtProfilerStart(module, device_num, device_id_ptr.get());
  515. if (rt_ret != RT_ERROR_NONE) {
  516. GELOGE(FAILED, "[Start][Profiler]Runtime profiler config proc failed, config param 0x%lx, "
  517. "device num %d, ret 0x%X", module, device_num, rt_ret);
  518. REPORT_CALL_ERROR("E19999", "Runtime profiler config proc failed, config param 0x%lx, "
  519. "device num %d, ret 0x%X", module, device_num, rt_ret);
  520. return FAILED;
  521. }
  522. if ((module & PROF_MODEL_EXECUTE_MASK) == PROF_MODEL_EXECUTE_MASK) {
  523. for (int32_t i = 0; i < device_num; i++) {
  524. if (std::find(device_id_.begin(), device_id_.end(), device_list[i]) == device_id_.end()) {
  525. device_id_.push_back(device_list[i]);
  526. }
  527. }
  528. GELOGI("Prof start: ge execute model start profiling.");
  529. }
  530. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  531. GELOGW("Prof start: load model module is invalid.");
  532. }
  533. UpdateDeviceIdModuleMap(kProfStart, module, device_list);
  534. GELOGI("Prof start profiling success.");
  535. return SUCCESS;
  536. }
  537. Status ProfilingManager::ProfStopProfiling(uint64_t module, const std::map<std::string, std::string> &config_para) {
  538. std::lock_guard<std::mutex> lock(mutex_);
  539. int32_t device_num = 0;
  540. vector<int32_t> device_list;
  541. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  542. GELOGE(FAILED, "[Stop][Profiling]Prof stop parse param failed, device num %d, "
  543. "device list size %zu", device_num, device_list.size());
  544. REPORT_CALL_ERROR("E19999", "Prof stop parse param failed, device num %d, device list size %zu",
  545. device_num, device_list.size());
  546. return FAILED;
  547. }
  548. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  549. if (device_id_ptr == nullptr) {
  550. GELOGE(FAILED, "[Stop][Profiling]Malloc buffer failed when stop profiling, device num %d",
  551. device_num);
  552. REPORT_CALL_ERROR("E19999", "Malloc buffer failed when stop profiling, device num %d",
  553. device_num);
  554. return FAILED;
  555. }
  556. for (int32_t i = 0; i < device_num; i++) {
  557. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  558. }
  559. GELOGI("Prof stop: runtime config param: 0x%lx, device num: %d", module, device_num);
  560. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  561. if (rt_ret != RT_ERROR_NONE) {
  562. GELOGE(FAILED, "[Stop][Profiler]Runtime profiler config proc failed, config param 0x%lx, "
  563. "device num: %d, ret 0x%X", module, device_num, rt_ret);
  564. REPORT_CALL_ERROR("E19999", "Runtime profiler config proc failed, config param 0x%lx, "
  565. "device num %d, ret 0x%X", module, device_num, rt_ret);
  566. return FAILED;
  567. }
  568. uint64_t execute_model_mask = module & PROF_MODEL_EXECUTE_MASK;
  569. if (execute_model_mask == PROF_MODEL_EXECUTE_MASK) {
  570. for (int32_t i = 0; i < device_num; i++) {
  571. auto iter = std::find(device_id_.begin(), device_id_.end(), device_list[i]);
  572. if (iter != device_id_.end()) {
  573. device_id_.erase(iter);
  574. }
  575. }
  576. GELOGI("Prof stop: ge execute model stop profiling.");
  577. }
  578. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  579. GELOGW("Prof stop: load model module is invalid.");
  580. }
  581. UpdateDeviceIdModuleMap(kProfStop, module, device_list);
  582. GELOGI("Prof stop profiling success.");
  583. return SUCCESS;
  584. }
  585. void ProfilingManager::UpdateDeviceIdModuleMap(string prof_type, uint64_t module, const vector<int32_t> &device_list) {
  586. if (prof_type == kProfStart) {
  587. for (uint32_t i = 0; i < device_list.size(); i++) {
  588. auto iter = device_id_module_map_.find(device_list[i]);
  589. if (iter != device_id_module_map_.end()) {
  590. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  591. // save all profiling on module of device
  592. device_id_module_map_[device_list[i]] = prof_on_module | module;
  593. } else {
  594. device_id_module_map_[device_list[i]] = module;
  595. }
  596. }
  597. } else if (prof_type == kProfStop) {
  598. for (uint32_t i = 0; i < device_list.size(); i++) {
  599. auto iter = device_id_module_map_.find(device_list[i]);
  600. if (iter != device_id_module_map_.end()) {
  601. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  602. uint64_t prof_off_module = prof_on_module & module;
  603. uint64_t prof_on_left_module = prof_on_module & (~prof_off_module);
  604. // stop profiling on module of device
  605. device_id_module_map_[device_list[i]] = prof_on_left_module;
  606. }
  607. }
  608. } else {
  609. GELOGI("No need to update device_id module map.");
  610. }
  611. }
  612. bool ProfilingManager::ProfilingModelExecuteOn() const {
  613. int32_t logic_device_id = 0;
  614. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  615. if (rt_ret != RT_ERROR_NONE) {
  616. GELOGE(rt_ret, "[Get][LogicDeviceId]Failed, ret 0x%X", rt_ret);
  617. REPORT_CALL_ERROR("E19999", "Get logic device id failed, ret 0x%X", rt_ret);
  618. }
  619. GELOGI("Current logic_device_id:%d", logic_device_id);
  620. bool execute_model_prof_on = false;
  621. auto iter = std::find(device_id_.begin(), device_id_.end(), logic_device_id);
  622. if (iter != device_id_.end()) {
  623. execute_model_prof_on = true;
  624. }
  625. auto is_execute_profiling = ProfilingProperties::Instance().IsExecuteProfiling();
  626. GELOGI("Flag is_execute_profiling: %d, execute_model_prof_on: %d", is_execute_profiling, execute_model_prof_on);
  627. return execute_model_prof_on;
  628. }
  629. Status ProfilingManager::PluginInit() {
  630. if (reporter_callback_ == nullptr) {
  631. GELOGE(ge::PARAM_INVALID, "[Check][Param]MsprofReporterCallback callback is nullptr");
  632. REPORT_INNER_ERROR("E19999", "MsprofReporterCallback callback is nullptr");
  633. return ge::PARAM_INVALID;
  634. }
  635. int32_t cb_ret = reporter_callback_(
  636. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  637. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_INIT),
  638. nullptr, 0);
  639. if (cb_ret != MSPROF_ERROR_NONE) {
  640. REPORT_CALL_ERROR("E19999", "Profiling reporter init failed, ret 0x%X", cb_ret);
  641. GELOGE(INTERNAL_ERROR, "[Init][ProfilingReporter]Failed, ret 0x%X", cb_ret);
  642. return INTERNAL_ERROR;
  643. }
  644. cb_ret = reporter_callback_(
  645. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  646. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_DATA_MAX_LEN),
  647. &reporter_max_len_, sizeof(uint32_t));
  648. if (cb_ret != MSPROF_ERROR_NONE) {
  649. REPORT_CALL_ERROR("E19999", "Get profiling reporter data max len failed, ret 0x%X", cb_ret);
  650. GELOGE(INTERNAL_ERROR, "[Get][ProfilingDataMaxLen]Failed, ret 0x%X", cb_ret);
  651. return INTERNAL_ERROR;
  652. }
  653. return SUCCESS;
  654. }
  655. void ProfilingManager::PluginUnInit() const {
  656. if (reporter_callback_ == nullptr) {
  657. GELOGE(ge::PARAM_INVALID, "[Check][Param]MsprofReporterCallback callback is nullptr");
  658. REPORT_INNER_ERROR("E19999", "MsprofReporterCallback callback is nullptr");
  659. return;
  660. }
  661. int32_t cb_ret = reporter_callback_(
  662. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  663. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_UNINIT),
  664. nullptr, 0);
  665. if (cb_ret != 0) {
  666. GELOGW("profiling plugin uninit failed, ret:%d", cb_ret);
  667. }
  668. }
  669. Status ProfilingManager::CallMsprofReport(ReporterData &reporter_data) const {
  670. if (reporter_callback_ == nullptr) {
  671. GELOGE(ge::PARAM_INVALID, "[Check][Param]MsprofReporterCallback callback is nullptr");
  672. REPORT_INNER_ERROR("E19999", "MsprofReporterCallback callback is nullptr");
  673. return ge::PARAM_INVALID;
  674. }
  675. return reporter_callback_(
  676. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  677. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_REPORT),
  678. static_cast<void *>(&reporter_data), sizeof(ReporterData));
  679. }
  680. void ProfilingManager::GetOpInputInfo(const OpDescPtr &op, TaskDescInfo &task_desc_info) const {
  681. std::vector<Format> input_format;
  682. std::vector<std::vector<int64_t>> input_shape;
  683. std::vector<DataType> input_data_type;
  684. for (size_t i = 0; i < op->GetAllInputsSize(); ++i) {
  685. GeTensorDescPtr input_tensor_desc = op->MutableInputDesc(i);
  686. if (input_tensor_desc == nullptr) {
  687. continue;
  688. }
  689. input_format.emplace_back(input_tensor_desc->GetFormat());
  690. input_shape.emplace_back(input_tensor_desc->GetShape().GetDims());
  691. input_data_type.emplace_back(input_tensor_desc->GetDataType());
  692. }
  693. std::vector<Format> format_default = { FORMAT_NULL };
  694. std::vector<std::vector<int64_t>> shape_default = { {0} };
  695. std::vector<DataType> data_type_default = { DT_UNDEFINED };
  696. task_desc_info.input_format = input_format.empty() ? format_default : input_format;
  697. task_desc_info.input_shape = input_shape.empty() ? shape_default : input_shape;
  698. task_desc_info.input_data_type = input_data_type.empty() ? data_type_default : input_data_type;
  699. }
  700. void ProfilingManager::GetOpOutputInfo(const OpDescPtr &op, TaskDescInfo &task_desc_info) const {
  701. std::vector<Format> output_format;
  702. std::vector<std::vector<int64_t>> output_shape;
  703. std::vector<DataType> output_data_type;
  704. for (size_t j = 0; j < op->GetOutputsSize(); ++j) {
  705. GeTensorDescPtr output_tensor_desc = op->MutableOutputDesc(j);
  706. if (output_tensor_desc == nullptr) {
  707. continue;
  708. }
  709. output_format.emplace_back(output_tensor_desc->GetFormat());
  710. output_shape.emplace_back(output_tensor_desc->GetShape().GetDims());
  711. output_data_type.emplace_back(output_tensor_desc->GetDataType());
  712. }
  713. std::vector<Format> format_default = { FORMAT_NULL };
  714. std::vector<std::vector<int64_t>> shape_default = { {0} };
  715. std::vector<DataType> data_type_default = { DT_UNDEFINED };
  716. task_desc_info.output_format = output_format.empty() ? format_default : output_format;
  717. task_desc_info.output_shape = output_shape.empty() ? shape_default : output_shape;
  718. task_desc_info.output_data_type = output_data_type.empty() ? data_type_default : output_data_type;
  719. }
  720. void ProfilingManager::GetOpInputOutputInfo(const OpDescPtr &op, TaskDescInfo &task_desc_info) const {
  721. GetOpInputInfo(op, task_desc_info);
  722. GetOpOutputInfo(op, task_desc_info);
  723. }
  724. void ProfilingManager::GetFpBpPoint(std::string &fp_point, std::string &bp_point) {
  725. // Env or options mode, fp_point_/bp_point_ have initiliazed on profiling init
  726. if (!fp_point_.empty() && !bp_point_.empty()) {
  727. fp_point = fp_point_;
  728. bp_point = bp_point_;
  729. GELOGI("Bp Fp have been initialized in env or options. bp_point: %s, fp_point: %s", bp_point.c_str(),
  730. fp_point.c_str());
  731. return;
  732. }
  733. // ProfApi mode and training trace is set
  734. // Parse options first
  735. char env_profiling_options[MSPROF_OPTIONS_DEF_LEN_MAX] = {0x00};
  736. bool is_profiling_valid = false;
  737. std::string profiling_options;
  738. if (ge::GetContext().GetOption(OPTION_EXEC_PROFILING_OPTIONS, profiling_options) == SUCCESS &&
  739. !profiling_options.empty()) {
  740. is_profiling_valid = true;
  741. } else {
  742. INT32 ret = mmGetEnv("PROFILING_OPTIONS", env_profiling_options, MSPROF_OPTIONS_DEF_LEN_MAX);
  743. if (ret != EN_OK) {
  744. GELOGI("PROFILING_OPTIONS env is not exist.");
  745. return;
  746. }
  747. GELOGI("Parse env PROFILING_OPTIONS:%s.", env_profiling_options);
  748. profiling_options = env_profiling_options;
  749. is_profiling_valid = true;
  750. }
  751. if (is_profiling_valid) {
  752. try {
  753. Json prof_options = Json::parse(profiling_options);
  754. if (prof_options.contains(kFpPoint)) {
  755. fp_point_ = prof_options[kFpPoint];
  756. }
  757. if (prof_options.contains(kBpPoint)) {
  758. bp_point_ = prof_options[kBpPoint];
  759. }
  760. fp_point = fp_point_;
  761. bp_point = bp_point_;
  762. if (!fp_point_.empty() && !bp_point_.empty()) {
  763. GELOGI("Training trace bp fp is set, bp_point:%s, fp_point:%s.", bp_point_.c_str(), fp_point_.c_str());
  764. }
  765. } catch (...) {
  766. GELOGW("Json prof options is invalid.");
  767. return;
  768. }
  769. }
  770. return;
  771. }
  772. Status ProfilingManager::GetDeviceIdFromGraph(uint32_t graph_id, uint32_t &device_id) {
  773. auto iter = device_id_map_.find(graph_id);
  774. if (iter != device_id_map_.end()) {
  775. device_id = iter->second;
  776. return SUCCESS;
  777. }
  778. REPORT_CALL_ERROR("E19999", "graph_id:%u does not exist!", graph_id);
  779. GELOGE(PARAM_INVALID, "[Check][GraphId]graph_id:%u does not exist!", graph_id);
  780. return FAILED;
  781. }
  782. void ProfilingManager::SetSubscribeInfo(uint64_t prof_switch, uint32_t model_id, bool is_subscribe) {
  783. subscribe_info_.is_subscribe = is_subscribe;
  784. subscribe_info_.prof_switch = prof_switch;
  785. subscribe_info_.graph_id = model_id;
  786. }
  787. void ProfilingManager::CleanSubscribeInfo() {
  788. subscribe_info_.is_subscribe = false;
  789. subscribe_info_.prof_switch = 0;
  790. subscribe_info_.graph_id = 0;
  791. }
  792. Status ProfilingManager::GetModelIdFromGraph(uint32_t graph_id, uint32_t &model_id) {
  793. auto iter = model_id_map_.find(graph_id);
  794. if (iter != model_id_map_.end()) {
  795. model_id = iter->second;
  796. return SUCCESS;
  797. }
  798. REPORT_CALL_ERROR("E19999", "graph_id:%u does not exist!", graph_id);
  799. GELOGE(PARAM_INVALID, "[Check][GraphId]graph_id:%u does not exist!", graph_id);
  800. return FAILED;
  801. }
  802. } // namespace ge

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