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.

tensor_value.cc 3.8 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include "hybrid/common/tensor_value.h"
  17. #include <sstream>
  18. #include "framework/common/debug/ge_log.h"
  19. #include "hybrid/common/npu_memory_allocator.h"
  20. namespace ge {
  21. namespace hybrid {
  22. TensorBuffer::TensorBuffer(NpuMemoryAllocator *allocator, void *buffer, size_t size, MemStorageType mem_type)
  23. : allocator_(allocator), buffer_(buffer), size_(size), mem_type_(mem_type) {}
  24. std::unique_ptr<TensorBuffer> TensorBuffer::Create(NpuMemoryAllocator *allocator, size_t size, AllocationAttr *attr) {
  25. void *buffer = nullptr;
  26. if (size == 0) {
  27. GELOGD("size is 0");
  28. return Create(buffer, 0U);
  29. }
  30. if (allocator == nullptr) {
  31. GELOGE(INTERNAL_ERROR, "allocator is NULL");
  32. return nullptr;
  33. }
  34. MemStorageType mem_type = HBM;
  35. if (attr != nullptr) {
  36. mem_type = attr->GetMemType();
  37. }
  38. buffer = allocator->Allocate(size, attr);
  39. if (buffer == nullptr) {
  40. GELOGE(MEMALLOC_FAILED, "Failed to allocate memory. size = %zu", size);
  41. return nullptr;
  42. }
  43. GELOGD("Tensor created. addr = %p, size = %zu, mem_type = %d", buffer, size, static_cast<int32_t>(mem_type));
  44. return std::unique_ptr<TensorBuffer>(new (std::nothrow) TensorBuffer(allocator, buffer, size, mem_type));
  45. }
  46. std::unique_ptr<TensorBuffer> TensorBuffer::Create(void *buffer, size_t size) {
  47. GELOGD("Tensor created. addr = %p, size = %zu", buffer, size);
  48. return std::unique_ptr<TensorBuffer>(new (std::nothrow) TensorBuffer(nullptr, buffer, size));
  49. }
  50. TensorBuffer::~TensorBuffer() {
  51. if (allocator_ != nullptr && buffer_ != nullptr) {
  52. allocator_->Deallocate(buffer_, mem_type_);
  53. buffer_ = nullptr;
  54. }
  55. }
  56. TensorValue::TensorValue(std::shared_ptr<TensorBuffer> buffer) : buffer_(std::move(buffer)) {
  57. }
  58. TensorValue::TensorValue(void *buffer, size_t size) : ref_buffer_(buffer), ref_size_(size) {
  59. }
  60. TensorValue::~TensorValue() { Destroy(); }
  61. void TensorValue::Destroy() {
  62. if (buffer_ != nullptr || ref_buffer_ != nullptr) {
  63. GELOGD("Unref tensor: %s", DebugString().c_str());
  64. buffer_.reset();
  65. }
  66. }
  67. size_t TensorValue::GetSize() const {
  68. if (ref_buffer_ != nullptr) {
  69. return ref_size_;
  70. }
  71. if (buffer_ == nullptr) {
  72. GELOGD("TensorValue[%s] is empty", name_.c_str());
  73. return 0;
  74. }
  75. return buffer_->GetSize();
  76. }
  77. const void *TensorValue::GetData() const {
  78. if (ref_buffer_ != nullptr) {
  79. return ref_buffer_;
  80. }
  81. if (buffer_ == nullptr) {
  82. GELOGD("TensorValue[%s] is empty", name_.c_str());
  83. return nullptr;
  84. }
  85. return buffer_->GetData();
  86. }
  87. void *TensorValue::MutableData() {
  88. if (ref_buffer_ != nullptr) {
  89. return ref_buffer_;
  90. }
  91. if (buffer_ == nullptr) {
  92. GELOGD("TensorValue[%s] is empty", name_.c_str());
  93. return nullptr;
  94. }
  95. return buffer_->GetData();
  96. }
  97. std::string TensorValue::DebugString() const {
  98. std::stringstream ss;
  99. ss << "TensorValue[";
  100. if (name_.empty()) {
  101. ss << "unnamed] ";
  102. } else {
  103. ss << name_ << "] ";
  104. }
  105. if (ref_buffer_ != nullptr) {
  106. ss << "ref_addr = " << ref_buffer_ << ", size = " << ref_size_;
  107. } else if (buffer_ != nullptr) {
  108. ss << "addr = " << buffer_->GetData() << ", size = " << buffer_->GetSize();
  109. } else {
  110. ss << "addr = (nil)";
  111. }
  112. return ss.str();
  113. }
  114. } // namespace hybrid
  115. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示