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

5 years ago
6 years ago
4 years ago
5 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/executor/dynamic_kernel.h"
  36. #include "ir/device_event.h"
  37. using mindspore::tensor::Tensor;
  38. using std::vector;
  39. using TensorPtr = std::shared_ptr<Tensor>;
  40. using mindspore::kernel::AddressPtr;
  41. using AddressPtrList = std::vector<mindspore::kernel::AddressPtr>;
  42. namespace mindspore {
  43. #ifndef ENABLE_DEBUGGER
  44. class Debugger;
  45. #endif
  46. namespace device {
  47. class KernelRuntime {
  48. public:
  49. KernelRuntime() = default;
  50. virtual ~KernelRuntime();
  51. virtual bool Init() = 0;
  52. virtual void AssignMemory(session::KernelGraph *graph);
  53. void RunOpAssignMemory(const std::vector<tensor::TensorPtr> &input_tensors, session::KernelGraph *graph,
  54. const std::map<tensor::TensorPtr, session::KernelWithIndex> &tensor_to_node = {});
  55. void RunOpClearMemory(const session::KernelGraph *graph) const;
  56. void RunOpMallocPre(const session::KernelGraph &graph, const std::vector<tensor::TensorPtr> &input_tensors);
  57. #ifdef ENABLE_DEBUGGER
  58. static bool DumpDataEnabled();
  59. static bool DumpDataEnabledIteration();
  60. #endif
  61. virtual bool LoadData(session::KernelGraph *graph);
  62. virtual bool Load(session::KernelGraph *graph, bool is_task_sink);
  63. virtual bool Run(session::KernelGraph *graph, bool is_task_sink) = 0;
  64. virtual bool GenDynamicKernel(const session::KernelGraph *graph) = 0;
  65. virtual bool RunDynamicKernelAsync(const session::KernelGraph *graph) = 0;
  66. bool LaunchKernels(const session::KernelGraph *graph);
  67. virtual bool LaunchKernel(const AnfNodePtr &kernel);
  68. virtual void AssignStaticMemoryInput(const session::KernelGraph *graph);
  69. virtual void AssignStaticMemoryValueNode(session::KernelGraph *graph);
  70. virtual void ClearGraphRuntimeResource(uint32_t graph_id);
  71. virtual bool SyncStream() = 0;
  72. virtual bool MemcpyAsync(void *dst, const void *src, uint64_t size, int32_t kind) = 0;
  73. virtual void ClearGlobalIdleMem() {}
  74. virtual void CreateContext() {}
  75. virtual void SetContext() {}
  76. virtual const void *context() const { return nullptr; }
  77. uint8_t *MallocMem(MemType type, size_t size, const DeviceAddressPtr &address) {
  78. return mem_manager_->MallocMem(type, size, address);
  79. }
  80. uint8_t *MallocCommunicationMemFromMemPool(size_t size) {
  81. return mem_manager_->MallocCommunicationMemFromMemPool(size);
  82. }
  83. static void GenLaunchArgs(const mindspore::kernel::KernelMod &kernel_mod, const AnfNodePtr &kernel,
  84. AddressPtrList *kernel_inputs, AddressPtrList *kernel_workspaces,
  85. AddressPtrList *kernel_outputs);
  86. // for GPU and D to impl
  87. virtual void ReleaseDeviceRes() {}
  88. void set_device_id(uint32_t device_id) { device_id_ = device_id; }
  89. uint32_t device_id() { return device_id_; }
  90. #ifdef ENABLE_DEBUGGER
  91. // set debugger
  92. void SetDebugger() {
  93. #if !defined(_WIN32) && !defined(_WIN64)
  94. debugger_ = Debugger::GetInstance();
  95. #endif
  96. }
  97. #endif
  98. #ifndef ENABLE_SECURITY
  99. virtual void PreInit() {}
  100. #endif
  101. virtual uint64_t GetAvailableMemMaxSize() const { return 0; }
  102. virtual void GenKernelEvents(const session::KernelGraph *graph);
  103. virtual std::shared_ptr<DeviceEvent> CreateDeviceEvent() { return nullptr; }
  104. virtual std::shared_ptr<DeviceEvent> CreateDeviceTimeEvent() { return nullptr; }
  105. virtual DeviceAddressType GetTargetDeviceAddressType() const = 0;
  106. virtual void *compute_stream() const { return nullptr; }
  107. virtual void *communication_stream() const { return nullptr; }
  108. void UpdateRefNodeOutputMem(const session::KernelGraph *graph);
  109. virtual DeviceAddressPtr AssignExtraStaticMem(const TensorPtr &tensor, const AnfNodePtr &node, int index);
  110. virtual void *GetModelStream(uint32_t graph_id) const { return nullptr; }
  111. protected:
  112. virtual DeviceAddressPtr CreateDeviceAddress(void *device_ptr, size_t device_size, const string &format,
  113. TypeId type_id) = 0;
  114. virtual DeviceAddressPtr CreateDeviceAddress(void *device_ptr, size_t device_size, const string &format,
  115. TypeId type_id, const KernelWithIndex &node_index) = 0;
  116. virtual bool NodeOutputDeviceAddressExist(const AnfNodePtr &node, size_t index);
  117. virtual bool KernelMemNotReuse(const AnfNodePtr &node);
  118. void AssignStaticMemory(session::KernelGraph *graph);
  119. void AssignDynamicMemory(session::KernelGraph *graph);
  120. void AssignNodeOutputMem(MemType type, const AnfNodePtr &node, int index);
  121. void AssignWorkSpaceMem(MemType type, const AnfNodePtr &node);
  122. void AssignCommunicationNodeOutputMem(MemType type, const AnfNodePtr &node);
  123. void AssignCommunicationNodeInputMem(MemType type, const AnfNodePtr &node);
  124. void AssignCommunicationNodeMem(MemType type, const AnfNodePtr &node);
  125. bool LaunchKernelWithPynativeProfiling(kernel::KernelMod *kernel_mod, const std::string &op_name,
  126. const std::vector<AddressPtr> &inputs,
  127. const std::vector<AddressPtr> &workspace,
  128. const std::vector<AddressPtr> &outputs, void *stream);
  129. virtual void KernelLaunchProfiling(const std::string &kernel_name) {}
  130. private:
  131. void AssignStaticMemoryOutput(const session::KernelGraph *graph);
  132. bool LaunchKernelMod(const session::KernelGraph &graph);
  133. void LaunchKernelEvent(const std::vector<std::vector<std::function<void()>>> &run_events, size_t index) const;
  134. void DebugStreamSync(const CNodePtr &kernel);
  135. static void GenAddrCleanLaunchArgs(const CNodePtr &cnode, AddressPtrList *kernel_inputs);
  136. void RunOpAssignInputMemory(const std::vector<tensor::TensorPtr> &input_tensors, const session::KernelGraph *graph);
  137. void RunOpAssignOutputMemory(const AnfNodePtr &kernel,
  138. const std::map<tensor::TensorPtr, session::KernelWithIndex> &tensor_to_node = {});
  139. void RunOpAssignWorkSpaceMemory(const AnfNodePtr &kernel);
  140. void RunOpAssignOutputNodeMemory(const ValuePtr &pre_output_value, session::KernelGraph *graph);
  141. void AssignValueNodeTensor(const ValueNodePtr &value_node, const ValuePtr &node_value, size_t output_idx);
  142. DeviceAddressPtr PreAssignCNodeMemory(const AnfNodePtr &anf_node, size_t index);
  143. #if ((defined ENABLE_CPU) && (!defined _WIN32))
  144. void GetFirstPSEmbeddingCache(const session::KernelGraph *graph, AnfNodePtr *const first_cache_input_index,
  145. size_t *const first_cache_size);
  146. void CheckIfSupportPSEmbeddingCache(const session::KernelGraph *graph);
  147. void CheckSparsePSEmbeddingCache(const CNodePtr &node);
  148. #endif
  149. protected:
  150. uint32_t device_id_{0};
  151. bool pynative_mode_profiling_flag_{false};
  152. #if defined(ENABLE_DEBUGGER) && !defined(_WIN32) && !defined(_WIN64)
  153. std::shared_ptr<Debugger> debugger_;
  154. #endif
  155. void *stream_{nullptr};
  156. void *independent_stream_{nullptr};
  157. void *communication_stream_{nullptr};
  158. std::shared_ptr<MemoryManager> mem_manager_{nullptr};
  159. std::map<uint32_t, std::vector<DynamicKernelPtr>> graph_dynamic_kernel_map_;
  160. std::map<uint32_t,
  161. std::pair<std::vector<std::vector<std::function<void()>>>, std::vector<std::vector<std::function<void()>>>>>
  162. graph_kernel_events_map_;
  163. };
  164. using KernelRuntimePtr = std::shared_ptr<KernelRuntime>;
  165. } // namespace device
  166. } // namespace mindspore
  167. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_H_