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.0 kB

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

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