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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_DEVICE_KERNEL_RUNTIME_H_
  17. #define MINDSPORE_CCSRC_DEVICE_KERNEL_RUNTIME_H_
  18. #include <vector>
  19. #include <memory>
  20. #include <string>
  21. #include <map>
  22. #include "device/device_address.h"
  23. #include "ir/tensor.h"
  24. #include "predict/generator/utils/ir_model_util.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 "session/kernel_graph.h"
  32. #include "session/anf_runtime_algorithm.h"
  33. #include "kernel/kernel.h"
  34. #include "utils/context/ms_context.h"
  35. #include "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 std::vector<tensor::TensorPtr> &input_tensors, session::KernelGraph *graph);
  53. void RunOpClearMemory(const session::KernelGraph *graph);
  54. virtual bool Run(session::KernelGraph *graph);
  55. virtual bool DumpData(session::KernelGraph *graph);
  56. virtual bool LoadData(session::KernelGraph *graph, Debugger *debugger);
  57. virtual bool RunTask(const session::KernelGraph *graph);
  58. virtual bool GenTask(const session::KernelGraph *graph);
  59. bool LaunchKernel(const session::KernelGraph *graph);
  60. virtual void AssignStaticMemoryInput(const session::KernelGraph *graph);
  61. virtual void AssignStaticMemoryValueNode(session::KernelGraph *graph);
  62. virtual void ClearGraphRuntimeResource(uint32_t graph_id);
  63. virtual bool SyncStream() = 0;
  64. #ifdef ENABLE_DUMP_E2E
  65. DumpConfPtr GetDumpConf();
  66. #endif
  67. virtual bool LoadTask(const session::KernelGraph *graph);
  68. // for GPU and D to impl
  69. virtual void ReleaseDeviceRes() {}
  70. void set_device_id(uint32_t device_id) { device_id_ = device_id; }
  71. protected:
  72. virtual DeviceAddressPtr CreateDeviceAddress(void *device_ptr, size_t device_size, const string &format,
  73. TypeId type_id) = 0;
  74. virtual bool NodeOutputDeviceAddressExist(const AnfNodePtr &node, size_t index);
  75. void AssignStaticMemory(session::KernelGraph *graph);
  76. void AssignDynamicMemory(session::KernelGraph *graph);
  77. void ReuseAssignDynamicMemory(session::KernelGraph *graph);
  78. void AssignNodeOutputMem(int flag, const AnfNodePtr &node, int index);
  79. void AssignWorkSpaceMem(int flag, const AnfNodePtr &node);
  80. void AssignReuseWorkSpaceMem(const AnfNodePtr &node);
  81. void UpdateRefNodeOutputMem(const session::KernelGraph *graph);
  82. void AssignCommunicationNodeOutputMem(int flag, const AnfNodePtr &node);
  83. void AssignCommunicationNodeInputMem(const AnfNodePtr &node);
  84. void AssignCommunicationNodeMem(int flag, const AnfNodePtr &node);
  85. #ifdef ENABLE_DUMP_E2E
  86. bool SetDumpConf();
  87. #endif
  88. private:
  89. void AssignStaticMemoryOutput(session::KernelGraph *graph);
  90. void GenLaunchArgs(const session::KernelGraph &graph, const AnfNodePtr &kernel, AddressPtrList *kernel_inputs,
  91. AddressPtrList *kernel_workspaces, AddressPtrList *kernel_outputs);
  92. bool LaunchKernelMod(const session::KernelGraph &graph);
  93. void GenAddrCleanLaunchArgs(const CNodePtr &cnode, AddressPtrList *kernel_inputs);
  94. size_t CountNodeDeviceMemorySize(const AnfNodePtr &node, size_t output_index);
  95. void RunOpAssignInputMemory(const std::vector<tensor::TensorPtr> &input_tensors, const session::KernelGraph *graph);
  96. void RunOpAssignOutputMemory(const AnfNodePtr &kernel);
  97. void RunOpAssignWorkSpaceMemory(const AnfNodePtr &kernel);
  98. void AssignValueNodeTensor(const ValueNodePtr &value_node, const ValuePtr &node_value, size_t output_idx);
  99. DeviceAddressPtr PreAssignCNodeMemory(const AnfNodePtr &anf_node, size_t index);
  100. protected:
  101. uint32_t device_id_{0};
  102. #ifdef ENABLE_DUMP_E2E
  103. DumpConfPtr dump_conf_ptr_;
  104. #endif
  105. void *stream_ = nullptr;
  106. std::shared_ptr<MemoryManager> mem_manager_{nullptr};
  107. };
  108. using KernelRuntimePtr = std::shared_ptr<KernelRuntime>;
  109. } // namespace device
  110. } // namespace mindspore
  111. #endif // MINDSPORE_CCSRC_DEVICE_KERNEL_RUNTIME_H_