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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. struct ModelRelation;
  31. class PneModel {
  32. public:
  33. PneModel() = default;
  34. explicit PneModel(const ComputeGraphPtr &root_graph) : root_graph_(root_graph){};
  35. virtual ~PneModel() = default;
  36. PneModel(const PneModel &other) = delete;
  37. PneModel &operator=(const PneModel &other) = delete;
  38. public:
  39. inline Status AddSubModel(const shared_ptr<PneModel> &submodel, std::string type = "") {
  40. if (submodel == nullptr) {
  41. GELOGE(INTERNAL_ERROR, "submodel is nullptr, type = %s", type.c_str());
  42. return INTERNAL_ERROR;
  43. }
  44. submodel->SetModelType(type);
  45. if (!submodels_.emplace(submodel->GetModelName(), submodel).second) {
  46. GELOGE(INTERNAL_ERROR, "submodel already exist, name = %s, type = %s", submodel->GetModelName().c_str(),
  47. type.c_str());
  48. return INTERNAL_ERROR;
  49. }
  50. return SUCCESS;
  51. }
  52. inline const std::shared_ptr<PneModel> GetSubmodel(const std::string &name) const {
  53. const auto &it = submodels_.find(name);
  54. if (it == submodels_.end()) {
  55. return nullptr;
  56. }
  57. return it->second;
  58. }
  59. inline const std::map<std::string, std::shared_ptr<PneModel>> &GetSubmodels() const {
  60. return submodels_;
  61. }
  62. inline void SetModelType(const std::string &type) {
  63. model_type_ = type;
  64. }
  65. inline const std::string &GetModelType() const {
  66. return model_type_;
  67. }
  68. inline void SetModelName(const std::string &model_name) {
  69. model_name_ = model_name;
  70. }
  71. inline const std::string &GetModelName() const {
  72. return model_name_;
  73. }
  74. inline void SetRootGraph(const ComputeGraphPtr graph) {
  75. root_graph_ = graph;
  76. }
  77. inline const ComputeGraphPtr &GetRootGraph() const {
  78. return root_graph_;
  79. }
  80. inline void SetModelRelation(std::shared_ptr<ModelRelation> model_relation) {
  81. model_relation_ = std::move(model_relation);
  82. }
  83. inline const std::shared_ptr<ModelRelation> GetModelRelation() const {
  84. return model_relation_;
  85. }
  86. public:
  87. virtual Status SerializeModel(ModelBufferData &model_buff) = 0;
  88. virtual Status UnSerializeModel(const ModelBufferData &model_buff) = 0;
  89. virtual void SetModelId(const uint32_t model_id) {
  90. model_id_ = model_id;
  91. }
  92. virtual uint32_t GetModelId() const {
  93. return model_id_;
  94. }
  95. private:
  96. std::map<std::string, std::shared_ptr<PneModel>> submodels_;
  97. std::shared_ptr<ModelRelation> model_relation_;
  98. ComputeGraphPtr root_graph_ = nullptr;
  99. std::string model_name_;
  100. std::string model_type_;
  101. uint32_t model_id_ = INVALID_MODEL_ID;
  102. };
  103. using PneModelPtr = std::shared_ptr<PneModel>;
  104. } // namespace ge
  105. #endif // INC_FRAMEWORK_PNE_MODEL_H_

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