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.

attributes_holder.cc 7.4 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "detail/attributes_holder.h"
  17. #include <map>
  18. #include "debug/ge_log.h"
  19. #include "debug/ge_util.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "graph/ge_attr_value.h"
  22. #include "proto/ge_ir.pb.h"
  23. namespace ge {
  24. using std::map;
  25. using std::unordered_set;
  26. void AttrHolder::CopyAttrsFrom(const AttrHolder &holder) { MutableAttrMap().CopyValueFrom(holder.GetAttrMap()); }
  27. graphStatus AttrHolder::SetAttr(const std::string &name, const GeAttrValue &value) {
  28. if (value.IsEmpty()) {
  29. GELOGE(GRAPH_FAILED, "value is empty, key %s", name.c_str());
  30. return GRAPH_FAILED;
  31. }
  32. auto proto_map = MutableAttrMap().GetProtoMsg();
  33. auto proto_val = value.value_.GetProtoMsg();
  34. if (proto_map == nullptr || proto_val == nullptr) {
  35. return GRAPH_FAILED;
  36. }
  37. auto it = proto_map->find(name);
  38. if (it != proto_map->end()) {
  39. if (it->second.value_case() != proto::AttrDef::VALUE_NOT_SET &&
  40. it->second.value_case() != proto_val->value_case()) {
  41. return GRAPH_FAILED;
  42. }
  43. }
  44. (*proto_map)[name] = *proto_val;
  45. return GRAPH_SUCCESS;
  46. }
  47. graphStatus AttrHolder::AddRequiredAttr(const std::string &name) {
  48. if (HasAttr(name)) {
  49. return GRAPH_FAILED;
  50. }
  51. requiredAttrs_.push_back(name);
  52. return GRAPH_SUCCESS;
  53. }
  54. graphStatus AttrHolder::GetAttr(const std::string &name, GeAttrValue &value) const {
  55. auto proto_map = GetAttrMap().GetProtoMsg();
  56. auto proto_val = value.value_.GetProtoMsg();
  57. if (proto_map == nullptr || proto_val == nullptr) {
  58. return GRAPH_FAILED;
  59. }
  60. auto it = proto_map->find(name);
  61. if (it != proto_map->end()) {
  62. *proto_val = it->second;
  63. return GRAPH_SUCCESS;
  64. }
  65. return GRAPH_FAILED;
  66. }
  67. bool AttrHolder::HasAttr(const std::string &name) const {
  68. auto proto_map = GetAttrMap().GetProtoMsg();
  69. if (proto_map != nullptr) {
  70. if (proto_map->find(name) != proto_map->end()) {
  71. return true;
  72. }
  73. }
  74. return std::find(requiredAttrs_.begin(), requiredAttrs_.end(), name) != requiredAttrs_.end();
  75. }
  76. graphStatus AttrHolder::DelAttr(const std::string &name) {
  77. auto proto_map = MutableAttrMap().GetProtoMsg();
  78. if (proto_map == nullptr) {
  79. return GRAPH_FAILED;
  80. }
  81. auto it = proto_map->find(name);
  82. if (it != proto_map->end()) {
  83. (void)proto_map->erase(it);
  84. return GRAPH_SUCCESS;
  85. }
  86. return GRAPH_FAILED;
  87. }
  88. const std::map<string, GeAttrValue> AttrHolder::GetAllAttrs() const {
  89. std::map<string, GeAttrValue> attr_value_map;
  90. auto proto_map = GetAttrMap().GetProtoMsg();
  91. if (proto_map != nullptr) {
  92. auto proto_owner = GetAttrMap().GetProtoOwner();
  93. GE_CHK_BOOL_EXEC(proto_owner != nullptr, return attr_value_map, "proto_owner is nullptr");
  94. for (const auto &it : *proto_map) {
  95. attr_value_map[it.first] = GeAttrValue(proto_owner, const_cast<proto::AttrDef *>(&it.second));
  96. }
  97. }
  98. return attr_value_map;
  99. }
  100. const std::unordered_set<string> AttrHolder::GetAllAttrNames() const {
  101. std::unordered_set<string> names;
  102. auto proto_map = GetAttrMap().GetProtoMsg();
  103. if (proto_map != nullptr) {
  104. for (const auto &it : *proto_map) {
  105. (void)names.insert(it.first);
  106. }
  107. }
  108. for (const string &it : requiredAttrs_) {
  109. (void)names.insert(it);
  110. }
  111. return names;
  112. }
  113. template <>
  114. void GeIrProtoHelper<proto::AttrDef>::InitDefault() {
  115. std::shared_ptr<proto::AttrDef> proto_owner;
  116. proto_owner = ComGraphMakeShared<proto::AttrDef>();
  117. if (proto_owner == nullptr) {
  118. GELOGE(GRAPH_FAILED, "proto::AttrDef make shared failed");
  119. return;
  120. }
  121. protoMsg_ = proto_owner.get();
  122. protoOwner_ = proto_owner;
  123. }
  124. template <>
  125. void GeIrProtoHelper<proto::TensorDef>::InitDefault() {
  126. std::shared_ptr<proto::TensorDef> proto_owner;
  127. proto_owner = ComGraphMakeShared<proto::TensorDef>();
  128. if (proto_owner == nullptr) {
  129. GELOGE(GRAPH_FAILED, "proto::TensorDef make shared failed");
  130. return;
  131. }
  132. protoMsg_ = proto_owner.get();
  133. protoOwner_ = proto_owner;
  134. }
  135. template <>
  136. void GeIrProtoHelper<proto::TensorDescriptor>::InitDefault() {
  137. std::shared_ptr<proto::TensorDescriptor> proto_owner;
  138. proto_owner = ComGraphMakeShared<proto::TensorDescriptor>();
  139. if (proto_owner == nullptr) {
  140. GELOGE(GRAPH_FAILED, "proto::TensorDescriptor make shared failed");
  141. return;
  142. }
  143. protoMsg_ = proto_owner.get();
  144. protoOwner_ = proto_owner;
  145. }
  146. template <>
  147. void GeIrProtoHelper<proto::ShapeDef>::InitDefault() {
  148. std::shared_ptr<proto::ShapeDef> proto_owner;
  149. proto_owner = ComGraphMakeShared<proto::ShapeDef>();
  150. if (proto_owner == nullptr) {
  151. GELOGE(GRAPH_FAILED, "proto::ShapeDef make shared failed");
  152. return;
  153. }
  154. protoMsg_ = proto_owner.get();
  155. protoOwner_ = proto_owner;
  156. }
  157. template <>
  158. void GeIrProtoHelper<proto::NamedAttrs>::InitDefault() {
  159. std::shared_ptr<proto::NamedAttrs> proto_owner;
  160. proto_owner = ComGraphMakeShared<proto::NamedAttrs>();
  161. if (proto_owner == nullptr) {
  162. GELOGE(GRAPH_FAILED, "proto::NamedAttrs make shared failed");
  163. return;
  164. }
  165. protoMsg_ = proto_owner.get();
  166. protoOwner_ = proto_owner;
  167. }
  168. template <>
  169. void GeIrProtoHelper<proto::ModelDef>::InitDefault() {
  170. std::shared_ptr<proto::ModelDef> proto_owner;
  171. proto_owner = ComGraphMakeShared<proto::ModelDef>();
  172. if (proto_owner == nullptr) {
  173. GELOGE(GRAPH_FAILED, "proto::ModelDef make shared failed");
  174. return;
  175. }
  176. protoMsg_ = proto_owner.get();
  177. protoOwner_ = proto_owner;
  178. }
  179. template <>
  180. void GeIrProtoHelper<proto::OpDef>::InitDefault() {
  181. std::shared_ptr<proto::OpDef> proto_owner;
  182. proto_owner = ComGraphMakeShared<proto::OpDef>();
  183. if (proto_owner == nullptr) {
  184. GELOGE(GRAPH_FAILED, "proto::OpDef make shared failed");
  185. return;
  186. }
  187. protoMsg_ = proto_owner.get();
  188. protoOwner_ = proto_owner;
  189. }
  190. template <>
  191. void GeIrProtoHelper<proto::GraphDef>::InitDefault() {
  192. std::shared_ptr<proto::GraphDef> proto_owner;
  193. proto_owner = ComGraphMakeShared<proto::GraphDef>();
  194. if (proto_owner == nullptr) {
  195. GELOGE(GRAPH_FAILED, "proto::GraphDef make shared failed");
  196. return;
  197. }
  198. protoMsg_ = proto_owner.get();
  199. protoOwner_ = proto_owner;
  200. }
  201. template <>
  202. void GeIrProtoHelper<ProtoAttrMap>::InitDefault() {
  203. std::shared_ptr<proto::TensorDescriptor> proto_owner;
  204. proto_owner = ComGraphMakeShared<proto::TensorDescriptor>();
  205. if (proto_owner == nullptr) {
  206. GELOGE(GRAPH_FAILED, "proto::TensorDescriptor make shared failed");
  207. return;
  208. }
  209. protoMsg_ = proto_owner->mutable_attr();
  210. protoOwner_ = proto_owner;
  211. }
  212. template <>
  213. void GeIrProtoHelper<const ProtoAttrMap>::InitDefault() {
  214. std::shared_ptr<proto::TensorDescriptor> proto_owner;
  215. proto_owner = ComGraphMakeShared<proto::TensorDescriptor>();
  216. if (proto_owner == nullptr) {
  217. GELOGE(GRAPH_FAILED, "proto::TensorDescriptor make shared failed");
  218. return;
  219. }
  220. protoMsg_ = &proto_owner->attr();
  221. protoOwner_ = proto_owner;
  222. }
  223. } // namespace ge

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