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_davinci_model.cc 3.7 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <memory>
  17. #include "hybrid_davinci_model.h"
  18. #include "hybrid/model/hybrid_model.h"
  19. #include "hybrid/executor/hybrid_model_async_executor.h"
  20. #include "hybrid/node_executor/node_executor.h"
  21. namespace ge {
  22. namespace hybrid {
  23. class HybridDavinciModel::Impl {
  24. public:
  25. explicit Impl(GeRootModelPtr ge_model) : model_(std::move(ge_model)), executor_(&model_) {
  26. }
  27. ~Impl() {
  28. NodeExecutorManager::GetInstance().FinalizeExecutors();
  29. }
  30. Status Init() {
  31. GE_CHK_STATUS_RET(NodeExecutorManager::GetInstance().EnsureInitialized(), "Failed to initialize executors");
  32. GE_CHK_STATUS_RET(model_.Init(), "Failed to init model.")
  33. GE_CHK_STATUS_RET(executor_.Init(), "Failed to init model executor.")
  34. return SUCCESS;
  35. }
  36. Status Execute(const vector<GeTensor> &inputs, vector<GeTensor> &outputs) {
  37. return executor_.Execute(inputs, outputs);
  38. }
  39. Status ModelRunStart() {
  40. return executor_.Start(listener_);
  41. }
  42. Status ModelRunStop() {
  43. return executor_.Stop();
  44. }
  45. Status EnqueueData(const std::shared_ptr<InputDataWrapper> &data) {
  46. return executor_.EnqueueData(data);
  47. }
  48. void SetListener(const shared_ptr<ModelListener> &listener) {
  49. listener_ = listener;
  50. }
  51. void SetModelId(uint32_t model_id) {
  52. executor_.SetModelId(model_id);
  53. model_.SetModelId(model_id);
  54. }
  55. void SetDeviceId(uint32_t device_id) {
  56. model_.SetDeviceId(device_id);
  57. executor_.SetDeviceId(device_id);
  58. }
  59. private:
  60. std::shared_ptr<ModelListener> listener_;
  61. HybridModel model_;
  62. HybridModelAsyncExecutor executor_;
  63. };
  64. HybridDavinciModel::~HybridDavinciModel() {
  65. delete impl_;
  66. }
  67. unique_ptr<HybridDavinciModel> HybridDavinciModel::Create(const GeRootModelPtr &ge_root_model) {
  68. auto instance = unique_ptr<HybridDavinciModel>(new (std::nothrow)HybridDavinciModel());
  69. if (instance != nullptr) {
  70. instance->impl_ = new (std::nothrow) HybridDavinciModel::Impl(ge_root_model);
  71. if (instance->impl_ != nullptr) {
  72. return instance;
  73. }
  74. }
  75. return nullptr;
  76. }
  77. Status HybridDavinciModel::Init() {
  78. GE_CHECK_NOTNULL(impl_);
  79. return impl_->Init();
  80. }
  81. Status HybridDavinciModel::Execute(const vector<GeTensor> &inputs, vector<GeTensor> &outputs) {
  82. GE_CHECK_NOTNULL(impl_);
  83. return impl_->Execute(inputs, outputs);
  84. }
  85. Status HybridDavinciModel::ModelRunStart() {
  86. GE_CHECK_NOTNULL(impl_);
  87. return impl_->ModelRunStart();
  88. }
  89. Status HybridDavinciModel::ModelRunStop() {
  90. GE_CHECK_NOTNULL(impl_);
  91. return impl_->ModelRunStop();
  92. }
  93. Status HybridDavinciModel::EnqueueData(const shared_ptr<InputDataWrapper> &data) {
  94. GE_CHECK_NOTNULL(impl_);
  95. return impl_->EnqueueData(data);
  96. }
  97. void HybridDavinciModel::SetListener(const shared_ptr<ModelListener> &listener) {
  98. if (impl_ != nullptr) {
  99. impl_->SetListener(listener);
  100. }
  101. }
  102. void HybridDavinciModel::SetModelId(uint32_t model_id) {
  103. if (impl_ != nullptr) {
  104. impl_->SetModelId(model_id);
  105. }
  106. }
  107. void HybridDavinciModel::SetDeviceId(uint32_t device_id) {
  108. if (impl_ != nullptr) {
  109. impl_->SetDeviceId(device_id);
  110. }
  111. }
  112. } // namespace hybrid
  113. } // namespace ge

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