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.

device_context.h 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * Copyright 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_HARDWARE_DEVICE_CONTEXT_H_
  17. #define MINDSPORE_CCSRC_RUNTIME_HARDWARE_DEVICE_CONTEXT_H_
  18. #include <string>
  19. #include <vector>
  20. #include <memory>
  21. #include "runtime/device/device_address.h"
  22. #include "runtime/device/bucket.h"
  23. #include "runtime/hardware/collective/collective_communication_lib.h"
  24. #include "runtime/hardware/collective/collective_comm_lib_loader.h"
  25. #include "backend/session/kernel_graph.h"
  26. #include "backend/session/anf_runtime_algorithm.h"
  27. #include "backend/optimizer/common/common_backend_optimization.h"
  28. namespace mindspore {
  29. namespace device {
  30. using mindspore::kernel::AddressPtr;
  31. using mindspore::kernel::KernelMod;
  32. const size_t kDeviceContextsNumOne = 1;
  33. const size_t kDeviceContextsNumTwo = 2;
  34. struct DeviceContextKey {
  35. // device type name, such as 'GPU' 'Ascend' 'CPU'.
  36. std::string device_name_;
  37. uint32_t device_id_{0};
  38. // Use the result of ToString() as key to look up DeviceContext
  39. // in cache map which maintains created DeviceContext objects.
  40. std::string ToString() const { return device_name_ + "_" + std::to_string(device_id_); }
  41. };
  42. // DeviceContext is unified interface of interaction with device.
  43. class DeviceContext {
  44. public:
  45. explicit DeviceContext(const DeviceContextKey &device_context_key)
  46. : device_context_key_(device_context_key), collective_comm_lib_(nullptr) {}
  47. virtual ~DeviceContext() = default;
  48. // Initialize the device context.
  49. virtual void Initialize() = 0;
  50. // Destroy device context and release device resource.
  51. virtual void Destroy() {}
  52. // Partition the function graph through the device capability and return the partition segments.
  53. // The second parameter is the default partition segments which are provided by the framework.
  54. // Device can reprocess the default partition segments to new segments, also can partition the function graph again.
  55. // If Device can launch the whole graph and not expect partitioning the function graph, then return the empty
  56. // segments. The default behavior is return the default partition segments.
  57. virtual std::vector<GraphSegmentPtr> PartitionGraph(const FuncGraphPtr &func_graph,
  58. const std::vector<GraphSegmentPtr> &default_partition_segments) {
  59. return default_partition_segments;
  60. }
  61. // Relevant function to allocate and free device memory.
  62. virtual bool AllocateMemory(DeviceAddress *const &address, size_t size) const = 0;
  63. virtual void FreeMemory(DeviceAddress *const &address) const = 0;
  64. // Allocate continuous device memory end to end into 'addr_list'.
  65. // Communication operators may need continuous memory for input and output
  66. // to optimize the communication performance.
  67. virtual bool AllocateContinuousMemory(const std::vector<DeviceAddressPtr> &addr_list, size_t total_size,
  68. const std::vector<size_t> &size_list) const {
  69. return true;
  70. }
  71. // Create concrete device address according different device type.
  72. virtual DeviceAddressPtr CreateDeviceAddress(void *const device_ptr, size_t device_size, const string &format,
  73. TypeId type_id) const = 0;
  74. // Get device address type according different device type, such GPU, Ascend.
  75. virtual DeviceAddressType GetDeviceAddressType() const = 0;
  76. // Unify the MindIR, the default behavior uses the common unified MindIR.
  77. virtual void UnifyMindIR(const KernelGraphPtr &graph) const { opt::CommonUnifyMindIR(graph); }
  78. // Optimize the kernel graph for graph mode.
  79. virtual void OptimizeGraph(const KernelGraphPtr &graph) const {}
  80. // Optimize the single operator graph for PyNative mode.
  81. virtual void OptimizeSingleOpGraph(const KernelGraphPtr &graph) const {}
  82. // Select the matching backend kernels according to the data type and format of input and output for all
  83. // execution operators, and set final device data type and format information for backend kernels, device
  84. // data type and format which replace original data type and format will use for executing kernels.
  85. virtual void SetOperatorInfo(const std::vector<CNodePtr> &nodes) const = 0;
  86. // Generate 'KernelMod' for all kernels and set 'KernelMod' into kernel,
  87. // 'KernelMod' is real executive object of kernel.
  88. virtual void CreateKernel(const std::vector<CNodePtr> &nodes) const = 0;
  89. // Adjust kernel graph before run graph, used in Graph Mode.
  90. virtual void PreprocessBeforeRunGraph(const KernelGraphPtr &graph) const {}
  91. // Adjust single op kernel graph before run graph, used in PyNative Mode.
  92. virtual void PreprocessBeforeRunSingleOpGraph(const KernelGraphPtr &graph) const {}
  93. // Infer kernel shape and update abstract info for dynamic shape kernel.
  94. virtual void UpdateDynamicShape(const CNodePtr &kernel) const { AnfAlgo::InferShape(kernel); }
  95. // Whether the graph sink executing through the device capability, the default behavior is not sink and return false.
  96. virtual bool IsExecutingSink(const KernelGraphPtr &graph) const { return false; }
  97. // Whether the graph loop sink executing through the device capability, the default behavior is not loop sink and
  98. // return false.
  99. virtual bool IsLoopCountSink(const KernelGraphPtr &graph) const { return false; }
  100. // Launch graph, device such as Ascend support the whole graph sink to the device executing.
  101. virtual bool LaunchGraph(const KernelGraphPtr &graph) const { return true; }
  102. // Launch a kernel via 'KernelMod' of the kernel.
  103. virtual bool LaunchKernel(const CNodePtr &kernel, const std::vector<AddressPtr> &inputs,
  104. const std::vector<AddressPtr> &workspace, const std::vector<AddressPtr> &outputs,
  105. bool is_dynamic_shape = false) const {
  106. return true;
  107. }
  108. // Synchronize stream, device such as GPU and Ascend need stream to launch kernel asynchronously,
  109. // using 'SyncStream' to block thread and wait for completing all tasks in stream.
  110. // Devices that do not need stream could ignore the implementation of this function.
  111. virtual bool SyncStream(size_t stream_id = 0) const { return true; }
  112. // Get device_context_key_ to obtain device name and device id.
  113. const DeviceContextKey &device_context_key() const { return device_context_key_; }
  114. // Get rank id for distributed training.
  115. virtual uint32_t GetRankID() const { return 0; }
  116. // Create and initialize bucket for every allreduce operator. Bucket is used in PyNative distributed training mode,
  117. // one bucket handles all resource to launch and sync allreduce operator.
  118. virtual std::shared_ptr<Bucket> CreateBucket(uint32_t bucket_id, uint32_t bucket_size) const { return nullptr; }
  119. // Dynamically load collecitve communication library.
  120. // Currently four types are supported: OpenMPI and self developed framework for CPU. NCCL for GPU. HCCL for Ascend.
  121. virtual bool LoadCollectiveCommLib() { return true; }
  122. // Return collective communication object for caller to access
  123. CollectiveCommunicationLib *collective_comm_lib() const { return collective_comm_lib_; }
  124. // TODO(jiaorui): will be delete
  125. // Dump all graphs.
  126. virtual void DumpAllGraphs(const std::vector<KernelGraphPtr> &all_graphs) const {}
  127. protected:
  128. DeviceContextKey device_context_key_;
  129. // The collective communication library.
  130. CollectiveCommunicationLib *collective_comm_lib_;
  131. };
  132. using DeviceContextPtr = std::shared_ptr<DeviceContext>;
  133. } // namespace device
  134. } // namespace mindspore
  135. #endif // MINDSPORE_CCSRC_RUNTIME_HARDWARE_DEVICE_CONTEXT_H_