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.

model_executor.h 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Copyright 2021 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_MODEL_EXECUTOR_H
  17. #define GE_GRAPH_EXECUTE_MODEL_EXECUTOR_H
  18. #include <thread>
  19. #include "common/executor.h"
  20. #include "graph/execute/graph_execute.h"
  21. namespace ge {
  22. class ModelExecutor : public Executor {
  23. public:
  24. ///
  25. /// @ingroup ge
  26. /// @brief graph executor init
  27. /// @param [in] options user config params
  28. /// @return Status result of function
  29. ///
  30. Status Initialize(const map<string, string> &options);
  31. ///
  32. /// @ingroup ge
  33. /// @brief graph executor finalize
  34. /// @return Status result of function
  35. ///
  36. Status Finalize();
  37. ///
  38. /// @ingroup ge
  39. /// @brief Load mode for graph.
  40. /// @param [in] GeRootModel: root model of graph compiled.
  41. /// @param [in] GraphNode: node of graph.
  42. /// @return Status result of function
  43. ///
  44. Status LoadGraph(const GeRootModelPtr &ge_root_model, const GraphNodePtr &graph_node);
  45. ///
  46. /// @ingroup ge
  47. /// @brief Unload mode for graph.
  48. /// @param [in] GeRootModel: root model of graph compiled.
  49. /// @param [in] graph_id: graph identifier.
  50. /// @return Status result of function
  51. ///
  52. Status UnloadGraph(const GeRootModelPtr &ge_root_model, uint32_t graph_id);
  53. ///
  54. /// @ingroup ge
  55. /// @brief Push model execution params to queue.
  56. /// @param [in] RunArgs of for model execution.
  57. /// @return Status result of function
  58. ///
  59. Status PushGraph(const RunArgs &args);
  60. ///
  61. /// @ingroup ge
  62. /// @brief Run graph for synchronize model.
  63. /// @param [in] graph_node: node of graph.
  64. /// @param [in] graph_id: graph identifier.
  65. /// @param [in] inputs: input data for the graph running.
  66. /// @param [out] outputs: output data of the graph running
  67. /// @return Status result of function
  68. ///
  69. Status RunGraph(const GraphNodePtr &graph_node, GraphId graph_id,
  70. const std::vector<GeTensor> &inputs, std::vector<GeTensor> &outputs);
  71. ///
  72. /// @ingroup ge
  73. /// @brief Run graph for NN synchronize model.
  74. /// @param [in] graph_node: node of graph.
  75. /// @param [in] graph_id: graph identifier.
  76. /// @param [in] stream: Stream for model running.
  77. /// @param [in] inputs: input data for the graph running.
  78. /// @param [out] outputs: output data of the graph running
  79. /// @return Status result of function
  80. ///
  81. Status RunGraphWithStream(const GraphNodePtr &graph_node, GraphId graph_id, rtStream_t stream,
  82. const std::vector<GeTensor> &inputs, std::vector<GeTensor> &outputs);
  83. private:
  84. bool ParseTrainGraphFlag();
  85. void AddGraphNode(GraphId graph_id, const GraphNodePtr &graph_node);
  86. void RemoveGraphNode(GraphId graph_id);
  87. Status ModelLoadSync(const GeRootModelPtr &ge_root_model, const GraphNodePtr &graph_node);
  88. Status ModelLoadAsync(const GeRootModelPtr &ge_root_model, const GraphNodePtr &graph_node);
  89. Status ModelLoad(const GeRootModelPtr &ge_root_model, const GraphNodePtr &graph_node,
  90. const std::shared_ptr<ModelListener> &listener);
  91. Status UnloadModel(const GeRootModelPtr &ge_root_model, uint32_t graph_id);
  92. void ReleaseMemory(const GeModelPtr &ge_model, const GraphNodePtr &graph_node, const std::vector<uint32_t> &model_ids,
  93. uint32_t graph_id, uint64_t session_id);
  94. Status CheckAndReleaseMemory(const GeModelPtr &ge_model, const GraphNodePtr &graph_node);
  95. void UpdateLocalOmeContext(const GraphNodePtr &graph_node);
  96. void RunThread();
  97. void StopQueue();
  98. void ReturnError(RunAsyncCallback callback, Status ret, const string &log);
  99. void ParseInputsDimsForData(const std::vector<ge::Tensor> &input_tensor);
  100. Status ParseInputsDimsForGetNextNoSinkAndData(const vector<NodePtr> &dynamic_nodes,
  101. const std::vector<ge::Tensor> &input_tensor);
  102. Status ParseInputsDims(const std::vector<ge::Tensor> &input_tensor);
  103. bool init_flag_{false};
  104. bool train_graph_flag_{false};
  105. GraphExecutor graph_executor_;
  106. std::mutex mutex_;
  107. std::map<GraphId, GraphNodePtr> graph_nodes_;
  108. std::thread run_thread_;
  109. std::atomic_bool thread_run_flag_{false};
  110. BlockingQueue<RunArgs> run_args_q_;
  111. // for run graph synchronous return
  112. std::mutex sync_run_mutex_;
  113. std::condition_variable condition_;
  114. // run graph synchronization call back listener
  115. std::shared_ptr<GraphModelListener> graph_run_listener_;
  116. };
  117. }
  118. #endif // GE_GRAPH_EXECUTE_MODEL_EXECUTOR_H

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