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.h 4.5 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #ifndef INC_GRAPH_DETAIL_ATTRIBUTES_HOLDER_H_
  17. #define INC_GRAPH_DETAIL_ATTRIBUTES_HOLDER_H_
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <unordered_set>
  22. #include <utility>
  23. #include <vector>
  24. #include "graph/detail/any_map.h"
  25. #include "graph/ge_error_codes.h"
  26. #include "graph/types.h"
  27. namespace google {
  28. namespace protobuf {
  29. class Message;
  30. template <typename Key, typename T>
  31. class Map;
  32. } // namespace protobuf
  33. } // namespace google
  34. namespace ge {
  35. using std::string;
  36. class GeAttrValue;
  37. namespace proto {
  38. class AttrDef;
  39. class TensorDef;
  40. class TensorDescriptor;
  41. class ShapeDef;
  42. class NamedAttrs;
  43. class ModelDef;
  44. class OpDef;
  45. class GraphDef;
  46. } // namespace proto
  47. using ProtoAttrMap = ::google::protobuf::Map<::std::string, ::ge::proto::AttrDef>;
  48. using ProtoMsgOwner = std::shared_ptr<::google::protobuf::Message>;
  49. template <class ProtoType>
  50. class GeIrProtoHelper {
  51. public:
  52. GeIrProtoHelper(const ProtoMsgOwner &protoOwner, ProtoType *protoMsg)
  53. : protoOwner_(protoOwner), protoMsg_(protoMsg) {}
  54. GeIrProtoHelper() {
  55. protoOwner_ = std::shared_ptr<::google::protobuf::Message>(nullptr);
  56. protoMsg_ = nullptr;
  57. }
  58. virtual ~GeIrProtoHelper() = default;
  59. template <typename T>
  60. GeIrProtoHelper(const GeIrProtoHelper<T> &other) {
  61. protoOwner_ = other.protoOwner_;
  62. protoMsg_ = other.protoMsg_;
  63. }
  64. template <typename T>
  65. GeIrProtoHelper &operator=(const GeIrProtoHelper<T> &other) {
  66. protoOwner_ = other.protoOnwer_;
  67. protoMsg_ = other.protoMsg_;
  68. return *this;
  69. }
  70. void InitDefault();
  71. template <typename T>
  72. bool operator==(const GeIrProtoHelper<T> &other) const {
  73. return protoOwner_ == other.protoOwner_ && protoMsg_ == other.protoMsg_;
  74. }
  75. inline const ProtoMsgOwner &GetProtoOwner() const { return protoOwner_; }
  76. inline ProtoType *GetProtoMsg() const { return protoMsg_; }
  77. void CopyValueFrom(const GeIrProtoHelper<const ProtoType> &other) {
  78. if (other.protoMsg_ != nullptr && protoMsg_ != nullptr) {
  79. *protoMsg_ = *other.protoMsg_;
  80. }
  81. }
  82. void MoveValueFrom(GeIrProtoHelper<ProtoType> &&other) {
  83. if (other.protoMsg_ != nullptr && protoMsg_ != nullptr) {
  84. *protoMsg_ = std::move(*other.protoMsg_);
  85. }
  86. }
  87. // protoMsg_ is part of protoOwner_ and they have the same runtime
  88. ProtoMsgOwner protoOwner_ = nullptr;
  89. ProtoType *protoMsg_ = nullptr;
  90. friend class GeIrProtoHelper<typename std::conditional<
  91. std::is_const<ProtoType>::value, typename std::remove_const<ProtoType>::type, const ProtoType>::type>;
  92. };
  93. using ProtoAttrMapHelper = GeIrProtoHelper<ProtoAttrMap>;
  94. using ConstProtoAttrMapHelper = GeIrProtoHelper<const ProtoAttrMap>;
  95. class GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY AttrHolder {
  96. public:
  97. AttrHolder() = default;
  98. virtual ~AttrHolder() = default;
  99. graphStatus SetAttr(const string &name, const GeAttrValue &value);
  100. graphStatus GetAttr(const string &name, GeAttrValue &value) const;
  101. bool HasAttr(const string &name) const;
  102. graphStatus DelAttr(const string &name);
  103. void CopyAttrsFrom(const AttrHolder &holder);
  104. template <class T>
  105. bool SetExtAttr(const string &name, const T &value) {
  106. return extAttrs_.Set(name, value);
  107. }
  108. template <class T>
  109. T TryGetExtAttr(const string &name, T defaultValue) const {
  110. T ret(defaultValue);
  111. (void)extAttrs_.Get(name, ret);
  112. return ret;
  113. }
  114. protected:
  115. graphStatus AddRequiredAttr(const std::string &name);
  116. const std::unordered_set<string> GetAllAttrNames() const;
  117. const std::map<string, GeAttrValue> GetAllAttrs() const;
  118. virtual ProtoAttrMapHelper MutableAttrMap() = 0;
  119. virtual ConstProtoAttrMapHelper GetAttrMap() const = 0;
  120. friend class ModelSerializeImp;
  121. friend class AttrUtils;
  122. friend class AttrUtilsHelper;
  123. std::vector<string> requiredAttrs_;
  124. private:
  125. AnyMap extAttrs_;
  126. };
  127. } // namespace ge
  128. #endif // INC_GRAPH_DETAIL_ATTRIBUTES_HOLDER_H_

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

Contributors (1)