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