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.

buffer_pool_memory_pass.h 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_PASSES_BUFFER_POOL_MEMORY_PASS_H_
  17. #define GE_GRAPH_PASSES_BUFFER_POOL_MEMORY_PASS_H_
  18. #include <queue>
  19. #include "external/graph/graph.h"
  20. #include "inc/graph_pass.h"
  21. namespace ge {
  22. class BufferPoolMemoryPass : public GraphPass {
  23. public:
  24. explicit BufferPoolMemoryPass() : logic_event_num_(0) {}
  25. ~BufferPoolMemoryPass() override = default;
  26. struct BufferPool {
  27. int64_t pool_id = 0;
  28. int64_t pool_size = 0;
  29. std::unordered_map<NodePtr, NodePtr> buffer_node_to_calc;
  30. BufferPool(int64_t id, int64_t size, const std::unordered_map<NodePtr, NodePtr> &node_map)
  31. : pool_id(id), pool_size(size), buffer_node_to_calc(node_map) {}
  32. };
  33. struct BufferPoolNodeItem {
  34. NodePtr node = nullptr;
  35. NodePtr out_calc_node = nullptr;
  36. NodePtr pre_buffer_pool_node = nullptr;
  37. int64_t total_size = 0;
  38. int64_t offset_start = 0;
  39. int64_t offset_end = 0;
  40. bool is_last_input = true;
  41. BufferPoolNodeItem(const NodePtr &buffer_n, const NodePtr &calc_n, const NodePtr &pre_buffer_n,
  42. int64_t size, int64_t start, int64_t end, bool last)
  43. : node(std::move(buffer_n)),
  44. out_calc_node(std::move(calc_n)),
  45. pre_buffer_pool_node(std::move(pre_buffer_n)),
  46. total_size(size),
  47. offset_start(start),
  48. offset_end(end),
  49. is_last_input(last) {}
  50. BufferPoolNodeItem(const NodePtr &buffer_n, int64_t start, int64_t end)
  51. : node(std::move(buffer_n)),
  52. out_calc_node(nullptr),
  53. pre_buffer_pool_node(nullptr),
  54. total_size(0),
  55. offset_start(start),
  56. offset_end(end),
  57. is_last_input(true) {}
  58. };
  59. Status Run(ComputeGraphPtr graph) override;
  60. private:
  61. static void ClearQueue(std::queue<std::pair<std::string, uint32_t>> &q);
  62. static Status IsBufferPoolMemEnable(const ComputeGraphPtr &graph);
  63. static Status CheckBufferPoolSize(int64_t total_size, int64_t pool_id, int64_t buffer_pool_size,
  64. std::unordered_map<int64_t, int64_t> &calc_total_size);
  65. static Status TryToFixNodeOrder(NodePtr &pre_node, NodePtr &curr_node, bool &not_change);
  66. Status InsertMemCpyNodeAfter(ComputeGraphPtr &graph, NodePtr &node);
  67. Status CopyOutForMultiUsedOutput(ComputeGraphPtr &graph);
  68. Status GetBufferPoolAndPeerCalcNodes(const ComputeGraphPtr &graph);
  69. Status SetBufferPoolSize(const std::string &batch_label, int64_t id, int64_t size);
  70. Status AllocateAllBufferPoolSpace();
  71. Status AllocateSpaceInBatch(const std::map<int64_t, std::vector<NodePtr>> &calc_nodes,
  72. const std::unordered_map<int64_t, int64_t> &buffer_pool_size_map,
  73. const std::unordered_map<NodePtr, NodePtr> &buffer_node_to_calc,
  74. std::unordered_map<NodePtr, std::vector<BufferPoolNodeItem>> &buffer_pool_nodes_item);
  75. Status AllocateSpaceInBufferPool(const BufferPool &buffer_pool,
  76. const std::vector<NodePtr> &calc_nodes_in_pool,
  77. std::unordered_map<NodePtr, std::vector<BufferPoolNodeItem>> &buffer_pool_nodes_item);
  78. Status AllocateSpaceForBufferPoolNode(int64_t &next_start,
  79. const BufferPool buffer_pool,
  80. BufferPoolNodeItem &buffer_pool_node_item,
  81. std::queue<BufferPoolNodeItem> &node_mem_range_in_pool);
  82. NodePtr GetOffsetAndDependency(int64_t &next_start,
  83. int64_t total_mem_size,
  84. int64_t buffer_pool_size,
  85. const std::unordered_map<NodePtr, NodePtr> &buffer_node_to_calc,
  86. std::queue<BufferPoolNodeItem> &nodes_in_buffer);
  87. Status FixTheTimingOfDependentNodes(NodePtr &dependent_calc_node, NodePtr &curr_pool_node);
  88. uint32_t GenerateEventId(const std::string &node_name, std::queue<std::pair<std::string, uint32_t>> &event_queue);
  89. Status SetResultOfMemoryAndEvent();
  90. // Use map to ensure that each visit is in the order of batch label and pool id
  91. std::map<std::string, std::map<int64_t, std::vector<NodePtr>>> calc_nodes_;
  92. std::unordered_map<std::string, std::unordered_map<NodePtr, NodePtr>> buffer_node_to_calc_;
  93. std::unordered_map<std::string, std::unordered_map<NodePtr, std::vector<BufferPoolNodeItem>>> peer_buffer_node_item_;
  94. std::unordered_map<std::string, std::unordered_map<int64_t, int64_t>> buffer_pool_size_;
  95. uint32_t logic_event_num_;
  96. std::queue<std::pair<std::string, uint32_t>> mem_ctrl_event_;
  97. std::queue<std::pair<std::string, uint32_t>> stream_ctrl_event_;
  98. std::unordered_map<NodePtr, std::vector<std::string>> node_event_multiplexing_;
  99. std::unordered_map<NodePtr, std::vector<int64_t>> buffer_node_logical_offset_;
  100. };
  101. } // namespace ge
  102. #endif // GE_GRAPH_PASSES_BUFFER_POOL_MEMORY_PASS_H_

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