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.

buffer.cc 3.0 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "graph/buffer.h"
  17. #include "proto/ge_ir.pb.h"
  18. #include "framework/common/debug/ge_log.h"
  19. namespace ge {
  20. Buffer::Buffer() {
  21. data_.InitDefault();
  22. if (data_.GetProtoMsg()) {
  23. buffer_ = data_.GetProtoMsg()->mutable_bt();
  24. }
  25. }
  26. Buffer::Buffer(const Buffer &other) {
  27. // Share data
  28. data_ = other.data_;
  29. buffer_ = other.buffer_;
  30. }
  31. Buffer::Buffer(std::size_t buffer_size, std::uint8_t default_val) : Buffer() { // default
  32. auto proto_msg = data_.GetProtoMsg();
  33. if (proto_msg != nullptr) {
  34. try {
  35. proto_msg->set_bt(std::string(buffer_size, default_val));
  36. buffer_ = proto_msg->mutable_bt();
  37. } catch (std::bad_alloc &e) {
  38. GELOGE(MEMALLOC_FAILED, "Failed to alloc buffer memory, buffer size %zu", buffer_size);
  39. buffer_ = nullptr;
  40. }
  41. }
  42. }
  43. Buffer Buffer::CopyFrom(const std::uint8_t *data, std::size_t buffer_size) {
  44. Buffer buffer;
  45. auto proto_msg = buffer.data_.GetProtoMsg();
  46. if (proto_msg != nullptr && data != nullptr) {
  47. try {
  48. proto_msg->set_bt(data, buffer_size);
  49. buffer.buffer_ = proto_msg->mutable_bt();
  50. } catch (std::bad_alloc &e) {
  51. GELOGE(MEMALLOC_FAILED, "Failed to alloc buffer memory, buffer size %zu", buffer_size);
  52. buffer.buffer_ = nullptr;
  53. }
  54. }
  55. return buffer;
  56. }
  57. Buffer::Buffer(const std::shared_ptr<google::protobuf::Message> &proto_owner, proto::AttrDef *buffer)
  58. : data_(proto_owner, buffer) {
  59. if (data_.GetProtoMsg() != nullptr) {
  60. buffer_ = data_.GetProtoMsg()->mutable_bt();
  61. }
  62. }
  63. Buffer::Buffer(const std::shared_ptr<google::protobuf::Message> &proto_owner, std::string *buffer)
  64. : data_(proto_owner, nullptr) {
  65. buffer_ = buffer;
  66. }
  67. Buffer &Buffer::operator=(const Buffer &other) {
  68. if (&other != this) {
  69. // Share data
  70. data_ = other.data_;
  71. buffer_ = other.buffer_;
  72. }
  73. return *this;
  74. }
  75. const std::uint8_t *Buffer::GetData() const {
  76. if (buffer_ != nullptr) {
  77. return (const std::uint8_t *)buffer_->data();
  78. }
  79. return nullptr;
  80. }
  81. std::uint8_t *Buffer::GetData() {
  82. if (buffer_ != nullptr && !buffer_->empty()) {
  83. // Avoid copy on write
  84. (void)(*buffer_)[0];
  85. return reinterpret_cast<uint8_t *>(const_cast<char *>(buffer_->data()));
  86. }
  87. return nullptr;
  88. }
  89. std::size_t Buffer::GetSize() const {
  90. if (buffer_ != nullptr) {
  91. return buffer_->size();
  92. }
  93. return 0;
  94. }
  95. void Buffer::ClearBuffer() {
  96. if (buffer_ != nullptr) {
  97. buffer_->clear();
  98. }
  99. }
  100. } // namespace ge

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