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.

kernel_adjust.h 9.2 kB

5 years ago
5 years ago
5 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_ADJUST_H_
  17. #define MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_ADJUST_H_
  18. #include <memory>
  19. #include <map>
  20. #include <string>
  21. #include <vector>
  22. #include <unordered_set>
  23. #include "ir/anf.h"
  24. #include "backend/session/kernel_graph.h"
  25. #include "backend/kernel_compiler/kernel_build_info.h"
  26. #include "backend/session/session_context.h"
  27. #include "ir/tensor.h"
  28. #include "runtime/device/ascend/profiling/profiling_utils.h"
  29. #include "runtime/device/kernel_info.h"
  30. using mindspore::device::ascend::ProfilingTraceInfo;
  31. using mindspore::device::ascend::ProfilingUtils;
  32. namespace mindspore {
  33. constexpr auto kCurLoopCountParamName = "cur_loop_count";
  34. constexpr auto kNextLoopCountParamName = "next_loop_count";
  35. constexpr auto kIterLoopParamName = "iter_loop";
  36. constexpr auto kOneParamName = "one";
  37. constexpr auto kEpochParamName = "loop_epoch";
  38. constexpr auto kStreamNeedActivedFirst = "stream_need_active_first";
  39. constexpr uint32_t kSecondStreamSwitchLabel = 2;
  40. enum StreamSwitchKind {
  41. kFpBpStreamSwitch = 0,
  42. kGetNextStreamSwitch = 1,
  43. kEosStreamSwitch = 2,
  44. kIndependentStreamSwitch = 3
  45. };
  46. namespace device {
  47. class KernelAdjust {
  48. public:
  49. static KernelAdjust &GetInstance() {
  50. static KernelAdjust instance;
  51. return instance;
  52. }
  53. void InsertSwitchLoop(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr);
  54. bool StepLoadCtrlInputs(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr);
  55. void Profiling(NotNull<session::KernelGraph *> kernel_graph_ptr);
  56. static bool NeedInsertSwitch();
  57. CNodePtr CreateStreamActiveOp(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr);
  58. private:
  59. KernelAdjust() = default;
  60. ~KernelAdjust() = default;
  61. void ReorderGetNext(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr);
  62. CNodePtr CreateRecvApplyKernel(const std::shared_ptr<session::KernelGraph> &graph_ptr, uint32_t event_id);
  63. CNodePtr CreateSendApplyKernel(const std::shared_ptr<session::KernelGraph> &graph_ptr, uint32_t event_id);
  64. void CreateSwitchOpParameters(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  65. std::map<std::string, mindspore::ParameterPtr> *switch_loop_input);
  66. CNodePtr CreateStreamSwitchOp(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  67. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input,
  68. StreamSwitchKind kind);
  69. CNodePtr CreatTupleGetItemNode(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr, const CNodePtr &node,
  70. size_t output_idx);
  71. CNodePtr CreateEndOfSequenceOP(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  72. const CNodePtr &getnext_cnode);
  73. CNodePtr CreateStreamAssignAddnOP(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  74. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input,
  75. bool cur_loop);
  76. kernel::KernelBuildInfo::KernelBuildInfoBuilder CreateMngKernelBuilder(const std::vector<std::string> &formats,
  77. const std::vector<TypeId> &type_ids);
  78. void LoadSwitchInputs(std::vector<tensor::TensorPtr> *inputs);
  79. void InsertProfilingKernel(const ProfilingTraceInfo &profiling_trace_info,
  80. NotNull<session::KernelGraph *> kernel_graph_ptr);
  81. bool ExistIndependent(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr);
  82. bool ExistGetNext(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr);
  83. void InsertSwitchLoopInput(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  84. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input);
  85. void InsertGetNextLoopStreamSwitch(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  86. std::vector<CNodePtr> *exec_order, uint32_t *getnext_switch_stream_id,
  87. uint32_t *getnext_stream_id,
  88. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input);
  89. void SetBeforeGetNextStreamID(std::vector<CNodePtr> *exec_order, const std::vector<CNodePtr> &orders,
  90. size_t *order_index, CNodePtr getnext_cnode, uint32_t getnext_stream_id);
  91. void InsertGetNextLoopFpBpStartSend(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  92. std::vector<CNodePtr> *exec_order, uint32_t *fpbp_start_event_id,
  93. uint32_t getnext_stream_id);
  94. void InsertGetNextLoopEosStartSend(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  95. std::vector<CNodePtr> *exec_order, uint32_t *eos_start_event_id,
  96. uint32_t getnext_stream_id);
  97. void InsertEosStreamSwitch(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  98. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input,
  99. std::vector<CNodePtr> *exec_order, uint32_t *eos_switch_stream_id,
  100. uint32_t *eos_stream_id);
  101. void InsertGetNextLoopEosStartRecv(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  102. std::vector<CNodePtr> *exec_order, uint32_t eos_start_event_id,
  103. uint32_t eos_stream_id);
  104. void InsertEosOp(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr, std::vector<CNodePtr> *exec_order,
  105. const CNodePtr &getnext_cnode, uint32_t eos_stream_id);
  106. void InsertEosDoneSend(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  107. std::vector<CNodePtr> *exec_order, uint32_t *eos_done_event_id, uint32_t eos_stream_id);
  108. void InsertIndepentParallel(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  109. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input,
  110. std::vector<CNodePtr> *exec_order);
  111. void InsertFpBpLoopStreamSwitch(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  112. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input,
  113. std::vector<CNodePtr> *exec_order, uint32_t *fpbp_stream_id,
  114. uint32_t *fpbp_switch_stream_id);
  115. void InsertFpBpStartRecv(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  116. std::vector<CNodePtr> *exec_order, uint32_t fpbp_start_event_id, uint32_t fpbp_stream_id);
  117. void InsertNextLoopAssignAdd(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  118. std::vector<CNodePtr> *exec_order,
  119. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input,
  120. uint32_t fpbp_stream_id);
  121. void CopyMemcpyList(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  122. const std::vector<CNodePtr> &orders, size_t order_index, std::vector<CNodePtr> *memcpy_list,
  123. std::vector<CNodePtr> *other_list);
  124. void InsertEosDoneRecv(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  125. std::vector<CNodePtr> *exec_order, uint32_t eos_done_event_id, uint32_t fpbp_stream_id);
  126. void InsertGetNextLoopStreamActive(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  127. std::vector<CNodePtr> *exec_order,
  128. const std::vector<uint32_t> &getnext_active_streams);
  129. void InsertCurrentLoopAssignAdd(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  130. std::vector<CNodePtr> *exec_order,
  131. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input);
  132. void InsertFpBpAndEosLoopStreamActive(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  133. std::vector<CNodePtr> *exec_order,
  134. const std::vector<uint32_t> &fpbp_active_streams);
  135. };
  136. } // namespace device
  137. } // namespace mindspore
  138. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_ADJUST_H_