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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "common/debug/log.h"
  24. #include "common/debug/memory_dumper.h"
  25. #include "common/ge_types.h"
  26. #include "common/properties_manager.h"
  27. #include "common/string_util.h"
  28. #include "common/types.h"
  29. #include "common/util.h"
  30. #include "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. Status SetGraphContext(GraphContextPtr graph_context_ptr);
  54. static Status SetDynamicSize(uint32_t model_id, const std::vector<uint64_t> &batch_num, int32_t dynamic_type);
  55. void SetTrainFlag(bool is_train_graph);
  56. const std::vector<InputOutputDescInfo> &GetOutputsDesc() const { return outputs_desc_; }
  57. Status FreeExecuteMemory();
  58. static Status DataInput(const InputData &input_data, OutputData &output_data);
  59. static Status GetInputOutputDescInfo(const uint32_t model_id, vector<InputOutputDescInfo> &input_desc,
  60. vector<InputOutputDescInfo> &output_desc);
  61. static Status GetInputOutputDescInfo(const uint32_t model_id, vector<InputOutputDescInfo> &input_desc,
  62. vector<InputOutputDescInfo> &output_desc, std::vector<uint32_t> &input_formats,
  63. std::vector<uint32_t> &output_formats, bool new_model_desc = false);
  64. static Status GetAippInfo(uint32_t model_id, uint32_t index, AippConfigInfo &aipp_info);
  65. static Status GetAippType(uint32_t model_id, uint32_t index, InputAippType &type, size_t &aipp_index);
  66. ///
  67. /// @ingroup ge
  68. /// @brief Get dynamic batch_info
  69. /// @param [in] model_id
  70. /// @param [out] batch_info
  71. /// @param [out] dynamic_type
  72. /// @return execute result
  73. ///
  74. static Status GetDynamicBatchInfo(uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info,
  75. int32_t &dynamic_type);
  76. ///
  77. /// @ingroup ge
  78. /// @brief Get combined dynamic dims info
  79. /// @param [in] model_id
  80. /// @param [out] batch_info
  81. /// @return execute result
  82. ///
  83. static Status GetCombinedDynamicDims(uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info);
  84. ///
  85. /// @ingroup ge
  86. /// @brief Get user designate shape order
  87. /// @param [in] model_id
  88. /// @param [out] user_input_shape_order
  89. /// @return execute result
  90. ///
  91. static Status GetUserDesignateShapeOrder(uint32_t model_id, std::vector<std::string> &user_input_shape_order);
  92. static Status GetCurShape(const uint32_t model_id, std::vector<int64_t> &batch_info, int32_t &dynamic_type);
  93. static Status GetOpAttr(uint32_t model_id, const std::string &op_name, const std::string &attr_name,
  94. std::string &attr_value);
  95. static Status GetModelAttr(uint32_t model_id, std::vector<string> &dynamic_output_shape_info);
  96. static Status GetOrigInputInfo(uint32_t model_id, uint32_t index, OriginInputInfo &orig_input_info);
  97. static Status GetAllAippInputOutputDims(uint32_t model_id, uint32_t index, std::vector<InputOutputDims> &input_dims,
  98. std::vector<InputOutputDims> &output_dims);
  99. static Status GetOpDescInfo(uint32_t device_id, uint32_t stream_id, uint32_t task_id, OpDescInfo &op_desc_info);
  100. uint32_t GetExecuteModelId(const GeRootModelPtr &ge_root_model);
  101. private:
  102. Status PrepareInputData(const std::vector<GeTensor> &input_tensor, InputData &graph_input_data,
  103. OutputData &graph_output_data, std::vector<InputOutputDescInfo> &output_desc);
  104. Status GetExecuteData(const std::vector<GeTensor> &input_tensor, std::vector<DataBuffer> &blobs,
  105. std::vector<GeTensorDesc> &tensor_desc);
  106. Status SyncExecuteModel(uint32_t model_id, const std::vector<GeTensor> &input_tensor,
  107. std::vector<GeTensor> &output_tensor);
  108. Status AsyncExecuteModel(const GeRootModelPtr &ge_root_model, const std::vector<ge::Tensor> &input_tensor,
  109. const RunAsyncCallback &callback);
  110. void InitModelIdInfo(std::vector<uint32_t> &out_model_id_info, std::vector<SubGraphInfoPtr> &sub_graph_vec,
  111. uint32_t output_size);
  112. Status FreeInOutBuffer();
  113. Status MallocInOutBuffer(const std::vector<uint64_t> &buffer_size, std::vector<void *> &data_addr);
  114. static Status SetCallback(uint32_t model_id, const GeRootModelPtr &ge_root_model,
  115. const RunAsyncCallback &callback);
  116. Status ModelSubscribe(uint32_t graph_id);
  117. Status GetModelByID(uint32_t model_id, std::shared_ptr<DavinciModel> &davinci_model);
  118. bool init_flag_;
  119. bool train_graph_flag_;
  120. // For run graph synchronous return
  121. std::mutex *sync_run_mutex_;
  122. std::condition_variable *condition_;
  123. // Run graph asynchronous call back listener
  124. std::shared_ptr<GraphModelListener> graph_run_listener_;
  125. GraphContextPtr graph_context_;
  126. std::vector<InputOutputDescInfo> outputs_desc_;
  127. GraphId last_graph_id_;
  128. bool malloc_flag_;
  129. std::vector<void *> buffer_addr_;
  130. std::vector<uint64_t> buffer_size_;
  131. };
  132. } // namespace ge
  133. #endif // GE_GRAPH_EXECUTE_GRAPH_EXECUTE_H_

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