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

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. }
  34. Status HybridModelExecutor::Init() {
  35. GELOGD("Start to init HybridGraphEngine.");
  36. GE_CHK_STATUS_RET_NOLOG(InitExecutionContext());
  37. GELOGD("HybridGraphEngine initialized successfully.");
  38. return SUCCESS;
  39. }
  40. Status HybridModelExecutor::Execute(HybridModelExecutor::ExecuteArgs &args) {
  41. GELOGD("Start to execute model.");
  42. auto root_graph_item = model_->GetRootGraphItem();
  43. GE_CHECK_NOTNULL(root_graph_item);
  44. if (context_.global_step != nullptr) {
  45. GE_CHK_RT_RET(rtMemcpyAsync(context_.global_step, sizeof(uint64_t), &context_.iteration,
  46. sizeof(uint64_t), RT_MEMCPY_HOST_TO_DEVICE_EX, context_.stream));
  47. }
  48. SubgraphExecutor executor(model_->GetRootGraphItem(), &context_);
  49. auto ret = ExecuteGraphInternal(executor, args);
  50. Cleanup();
  51. RECORD_MODEL_EXECUTION_EVENT(&context_, "[Cleanup] End");
  52. GELOGD("Model executed successfully.");
  53. if (context_.profiler != nullptr) {
  54. context_.profiler->Dump(std::cout);
  55. context_.profiler->Reset();
  56. }
  57. context_.iteration += 1;
  58. if (ret == END_OF_SEQUENCE) {
  59. args.is_eos = true;
  60. } else {
  61. GE_CHK_STATUS_RET(ret, "Failed to execute model");
  62. }
  63. return SUCCESS;
  64. }
  65. Status HybridModelExecutor::ExecuteGraphInternal(SubgraphExecutor &executor,
  66. HybridModelExecutor::ExecuteArgs &args) {
  67. RECORD_MODEL_EXECUTION_EVENT(&context_, "[InitContext] Start");
  68. GE_CHK_STATUS_RET_NOLOG(ResetExecutionContext(context_));
  69. RECORD_MODEL_EXECUTION_EVENT(&context_, "[InitContext] End");
  70. HYBRID_CHK_STATUS_RET(executor.ExecuteAsync(args.inputs, args.input_desc, args.outputs),
  71. "Failed to execute partitioned call.");
  72. RECORD_MODEL_EXECUTION_EVENT(&context_, "[ExecuteAsync] End");
  73. HYBRID_CHK_STATUS_RET(executor.Synchronize(), "Failed to sync root graph.");
  74. RECORD_MODEL_EXECUTION_EVENT(&context_, "[Synchronize] End");
  75. args.outputs.clear();
  76. HYBRID_CHK_STATUS_RET(executor.GetOutputs(args.outputs, args.output_desc), "Failed to get outputs");
  77. RECORD_MODEL_EXECUTION_EVENT(&context_, "[GetOutput] End");
  78. return SUCCESS;
  79. }
  80. Status HybridModelExecutor::Cleanup() {
  81. GELOGD("Start to cleanup.");
  82. context_.callback_manager->Destroy();
  83. RuntimeInferenceContext::DestroyContext(std::to_string(context_.context_id));
  84. GELOGD("Cleanup successfully.");
  85. return SUCCESS;
  86. }
  87. Status HybridModelExecutor::InitExecutionContext() {
  88. GE_CHK_RT_RET(rtCtxGetCurrent(&context_.rt_context));
  89. GE_CHK_RT_RET(rtCtxCreate(&context_.rt_gen_context, RT_CTX_GEN_MODE, 0));
  90. GE_CHK_RT_RET(rtCtxSetCurrent(context_.rt_context));
  91. context_.global_step = model_->GetGlobalStep();
  92. context_.stream = stream_;
  93. context_.model = model_;
  94. context_.is_eos_ = false;
  95. context_.session_id = ::ge::GetContext().SessionId();
  96. context_.ge_context = &GetThreadLocalContext();
  97. GELOGD("session id from model = %lu, from context = %lu", model_->GetSessionId(), context_.session_id);
  98. context_.allocator = NpuMemoryAllocator::GetAllocator(device_id_);
  99. GE_CHECK_NOTNULL(context_.allocator);
  100. context_.callback_manager = std::unique_ptr<CallbackManager>(new(std::nothrow)CallbackManager());
  101. GE_CHECK_NOTNULL(context_.callback_manager);
  102. context_.dump_properties = DumpManager::GetInstance().GetDumpProperties(context_.session_id);
  103. const char *profiling_level = std::getenv(kEnvProfilingLevel);
  104. if (profiling_level != nullptr) {
  105. context_.profiling_level = std::strtol(profiling_level, nullptr, kIntBase);
  106. GELOGD("Got profiling level = %ld", context_.profiling_level);
  107. if (context_.profiling_level > 0) {
  108. context_.profiler.reset(new(std::nothrow)HybridProfiler());
  109. GE_CHECK_NOTNULL(context_.profiler);
  110. }
  111. }
  112. if (IsLogEnable(GE_MODULE_NAME, DLOG_DEBUG)) {
  113. context_.trace_enabled = true;
  114. }
  115. return SUCCESS;
  116. }
  117. Status HybridModelExecutor::ResetExecutionContext(GraphExecutionContext &context) {
  118. GE_CHK_STATUS_RET_NOLOG(context.callback_manager->Init());
  119. string ctx_id = std::to_string(context.context_id);
  120. RuntimeInferenceContext::DestroyContext(ctx_id);
  121. GE_CHK_GRAPH_STATUS_RET(RuntimeInferenceContext::CreateContext(ctx_id), "Failed to Destroy RuntimeInferenceContext");
  122. RuntimeInferenceContext *ctx = nullptr;
  123. GE_CHK_GRAPH_STATUS_RET(RuntimeInferenceContext::GetContext(ctx_id, &ctx), "Failed to get context");
  124. for (auto &host_tensor : context.model->GetHostTensors()) {
  125. auto node_id = host_tensor.first;
  126. for (const auto &output_idx_and_tensor : host_tensor.second) {
  127. auto output_idx = output_idx_and_tensor.first;
  128. GELOGD("Preload const host tensor, node_id = %ld, output id = %d", node_id, output_idx);
  129. ctx->SetTensor(node_id, output_idx, output_idx_and_tensor.second.Clone());
  130. }
  131. }
  132. return SUCCESS;
  133. }
  134. } // namespace hybrid
  135. } // namespace ge

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