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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. ~Impl() { NodeExecutorManager::GetInstance().FinalizeExecutors(); }
  27. Status Init() {
  28. GE_CHK_STATUS_RET(NodeExecutorManager::GetInstance().EnsureInitialized(), "Failed to initialize executors");
  29. GE_CHK_STATUS_RET(model_.Init(), "Failed to init model.")
  30. GE_CHK_STATUS_RET(executor_.Init(), "Failed to init model executor.")
  31. return SUCCESS;
  32. }
  33. Status Execute(const vector<GeTensor> &inputs, vector<GeTensor> &outputs) {
  34. return executor_.Execute(inputs, outputs);
  35. }
  36. Status ModelRunStart() { return executor_.Start(listener_); }
  37. Status ModelRunStop() { return executor_.Stop(); }
  38. Status EnqueueData(const std::shared_ptr<InputDataWrapper> &data) { return executor_.EnqueueData(data); }
  39. void SetListener(const shared_ptr<ModelListener> &listener) { listener_ = listener; }
  40. void SetModelId(uint32_t model_id) {
  41. executor_.SetModelId(model_id);
  42. model_.SetModelId(model_id);
  43. }
  44. void SetDeviceId(uint32_t device_id) {
  45. model_.SetDeviceId(device_id);
  46. executor_.SetDeviceId(device_id);
  47. }
  48. private:
  49. std::shared_ptr<ModelListener> listener_;
  50. HybridModel model_;
  51. HybridModelAsyncExecutor executor_;
  52. };
  53. HybridDavinciModel::~HybridDavinciModel() { delete impl_; }
  54. unique_ptr<HybridDavinciModel> HybridDavinciModel::Create(const GeRootModelPtr &ge_root_model) {
  55. auto instance = unique_ptr<HybridDavinciModel>(new (std::nothrow) HybridDavinciModel());
  56. if (instance != nullptr) {
  57. instance->impl_ = new (std::nothrow) HybridDavinciModel::Impl(ge_root_model);
  58. if (instance->impl_ != nullptr) {
  59. return instance;
  60. }
  61. }
  62. return nullptr;
  63. }
  64. Status HybridDavinciModel::Init() {
  65. GE_CHECK_NOTNULL(impl_);
  66. return impl_->Init();
  67. }
  68. Status HybridDavinciModel::Execute(const vector<GeTensor> &inputs, vector<GeTensor> &outputs) {
  69. GE_CHECK_NOTNULL(impl_);
  70. return impl_->Execute(inputs, outputs);
  71. }
  72. Status HybridDavinciModel::ModelRunStart() {
  73. GE_CHECK_NOTNULL(impl_);
  74. return impl_->ModelRunStart();
  75. }
  76. Status HybridDavinciModel::ModelRunStop() {
  77. GE_CHECK_NOTNULL(impl_);
  78. return impl_->ModelRunStop();
  79. }
  80. Status HybridDavinciModel::EnqueueData(const shared_ptr<InputDataWrapper> &data) {
  81. GE_CHECK_NOTNULL(impl_);
  82. return impl_->EnqueueData(data);
  83. }
  84. void HybridDavinciModel::SetListener(const shared_ptr<ModelListener> &listener) {
  85. if (impl_ != nullptr) {
  86. impl_->SetListener(listener);
  87. }
  88. }
  89. void HybridDavinciModel::SetModelId(uint32_t model_id) {
  90. if (impl_ != nullptr) {
  91. impl_->SetModelId(model_id);
  92. }
  93. }
  94. void HybridDavinciModel::SetDeviceId(uint32_t device_id) {
  95. if (impl_ != nullptr) {
  96. impl_->SetDeviceId(device_id);
  97. }
  98. }
  99. } // namespace hybrid
  100. } // namespace ge

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