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.

pne_model.h 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
  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. #ifndef INC_FRAMEWORK_PNE_MODEL_H_
  17. #define INC_FRAMEWORK_PNE_MODEL_H_
  18. #include <map>
  19. #include <string>
  20. #include <vector>
  21. #include "graph/compute_graph.h"
  22. #include "framework/common/debug/log.h"
  23. #include "framework/common/ge_inner_error_codes.h"
  24. #include "framework/common/ge_types.h"
  25. #include "framework/engine/dnnengine.h"
  26. #include "external/ge/ge_ir_build.h"
  27. namespace ge {
  28. const std::string PNE_ID_NPU = "NPU";
  29. const std::string PNE_ID_CPU = "HOST_CPU";
  30. const std::string PNE_ID_UDF = "UDF";
  31. struct ModelRelation;
  32. struct ModelDeployResource;
  33. class PneModel {
  34. public:
  35. PneModel() = default;
  36. explicit PneModel(const ComputeGraphPtr &root_graph) : root_graph_(root_graph){};
  37. virtual ~PneModel() = default;
  38. PneModel(const PneModel &other) = delete;
  39. PneModel &operator=(const PneModel &other) = delete;
  40. public:
  41. inline Status AddSubModel(const shared_ptr<PneModel> &submodel, std::string type = "") {
  42. if (submodel == nullptr) {
  43. GELOGE(INTERNAL_ERROR, "submodel is nullptr, type = %s", type.c_str());
  44. return INTERNAL_ERROR;
  45. }
  46. submodel->SetModelType(type);
  47. if (!submodels_.emplace(submodel->GetModelName(), submodel).second) {
  48. GELOGE(INTERNAL_ERROR, "submodel already exist, name = %s, type = %s", submodel->GetModelName().c_str(),
  49. type.c_str());
  50. return INTERNAL_ERROR;
  51. }
  52. return SUCCESS;
  53. }
  54. inline const std::shared_ptr<PneModel> GetSubmodel(const std::string &name) const {
  55. const auto &it = submodels_.find(name);
  56. if (it == submodels_.end()) {
  57. return nullptr;
  58. }
  59. return it->second;
  60. }
  61. inline const std::map<std::string, std::shared_ptr<PneModel>> &GetSubmodels() const {
  62. return submodels_;
  63. }
  64. inline void SetModelType(const std::string &type) {
  65. model_type_ = type;
  66. }
  67. inline const std::string &GetModelType() const {
  68. return model_type_;
  69. }
  70. inline void SetModelName(const std::string &model_name) {
  71. model_name_ = model_name;
  72. }
  73. inline const std::string &GetModelName() const {
  74. return model_name_;
  75. }
  76. inline void SetRootGraph(const ComputeGraphPtr graph) {
  77. root_graph_ = graph;
  78. }
  79. inline const ComputeGraphPtr &GetRootGraph() const {
  80. return root_graph_;
  81. }
  82. inline void SetModelRelation(std::shared_ptr<ModelRelation> model_relation) {
  83. model_relation_ = std::move(model_relation);
  84. }
  85. inline const std::shared_ptr<ModelRelation> GetModelRelation() const {
  86. return model_relation_;
  87. }
  88. inline void SetDeployResource(std::shared_ptr<ModelDeployResource> deploy_resource) {
  89. deploy_resource_ = std::move(deploy_resource);
  90. }
  91. inline const std::shared_ptr<ModelDeployResource> GetDeployResource() const {
  92. return deploy_resource_;
  93. }
  94. public:
  95. virtual Status SerializeModel(ModelBufferData &model_buff) = 0;
  96. virtual Status UnSerializeModel(const ModelBufferData &model_buff) = 0;
  97. virtual void SetModelId(const uint32_t model_id) {
  98. model_id_ = model_id;
  99. }
  100. virtual uint32_t GetModelId() const {
  101. return model_id_;
  102. }
  103. virtual std::string GetLogicDeviceId() const {
  104. return "";
  105. }
  106. private:
  107. std::map<std::string, std::shared_ptr<PneModel>> submodels_;
  108. std::shared_ptr<ModelRelation> model_relation_;
  109. std::shared_ptr<ModelDeployResource> deploy_resource_;
  110. ComputeGraphPtr root_graph_ = nullptr;
  111. std::string model_name_;
  112. std::string model_type_;
  113. uint32_t model_id_ = INVALID_MODEL_ID;
  114. };
  115. using PneModelPtr = std::shared_ptr<PneModel>;
  116. } // namespace ge
  117. #endif // INC_FRAMEWORK_PNE_MODEL_H_

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