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

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