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 10 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
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #ifdef ENABLE_DEBUGGER
  94. // set debugger
  95. void SetDebugger() {
  96. #if !defined(_WIN32) && !defined(_WIN64)
  97. debugger_ = Debugger::GetInstance();
  98. #endif
  99. }
  100. #endif
  101. #ifndef ENABLE_SECURITY
  102. virtual void PreInit() {}
  103. #endif
  104. virtual uint64_t GetAvailableMemMaxSize() const { return 0; }
  105. virtual void GenKernelEvents(const session::KernelGraph &graph);
  106. virtual std::shared_ptr<DeviceEvent> CreateDeviceEvent() { return nullptr; }
  107. virtual std::shared_ptr<DeviceEvent> CreateDeviceTimeEvent() { return nullptr; }
  108. virtual DeviceAddressType GetTargetDeviceAddressType() const = 0;
  109. virtual void *compute_stream() const { return nullptr; }
  110. virtual void *communication_stream() const { return nullptr; }
  111. void UpdateRefNodeOutputMem(const session::KernelGraph &graph);
  112. virtual DeviceAddressPtr AssignExtraStaticMem(const TensorPtr &tensor, const AnfNodePtr &node, size_t index);
  113. virtual void *GetModelStream(uint32_t graph_id) const { return nullptr; }
  114. // add for MindRT
  115. std::shared_ptr<MemoryManager> GetMemoryManager() { return mem_manager_; }
  116. void AssignStaticMemoryOutput(const session::KernelGraph &graph);
  117. void AssignDynamicMemory(const session::KernelGraph &graph);
  118. protected:
  119. virtual DeviceAddressPtr CreateDeviceAddress(void *device_ptr, size_t device_size, const string &format,
  120. TypeId type_id) const = 0;
  121. virtual DeviceAddressPtr CreateDeviceAddress(void *device_ptr, size_t device_size, const string &format,
  122. TypeId type_id, const KernelWithIndex &node_index) const = 0;
  123. virtual bool NodeOutputDeviceAddressExist(const AnfNodePtr &node, size_t index);
  124. virtual bool KernelMemNotReuse(const AnfNodePtr &node);
  125. void AssignStaticMemory(const session::KernelGraph &graph);
  126. void AssignNodeOutputMem(MemType type, const AnfNodePtr &node, int index);
  127. void AssignWorkSpaceMem(MemType type, const AnfNodePtr &node);
  128. void AssignCommunicationNodeOutputMem(MemType type, const AnfNodePtr &node);
  129. void AssignCommunicationNodeInputMem(MemType type, const AnfNodePtr &node);
  130. void AssignCommunicationNodeMem(MemType type, const AnfNodePtr &node);
  131. bool LaunchKernelWithPynativeProfiling(kernel::KernelMod *kernel_mod, const std::string &op_name,
  132. const KernelLaunchInfo &kernel_launch_address, void *stream);
  133. virtual void KernelLaunchProfiling(const std::string &kernel_name) {}
  134. private:
  135. void UseMemSchedulerIfNeeded(const session::KernelGraph &graph);
  136. bool LaunchKernel(const session::KernelGraph &graph, const AnfNodePtr &kernel,
  137. const std::shared_ptr<MemScheduler> &mem_scheduler, bool mock = false);
  138. void ResetNodeAddress(const session::KernelGraph &graph);
  139. void AssignKernelAddress(const std::shared_ptr<MemScheduler> &mem_scheduler, const AnfNodePtr &kernel,
  140. KernelLaunchInfo *kernel_launch_address);
  141. static void GetOrMallocAddress(const std::shared_ptr<MemScheduler> &mem_scheduler,
  142. const DeviceAddress *device_address, const kernel::AddressPtr &kernel_addr);
  143. void InitGraphInputTensors(const std::shared_ptr<MemScheduler> &mem_scheduler, const session::KernelGraph &graph);
  144. void SyncNodeOutputTensors(const std::shared_ptr<MemScheduler> &mem_scheduler, const session::KernelGraph &graph,
  145. const AnfNodePtr &kernel, bool mock);
  146. void AssignCommunicationMem(const session::KernelGraph &graph);
  147. bool LaunchKernelMod(const session::KernelGraph &graph, bool mock = false);
  148. void LaunchKernelEvent(const std::vector<std::vector<std::function<void()>>> &run_events, size_t index) const;
  149. void DebugStreamSync(const CNodePtr &kernel);
  150. static void GenAddrCleanLaunchArgs(const CNodePtr &cnode, AddressPtrList *kernel_inputs,
  151. const std::shared_ptr<MemScheduler> &mem_schedule = nullptr);
  152. void RunOpAssignInputMemory(const std::vector<tensor::TensorPtr> &input_tensors, const session::KernelGraph &graph);
  153. void RunOpAssignOutputMemory(const AnfNodePtr &kernel,
  154. const std::map<tensor::TensorPtr, session::KernelWithIndex> &tensor_to_node = {});
  155. void RunOpAssignWorkSpaceMemory(const AnfNodePtr &kernel);
  156. void RunOpAssignOutputNodeMemory(const ValuePtr &pre_output_value, const session::KernelGraph &graph);
  157. void AssignValueNodeTensor(const ValueNodePtr &value_node, const ValuePtr &node_value, size_t output_idx);
  158. DeviceAddressPtr PreAssignCNodeMemory(const AnfNodePtr &anf_node, size_t index) const;
  159. #if ((defined ENABLE_CPU) && (!defined _WIN32))
  160. void GetFirstPSEmbeddingCache(const session::KernelGraph &graph, AnfNodePtr *const first_cache_input_index,
  161. size_t *const first_cache_size);
  162. void CheckIfSupportPSEmbeddingCache(const session::KernelGraph &graph);
  163. void CheckSparsePSEmbeddingCache(const CNodePtr &node);
  164. #endif
  165. void GetCommunicationInputInfo(const AnfNodePtr &node, size_t *total_size, DeviceAddressPtrList *address_list,
  166. std::vector<size_t> *align_size_list) const;
  167. void GetCommunicationOutputInfo(const AnfNodePtr &node, size_t *total_size, DeviceAddressPtrList *address_list,
  168. std::vector<size_t> *align_size_list) const;
  169. protected:
  170. uint32_t device_id_{0};
  171. bool pynative_mode_profiling_flag_{false};
  172. #if defined(ENABLE_DEBUGGER) && !defined(_WIN32) && !defined(_WIN64)
  173. std::shared_ptr<Debugger> debugger_;
  174. #endif
  175. void *stream_{nullptr};
  176. void *independent_stream_{nullptr};
  177. void *communication_stream_{nullptr};
  178. std::shared_ptr<MemoryManager> mem_manager_{nullptr};
  179. std::map<uint32_t, std::vector<DynamicKernelPtr>> graph_dynamic_kernel_map_;
  180. std::map<uint32_t,
  181. std::pair<std::vector<std::vector<std::function<void()>>>, std::vector<std::vector<std::function<void()>>>>>
  182. graph_kernel_events_map_;
  183. MemSchedulerManager mem_scheduler_manager_;
  184. };
  185. using KernelRuntimePtr = std::shared_ptr<KernelRuntime>;
  186. } // namespace device
  187. } // namespace mindspore
  188. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_H_