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.

ge_generator.h 6.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Copyright (c) Huawei Technologies Co., Ltd. 2021. 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_GENERATOR_GE_GENERATOR_H_
  17. #define INC_FRAMEWORK_GENERATOR_GE_GENERATOR_H_
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include "external/ge/ge_ir_build.h"
  23. #include "framework/common/ge_inner_error_codes.h"
  24. #include "framework/common/ge_types.h"
  25. #include "graph/ge_tensor.h"
  26. #include "graph/graph.h"
  27. #include "graph/op_desc.h"
  28. #include "framework/omg/omg_inner_types.h"
  29. #include "graph/detail/attributes_holder.h"
  30. namespace ge {
  31. const std::string kAttrSupportDynamicShape = "support_dynamicshape";
  32. class GeRootModel;
  33. class GE_FUNC_VISIBILITY GeGenerator {
  34. public:
  35. using InOutTensorRef = std::pair<const std::vector<ge::GeTensor> &, const std::vector<ge::GeTensor> &>;
  36. static GeGenerator &GetInstance() {
  37. static GeGenerator Instance;
  38. return Instance;
  39. }
  40. GeGenerator() = default;
  41. ~GeGenerator() {
  42. (void)Finalize();
  43. }
  44. GeGenerator(const GeGenerator &) = delete;
  45. GeGenerator &operator=(const GeGenerator &) = delete;
  46. Status Initialize(const std::map<std::string, std::string> &options);
  47. Status Initialize(const std::map<std::string, std::string> &options, OmgContext &context);
  48. Status Finalize();
  49. Status GenerateOfflineModel(const Graph &graph, const std::string &file_name_prefix,
  50. const std::vector<GeTensor> &inputs = std::vector<GeTensor>());
  51. Status GenerateOnlineModel(const Graph &graph, const std::vector<GeTensor> &inputs, ge::ModelBufferData &model);
  52. Status GenerateInfershapeGraph(const Graph &graph);
  53. ///
  54. /// @ingroup ge
  55. /// @brief: Build single OP in Model.
  56. /// @param [in] op_desc: the OP description.
  57. /// @param [in] inputs: input tensors.
  58. /// @param [in] outputs: output tensors.
  59. /// @param [in] model_file_name: name of model file.
  60. /// @param [in] compile_flag: op build flag, accurate build is 0, fuzz build is 1
  61. /// @return SUCCESS or FAILED
  62. ///
  63. Status BuildSingleOpModel(OpDescPtr &op_desc, const std::vector<GeTensor> &inputs,
  64. const std::vector<GeTensor> &outputs, const std::string &model_file_name,
  65. int32_t compile_flag = 0);
  66. ///
  67. /// @ingroup ge
  68. /// @brief: Build single Op into model buff.
  69. /// @param [in] op_desc: the OP description.
  70. /// @param [in] inputs: input tensors.
  71. /// @param [in] outputs: output tensors.
  72. /// @param [in] engine_type: engine type.
  73. /// @param [in] compile_flag: op build flag, accurate build is 0, fuzz build is 1
  74. /// @param [out] model_buff: model buff of op.
  75. /// @return SUCCESS or FAILED
  76. Status BuildSingleOpModel(OpDescPtr &op_desc, const std::vector<GeTensor> &inputs,
  77. const std::vector<GeTensor> &outputs, OpEngineType engine_type,
  78. ModelBufferData &model_buff);
  79. Status BuildSingleOpModel(OpDescPtr &op_desc, const std::vector<GeTensor> &inputs,
  80. const std::vector<GeTensor> &outputs, OpEngineType engine_type, int32_t compile_flag,
  81. ModelBufferData &model_buff);
  82. Status BuildSingleOpModel(OpDescPtr &op_desc, const std::vector<GeTensor> &inputs,
  83. const std::vector<GeTensor> &outputs, OpEngineType engine_type, int32_t compile_flag,
  84. ModelBufferData &model_buff, GraphStage graph_stage, ComputeGraphPtr &compute_graph);
  85. ///
  86. /// @ingroup ge
  87. /// @brief: Build single Op into model buff.
  88. /// @param [in] op_desc: the OP description.
  89. /// @param [in] inputs: input tensors.
  90. /// @param [in] outputs: output tensors.
  91. /// @param [in] graph_name: graph name.
  92. /// @param [out] graph: graph of single op.
  93. /// @return SUCCESS or FAILED
  94. Status BuildSingleOpGraph(const OpDescPtr &op_desc, const InOutTensorRef &inputs_outputs, std::string graph_name,
  95. Graph &graph, std::vector<std::pair<std::string, std::string>> &inputs_name_type) const;
  96. Status BuildOriginalGraphInfo(OpDescPtr &op_desc, const std::vector<GeTensor> &inputs,
  97. const std::vector<GeTensor> &outputs, const std::string &model_file_name,
  98. bool is_offline, int32_t compile_flag, GraphStage graph_stage, Graph &graph,
  99. ComputeGraphPtr &compute_graph, bool &fuzz_compile_flag,
  100. std::vector<std::pair<std::string, std::string>> &inputs_name_type);
  101. private:
  102. Status GenerateModel(const Graph &graph, const std::string &file_name_prefix, const std::vector<GeTensor> &inputs,
  103. ge::ModelBufferData &model, bool is_offline = true);
  104. Status BuildSingleOp(OpDescPtr &op_desc, const std::vector<GeTensor> &inputs, const std::vector<GeTensor> &outputs,
  105. const std::string &model_file_name, OpEngineType engine_type, ModelBufferData &model_buff,
  106. ComputeGraphPtr &comp_graph, bool is_offline = true, int32_t compile_flag = 0,
  107. GraphStage graph_stage = GraphStage::GRAPH_STAGE_RESERVED);
  108. bool CheckNoAicore(const ComputeGraphPtr &graph);
  109. void RemoveConst(const std::vector<GeTensor> &inputs, std::vector<GeTensor> &outputs) const;
  110. Status CheckForSingleOp(const OpDescPtr &op_desc, const std::vector<GeTensor> &inputs,
  111. const std::vector<GeTensor> &outputs) const;
  112. Status InferFormatForSingleOp(const OpDescPtr &op_desc, const Graph &graph) const;
  113. using GeRootModelPtr = std::shared_ptr<ge::GeRootModel>;
  114. Status SetModelNameForDump(const GeRootModelPtr &ge_root_model);
  115. Status CreateGeneralizedBuildAttrs(const GeRootModelPtr &ge_root_model, const std::vector<GeTensor> &inputs,
  116. const std::vector<GeTensor> &outputs,
  117. const std::vector<std::pair<std::string, std::string>> &inputs_name_type,
  118. std::vector<ge::NamedAttrs> &generalized_build_attrs) const;
  119. class Impl;
  120. std::shared_ptr<Impl> impl_;
  121. };
  122. } // namespace ge
  123. #endif // INC_FRAMEWORK_GENERATOR_GE_GENERATOR_H_

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