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_runtime.h 12 kB

4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * Copyright 2019-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 MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_H_
  17. #define MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_H_
  18. #include <vector>
  19. #include <memory>
  20. #include <string>
  21. #include <map>
  22. #include <utility>
  23. #include <unordered_set>
  24. #include "runtime/device/device_address.h"
  25. #include "ir/tensor.h"
  26. #include "include/common/utils/convert_utils.h"
  27. #ifdef ENABLE_DEBUGGER
  28. #include "debug/debugger/debugger.h"
  29. #endif
  30. #include "backend/common/session/kernel_graph.h"
  31. #include "backend/common/session/anf_runtime_algorithm.h"
  32. #include "include/common/utils/anfalgo.h"
  33. #include "kernel/kernel.h"
  34. #include "utils/ms_context.h"
  35. #include "runtime/device/memory_manager.h"
  36. #include "runtime/device/memory_scheduler.h"
  37. #include "runtime/device/executor/dynamic_kernel.h"
  38. #include "ir/device_event.h"
  39. using mindspore::tensor::Tensor;
  40. using std::vector;
  41. using TensorPtr = std::shared_ptr<Tensor>;
  42. using mindspore::kernel::AddressPtr;
  43. using mindspore::kernel::AddressPtrList;
  44. using mindspore::kernel::KernelLaunchInfo;
  45. namespace mindspore {
  46. #ifndef ENABLE_DEBUGGER
  47. class Debugger;
  48. #endif
  49. namespace device {
  50. class KernelRuntime {
  51. public:
  52. KernelRuntime() = default;
  53. virtual ~KernelRuntime();
  54. virtual bool Init() = 0;
  55. virtual void AssignMemory(const session::KernelGraph &graph);
  56. void RunOpAssignMemory(const std::vector<tensor::TensorPtr> &input_tensors, const session::KernelGraph &graph,
  57. bool is_gradient_out,
  58. const std::map<tensor::TensorPtr, session::KernelWithIndex> &tensor_to_node = {});
  59. void AssignCommunicationOutputFromMemoryPool(const AnfNodePtr &node) const;
  60. void AssignCommunicationInputFromMemoryPool(const AnfNodePtr &node) const;
  61. void RunOpClearMemory(const session::KernelGraph &graph) const;
  62. void RunOpMallocPre(const session::KernelGraph &graph, const std::vector<tensor::TensorPtr> &input_tensors);
  63. #ifdef ENABLE_DEBUGGER
  64. static bool DumpDataEnabled();
  65. static bool DumpDataEnabledIteration();
  66. #endif
  67. virtual bool LoadData(const session::KernelGraph &graph);
  68. virtual bool Load(const session::KernelGraph &graph, bool is_task_sink);
  69. virtual bool Run(const session::KernelGraph &graph, bool is_task_sink) = 0;
  70. virtual bool GenDynamicKernel(const session::KernelGraph &graph) = 0;
  71. virtual bool RunDynamicKernelAsync(const session::KernelGraph &graph) = 0;
  72. bool LaunchKernels(const session::KernelGraph &graph);
  73. virtual void AssignStaticMemoryInput(const session::KernelGraph &graph);
  74. virtual void AssignStaticMemoryValueNode(const session::KernelGraph &graph);
  75. virtual void ClearGraphRuntimeResource(uint32_t graph_id);
  76. virtual bool SyncStream() = 0;
  77. virtual bool MemcpyAsync(void *dst, const void *src, uint64_t size, int32_t kind) = 0;
  78. virtual void ClearGlobalIdleMem() {}
  79. virtual void CreateContext() {}
  80. virtual void SetContext() {}
  81. virtual const void *context() const { return nullptr; }
  82. uint8_t *MallocMem(MemType type, size_t size, const DeviceAddressPtr &address) {
  83. return mem_manager_->MallocMem(type, size, address);
  84. }
  85. uint8_t *MallocCommunicationMemFromMemPool(size_t size) {
  86. return mem_manager_->MallocCommunicationMemFromMemPool(size);
  87. }
  88. bool MallocContinuousMemFromMemPool(const DeviceAddressPtrList &addr_list, size_t total_size,
  89. std::vector<size_t> size_list) {
  90. return mem_manager_->MallocContinuousMemFromMemPool(addr_list, total_size, size_list);
  91. }
  92. static void GenLaunchArgs(const mindspore::kernel::KernelMod &kernel_mod, const AnfNodePtr &kernel,
  93. KernelLaunchInfo *kernel_launch_info);
  94. // for GPU and D to impl
  95. virtual void ReleaseDeviceRes() {}
  96. void set_device_id(uint32_t device_id) { device_id_ = device_id; }
  97. uint32_t device_id() { return device_id_; }
  98. static bool UseMemScheduler();
  99. void SyncParameter(const session::KernelGraph &graph, const std::shared_ptr<MemScheduler> &mem_scheduler);
  100. #ifdef ENABLE_DEBUGGER
  101. // set debugger
  102. void SetDebugger() {
  103. #if !defined(_WIN32) && !defined(_WIN64)
  104. debugger_ = Debugger::GetInstance();
  105. #endif
  106. }
  107. #endif
  108. #ifndef ENABLE_SECURITY
  109. virtual void PreInit() {}
  110. #endif
  111. virtual uint64_t GetAvailableMemMaxSize() const { return 0; }
  112. virtual uint64_t GetMsUsedHbmSize() const { return 0; }
  113. virtual void GenKernelEvents(const session::KernelGraph &graph);
  114. virtual std::shared_ptr<DeviceEvent> CreateDeviceEvent() { return nullptr; }
  115. virtual std::shared_ptr<DeviceEvent> CreateDeviceTimeEvent() { return nullptr; }
  116. virtual DeviceAddressType GetTargetDeviceAddressType() const = 0;
  117. virtual void *compute_stream() const { return nullptr; }
  118. virtual void *communication_stream() const { return nullptr; }
  119. void UpdateRefNodeOutputMem(const session::KernelGraph &graph);
  120. virtual DeviceAddressPtr AssignExtraStaticMem(const TensorPtr &tensor, const AnfNodePtr &node, size_t index);
  121. virtual void *GetModelStream(uint32_t graph_id) const { return nullptr; }
  122. virtual DeviceAddressPtr GetInternalDeviceAddress(const session::KernelGraph &graph, const AnfNodePtr &node) {
  123. return nullptr;
  124. }
  125. virtual void GetShadowBackendNodeMap(const session::KernelGraph &graph,
  126. std::map<AnfNodePtr, AnfNodePtr> *shadow_backend_node_map) {
  127. return;
  128. }
  129. // add for MindRT
  130. std::shared_ptr<MemoryManager> GetMemoryManager() { return mem_manager_; }
  131. void AssignStaticMemoryOutput(const session::KernelGraph &graph);
  132. void AssignDynamicMemory(const session::KernelGraph &graph);
  133. // lock runtime
  134. static std::lock_guard<std::mutex> LockRuntime();
  135. protected:
  136. virtual DeviceAddressPtr CreateDeviceAddress(void *device_ptr, size_t device_size, const string &format,
  137. TypeId type_id) const = 0;
  138. virtual DeviceAddressPtr CreateDeviceAddress(void *device_ptr, size_t device_size, const string &format,
  139. TypeId type_id, const KernelWithIndex &node_index) const = 0;
  140. virtual bool NodeOutputDeviceAddressExist(const AnfNodePtr &node, size_t index);
  141. virtual bool KernelMemNotReuse(const AnfNodePtr &node);
  142. void AssignStaticMemory(const session::KernelGraph &graph);
  143. void AssignNodeOutputMem(MemType type, const AnfNodePtr &node, int index);
  144. void AssignWorkSpaceMem(MemType type, const AnfNodePtr &node);
  145. void AssignCommunicationNodeOutputMem(MemType type, const AnfNodePtr &node);
  146. void AssignCommunicationNodeInputMem(MemType type, const AnfNodePtr &node);
  147. void AssignCommunicationNodeMem(MemType type, const AnfNodePtr &node);
  148. bool LaunchKernelWithPynativeProfiling(kernel::KernelMod *kernel_mod, const std::string &op_name,
  149. const KernelLaunchInfo &kernel_launch_address, void *stream);
  150. virtual void KernelLaunchProfiling(const std::string &kernel_name) {}
  151. private:
  152. void GetDeviceAddress(const AnfNodePtr &item, const std::map<AnfNodePtr, AnfNodePtr> shadow_backend_node_map,
  153. size_t index, uint32_t graph_id, DeviceAddressPtr *device_address);
  154. void UseMemSchedulerIfNeeded(const session::KernelGraph &graph);
  155. bool LaunchKernel(const session::KernelGraph &graph, const AnfNodePtr &kernel,
  156. const std::shared_ptr<MemScheduler> &mem_scheduler, bool mock = false);
  157. void ResetNodeAddress(const session::KernelGraph &graph);
  158. void AssignKernelAddress(const std::shared_ptr<MemScheduler> &mem_scheduler, const AnfNodePtr &kernel,
  159. KernelLaunchInfo *kernel_launch_address);
  160. static void GetOrMallocAddress(const std::shared_ptr<MemScheduler> &mem_scheduler,
  161. const DeviceAddress *device_address, const kernel::AddressPtr &kernel_addr);
  162. void InitGraphInputTensors(const std::shared_ptr<MemScheduler> &mem_scheduler, const session::KernelGraph &graph);
  163. void SyncNodeOutputTensors(const std::shared_ptr<MemScheduler> &mem_scheduler, const session::KernelGraph &graph,
  164. const AnfNodePtr &kernel);
  165. void SyncNodeOutputTensor(const std::shared_ptr<MemScheduler> &mem_scheduler, const KernelWithIndex &output,
  166. const session::KernelGraph &graph);
  167. void AssignCommunicationMem(const session::KernelGraph &graph);
  168. bool LaunchKernelMod(const session::KernelGraph &graph, bool mock = false);
  169. void LaunchKernelEvent(const std::map<AnfNodePtr, std::vector<std::function<void()>>> &run_events,
  170. const AnfNodePtr &node) const;
  171. void DebugStreamSync(const CNodePtr &kernel);
  172. static void GenAddrCleanLaunchArgs(const CNodePtr &cnode, AddressPtrList *kernel_inputs,
  173. const std::shared_ptr<MemScheduler> &mem_schedule = nullptr);
  174. void RunOpAssignInputMemory(const std::vector<tensor::TensorPtr> &input_tensors, const session::KernelGraph &graph);
  175. void RunOpAssignOutputMemory(const AnfNodePtr &kernel,
  176. const std::map<tensor::TensorPtr, session::KernelWithIndex> &tensor_to_node,
  177. bool is_gradient_out);
  178. void RunOpAssignWorkSpaceMemory(const AnfNodePtr &kernel);
  179. void RunOpAssignOutputNodeMemory(const ValuePtr &pre_output_value, const session::KernelGraph &graph);
  180. void AssignValueNodeTensor(const ValueNodePtr &value_node, const ValuePtr &node_value, size_t output_idx);
  181. DeviceAddressPtr PreAssignCNodeMemory(const AnfNodePtr &anf_node, size_t index) const;
  182. #if ((defined ENABLE_CPU) && (!defined _WIN32))
  183. void GetFirstPSEmbeddingCache(const session::KernelGraph &graph, AnfNodePtr *const first_cache_input_index,
  184. size_t *const first_cache_size);
  185. void CheckIfSupportPSEmbeddingCache(const session::KernelGraph &graph);
  186. void CheckSparsePSEmbeddingCache(const CNodePtr &node);
  187. #endif
  188. void GetCommunicationInputInfo(const AnfNodePtr &node, size_t *total_size, DeviceAddressPtrList *address_list,
  189. std::vector<size_t> *align_size_list) const;
  190. void GetCommunicationOutputInfo(const AnfNodePtr &node, size_t *total_size, DeviceAddressPtrList *address_list,
  191. std::vector<size_t> *align_size_list) const;
  192. DeviceAddressPtr CreateDeviceAddressForStringValue(const ValuePtr &value, bool use_mem_pool, uint32_t graph_id);
  193. protected:
  194. uint32_t device_id_{0};
  195. bool pynative_mode_profiling_flag_{false};
  196. #if defined(ENABLE_DEBUGGER) && !defined(_WIN32) && !defined(_WIN64)
  197. std::shared_ptr<Debugger> debugger_;
  198. #endif
  199. void *stream_{nullptr};
  200. void *independent_stream_{nullptr};
  201. void *communication_stream_{nullptr};
  202. std::shared_ptr<MemoryManager> mem_manager_{nullptr};
  203. std::map<uint32_t, std::vector<DynamicKernelPtr>> graph_dynamic_kernel_map_;
  204. std::map<uint32_t, std::pair<std::map<AnfNodePtr, std::vector<std::function<void()>>>,
  205. std::map<AnfNodePtr, std::vector<std::function<void()>>>>>
  206. graph_kernel_events_map_;
  207. MemSchedulerManager mem_scheduler_manager_;
  208. };
  209. using KernelRuntimePtr = std::shared_ptr<KernelRuntime>;
  210. } // namespace device
  211. } // namespace mindspore
  212. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_H_