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 5.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. private:
  73. static GeTensorPtr MutableWeights(ge::OpDesc& op_desc);
  74. static GeTensorPtr MutableWeights(ge::OpDescPtr op_desc);
  75. static graphStatus SetWeights(ge::OpDesc& op_desc, const GeTensorPtr weight);
  76. static graphStatus SetWeights(ge::OpDescPtr op_desc, const GeTensorPtr weight);
  77. };
  78. class OpDescBuilder {
  79. public:
  80. OpDescBuilder(std::string name, std::string type) : name_(std::move(name)), type_(std::move(type)) {}
  81. OpDescBuilder(const OpDescBuilder&) = delete;
  82. OpDescBuilder& operator=(const OpDescBuilder&) = delete;
  83. OpDescBuilder(const OpDescBuilder&&) = delete;
  84. OpDescBuilder& operator=(const OpDescBuilder&&) = delete;
  85. ~OpDescBuilder() = default;
  86. ///
  87. /// @brief Add input
  88. /// @param [in] name
  89. /// @return OpDescBuilder
  90. ///
  91. OpDescBuilder& AddInput(const std::string& name);
  92. ///
  93. /// @brief Add dynamic input
  94. /// @param [in] name
  95. /// @param [in] num
  96. /// @return OpDescBuilder
  97. ///
  98. OpDescBuilder& AddDynamicInput(const std::string& name, uint32_t num);
  99. ///
  100. /// @brief Add output
  101. /// @param [in] name
  102. /// @return OpDescBuilder
  103. ///
  104. OpDescBuilder& AddOutput(const std::string& name);
  105. ///
  106. /// @brief Add dynamic output
  107. /// @param [in] name
  108. /// @param [in] num
  109. /// @return OpDescBuilder
  110. ///
  111. OpDescBuilder& AddDynamicOutput(const std::string& name, uint32_t num);
  112. ///
  113. /// @brief Build op_desc
  114. /// @return OpDescPtr
  115. ///
  116. OpDescPtr Build();
  117. private:
  118. std::string name_;
  119. std::string type_;
  120. std::vector<std::string> inputs_;
  121. std::vector<std::string> outputs_;
  122. };
  123. } // namespace ge
  124. #endif // INC_GRAPH_UTILS_OP_DESC_UTILS_H_

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