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

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. }
  54. }
  55. TensorValue::TensorValue(std::shared_ptr<TensorBuffer> buffer) : buffer_(std::move(buffer)) {
  56. }
  57. TensorValue::TensorValue(void *buffer, size_t size) : ref_buffer_(buffer), ref_size_(size) {
  58. }
  59. TensorValue::~TensorValue() { Destroy(); }
  60. void TensorValue::Destroy() {
  61. if (buffer_ != nullptr || ref_buffer_ != nullptr) {
  62. GELOGD("Unref tensor: %s", DebugString().c_str());
  63. buffer_.reset();
  64. }
  65. }
  66. size_t TensorValue::GetSize() const {
  67. if (ref_buffer_ != nullptr) {
  68. return ref_size_;
  69. }
  70. if (buffer_ == nullptr) {
  71. GELOGD("TensorValue[%s] is empty", name_.c_str());
  72. return 0;
  73. }
  74. return buffer_->GetSize();
  75. }
  76. const void *TensorValue::GetData() const {
  77. if (ref_buffer_ != nullptr) {
  78. return ref_buffer_;
  79. }
  80. if (buffer_ == nullptr) {
  81. GELOGD("TensorValue[%s] is empty", name_.c_str());
  82. return nullptr;
  83. }
  84. return buffer_->GetData();
  85. }
  86. void *TensorValue::MutableData() {
  87. if (ref_buffer_ != nullptr) {
  88. return ref_buffer_;
  89. }
  90. if (buffer_ == nullptr) {
  91. GELOGD("TensorValue[%s] is empty", name_.c_str());
  92. return nullptr;
  93. }
  94. return buffer_->GetData();
  95. }
  96. std::string TensorValue::DebugString() const {
  97. std::stringstream ss;
  98. ss << "TensorValue[";
  99. if (name_.empty()) {
  100. ss << "unnamed] ";
  101. } else {
  102. ss << name_ << "] ";
  103. }
  104. if (ref_buffer_ != nullptr) {
  105. ss << "ref_addr = " << ref_buffer_ << ", size = " << ref_size_;
  106. } else if (buffer_ != nullptr) {
  107. ss << "addr = " << buffer_->GetData() << ", size = " << buffer_->GetSize();
  108. } else {
  109. ss << "addr = (nil)";
  110. }
  111. return ss.str();
  112. }
  113. } // namespace hybrid
  114. } // namespace ge

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