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.1 kB

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
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. namespace ge {
  38. class GraphExecutor {
  39. public:
  40. GraphExecutor();
  41. virtual ~GraphExecutor();
  42. Status ExecuteGraph(GraphId graph_id, const GeRootModelPtr &ge_root_model, const std::vector<GeTensor> &input_tensor,
  43. std::vector<GeTensor> &output_tensor);
  44. ge::Status ExecuteGraphAsync(GraphId graph_id, const GeRootModelPtr &ge_root_model,
  45. const std::vector<InputTensorInfo> &input_tensor, const RunAsyncCallback &callback);
  46. Status SetCondition(std::mutex *mutex, std::condition_variable *cond, std::shared_ptr<GraphModelListener> listener);
  47. Status SetGraphContext(GraphContextPtr graph_context_ptr);
  48. static Status SetDynamicSize(uint32_t model_id, const std::vector<uint64_t> &batch_num, int32_t dynamic_type);
  49. void SetTrainFlag(bool is_train_graph);
  50. const std::vector<InputOutputDescInfo> &GetOutputsDesc() const { return outputs_desc_; }
  51. Status FreeExecuteMemory();
  52. static Status DataInput(const InputData &input_data, OutputData &output_data);
  53. static Status GetInputOutputDescInfo(const uint32_t model_id, vector<InputOutputDescInfo> &input_desc,
  54. vector<InputOutputDescInfo> &output_desc);
  55. static Status GetInputOutputDescInfo(const uint32_t model_id, vector<InputOutputDescInfo> &input_desc,
  56. vector<InputOutputDescInfo> &output_desc, std::vector<uint32_t> &input_formats,
  57. std::vector<uint32_t> &output_formats, bool new_model_desc = false);
  58. static Status GetAippInfo(uint32_t model_id, uint32_t index, AippConfigInfo &aipp_info);
  59. static Status GetAippType(uint32_t model_id, uint32_t index, InputAippType &type, size_t &aipp_index);
  60. ///
  61. /// @ingroup ge
  62. /// @brief Get dynamic batch_info
  63. /// @param [in] model_id
  64. /// @param [out] batch_info
  65. /// @param [out] dynamic_type
  66. /// @return execute result
  67. ///
  68. static Status GetDynamicBatchInfo(uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info,
  69. int32_t &dynamic_type);
  70. ///
  71. /// @ingroup ge
  72. /// @brief Get combined dynamic dims info
  73. /// @param [in] model_id
  74. /// @param [out] batch_info
  75. /// @return execute result
  76. ///
  77. static Status GetCombinedDynamicDims(uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info);
  78. ///
  79. /// @ingroup ge
  80. /// @brief Get user designate shape order
  81. /// @param [in] model_id
  82. /// @param [out] user_input_shape_order
  83. /// @return execute result
  84. ///
  85. static Status GetUserDesignateShapeOrder(uint32_t model_id, std::vector<std::string> &user_input_shape_order);
  86. static Status GetCurShape(const uint32_t model_id, std::vector<int64_t> &batch_info, int32_t &dynamic_type);
  87. static Status GetModelAttr(uint32_t model_id, std::vector<string> &dynamic_output_shape_info);
  88. static Status GetOrigInputInfo(uint32_t model_id, uint32_t index, OriginInputInfo &orig_input_info);
  89. static Status GetAllAippInputOutputDims(uint32_t model_id, uint32_t index, std::vector<InputOutputDims> &input_dims,
  90. std::vector<InputOutputDims> &output_dims);
  91. static Status GetOpDescInfo(uint32_t device_id, uint32_t stream_id, uint32_t task_id, OpDescInfo &op_desc_info);
  92. uint32_t GetExecuteModelId(const GeRootModelPtr &ge_root_model);
  93. private:
  94. Status PrepareInputData(const std::vector<GeTensor> &input_tensor, InputData &graph_input_data,
  95. OutputData &graph_output_data, std::vector<InputOutputDescInfo> &output_desc);
  96. Status SyncExecuteModel(uint32_t model_id, const std::vector<GeTensor> &input_tensor,
  97. std::vector<GeTensor> &output_tensor);
  98. Status AsyncExecuteModel(const GeRootModelPtr &ge_root_model, const std::vector<InputTensorInfo> &input_tensor,
  99. const RunAsyncCallback &callback);
  100. void InitModelIdInfo(std::vector<uint32_t> &out_model_id_info, std::vector<SubGraphInfoPtr> &sub_graph_vec,
  101. uint32_t output_size);
  102. Status FreeInOutBuffer();
  103. Status MallocInOutBuffer(const std::vector<uint64_t> &buffer_size, std::vector<void *> &data_addr);
  104. static Status SetCallback(uint32_t model_id, const GeRootModelPtr &ge_root_model,
  105. const RunAsyncCallback &callback);
  106. bool init_flag_;
  107. bool train_graph_flag_;
  108. // For run graph synchronous return
  109. std::mutex *sync_run_mutex_;
  110. std::condition_variable *condition_;
  111. // Run graph asynchronous call back listener
  112. std::shared_ptr<GraphModelListener> graph_run_listener_;
  113. GraphContextPtr graph_context_;
  114. std::vector<InputOutputDescInfo> outputs_desc_;
  115. GraphId last_graph_id_;
  116. bool malloc_flag_;
  117. std::vector<void *> buffer_addr_;
  118. std::vector<uint64_t> buffer_size_;
  119. };
  120. } // namespace ge
  121. #endif // GE_GRAPH_EXECUTE_GRAPH_EXECUTE_H_

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