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.

dump_op.cc 10 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * Copyright 2019-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/dump/dump_op.h"
  17. #include "common/dump/dump_manager.h"
  18. #include "common/ge/datatype_util.h"
  19. #include "framework/common/debug/ge_log.h"
  20. #include "framework/common/util.h"
  21. #include "graph/anchor.h"
  22. #include "graph/ge_tensor.h"
  23. #include "graph/op_desc.h"
  24. #include "graph/utils/tensor_utils.h"
  25. #include "proto/ge_ir.pb.h"
  26. #include "proto/op_mapping_info.pb.h"
  27. #include "runtime/mem.h"
  28. #include "aicpu/common/aicpu_task_struct.h"
  29. namespace {
  30. const uint32_t kAicpuLoadFlag = 1;
  31. const char *const kDumpOutput = "output";
  32. const char *const kDumpInput = "input";
  33. const char *const kDumpAll = "all";
  34. const char *const kDumpKernelsDumpOp = "DumpDataInfo";
  35. } // namespace
  36. namespace ge {
  37. DumpOp::~DumpOp() {
  38. if (proto_dev_mem_ != nullptr) {
  39. (void)rtFree(proto_dev_mem_);
  40. }
  41. if (proto_size_dev_mem_ != nullptr) {
  42. (void)rtFree(proto_size_dev_mem_);
  43. }
  44. proto_dev_mem_ = nullptr;
  45. proto_size_dev_mem_ = nullptr;
  46. }
  47. void DumpOp::SetLoopAddr(void *global_step, void *loop_per_iter, void *loop_cond) {
  48. global_step_ = reinterpret_cast<uintptr_t>(global_step);
  49. loop_per_iter_ = reinterpret_cast<uintptr_t>(loop_per_iter);
  50. loop_cond_ = reinterpret_cast<uintptr_t>(loop_cond);
  51. }
  52. void DumpOp::SetDynamicModelInfo(const string &dynamic_model_name, uint32_t dynamic_model_id) {
  53. dynamic_model_name_ = dynamic_model_name;
  54. dynamic_model_id_ = dynamic_model_id;
  55. }
  56. static void SetOpMappingLoopAddr(uintptr_t step_id, uintptr_t loop_per_iter, uintptr_t loop_cond,
  57. aicpu::dump::OpMappingInfo &op_mapping_info) {
  58. if (step_id != 0) {
  59. GELOGI("step_id exists.");
  60. op_mapping_info.set_step_id_addr(static_cast<uint64_t>(step_id));
  61. } else {
  62. GELOGI("step_id is null.");
  63. }
  64. if (loop_per_iter != 0) {
  65. GELOGI("loop_per_iter exists.");
  66. op_mapping_info.set_iterations_per_loop_addr(static_cast<uint64_t>(loop_per_iter));
  67. } else {
  68. GELOGI("loop_per_iter is null.");
  69. }
  70. if (loop_cond != 0) {
  71. GELOGI("loop_cond exists.");
  72. op_mapping_info.set_loop_cond_addr(static_cast<uint64_t>(loop_cond));
  73. } else {
  74. GELOGI("loop_cond is null.");
  75. }
  76. }
  77. Status DumpOp::DumpOutput(aicpu::dump::Task &task) {
  78. GELOGI("Start dump output in Launch dump op");
  79. const auto &output_descs = op_desc_->GetAllOutputsDesc();
  80. for (size_t i = 0; i < output_descs.size(); ++i) {
  81. aicpu::dump::Output output;
  82. output.set_data_type(static_cast<int32_t>(DataTypeUtil::GetIrDataType(output_descs.at(i).GetDataType())));
  83. output.set_format(static_cast<int32_t>(output_descs.at(i).GetFormat()));
  84. for (auto dim : output_descs.at(i).GetShape().GetDims()) {
  85. output.mutable_shape()->add_dim(dim);
  86. }
  87. for (auto dim : output_descs.at(i).GetOriginShape().GetDims()) {
  88. output.mutable_origin_shape()->add_dim(dim);
  89. }
  90. int64_t output_size = 0;
  91. if (TensorUtils::GetTensorSizeInBytes(output_descs.at(i), output_size) != SUCCESS) {
  92. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR, "Get output size filed");
  93. return ACL_ERROR_GE_INTERNAL_ERROR;
  94. }
  95. GELOGD("Get output size in lanch dump op is %ld", output_size);
  96. output.set_size(output_size);
  97. output.set_address(static_cast<uint64_t>(output_addrs_[i]));
  98. task.mutable_output()->Add(std::move(output));
  99. }
  100. return SUCCESS;
  101. }
  102. Status DumpOp::DumpInput(aicpu::dump::Task &task) {
  103. GELOGI("Start dump input in Launch dump op");
  104. const auto &input_descs = op_desc_->GetAllInputsDesc();
  105. for (size_t i = 0; i < input_descs.size(); ++i) {
  106. aicpu::dump::Input input;
  107. input.set_data_type(static_cast<int32_t>(DataTypeUtil::GetIrDataType(input_descs.at(i).GetDataType())));
  108. input.set_format(static_cast<int32_t>(input_descs.at(i).GetFormat()));
  109. for (auto dim : input_descs.at(i).GetShape().GetDims()) {
  110. input.mutable_shape()->add_dim(dim);
  111. }
  112. for (auto dim : input_descs.at(i).GetOriginShape().GetDims()) {
  113. input.mutable_origin_shape()->add_dim(dim);
  114. }
  115. int64_t input_size = 0;
  116. if (TensorUtils::GetTensorSizeInBytes(input_descs.at(i), input_size) != SUCCESS) {
  117. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR, "Get output size filed");
  118. return ACL_ERROR_GE_INTERNAL_ERROR;
  119. }
  120. GELOGD("Get input size in lanch dump op is %ld", input_size);
  121. input.set_size(input_size);
  122. input.set_address(static_cast<uint64_t>(input_addrs_[i]));
  123. task.mutable_input()->Add(std::move(input));
  124. }
  125. return SUCCESS;
  126. }
  127. void DumpOp::SetDumpInfo(const DumpProperties &dump_properties, const OpDescPtr &op_desc, vector<uintptr_t> input_addrs,
  128. vector<uintptr_t> output_addrs, rtStream_t stream) {
  129. dump_properties_ = dump_properties;
  130. op_desc_ = op_desc;
  131. input_addrs_ = input_addrs;
  132. output_addrs_ = output_addrs;
  133. stream_ = stream;
  134. }
  135. Status DumpOp::ExecutorDumpOp(aicpu::dump::OpMappingInfo &op_mapping_info) {
  136. std::string proto_msg;
  137. size_t proto_size = op_mapping_info.ByteSizeLong();
  138. bool ret = op_mapping_info.SerializeToString(&proto_msg);
  139. if (!ret || proto_size == 0) {
  140. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR, "Protobuf serialize failed, proto_size is %zu", proto_size);
  141. return ACL_ERROR_GE_INTERNAL_ERROR;
  142. }
  143. rtError_t rt_ret = rtMalloc(&proto_dev_mem_, proto_size, RT_MEMORY_HBM);
  144. if (rt_ret != RT_ERROR_NONE) {
  145. GELOGE(rt_ret, "Call rtMalloc failed, ret: 0x%X", rt_ret);
  146. return RT_ERROR_TO_GE_STATUS(rt_ret);
  147. }
  148. rt_ret = rtMemcpy(proto_dev_mem_, proto_size, proto_msg.c_str(), proto_size, RT_MEMCPY_HOST_TO_DEVICE);
  149. if (rt_ret != RT_ERROR_NONE) {
  150. GELOGE(rt_ret, "Call rtMemcpy failed, ret: 0x%X", rt_ret);
  151. return RT_ERROR_TO_GE_STATUS(rt_ret);
  152. }
  153. rt_ret = rtMalloc(&proto_size_dev_mem_, sizeof(size_t), RT_MEMORY_HBM);
  154. if (rt_ret != RT_ERROR_NONE) {
  155. GELOGE(rt_ret, "Call rtMalloc failed, ret: 0x%X", rt_ret);
  156. return RT_ERROR_TO_GE_STATUS(rt_ret);
  157. }
  158. rt_ret = rtMemcpy(proto_size_dev_mem_, sizeof(size_t), &proto_size, sizeof(size_t), RT_MEMCPY_HOST_TO_DEVICE);
  159. if (rt_ret != RT_ERROR_NONE) {
  160. GELOGE(rt_ret, "Call rtMemcpy failed, ret: 0x%X", rt_ret);
  161. return RT_ERROR_TO_GE_STATUS(rt_ret);
  162. }
  163. constexpr int32_t io_addr_num = 2;
  164. constexpr uint32_t args_size = sizeof(aicpu::AicpuParamHead) + io_addr_num * sizeof(uint64_t);
  165. char args[args_size] = {0};
  166. auto param_head = reinterpret_cast<aicpu::AicpuParamHead *>(args);
  167. param_head->length = args_size;
  168. param_head->ioAddrNum = io_addr_num;
  169. auto io_addr = reinterpret_cast<uint64_t *>(args + sizeof(aicpu::AicpuParamHead));
  170. io_addr[0] = reinterpret_cast<uintptr_t>(proto_dev_mem_);
  171. io_addr[1] = reinterpret_cast<uintptr_t>(proto_size_dev_mem_);
  172. rt_ret = rtCpuKernelLaunch(nullptr, kDumpKernelsDumpOp,
  173. 1, // blockDim default 1
  174. args, args_size,
  175. nullptr, // no need smDesc
  176. stream_);
  177. if (rt_ret != RT_ERROR_NONE) {
  178. GELOGE(rt_ret, "Call rtCpuKernelLaunch failed,rt_ret:0x%X", rt_ret);
  179. return RT_ERROR_TO_GE_STATUS(rt_ret);
  180. }
  181. GELOGI("Kernel launch dump op success");
  182. return SUCCESS;
  183. }
  184. Status DumpOp::LaunchDumpOp() {
  185. GELOGI("Start to launch dump op %s", op_desc_->GetName().c_str());
  186. int32_t device_id = 0;
  187. rtError_t rt_ret = rtGetDevice(&device_id);
  188. if (rt_ret != RT_ERROR_NONE) {
  189. GELOGE(rt_ret, "Call rtGetDevice failed, ret = 0x%X, device_id = %d.", rt_ret, device_id);
  190. return RT_ERROR_TO_GE_STATUS(rt_ret);
  191. }
  192. if (device_id < 0) {
  193. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR,
  194. "Check device_id failed, device_id = %d, which should be not less than 0.",
  195. device_id);
  196. return ACL_ERROR_GE_INTERNAL_ERROR;
  197. }
  198. aicpu::dump::OpMappingInfo op_mapping_info;
  199. auto dump_path = dump_properties_.GetDumpPath() + std::to_string(device_id) + "/";
  200. op_mapping_info.set_dump_path(dump_path);
  201. op_mapping_info.set_flag(kAicpuLoadFlag);
  202. op_mapping_info.set_dump_step(dump_properties_.GetDumpStep());
  203. if (!dynamic_model_name_.empty()) {
  204. op_mapping_info.set_model_name(dynamic_model_name_);
  205. op_mapping_info.set_model_id(dynamic_model_id_);
  206. }
  207. SetOpMappingLoopAddr(global_step_, loop_per_iter_, loop_cond_, op_mapping_info);
  208. GELOGI("Dump step is %s ,dump path is %s ,in Launch dump op", dump_properties_.GetDumpStep().c_str(),
  209. dump_path.c_str());
  210. uint32_t task_id = 0;
  211. uint32_t stream_id = 0;
  212. rt_ret = rtGetTaskIdAndStreamID(&task_id, &stream_id);
  213. if (rt_ret != RT_ERROR_NONE) {
  214. GELOGW("call rtGetTaskIdAndStreamID failed, ret = 0x%X", rt_ret);
  215. }
  216. aicpu::dump::Task task;
  217. task.set_task_id(task_id);
  218. task.set_stream_id(stream_id);
  219. task.mutable_op()->set_op_name(op_desc_->GetName());
  220. task.mutable_op()->set_op_type(op_desc_->GetType());
  221. if (dump_properties_.GetDumpMode() == kDumpOutput) {
  222. auto ret = DumpOutput(task);
  223. if (ret != SUCCESS) {
  224. GELOGE(ret, "Dump output failed");
  225. return ret;
  226. }
  227. op_mapping_info.mutable_task()->Add(std::move(task));
  228. }
  229. if (dump_properties_.GetDumpMode() == kDumpInput) {
  230. auto ret = DumpInput(task);
  231. if (ret != SUCCESS) {
  232. GELOGE(ret, "Dump input failed");
  233. return ret;
  234. }
  235. op_mapping_info.mutable_task()->Add(std::move(task));
  236. }
  237. if (dump_properties_.GetDumpMode() == kDumpAll) {
  238. auto ret = DumpOutput(task);
  239. if (ret != SUCCESS) {
  240. GELOGE(ret, "Dump output failed when in dumping all");
  241. return ret;
  242. }
  243. ret = DumpInput(task);
  244. if (ret != SUCCESS) {
  245. GELOGE(ret, "Dump input failed when in dumping all");
  246. return ret;
  247. }
  248. op_mapping_info.mutable_task()->Add(std::move(task));
  249. }
  250. auto ret = ExecutorDumpOp(op_mapping_info);
  251. if (ret != SUCCESS) {
  252. GELOGE(ret, "Executor dump op failed");
  253. return ret;
  254. }
  255. return SUCCESS;
  256. }
  257. } // namesapce ge

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