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

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "runtime/device/device_address.h"
  23. #include "ir/tensor.h"
  24. #include "utils/convert_utils.h"
  25. #ifdef ENABLE_DUMP_E2E
  26. #include "debug/e2e_dump.h"
  27. #endif
  28. #ifdef ENABLE_DEBUGGER
  29. #include "debug/debugger/debugger.h"
  30. #endif
  31. #include "backend/session/kernel_graph.h"
  32. #include "backend/session/anf_runtime_algorithm.h"
  33. #include "backend/kernel_compiler/kernel.h"
  34. #include "utils/ms_context.h"
  35. #include "runtime/device/memory_manager.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 ValuePtr &pre_output_value, const std::vector<tensor::TensorPtr> &input_tensors,
  53. session::KernelGraph *graph);
  54. void RunOpClearMemory(const session::KernelGraph *graph);
  55. bool DumpDataEnabled();
  56. bool DumpDataEnabledIteration();
  57. virtual bool Run(session::KernelGraph *graph, Debugger *debugger = nullptr);
  58. virtual bool DumpData(session::KernelGraph *graph, Debugger *debugger = nullptr);
  59. virtual bool LoadData(session::KernelGraph *graph, Debugger *debugger);
  60. virtual bool RunTask(const session::KernelGraph *graph);
  61. virtual bool GenTask(const session::KernelGraph *graph);
  62. bool LaunchKernel(const session::KernelGraph *graph);
  63. bool LaunchTaskBasedOnSingleKernel(kernel::KernelModPtr kernel_mod_ptr, const AddressPtrList &kernel_inputs,
  64. const AddressPtrList &kernel_outputs,
  65. const AddressPtrList &kernel_workspaces) const;
  66. virtual void AssignStaticMemoryInput(const session::KernelGraph *graph);
  67. virtual void AssignStaticMemoryValueNode(session::KernelGraph *graph);
  68. virtual void ClearGraphRuntimeResource(uint32_t graph_id);
  69. virtual bool SyncStream() = 0;
  70. #ifdef ENABLE_DUMP_E2E
  71. DumpConfPtr GetDumpConf();
  72. #endif
  73. virtual bool LoadTask(const session::KernelGraph *graph);
  74. // for GPU and D to impl
  75. virtual void ReleaseDeviceRes() {}
  76. void set_device_id(uint32_t device_id) { device_id_ = device_id; }
  77. DeviceAddressPtr AssignSingleOpLaunchMemory(size_t size, const std::string &format, TypeId type);
  78. protected:
  79. virtual DeviceAddressPtr CreateDeviceAddress(void *device_ptr, size_t device_size, const string &format,
  80. TypeId type_id) = 0;
  81. virtual bool NodeOutputDeviceAddressExist(const AnfNodePtr &node, size_t index);
  82. void AssignStaticMemory(session::KernelGraph *graph);
  83. void AssignDynamicMemory(session::KernelGraph *graph);
  84. void ReuseAssignDynamicMemory(session::KernelGraph *graph);
  85. void AssignNodeOutputMem(MemType type, const AnfNodePtr &node, int index);
  86. void AssignWorkSpaceMem(MemType type, const AnfNodePtr &node);
  87. void AssignReuseWorkSpaceMem(const AnfNodePtr &node);
  88. void UpdateRefNodeOutputMem(const session::KernelGraph *graph);
  89. void AssignCommunicationNodeOutputMem(MemType type, const AnfNodePtr &node);
  90. void AssignCommunicationNodeInputMem(MemType type, const AnfNodePtr &node);
  91. void AssignCommunicationNodeMem(MemType type, const AnfNodePtr &node);
  92. #ifdef ENABLE_DUMP_E2E
  93. bool SetDumpConf();
  94. #endif
  95. private:
  96. void AssignStaticMemoryOutput(session::KernelGraph *graph);
  97. void GenLaunchArgs(const mindspore::kernel::KernelMod &kernel_mod, const AnfNodePtr &kernel,
  98. AddressPtrList *kernel_inputs, AddressPtrList *kernel_workspaces, AddressPtrList *kernel_outputs);
  99. bool LaunchKernelMod(const session::KernelGraph &graph);
  100. void GenAddrCleanLaunchArgs(const CNodePtr &cnode, AddressPtrList *kernel_inputs);
  101. size_t CountNodeDeviceMemorySize(const AnfNodePtr &node, size_t output_index);
  102. void RunOpAssignInputMemory(const std::vector<tensor::TensorPtr> &input_tensors, const session::KernelGraph *graph);
  103. void RunOpAssignOutputMemory(const AnfNodePtr &kernel);
  104. void RunOpAssignWorkSpaceMemory(const AnfNodePtr &kernel);
  105. void RunOpAssignOutputNodeMemory(const ValuePtr &pre_output_value, session::KernelGraph *graph);
  106. void AssignValueNodeTensor(const ValueNodePtr &value_node, const ValuePtr &node_value, size_t output_idx);
  107. DeviceAddressPtr PreAssignCNodeMemory(const AnfNodePtr &anf_node, size_t index);
  108. protected:
  109. uint32_t device_id_{0};
  110. #ifdef ENABLE_DUMP_E2E
  111. DumpConfPtr dump_conf_ptr_;
  112. #endif
  113. void *stream_ = nullptr;
  114. std::shared_ptr<MemoryManager> mem_manager_{nullptr};
  115. };
  116. using KernelRuntimePtr = std::shared_ptr<KernelRuntime>;
  117. } // namespace device
  118. } // namespace mindspore
  119. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_H_