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

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