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 10 kB

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