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

5 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * Copyright 2019-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_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 <unordered_set>
  23. #include "runtime/device/device_address.h"
  24. #include "ir/tensor.h"
  25. #include "utils/convert_utils.h"
  26. #ifdef ENABLE_DEBUGGER
  27. #include "debug/debugger/debugger.h"
  28. #endif
  29. #include "backend/session/kernel_graph.h"
  30. #include "backend/session/anf_runtime_algorithm.h"
  31. #include "backend/kernel_compiler/kernel.h"
  32. #include "utils/ms_context.h"
  33. #include "runtime/device/memory_manager.h"
  34. #include "runtime/device/executor/dynamic_kernel.h"
  35. #include "ir/device_event.h"
  36. using mindspore::tensor::Tensor;
  37. using std::vector;
  38. using TensorPtr = std::shared_ptr<Tensor>;
  39. using mindspore::kernel::AddressPtr;
  40. using AddressPtrList = std::vector<mindspore::kernel::AddressPtr>;
  41. namespace mindspore {
  42. #ifndef ENABLE_DEBUGGER
  43. class Debugger;
  44. #endif
  45. namespace device {
  46. class KernelRuntime {
  47. public:
  48. KernelRuntime() = default;
  49. virtual ~KernelRuntime();
  50. virtual bool Init() = 0;
  51. virtual void AssignMemory(session::KernelGraph *graph);
  52. void RunOpAssignMemory(const std::vector<tensor::TensorPtr> &input_tensors, session::KernelGraph *graph);
  53. void RunOpClearMemory(const session::KernelGraph *graph);
  54. static bool DumpDataEnabled();
  55. static bool DumpDataEnabledIteration();
  56. virtual bool LoadData(session::KernelGraph *graph);
  57. virtual bool Load(session::KernelGraph *graph, bool is_task_sink);
  58. virtual bool Run(session::KernelGraph *graph, bool is_task_sink) = 0;
  59. virtual bool GenDynamicKernel(const session::KernelGraph *graph) = 0;
  60. virtual bool RunDynamicKernelAsync(const session::KernelGraph *graph) = 0;
  61. bool LaunchKernel(const session::KernelGraph *graph);
  62. bool LaunchTaskBasedOnSingleKernel(kernel::KernelModPtr kernel_mod_ptr, const AddressPtrList &kernel_inputs,
  63. const AddressPtrList &kernel_outputs,
  64. const AddressPtrList &kernel_workspaces) const;
  65. virtual void AssignStaticMemoryInput(const session::KernelGraph *graph);
  66. virtual void AssignStaticMemoryValueNode(session::KernelGraph *graph);
  67. virtual void SyncValueNodeDeviceAddr(session::KernelGraph *graph);
  68. virtual void CleanValueNodeDeviceAddr(session::KernelGraph *graph);
  69. virtual void ClearGraphRuntimeResource(uint32_t graph_id, const std::vector<AnfNodePtr> &inputs,
  70. const std::unordered_set<ValueNodePtr> &value_nodes,
  71. const std::vector<CNodePtr> &execution_order);
  72. virtual void ClearOutputAddress(const std::vector<AnfNodePtr> &inputs,
  73. const std::unordered_set<ValueNodePtr> &value_nodes,
  74. const std::vector<CNodePtr> &execution_order);
  75. virtual bool SyncStream() = 0;
  76. virtual bool MemcpyAsync(void *dst, const void *src, uint64_t size, int32_t kind) = 0;
  77. virtual void ClearGlobalIdleMem() {}
  78. virtual void CreateContext() {}
  79. virtual void SetContext() {}
  80. virtual void *context() const { return nullptr; }
  81. uint8_t *MallocMem(MemType type, size_t size, const DeviceAddressPtr &address) {
  82. return mem_manager_->MallocMem(type, size, address);
  83. }
  84. uint8_t *MallocCommunicationMemFromMemPool(size_t size) {
  85. return mem_manager_->MallocCommunicationMemFromMemPool(size);
  86. }
  87. static void GenLaunchArgs(const mindspore::kernel::KernelMod &kernel_mod, const AnfNodePtr &kernel,
  88. AddressPtrList *kernel_inputs, AddressPtrList *kernel_workspaces,
  89. AddressPtrList *kernel_outputs);
  90. // for GPU and D to impl
  91. virtual void ReleaseDeviceRes() {}
  92. void set_device_id(uint32_t device_id) { device_id_ = device_id; }
  93. uint32_t device_id() { return device_id_; }
  94. DeviceAddressPtr AssignSingleOpLaunchMemory(size_t size, const std::string &format, TypeId type);
  95. // set debugger
  96. void SetDebugger() {
  97. #if !defined(_WIN32) && !defined(_WIN64)
  98. debugger_ = Debugger::GetInstance();
  99. #endif
  100. }
  101. virtual void PreInit() {}
  102. virtual uint64_t GetAvailableMemMaxSize() const { return 0; }
  103. void AddBufferPtr(std::shared_ptr<char[]> ptr) { buffer_ptrs_.push_back(ptr); }
  104. void FreeAndClearBufferPtrs() { buffer_ptrs_.clear(); }
  105. virtual DeviceAddressType GetTargetDeviceAddressType() const = 0;
  106. virtual void *compute_stream() const { return nullptr; }
  107. virtual void *communication_stream() const { return nullptr; }
  108. protected:
  109. virtual DeviceAddressPtr CreateDeviceAddress(void *device_ptr, size_t device_size, const string &format,
  110. TypeId type_id) = 0;
  111. virtual bool NodeOutputDeviceAddressExist(const AnfNodePtr &node, size_t index);
  112. virtual bool KernelMemNotReuse(const AnfNodePtr &node);
  113. void AssignStaticMemory(session::KernelGraph *graph);
  114. void AssignDynamicMemory(session::KernelGraph *graph);
  115. void ReuseAssignDynamicMemory(session::KernelGraph *graph);
  116. void AssignNodeOutputMem(MemType type, const AnfNodePtr &node, int index);
  117. void AssignWorkSpaceMem(MemType type, const AnfNodePtr &node);
  118. void AssignReuseWorkSpaceMem(const AnfNodePtr &node);
  119. void UpdateRefNodeOutputMem(const session::KernelGraph *graph);
  120. void AssignCommunicationNodeOutputMem(MemType type, const AnfNodePtr &node);
  121. void AssignCommunicationNodeInputMem(MemType type, const AnfNodePtr &node);
  122. void AssignCommunicationNodeMem(MemType type, const AnfNodePtr &node);
  123. virtual void KernelLaunchProfiling(const std::string &kernel_name) {}
  124. private:
  125. void AssignStaticMemoryOutput(const session::KernelGraph *graph);
  126. bool LaunchKernelMod(const session::KernelGraph &graph);
  127. static void GenAddrCleanLaunchArgs(const CNodePtr &cnode, AddressPtrList *kernel_inputs);
  128. size_t CountNodeDeviceMemorySize(const AnfNodePtr &node, size_t output_index);
  129. void RunOpAssignInputMemory(const std::vector<tensor::TensorPtr> &input_tensors, const session::KernelGraph *graph);
  130. void RunOpAssignOutputMemory(const AnfNodePtr &kernel);
  131. void RunOpAssignWorkSpaceMemory(const AnfNodePtr &kernel);
  132. void RunOpAssignOutputNodeMemory(const ValuePtr &pre_output_value, session::KernelGraph *graph);
  133. void AssignValueNodeTensor(const ValueNodePtr &value_node, const ValuePtr &node_value, size_t output_idx);
  134. DeviceAddressPtr PreAssignCNodeMemory(const AnfNodePtr &anf_node, size_t index);
  135. #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU))
  136. void GetFirstPSEmbeddingCache(const session::KernelGraph *graph, AnfNodePtr *first_cache_input_index,
  137. size_t *first_cache_size);
  138. void CheckIfSupportPSEmbeddingCache(const session::KernelGraph *graph);
  139. void CheckSparsePSEmbeddingCache(const CNodePtr &node);
  140. #endif
  141. protected:
  142. uint32_t device_id_{0};
  143. #if !defined(_WIN32) && !defined(_WIN64)
  144. std::shared_ptr<Debugger> debugger_;
  145. #endif
  146. void *stream_{nullptr};
  147. void *communication_stream_{nullptr};
  148. std::shared_ptr<MemoryManager> mem_manager_{nullptr};
  149. std::map<uint32_t, std::vector<DynamicKernelPtr>> graph_dynamic_kernel_map_;
  150. std::vector<std::shared_ptr<char[]>> buffer_ptrs_ = {};
  151. };
  152. using KernelRuntimePtr = std::shared_ptr<KernelRuntime>;
  153. } // namespace device
  154. } // namespace mindspore
  155. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_H_