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

6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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, kAscend, kCPU, 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. virtual void set_status(DeviceAddressStatus status) {}
  97. virtual DeviceAddressStatus status() const { return DeviceAddressStatus::kInDevice; }
  98. virtual DeviceAddressType DeviceType() const { return DeviceAddressType::kUnknown; }
  99. void *GetMutablePtr() const override { return ptr_; }
  100. virtual void SetNodeIndex(const AnfNodePtr &node, size_t out_index) { node_index_ = {node, out_index}; }
  101. KernelWithIndex GetNodeIndex() const {
  102. return node_index_.first.expired() ? KernelWithIndex{nullptr, node_index_.second}
  103. : KernelWithIndex{node_index_.first.lock(), node_index_.second};
  104. }
  105. virtual bool DumpMemToFile(const std::string &filepath, const std::string &host_fmt, const ShapeVector &host_shape,
  106. TypeId host_type, bool trans_flag) const {
  107. return true;
  108. }
  109. #ifdef ENABLE_DEBUGGER
  110. virtual bool LoadMemToHost(const std::string &tensor_name, int execution_order, const std::string &host_fmt,
  111. const ShapeVector &host_shape, TypeId host_type, size_t slot, bool keep_prev,
  112. uint32_t root_graph_id = 0) const {
  113. return true;
  114. }
  115. #endif
  116. protected:
  117. const void *ptr() const { return ptr_; }
  118. size_t size() const { return size_; }
  119. mutable void *ptr_{nullptr};
  120. size_t size_{0};
  121. string format_{"DefaultFormat"};
  122. TypeId type_id_{kNumberTypeFloat16};
  123. mutable bool from_mem_pool_{false};
  124. uint8_t *communication_ptr_{nullptr};
  125. ShapeVector host_shape_{};
  126. // {node, out_index}
  127. std::pair<AnfNodeWeakPtr, size_t> node_index_{AnfNodePtr(nullptr), 0};
  128. // The device address of the node that owns the device address cannot be updated and replaced.
  129. // application scenario: set to true when the hardware execution mode requires that ptr cannot be changed during
  130. // execution.
  131. bool is_ptr_persisted_{false};
  132. // The key of device context.
  133. std::string device_name_{""};
  134. uint32_t device_id_{0};
  135. friend class KernelRuntime;
  136. friend class MemoryManager;
  137. friend class mindspore::device::ascend::tasksink::TaskGenerator;
  138. friend class mindspore::device::cpu::CPUSimpleMemPlan;
  139. friend class mindspore::device::cpu::CPUMemoryManager;
  140. friend class mindspore::device::cpu::CPUKernelRuntime;
  141. friend class mindspore::device::cpu::CPUDeviceContext;
  142. friend class mindspore::device::gpu::GPUKernelRuntime;
  143. friend class mindspore::device::gpu::GPUMemoryManager;
  144. friend class mindspore::device::gpu::GPUDeviceContext;
  145. friend class mindspore::device::ascend::AscendKernelRuntime;
  146. friend class mindspore::device::ascend::AscendMemoryManager;
  147. friend class mindspore::device::ascend::AscendDeviceContext;
  148. #ifndef ENABLE_SECURITY
  149. friend class mindspore::device::ascend::DataDumper;
  150. #endif
  151. friend class mindspore::device::Bucket;
  152. };
  153. using DeviceAddressPtr = std::shared_ptr<DeviceAddress>;
  154. using DeviceAddressPtrList = std::vector<DeviceAddressPtr>;
  155. } // namespace device
  156. } // namespace mindspore
  157. #endif // MINDSPORE_DEVICE_TENSOR_H