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_address.h 8.6 kB

6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Copyright 2019-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_DEVICE_TENSOR_H
  17. #define MINDSPORE_DEVICE_TENSOR_H
  18. #include <string>
  19. #include <vector>
  20. #include <memory>
  21. #include <map>
  22. #include <utility>
  23. #include "ir/dtype.h"
  24. #include "ir/device_sync.h"
  25. #include "utils/shape_utils.h"
  26. namespace mindspore {
  27. namespace device {
  28. class Bucket;
  29. namespace cpu {
  30. class CPUSimpleMemPlan;
  31. class CPUMemoryManager;
  32. class CPUKernelRuntime;
  33. class CPUDeviceContext;
  34. } // namespace cpu
  35. namespace ascend {
  36. class AscendKernelRuntime;
  37. class AscendMemoryManager;
  38. class AscendDeviceContext;
  39. #ifndef ENABLE_SECURITY
  40. class DataDumper;
  41. #endif
  42. namespace tasksink {
  43. class TaskGenerator;
  44. } // namespace tasksink
  45. } // namespace ascend
  46. namespace gpu {
  47. class GPUKernelRuntime;
  48. class GPUMemoryManager;
  49. class GPUDeviceContext;
  50. } // namespace gpu
  51. } // namespace device
  52. } // namespace mindspore
  53. namespace mindspore {
  54. namespace device {
  55. using KernelWithIndex = std::pair<AnfNodePtr, size_t>;
  56. enum class DeviceAddressStatus { kInDevice, kInHost, kInDeviceToHost, kInHostToDevice };
  57. enum class DeviceAddressType { kUnknown, kCPU, kAscend, kGPU };
  58. static const std::map<DeviceAddressType, std::string> kDeviceTypeToName = {{DeviceAddressType::kUnknown, "Unknown"},
  59. {DeviceAddressType::kAscend, "Ascend"},
  60. {DeviceAddressType::kCPU, "CPU"},
  61. {DeviceAddressType::kGPU, "GPU"}};
  62. class DeviceAddress : public mindspore::DeviceSync {
  63. public:
  64. explicit DeviceAddress(void *ptr, size_t size) : ptr_(ptr), size_(size) {}
  65. explicit DeviceAddress(void *ptr, size_t size, const string &format, TypeId type_id)
  66. : ptr_(ptr), size_(size), format_(format), type_id_(type_id) {}
  67. explicit DeviceAddress(void *ptr, size_t size, const std::string &format, TypeId type_id,
  68. const KernelWithIndex &node_index)
  69. : ptr_(ptr), size_(size), format_(format), type_id_(type_id), node_index_(node_index) {}
  70. explicit DeviceAddress(void *ptr, size_t size, const std::string &device_name, uint32_t device_id)
  71. : ptr_(ptr), size_(size), device_name_(device_name), device_id_(device_id) {}
  72. explicit DeviceAddress(void *ptr, size_t size, const string &format, TypeId type_id, const std::string &device_name,
  73. uint32_t device_id)
  74. : ptr_(ptr), size_(size), format_(format), type_id_(type_id), device_name_(device_name), device_id_(device_id) {}
  75. explicit DeviceAddress(void *ptr, size_t size, const std::string &format, TypeId type_id,
  76. const KernelWithIndex &node_index, const std::string &device_name, uint32_t device_id)
  77. : ptr_(ptr),
  78. size_(size),
  79. format_(format),
  80. type_id_(type_id),
  81. node_index_(node_index),
  82. device_name_(device_name),
  83. device_id_(device_id) {}
  84. virtual ~DeviceAddress() { ptr_ = nullptr; }
  85. const void *GetPtr() const { return ptr_; }
  86. void set_ptr(void *ptr) { ptr_ = ptr; }
  87. size_t GetSize() const { return size_; }
  88. void SetSize(size_t size) { size_ = size; }
  89. std::string format() const { return format_; }
  90. TypeId type_id() const { return type_id_; }
  91. bool from_mem_pool() const { return from_mem_pool_; }
  92. void set_from_mem_pool(bool from_mem_pool) { from_mem_pool_ = from_mem_pool; }
  93. bool is_ptr_persisted() const { return is_ptr_persisted_; }
  94. void set_is_ptr_persisted(bool is_ptr_persisted) { is_ptr_persisted_ = is_ptr_persisted; }
  95. void set_host_shape(const ShapeVector &shape) { host_shape_ = shape; }
  96. ShapeVector host_shape() const { return host_shape_; }
  97. bool from_persistent_mem() const { return from_persistent_mem_; }
  98. void set_from_persistent_mem(bool from_persistent_mem) { from_persistent_mem_ = from_persistent_mem; }
  99. virtual void set_status(DeviceAddressStatus status) {}
  100. virtual DeviceAddressStatus status() const { return DeviceAddressStatus::kInDevice; }
  101. virtual DeviceAddressType DeviceType() const { return DeviceAddressType::kUnknown; }
  102. void *GetMutablePtr() const override { return ptr_; }
  103. std::string device_name() const { return device_name_; }
  104. uint32_t device_id() const { return device_id_; }
  105. virtual void SetNodeIndex(const AnfNodePtr &node, size_t out_index) { node_index_ = {node, out_index}; }
  106. KernelWithIndex GetNodeIndex() const {
  107. return node_index_.first.expired() ? KernelWithIndex{nullptr, node_index_.second}
  108. : KernelWithIndex{node_index_.first.lock(), node_index_.second};
  109. }
  110. // The related interface of dynamic reference count operation.
  111. void set_dynamic_ref_count(int32_t dynamic_ref_conut) { dynamic_ref_count_ = dynamic_ref_conut; }
  112. int32_t dynamic_ref_count() const { return dynamic_ref_count_; }
  113. void IncreaseDynamicRefCount(const std::string &op_object) {
  114. if (dynamic_ref_count_ < INT32_MAX) {
  115. (void)++dynamic_ref_count_;
  116. MS_LOG(DEBUG) << op_object << " increases dynamic ref count to:" << dynamic_ref_count_ << " for ptr:" << ptr_;
  117. }
  118. }
  119. void DecreaseDynamicRefCount(const std::string &op_object) {
  120. if (dynamic_ref_count_ <= 0) {
  121. MS_LOG(EXCEPTION) << "The dynamic reference count is invalid value:" << dynamic_ref_count_;
  122. }
  123. (void)--dynamic_ref_count_;
  124. MS_LOG(DEBUG) << op_object << " decreases dynamic ref count to:" << dynamic_ref_count_ << " for ptr:" << ptr_;
  125. }
  126. virtual bool DumpMemToFile(const std::string &filepath, const std::string &host_fmt, const ShapeVector &host_shape,
  127. TypeId host_type, bool trans_flag) const {
  128. return true;
  129. }
  130. #ifdef ENABLE_DEBUGGER
  131. virtual bool LoadMemToHost(const std::string &tensor_name, int execution_order, const std::string &host_fmt,
  132. const ShapeVector &host_shape, TypeId host_type, size_t slot, bool keep_prev,
  133. uint32_t root_graph_id, bool force_update) const {
  134. return true;
  135. }
  136. #endif
  137. protected:
  138. const void *ptr() const { return ptr_; }
  139. size_t size() const { return size_; }
  140. mutable void *ptr_{nullptr};
  141. size_t size_{0};
  142. string format_{"DefaultFormat"};
  143. TypeId type_id_{kNumberTypeFloat16};
  144. mutable bool from_mem_pool_{false};
  145. uint8_t *communication_ptr_{nullptr};
  146. ShapeVector host_shape_{};
  147. // {node, out_index}
  148. std::pair<AnfNodeWeakPtr, size_t> node_index_{AnfNodePtr(nullptr), 0};
  149. // The device address of the node that owns the device address cannot be updated and replaced.
  150. // Application scenario: set to true when the hardware execution mode requires that ptr cannot be changed during
  151. // execution.
  152. bool is_ptr_persisted_{false};
  153. // The device address generated in the control flow scene uses dynamic_ref_count_.
  154. std::atomic_int32_t dynamic_ref_count_{INT32_MAX};
  155. // The key of device context.
  156. std::string device_name_{""};
  157. uint32_t device_id_{0};
  158. bool from_persistent_mem_{false};
  159. friend class KernelRuntime;
  160. friend class MemoryManager;
  161. friend class mindspore::device::ascend::tasksink::TaskGenerator;
  162. friend class mindspore::device::cpu::CPUSimpleMemPlan;
  163. friend class mindspore::device::cpu::CPUMemoryManager;
  164. friend class mindspore::device::cpu::CPUKernelRuntime;
  165. friend class mindspore::device::cpu::CPUDeviceContext;
  166. friend class mindspore::device::gpu::GPUKernelRuntime;
  167. friend class mindspore::device::gpu::GPUMemoryManager;
  168. friend class mindspore::device::gpu::GPUDeviceContext;
  169. friend class mindspore::device::ascend::AscendKernelRuntime;
  170. friend class mindspore::device::ascend::AscendMemoryManager;
  171. friend class mindspore::device::ascend::AscendDeviceContext;
  172. #ifndef ENABLE_SECURITY
  173. friend class mindspore::device::ascend::DataDumper;
  174. #endif
  175. friend class mindspore::device::Bucket;
  176. };
  177. using DeviceAddressPtr = std::shared_ptr<DeviceAddress>;
  178. using DeviceAddressPtrList = std::vector<DeviceAddressPtr>;
  179. } // namespace device
  180. } // namespace mindspore
  181. #endif // MINDSPORE_DEVICE_TENSOR_H