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

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

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