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.

graph_execute.h 6.9 kB

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
5 years ago
5 years ago
5 years ago
4 years ago
4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Copyright 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. #ifndef GE_GRAPH_EXECUTE_GRAPH_EXECUTE_H_
  17. #define GE_GRAPH_EXECUTE_GRAPH_EXECUTE_H_
  18. #include <cstdarg>
  19. #include <fstream>
  20. #include <iostream>
  21. #include <memory>
  22. #include <vector>
  23. #include "framework/common/debug/log.h"
  24. #include "common/debug/memory_dumper.h"
  25. #include "framework/common/ge_types.h"
  26. #include "common/properties_manager.h"
  27. #include "framework/common/string_util.h"
  28. #include "framework/common/types.h"
  29. #include "framework/common/util.h"
  30. #include "external/ge/ge_api_types.h"
  31. #include "graph/compute_graph.h"
  32. #include "graph/manager/graph_context.h"
  33. #include "graph/manager/graph_manager_utils.h"
  34. #include "graph/model.h"
  35. #include "graph/utils/graph_utils.h"
  36. #include "graph/utils/tensor_utils.h"
  37. #include "graph/load/model_manager/davinci_model.h"
  38. namespace ge {
  39. class GraphExecutor {
  40. public:
  41. GraphExecutor();
  42. virtual ~GraphExecutor();
  43. Status ExecuteGraph(GraphId graph_id, const GeRootModelPtr &ge_root_model, const std::vector<GeTensor> &input_tensor,
  44. std::vector<GeTensor> &output_tensor);
  45. ge::Status ExecuteGraphAsync(GraphId graph_id, const GeRootModelPtr &ge_root_model,
  46. const std::vector<ge::Tensor> &input_tensor, const RunAsyncCallback &callback);
  47. Status ExecuteGraphWithStream(GraphId graph_id,
  48. rtStream_t stream,
  49. const GeRootModelPtr &ge_root_model,
  50. const std::vector<GeTensor> &input_tensor,
  51. std::vector<GeTensor> &output_tensor);
  52. Status SetCondition(std::mutex *mutex, std::condition_variable *cond, std::shared_ptr<GraphModelListener> listener);
  53. static Status SetDynamicSize(uint32_t model_id, const std::vector<uint64_t> &batch_num, int32_t dynamic_type);
  54. void SetTrainFlag(bool is_train_graph);
  55. const std::vector<InputOutputDescInfo> &GetOutputsDesc() const { return outputs_desc_; }
  56. Status FreeExecuteMemory();
  57. static Status DataInput(const InputData &input_data, OutputData &output_data);
  58. static Status GetInputOutputDescInfo(const uint32_t model_id, vector<InputOutputDescInfo> &input_desc,
  59. vector<InputOutputDescInfo> &output_desc);
  60. static Status GetInputOutputDescInfo(const uint32_t model_id, vector<InputOutputDescInfo> &input_desc,
  61. vector<InputOutputDescInfo> &output_desc, std::vector<uint32_t> &input_formats,
  62. std::vector<uint32_t> &output_formats, bool new_model_desc = false);
  63. static Status GetAippInfo(uint32_t model_id, uint32_t index, AippConfigInfo &aipp_info);
  64. static Status GetAippType(uint32_t model_id, uint32_t index, InputAippType &type, size_t &aipp_index);
  65. ///
  66. /// @ingroup ge
  67. /// @brief Get dynamic batch_info
  68. /// @param [in] model_id
  69. /// @param [out] batch_info
  70. /// @param [out] dynamic_type
  71. /// @return execute result
  72. ///
  73. static Status GetDynamicBatchInfo(uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info,
  74. int32_t &dynamic_type);
  75. ///
  76. /// @ingroup ge
  77. /// @brief Get combined dynamic dims info
  78. /// @param [in] model_id
  79. /// @param [out] batch_info
  80. /// @return execute result
  81. ///
  82. static Status GetCombinedDynamicDims(uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info);
  83. ///
  84. /// @ingroup ge
  85. /// @brief Get user designate shape order
  86. /// @param [in] model_id
  87. /// @param [out] user_input_shape_order
  88. /// @return execute result
  89. ///
  90. static Status GetUserDesignateShapeOrder(uint32_t model_id, std::vector<std::string> &user_input_shape_order);
  91. static Status GetCurShape(const uint32_t model_id, std::vector<int64_t> &batch_info, int32_t &dynamic_type);
  92. static Status GetOpAttr(uint32_t model_id, const std::string &op_name, const std::string &attr_name,
  93. std::string &attr_value);
  94. static Status GetModelAttr(uint32_t model_id, std::vector<string> &dynamic_output_shape_info);
  95. static Status GetOrigInputInfo(uint32_t model_id, uint32_t index, OriginInputInfo &orig_input_info);
  96. static Status GetAllAippInputOutputDims(uint32_t model_id, uint32_t index, std::vector<InputOutputDims> &input_dims,
  97. std::vector<InputOutputDims> &output_dims);
  98. static Status GetOpDescInfo(uint32_t device_id, uint32_t stream_id, uint32_t task_id, OpDescInfo &op_desc_info);
  99. uint32_t GetExecuteModelId(const GeRootModelPtr &ge_root_model);
  100. private:
  101. Status PrepareInputData(const std::vector<GeTensor> &input_tensor, InputData &graph_input_data,
  102. OutputData &graph_output_data, std::vector<InputOutputDescInfo> &output_desc);
  103. Status GetExecuteData(const std::vector<GeTensor> &input_tensor, std::vector<DataBuffer> &blobs,
  104. std::vector<GeTensorDesc> &tensor_desc);
  105. Status SyncExecuteModel(uint32_t model_id, const std::vector<GeTensor> &input_tensor,
  106. std::vector<GeTensor> &output_tensor);
  107. Status AsyncExecuteModel(const GeRootModelPtr &ge_root_model, const std::vector<ge::Tensor> &input_tensor,
  108. const RunAsyncCallback &callback);
  109. void InitModelIdInfo(std::vector<uint32_t> &out_model_id_info, std::vector<SubGraphInfoPtr> &sub_graph_vec,
  110. uint32_t output_size);
  111. Status FreeInOutBuffer();
  112. Status MallocInOutBuffer(const std::vector<uint64_t> &buffer_size, std::vector<void *> &data_addr);
  113. static Status SetCallback(uint32_t model_id, const GeRootModelPtr &ge_root_model,
  114. const RunAsyncCallback &callback);
  115. Status ModelSubscribe(uint32_t graph_id);
  116. Status GetModelByID(uint32_t model_id, std::shared_ptr<DavinciModel> &davinci_model);
  117. bool init_flag_;
  118. bool train_graph_flag_;
  119. // For run graph synchronous return
  120. std::mutex *sync_run_mutex_;
  121. std::condition_variable *condition_;
  122. // Run graph asynchronous call back listener
  123. std::shared_ptr<GraphModelListener> graph_run_listener_;
  124. std::vector<InputOutputDescInfo> outputs_desc_;
  125. GraphId last_graph_id_;
  126. bool malloc_flag_;
  127. std::vector<void *> buffer_addr_;
  128. std::vector<uint64_t> buffer_size_;
  129. };
  130. } // namespace ge
  131. #endif // GE_GRAPH_EXECUTE_GRAPH_EXECUTE_H_

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