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.

op_desc_utils.h 6.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_UTILS_OP_DESC_UTILS_H_
  17. #define INC_GRAPH_UTILS_OP_DESC_UTILS_H_
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include "graph/def_types.h"
  22. #include "graph/node.h"
  23. #include "graph/op_desc.h"
  24. #include "graph/operator.h"
  25. #include "graph/range_vistor.h"
  26. namespace ge {
  27. class OpDesc;
  28. using OpDescPtr = std::shared_ptr<OpDesc>;
  29. class OpDescUtils {
  30. public:
  31. template <class T>
  32. using Vistor = RangeVistor<T, std::shared_ptr<OpDesc>>;
  33. OpDescUtils() = default;
  34. ~OpDescUtils() = default;
  35. static bool HasQuantizeFactorParams(const OpDescPtr& op_desc);
  36. static bool HasQuantizeFactorParams(const OpDesc& op_desc);
  37. static graphStatus GetQuantizeFactorParams(const OpDescPtr& op_desc, QuantizeFactorParams& quant);
  38. static graphStatus GetQuantizeFactorParams(const OpDesc& op_desc, QuantizeFactorParams& quant);
  39. static graphStatus SetQuantizeFactorParams(const OpDescPtr& op_desc, const QuantizeFactorParams& quant);
  40. static graphStatus SetQuantizeFactorParams(OpDesc& op_desc, const QuantizeFactorParams& quant);
  41. static vector<ge::NodePtr> GetConstInputNode(const ge::Node& node);
  42. static vector<ConstGeTensorPtr> GetInputData(const vector<ge::NodePtr>& input_nodes);
  43. static vector<ConstGeTensorPtr> GetWeights(const ge::Node& node);
  44. static vector<ConstGeTensorPtr> GetWeights(const ge::ConstNodePtr& node);
  45. static vector<GeTensorPtr> MutableWeights(const ge::Node& node);
  46. static vector<GeTensorPtr> MutableWeights(const ge::NodePtr node);
  47. static graphStatus SetWeights(ge::Node& node, const vector<ge::GeTensorPtr>& weights);
  48. static graphStatus SetWeights(ge::NodePtr node, const vector<ge::GeTensorPtr>& weights);
  49. static graphStatus ClearWeights(ge::NodePtr node);
  50. static bool ClearInputDesc(ge::OpDescPtr op_desc, uint32_t index);
  51. static bool ClearInputDesc(const ge::NodePtr& node);
  52. static bool ClearOutputDesc(const ge::OpDescPtr& op_desc, uint32_t index);
  53. static bool ClearOutputDesc(const ge::NodePtr& node);
  54. static vector<ge::NodePtr> GetConstInputs(const ge::Node& node);
  55. static vector<ge::NodePtr> GetConstInputs(const ge::ConstNodePtr& node);
  56. static size_t GetNonConstInputsSize(const ge::Node& node);
  57. static size_t GetNonConstInputsSize(ge::ConstNodePtr node);
  58. // Index: Indicates the index of all non const inputs
  59. static GeTensorDesc GetNonConstInputTensorDesc(const ge::Node& node, size_t index_non_const = 0);
  60. static GeTensorDesc GetNonConstInputTensorDesc(const ge::ConstNodePtr& node, size_t index_non_const = 0);
  61. static bool GetNonConstInputIndex(const ge::Node& node, size_t index_non_const, size_t& index);
  62. static bool GetNonConstInputIndex(const ge::ConstNodePtr& node, size_t index_non_const, size_t& index);
  63. // Index: Indicates the index of all inputs
  64. static bool IsNonConstInput(const ge::Node& node, size_t index = 0);
  65. static bool IsNonConstInput(const ge::ConstNodePtr& node, size_t index = 0);
  66. static vector<ge::GeTensorDesc> GetNonConstTensorDesc(const ge::ConstNodePtr& node);
  67. static graphStatus AddConstOpToAnchor(InDataAnchorPtr in_anchor, const GeTensorPtr& tensor_ptr);
  68. static Operator CreateOperatorFromOpDesc(OpDescPtr op_desc);
  69. static Operator CreateOperatorFromNode(ge::ConstNodePtr node_ptr);
  70. static OpDescPtr GetOpDescFromOperator(const Operator& oprt);
  71. static OpDescPtr CreateConstOp(const GeTensorPtr& tensor_ptr);
  72. static graphStatus SetSubgraphInstanceName(const std::string& subgraph_name,
  73. const std::string& subgraph_instance_name, OpDescPtr& op_desc);
  74. private:
  75. static GeTensorPtr MutableWeights(ge::OpDesc& op_desc);
  76. static GeTensorPtr MutableWeights(ge::OpDescPtr op_desc);
  77. static graphStatus SetWeights(ge::OpDesc& op_desc, const GeTensorPtr weight);
  78. static graphStatus SetWeights(ge::OpDescPtr op_desc, const GeTensorPtr weight);
  79. };
  80. class OpDescBuilder {
  81. public:
  82. OpDescBuilder(std::string name, std::string type) : name_(std::move(name)), type_(std::move(type)) {}
  83. OpDescBuilder(const OpDescBuilder&) = delete;
  84. OpDescBuilder& operator=(const OpDescBuilder&) = delete;
  85. OpDescBuilder(const OpDescBuilder&&) = delete;
  86. OpDescBuilder& operator=(const OpDescBuilder&&) = delete;
  87. ~OpDescBuilder() = default;
  88. ///
  89. /// @brief Add input
  90. /// @param [in] name
  91. /// @return OpDescBuilder
  92. ///
  93. OpDescBuilder& AddInput(const std::string& name);
  94. ///
  95. /// @brief Add input
  96. /// @param [in] name
  97. /// @param [in] tensor
  98. /// @return OpDescBuilder
  99. ///
  100. OpDescBuilder& AddInput(const std::string& name, const GeTensorDesc& tensor);
  101. ///
  102. /// @brief Add dynamic input
  103. /// @param [in] name
  104. /// @param [in] num
  105. /// @return OpDescBuilder
  106. ///
  107. OpDescBuilder& AddDynamicInput(const std::string& name, uint32_t num);
  108. ///
  109. /// @brief Add dynamic input
  110. /// @param [in] name
  111. /// @param [in] num
  112. /// @param [in] tensor
  113. /// @return OpDescBuilder
  114. ///
  115. OpDescBuilder& AddDynamicInput(const std::string& name, uint32_t num, const GeTensorDesc& tensor);
  116. ///
  117. /// @brief Add output
  118. /// @param [in] name
  119. /// @return OpDescBuilder
  120. ///
  121. OpDescBuilder& AddOutput(const std::string& name);
  122. ///
  123. /// @brief Add output
  124. /// @param [in] name
  125. /// @param [in] tensor
  126. /// @return OpDescBuilder
  127. ///
  128. OpDescBuilder& AddOutput(const std::string& name, const GeTensorDesc& tensor);
  129. ///
  130. /// @brief Add dynamic output
  131. /// @param [in] name
  132. /// @param [in] num
  133. /// @return OpDescBuilder
  134. ///
  135. OpDescBuilder& AddDynamicOutput(const std::string& name, uint32_t num);
  136. ///
  137. /// @brief Add dynamic output
  138. /// @param [in] name
  139. /// @param [in] num
  140. /// @param [in] tensor
  141. /// @return OpDescBuilder
  142. ///
  143. OpDescBuilder& AddDynamicOutput(const std::string& name, uint32_t num, const GeTensorDesc& tensor);
  144. ///
  145. /// @brief Build op_desc
  146. /// @return OpDescPtr
  147. ///
  148. OpDescPtr Build();
  149. private:
  150. std::string name_;
  151. std::string type_;
  152. std::vector<std::pair<std::string, GeTensorDesc>> inputs_;
  153. std::vector<std::pair<std::string, GeTensorDesc>> outputs_;
  154. };
  155. } // namespace ge
  156. #endif // INC_GRAPH_UTILS_OP_DESC_UTILS_H_

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