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.

hybrid_model_executor.cc 5.5 kB

4 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
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "hybrid_model_executor.h"
  17. #include "graph/ge_context.h"
  18. #include "graph/runtime_inference_context.h"
  19. #include "common/dump/dump_manager.h"
  20. namespace ge {
  21. namespace hybrid {
  22. namespace {
  23. const int kIntBase = 10;
  24. const char *const kEnvProfilingLevel = "HYBRID_PROFILING_LEVEL";
  25. } // namespace
  26. HybridModelExecutor::HybridModelExecutor(HybridModel *model, uint32_t device_id, rtStream_t stream)
  27. : model_(model), device_id_(device_id), stream_(stream) {
  28. }
  29. HybridModelExecutor::~HybridModelExecutor() {
  30. if (context_.rt_gen_context != nullptr) {
  31. (void) rtCtxDestroy(context_.rt_gen_context);
  32. }
  33. if (context_.global_step != nullptr) {
  34. (void) rtFree(context_.global_step);
  35. }
  36. }
  37. Status HybridModelExecutor::Init() {
  38. GELOGD("Start to init HybridGraphEngine.");
  39. GE_CHK_STATUS_RET_NOLOG(InitExecutionContext());
  40. GELOGD("HybridGraphEngine initialized successfully.");
  41. return SUCCESS;
  42. }
  43. Status HybridModelExecutor::Execute(HybridModelExecutor::ExecuteArgs &args) {
  44. GELOGD("Start to execute model.");
  45. auto root_graph_item = model_->GetRootGraphItem();
  46. GE_CHECK_NOTNULL(root_graph_item);
  47. GE_CHK_RT_RET(rtMemcpyAsync(context_.global_step, sizeof(uint64_t), &context_.iteration,
  48. sizeof(uint64_t), RT_MEMCPY_HOST_TO_DEVICE_EX, context_.stream));
  49. SubgraphExecutor executor(model_->GetRootGraphItem(), &context_);
  50. auto ret = ExecuteGraphInternal(executor, args);
  51. Cleanup();
  52. RECORD_MODEL_EXECUTION_EVENT(&context_, "[Cleanup] End");
  53. GELOGD("Model executed successfully.");
  54. if (context_.profiler != nullptr) {
  55. context_.profiler->Dump(std::cout);
  56. context_.profiler->Reset();
  57. }
  58. context_.iteration += 1;
  59. if (ret == END_OF_SEQUENCE) {
  60. args.is_eos = true;
  61. } else {
  62. GE_CHK_STATUS_RET(ret, "Failed to execute model");
  63. }
  64. return SUCCESS;
  65. }
  66. Status HybridModelExecutor::ExecuteGraphInternal(SubgraphExecutor &executor,
  67. HybridModelExecutor::ExecuteArgs &args) {
  68. RECORD_MODEL_EXECUTION_EVENT(&context_, "[InitContext] Start");
  69. GE_CHK_STATUS_RET_NOLOG(ResetExecutionContext(context_));
  70. RECORD_MODEL_EXECUTION_EVENT(&context_, "[InitContext] End");
  71. HYBRID_CHK_STATUS_RET(executor.ExecuteAsync(args.inputs, args.input_desc, args.outputs),
  72. "Failed to execute partitioned call.");
  73. RECORD_MODEL_EXECUTION_EVENT(&context_, "[ExecuteAsync] End");
  74. HYBRID_CHK_STATUS_RET(executor.Synchronize(), "Failed to sync root graph.");
  75. RECORD_MODEL_EXECUTION_EVENT(&context_, "[Synchronize] End");
  76. args.outputs.clear();
  77. HYBRID_CHK_STATUS_RET(executor.GetOutputs(args.outputs, args.output_desc), "Failed to get outputs");
  78. RECORD_MODEL_EXECUTION_EVENT(&context_, "[GetOutput] End");
  79. return SUCCESS;
  80. }
  81. Status HybridModelExecutor::Cleanup() {
  82. GELOGD("Start to cleanup.");
  83. context_.callback_manager->Destroy();
  84. RuntimeInferenceContext::DestroyContext(std::to_string(context_.context_id));
  85. GELOGD("Cleanup successfully.");
  86. return SUCCESS;
  87. }
  88. Status HybridModelExecutor::InitExecutionContext() {
  89. GE_CHK_RT_RET(rtCtxGetCurrent(&context_.rt_context));
  90. GE_CHK_RT_RET(rtCtxCreate(&context_.rt_gen_context, RT_CTX_GEN_MODE, 0));
  91. GE_CHK_RT_RET(rtCtxSetCurrent(context_.rt_context));
  92. GE_CHK_RT_RET(rtMalloc(&context_.global_step, sizeof(uint64_t), RT_MEMORY_HBM));
  93. context_.stream = stream_;
  94. context_.model = model_;
  95. context_.is_eos_ = false;
  96. context_.session_id = ::ge::GetContext().SessionId();
  97. context_.ge_context = &GetThreadLocalContext();
  98. GELOGD("session id from model = %lu, from context = %lu", model_->GetSessionId(), context_.session_id);
  99. context_.allocator = NpuMemoryAllocator::GetAllocator(device_id_);
  100. GE_CHECK_NOTNULL(context_.allocator);
  101. context_.callback_manager = std::unique_ptr<CallbackManager>(new(std::nothrow)CallbackManager());
  102. GE_CHECK_NOTNULL(context_.callback_manager);
  103. context_.dump_properties = DumpManager::GetInstance().GetDumpProperties(context_.session_id);
  104. const char *profiling_level = std::getenv(kEnvProfilingLevel);
  105. if (profiling_level != nullptr) {
  106. context_.profiling_level = std::strtol(profiling_level, nullptr, kIntBase);
  107. GELOGD("Got profiling level = %ld", context_.profiling_level);
  108. if (context_.profiling_level > 0) {
  109. context_.profiler.reset(new(std::nothrow)HybridProfiler());
  110. GE_CHECK_NOTNULL(context_.profiler);
  111. }
  112. }
  113. if (IsLogEnable(GE_MODULE_NAME, DLOG_DEBUG)) {
  114. context_.trace_enabled = true;
  115. }
  116. return SUCCESS;
  117. }
  118. Status HybridModelExecutor::ResetExecutionContext(GraphExecutionContext &context) {
  119. GE_CHK_STATUS_RET_NOLOG(context.callback_manager->Init());
  120. string ctx_id = std::to_string(context.context_id);
  121. RuntimeInferenceContext::DestroyContext(ctx_id);
  122. GE_CHK_GRAPH_STATUS_RET(RuntimeInferenceContext::CreateContext(ctx_id), "Failed to Destroy RuntimeInferenceContext");
  123. return SUCCESS;
  124. }
  125. } // namespace hybrid
  126. } // namespace ge

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