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.3 kB

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