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.

opdebug_register.cc 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "opdebug_register.h"
  17. namespace {
  18. const size_t kOpDebugMemorySize = 2048UL;
  19. const size_t kDebugP2pSize = 8UL;
  20. } // namespace
  21. namespace ge {
  22. OpdebugRegister::~OpdebugRegister() {}
  23. Status OpdebugRegister::RegisterDebugForModel(rtModel_t model_handle, uint32_t op_debug_mode, DataDumper &data_dumper) {
  24. GELOGD("Start to register debug for model in overflow");
  25. auto ret = MallocMemForOpdebug();
  26. if (ret != SUCCESS) {
  27. GELOGE(ret, "Malloc memory for opdebug in model overflow failed ,ret:0x%X", ret);
  28. return ret;
  29. }
  30. uint32_t debug_stream_id = 0;
  31. uint32_t debug_task_id = 0;
  32. auto rt_ret = rtDebugRegister(model_handle, op_debug_mode, op_debug_addr_, &debug_stream_id, &debug_task_id);
  33. if (rt_ret != RT_ERROR_NONE) {
  34. GELOGE(RT_FAILED, "rtDebugRegister error, ret: 0x%X", rt_ret);
  35. return RT_ERROR_TO_GE_STATUS(rt_ret);
  36. }
  37. GELOGD("debug_task_id:%u, debug_stream_id:%u in model overflow", debug_task_id, debug_stream_id);
  38. data_dumper.SaveOpDebugId(debug_task_id, debug_stream_id, p2p_debug_addr_, true);
  39. return SUCCESS;
  40. }
  41. void OpdebugRegister::UnregisterDebugForModel(rtModel_t model_handle) {
  42. rtError_t rt_ret = RT_ERROR_NONE;
  43. if (model_handle != nullptr) {
  44. GELOGD("start to call rtDebugUnRegister in model overflow.");
  45. rt_ret = rtDebugUnRegister(model_handle);
  46. if (rt_ret != RT_ERROR_NONE) {
  47. GELOGW("rtDebugUnRegister failed, ret: 0x%X", rt_ret);
  48. }
  49. }
  50. if (op_debug_addr_ != nullptr) {
  51. rt_ret = rtFree(op_debug_addr_);
  52. if (rt_ret != RT_ERROR_NONE) {
  53. GELOGW("rtFree failed, ret: 0x%X", rt_ret);
  54. }
  55. op_debug_addr_ = nullptr;
  56. }
  57. if (p2p_debug_addr_ != nullptr) {
  58. rt_ret = rtFree(p2p_debug_addr_);
  59. if (rt_ret != RT_ERROR_NONE) {
  60. GELOGW("rtFree failed, ret: 0x%X", rt_ret);
  61. }
  62. p2p_debug_addr_ = nullptr;
  63. }
  64. return;
  65. }
  66. Status OpdebugRegister::RegisterDebugForStream(rtStream_t stream, uint32_t op_debug_mode, DataDumper &data_dumper) {
  67. GELOGD("Start to register debug for stream in stream overflow");
  68. auto ret = MallocMemForOpdebug();
  69. if (ret != SUCCESS) {
  70. GELOGE(ret, "Malloc memory for opdebug in stream overflow ,ret:0x%X", ret);
  71. return ret;
  72. }
  73. uint32_t debug_stream_id = 0;
  74. uint32_t debug_task_id = 0;
  75. auto rt_ret = rtDebugRegisterForStream(stream, op_debug_mode, op_debug_addr_, &debug_stream_id, &debug_task_id);
  76. if (rt_ret != RT_ERROR_NONE) {
  77. GELOGE(RT_FAILED, "rtDebugRegisterForStream error, ret: 0x%X", rt_ret);
  78. return RT_ERROR_TO_GE_STATUS(rt_ret);
  79. }
  80. GELOGD("debug_task_id:%u, debug_stream_id:%u in stream overflow.", debug_task_id, debug_stream_id);
  81. data_dumper.SaveOpDebugId(debug_task_id, debug_stream_id, p2p_debug_addr_, true);
  82. return SUCCESS;
  83. }
  84. void OpdebugRegister::UnregisterDebugForStream(rtStream_t stream) {
  85. rtError_t rt_ret = RT_ERROR_NONE;
  86. if (stream != nullptr) {
  87. GELOGD("start call rtDebugUnRegisterForStream in unknown shape over flow.");
  88. rt_ret = rtDebugUnRegisterForStream(stream);
  89. if (rt_ret != RT_ERROR_NONE) {
  90. GELOGW("rtDebugUnRegisterForStream failed, ret: 0x%X", rt_ret);
  91. }
  92. }
  93. if (op_debug_addr_ != nullptr) {
  94. rt_ret = rtFree(op_debug_addr_);
  95. if (rt_ret != RT_ERROR_NONE) {
  96. GELOGW("rtFree failed, ret: 0x%X", rt_ret);
  97. }
  98. op_debug_addr_ = nullptr;
  99. }
  100. if (p2p_debug_addr_ != nullptr) {
  101. rt_ret = rtFree(p2p_debug_addr_);
  102. if (rt_ret != RT_ERROR_NONE) {
  103. GELOGW("rtFree failed, ret: 0x%X", rt_ret);
  104. }
  105. p2p_debug_addr_ = nullptr;
  106. }
  107. return;
  108. }
  109. Status OpdebugRegister::MallocMemForOpdebug() {
  110. rtError_t rt_ret = rtMalloc(&op_debug_addr_, kOpDebugMemorySize, RT_MEMORY_DDR);
  111. if (rt_ret != RT_ERROR_NONE) {
  112. GELOGE(RT_FAILED, "rtMalloc error, ret: 0x%X", rt_ret);
  113. return RT_ERROR_TO_GE_STATUS(rt_ret);
  114. }
  115. uint64_t debug_addrs_tmp = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(op_debug_addr_));
  116. // For data dump, aicpu needs the pointer to pointer that save the real debug address.
  117. rt_ret = rtMalloc(&p2p_debug_addr_, kDebugP2pSize, RT_MEMORY_HBM);
  118. if (rt_ret != RT_ERROR_NONE) {
  119. GELOGE(RT_FAILED, "rtMalloc error, ret: 0x%X", rt_ret);
  120. return RT_ERROR_TO_GE_STATUS(rt_ret);
  121. }
  122. rt_ret = rtMemcpy(p2p_debug_addr_, sizeof(uint64_t), &debug_addrs_tmp, sizeof(uint64_t), RT_MEMCPY_HOST_TO_DEVICE);
  123. if (rt_ret != RT_ERROR_NONE) {
  124. GELOGE(RT_FAILED, "rtMemcpy to p2p_addr error: 0x%X", rt_ret);
  125. return RT_ERROR_TO_GE_STATUS(rt_ret);
  126. }
  127. return SUCCESS;
  128. }
  129. } // namespace ge

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