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.

node.cc 37 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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/node.h"
  17. #include <utility>
  18. #include "debug/ge_op_types.h"
  19. #include "debug/ge_util.h"
  20. #include "external/graph/operator_factory.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "graph/ge_tensor.h"
  23. #include "graph/operator_factory_impl.h"
  24. #include "graph/shape_refiner.h"
  25. #include "utils/ge_ir_utils.h"
  26. #include "utils/node_utils.h"
  27. #include "utils/op_desc_utils.h"
  28. #include "common/util/error_manager/error_manager.h"
  29. using std::string;
  30. using std::vector;
  31. namespace ge {
  32. Node::Node(const OpDescPtr &op, const ComputeGraphPtr &owner_graph)
  33. : op_(op),
  34. owner_graph_(owner_graph),
  35. in_data_anchors_(),
  36. out_data_anchors_(),
  37. in_control_anchor_(nullptr),
  38. out_control_anchor_(nullptr),
  39. attrs_(),
  40. has_init_(false) {
  41. anchor_status_updated_ = false;
  42. }
  43. Node::~Node() {
  44. for (const auto &in_data_anchor : in_data_anchors_) {
  45. if (in_data_anchor != nullptr) {
  46. in_data_anchor->UnlinkAll();
  47. }
  48. }
  49. for (const auto &out_data_anchor : out_data_anchors_) {
  50. if (out_data_anchor != nullptr) {
  51. out_data_anchor->UnlinkAll();
  52. }
  53. }
  54. if (in_control_anchor_ != nullptr) {
  55. in_control_anchor_->UnlinkAll();
  56. }
  57. if (out_control_anchor_ != nullptr) {
  58. out_control_anchor_->UnlinkAll();
  59. }
  60. }
  61. graphStatus Node::Init() {
  62. if (has_init_) {
  63. return GRAPH_SUCCESS;
  64. }
  65. GE_CHK_BOOL_EXEC(op_ != nullptr, return GRAPH_FAILED, "original OpDesc is nullptr");
  66. size_t size = op_->GetInputsSize();
  67. for (size_t i = 0; i < size; i++) {
  68. std::shared_ptr<InDataAnchor> anchor = ComGraphMakeShared<InDataAnchor>(shared_from_this(), i);
  69. if (anchor == nullptr) {
  70. GELOGE(GRAPH_FAILED, "Current in_data_anchor is null, malloc shared_ptr failed.");
  71. return GRAPH_FAILED;
  72. }
  73. in_data_anchors_.push_back(anchor);
  74. }
  75. size = op_->GetOutputsSize();
  76. for (size_t i = 0; i < size; i++) {
  77. std::shared_ptr<OutDataAnchor> anchor = ComGraphMakeShared<OutDataAnchor>(shared_from_this(), i);
  78. if (anchor == nullptr) {
  79. GELOGE(GRAPH_FAILED, "Current out_data_anchor is null, malloc shared_ptr failed.");
  80. return GRAPH_FAILED;
  81. }
  82. out_data_anchors_.push_back(anchor);
  83. }
  84. in_control_anchor_ = ComGraphMakeShared<InControlAnchor>(shared_from_this(), -1);
  85. out_control_anchor_ = ComGraphMakeShared<OutControlAnchor>(shared_from_this(), -1);
  86. if (in_control_anchor_ == nullptr || out_control_anchor_ == nullptr) {
  87. GELOGE(GRAPH_FAILED, "Current in_control_anchor or out_control_anchor is null, malloc shared_ptr failed.");
  88. return GRAPH_FAILED;
  89. }
  90. has_init_ = true;
  91. return GRAPH_SUCCESS;
  92. }
  93. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY std::string Node::GetName() const {
  94. GE_CHK_BOOL_EXEC(op_ != nullptr, return string(), "original OpDesc is nullptr");
  95. return op_->GetName();
  96. }
  97. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY std::string Node::GetType() const {
  98. GE_CHK_BOOL_EXEC(op_ != nullptr, return string(), "original OpDesc is nullptr");
  99. return op_->GetType();
  100. }
  101. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY bool Node::NodeAttrsAreEqual(const Node &r_node) const {
  102. const auto &attr_map = this->attrs_;
  103. const auto &r_attr_map = r_node.attrs_;
  104. // 1.Verify node's map<string, AttrValue> size
  105. if (attr_map.size() != r_attr_map.size()) {
  106. GELOGE(GRAPH_FAILED, "Size of node's attr map verify failed, node name: %s.", this->GetName().c_str());
  107. return false;
  108. }
  109. // 2.Verify node's map<string, AttrValue> key, verify values is temporarily not implemented
  110. for (const auto &it : attr_map) {
  111. if (r_attr_map.count(it.first) == 0) {
  112. GELOGE(GRAPH_FAILED, "Key of node's attr map verify failed, node name: %s key name: %s.", this->GetName().c_str(),
  113. it.first.c_str());
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY bool Node::NodeMembersAreEqual(const Node &r_node) const {
  120. return ((((this->op_ != nullptr) && (r_node.op_ != nullptr) && (IsEqual(*(this->op_), *(r_node.op_), "node.op_"))) ||
  121. ((this->op_ == nullptr) && (r_node.op_ == nullptr))) &&
  122. IsEqual(this->has_init_, r_node.has_init_, "node.has_init_") &&
  123. IsEqual(this->anchor_status_updated_, r_node.anchor_status_updated_, "node.anchor_status_updated_") &&
  124. IsEqual(this->send_event_id_list_, r_node.send_event_id_list_, "node.send_event_id_list_") &&
  125. IsEqual(this->recv_event_id_list_, r_node.recv_event_id_list_, "node.recv_event_id_list_"));
  126. }
  127. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY bool Node::NodeAnchorIsEqual(const AnchorPtr &left_anchor,
  128. const AnchorPtr &right_anchor,
  129. size_t i) const {
  130. GE_IF_BOOL_EXEC(left_anchor == nullptr, GELOGE(GRAPH_FAILED, "left_anchor is null."); return false);
  131. GE_IF_BOOL_EXEC(right_anchor == nullptr, GELOGE(GRAPH_FAILED, "right_anchor is null."); return false);
  132. const auto anchor_peer_size = left_anchor->GetPeerAnchors().size();
  133. const auto right_anchor_peer_size = right_anchor->GetPeerAnchors().size();
  134. // Firstly, verify anchor's peer anchors size equal or not
  135. if (anchor_peer_size != right_anchor_peer_size) {
  136. GELOGE(GRAPH_FAILED,
  137. "Size of anchor's peer anchors verify failed, node name: %s "
  138. "anchor_peer_size [%zu] is different form [%zu] at index [%zu].",
  139. this->GetName().c_str(), anchor_peer_size, right_anchor_peer_size, i);
  140. return false;
  141. }
  142. // Secondly, verify anchor's peer anchor owner node equal or not
  143. for (size_t j = 0; j < anchor_peer_size; j++) {
  144. const auto &peer_node = left_anchor->GetPeerAnchors().at(j)->GetOwnerNode();
  145. const auto &r_peer_node = right_anchor->GetPeerAnchors().at(j)->GetOwnerNode();
  146. if (peer_node == nullptr || r_peer_node == nullptr) {
  147. GELOGE(GRAPH_FAILED, "anchor's peer node is null, node name: %s index[%zu] peer node index[%zu]. ",
  148. this->GetName().c_str(), i, j);
  149. return false;
  150. }
  151. // Determine the connection relationship by linking the node's name
  152. if (peer_node->GetName() != r_peer_node->GetName()) {
  153. GELOGE(GRAPH_FAILED,
  154. "anchor's peer node name verify failed, node name: %s index[%zu]"
  155. "peer node name %s is different from %s at index [%zu].",
  156. this->GetName().c_str(), i, peer_node->GetName().c_str(), r_peer_node->GetName().c_str(), j);
  157. return false;
  158. }
  159. }
  160. return true;
  161. }
  162. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY bool Node::NodeInConnectsAreEqual(const Node &r_node) const {
  163. // 1.Verify all in data and control anchors size
  164. const auto in_data_anchor_size = this->GetAllInDataAnchors().size();
  165. const auto r_in_data_anchor_size = r_node.GetAllInDataAnchors().size();
  166. if (in_data_anchor_size != r_in_data_anchor_size) {
  167. GELOGE(GRAPH_FAILED, "Size of node's in data anchors verify failed, node name: %s.", this->GetName().c_str());
  168. return false;
  169. }
  170. const auto l_in_anchors = this->GetAllInAnchors();
  171. const auto r_in_anchors = r_node.GetAllInAnchors();
  172. // Data anchors size equal, all anchors size not equal, means control anchor size not equal
  173. const auto in_control_anchor_size = l_in_anchors.size() - in_data_anchor_size;
  174. const auto r_in_control_anchor_size = r_in_anchors.size() - r_in_data_anchor_size;
  175. if (in_control_anchor_size != r_in_control_anchor_size) {
  176. GELOGE(GRAPH_FAILED, "Size of node's in control anchors verify failed, node name: %s.", this->GetName().c_str());
  177. return false;
  178. }
  179. // 2.Verify all in data and control anchors connect info
  180. for (size_t i = 0; i < this->GetAllInAnchors().size(); i++) {
  181. // Verify data anchors
  182. if (i < in_data_anchor_size) {
  183. const auto &in_anchor = l_in_anchors.at(i);
  184. const auto &r_in_anchor = r_in_anchors.at(i);
  185. if (!(NodeAnchorIsEqual(in_anchor, r_in_anchor, i))) {
  186. GELOGE(GRAPH_FAILED, "Node's in data control anchor verify failed, node name: %s.", this->GetName().c_str());
  187. return false;
  188. }
  189. } else {
  190. // Verify control anchors
  191. const auto &in_control_anchor = l_in_anchors.at(i);
  192. const auto &r_in_control_anchor = r_in_anchors.at(i);
  193. if (!(NodeAnchorIsEqual(in_control_anchor, r_in_control_anchor, i - in_data_anchor_size))) {
  194. GELOGE(GRAPH_FAILED, "Node's in control anchor verify failed, node name: %s.", this->GetName().c_str());
  195. return false;
  196. }
  197. }
  198. }
  199. return true;
  200. }
  201. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY bool Node::NodeOutConnectsAreEqual(const Node &r_node) const {
  202. // 1.Verify all out data and control anchors size
  203. const auto l_out_data_anchors = this->GetAllOutDataAnchors();
  204. const auto r_out_data_anchors = r_node.GetAllOutDataAnchors();
  205. const auto out_data_anchor_size = l_out_data_anchors.size();
  206. const auto r_out_data_anchor_size = r_out_data_anchors.size();
  207. if (out_data_anchor_size != r_out_data_anchor_size) {
  208. GELOGE(GRAPH_FAILED, "Size of node's out data anchors verify failed, node name: %s.", this->GetName().c_str());
  209. return false;
  210. }
  211. const auto l_out_anchors = this->GetAllOutAnchors();
  212. const auto r_out_anchors = r_node.GetAllOutAnchors();
  213. // Data anchors size equal, all anchors size not equal, means control anchor size not equal
  214. const auto out_control_anchor_size = l_out_anchors.size() - out_data_anchor_size;
  215. const auto r_out_control_anchor_size = r_out_anchors.size() - r_out_data_anchor_size;
  216. if (out_control_anchor_size != r_out_control_anchor_size) {
  217. GELOGE(GRAPH_FAILED, "Size of node's out control anchors verify failed, node name: %s.", this->GetName().c_str());
  218. return false;
  219. }
  220. // 2.Verify all out data and control anchors connect info
  221. for (size_t i = 0; i < this->GetAllOutAnchors().size(); i++) {
  222. // Verify data anchors
  223. if (i < out_data_anchor_size) {
  224. const auto &out_anchor = l_out_data_anchors.at(i);
  225. const auto &r_out_anchor = r_out_data_anchors.at(i);
  226. if (!(NodeAnchorIsEqual(out_anchor, r_out_anchor, i))) {
  227. GELOGE(GRAPH_FAILED, "Node's out data control anchor verify failed, node name: %s.", this->GetName().c_str());
  228. return false;
  229. }
  230. } else {
  231. // Verify control anchors
  232. const auto &out_control_anchor = l_out_anchors.at(i);
  233. const auto &r_out_control_anchor = r_out_anchors.at(i);
  234. if (!(NodeAnchorIsEqual(out_control_anchor, r_out_control_anchor, i - out_data_anchor_size))) {
  235. GELOGE(GRAPH_FAILED, "Node's out control anchor verify failed, node name: %s.", this->GetName().c_str());
  236. return false;
  237. }
  238. }
  239. }
  240. return true;
  241. }
  242. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY bool Node::operator==(const Node &r_node) const {
  243. return (NodeMembersAreEqual(r_node) && NodeAttrsAreEqual(r_node) && NodeInConnectsAreEqual(r_node) &&
  244. NodeOutConnectsAreEqual(r_node));
  245. }
  246. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY graphStatus Node::AddLinkFrom(const NodePtr &input_node) {
  247. // This function is deprecated, please use other two overloaded functions
  248. GE_CHECK_NOTNULL(input_node);
  249. // Input_node ---> this
  250. auto out_anchors = input_node->GetAllOutDataAnchors();
  251. if (out_anchors.size() != 1) {
  252. GELOGE(GRAPH_FAILED, "out_anchor size is:%zu, only support 1", out_anchors.size());
  253. return GRAPH_PARAM_INVALID;
  254. }
  255. GE_CHK_BOOL_EXEC(op_ != nullptr, return GRAPH_FAILED, "original OpDesc is nullptr");
  256. auto op_desc = input_node->GetOpDesc();
  257. GE_CHECK_NOTNULL(op_desc);
  258. if (op_->AddInputDesc(op_desc->GetOutputDesc(0)) != GRAPH_SUCCESS) {
  259. GELOGE(GRAPH_FAILED, "add input desc failed.");
  260. return GRAPH_FAILED;
  261. }
  262. std::shared_ptr<InDataAnchor> anchor = ComGraphMakeShared<InDataAnchor>(shared_from_this(), in_data_anchors_.size());
  263. if (anchor == nullptr) {
  264. GELOGE(GRAPH_FAILED, "out_anchor size is:%zu, malloc shared_ptr failed.", out_anchors.size());
  265. return GRAPH_FAILED;
  266. }
  267. in_data_anchors_.push_back(anchor);
  268. (void)out_anchors.at(0)->LinkTo(in_data_anchors_.back());
  269. return GRAPH_SUCCESS;
  270. }
  271. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY graphStatus Node::AddLinkFrom(const uint32_t &index,
  272. NodePtr input_node) {
  273. GE_CHECK_NOTNULL(input_node);
  274. // Input_node ---> this
  275. auto out_anchors = input_node->GetAllOutDataAnchors();
  276. if (out_anchors.size() != 1) {
  277. GELOGE(GRAPH_FAILED, "out_anchor size is:%zu, only support 1", out_anchors.size());
  278. return GRAPH_PARAM_INVALID;
  279. }
  280. GE_CHECK_NOTNULL(op_);
  281. auto op_desc = input_node->GetOpDesc();
  282. GE_CHECK_NOTNULL(op_desc);
  283. if (op_->AddInputDesc(index, op_desc->GetOutputDesc(0)) != GRAPH_SUCCESS) {
  284. GELOGE(GRAPH_FAILED, "add input desc failed.");
  285. return GRAPH_FAILED;
  286. }
  287. std::shared_ptr<InDataAnchor> anchor = ComGraphMakeShared<InDataAnchor>(shared_from_this(), in_data_anchors_.size());
  288. if (anchor == nullptr) {
  289. GELOGE(GRAPH_FAILED, "out_anchor size is:%zu, malloc shared_ptr failed.", out_anchors.size());
  290. return GRAPH_FAILED;
  291. }
  292. in_data_anchors_.push_back(anchor);
  293. (void)out_anchors.at(0)->LinkTo(in_data_anchors_.back());
  294. return GRAPH_SUCCESS;
  295. }
  296. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY graphStatus Node::AddLinkFromForParse(const NodePtr &input_node) {
  297. // This function is used for ParseWeights.
  298. GE_CHECK_NOTNULL(input_node);
  299. // Input_node ---> this
  300. auto out_anchors = input_node->GetAllOutDataAnchors();
  301. if (out_anchors.size() != 1) {
  302. GELOGE(GRAPH_PARAM_INVALID, "out_anchor size is:%zu, only support 1", out_anchors.size());
  303. return GRAPH_PARAM_INVALID;
  304. }
  305. std::shared_ptr<InDataAnchor> anchor = ComGraphMakeShared<InDataAnchor>(shared_from_this(), in_data_anchors_.size());
  306. if (anchor == nullptr) {
  307. GELOGE(GRAPH_FAILED, "out_anchor size is:%zu, make anchor failed", out_anchors.size());
  308. return GRAPH_FAILED;
  309. }
  310. in_data_anchors_.push_back(anchor);
  311. (void)out_anchors.at(0)->LinkTo(in_data_anchors_.back());
  312. return GRAPH_SUCCESS;
  313. }
  314. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY graphStatus Node::AddLinkFrom(const string &name, NodePtr input_node) {
  315. GE_CHECK_NOTNULL(input_node);
  316. // Input_node ---> this
  317. auto out_anchors = input_node->GetAllOutDataAnchors();
  318. if (out_anchors.size() != 1) {
  319. GELOGE(GRAPH_PARAM_INVALID, "out_anchor size is:%zu, only support 1", out_anchors.size());
  320. return GRAPH_PARAM_INVALID;
  321. }
  322. GE_CHECK_NOTNULL(op_);
  323. auto op_desc = input_node->GetOpDesc();
  324. GE_CHECK_NOTNULL(op_desc);
  325. if (op_->AddInputDesc(name, op_desc->GetOutputDesc(0)) != GRAPH_SUCCESS) {
  326. GELOGE(GRAPH_FAILED, "add input desc failed.");
  327. return GRAPH_FAILED;
  328. }
  329. std::shared_ptr<InDataAnchor> anchor = ComGraphMakeShared<InDataAnchor>(shared_from_this(), in_data_anchors_.size());
  330. if (anchor == nullptr) {
  331. GELOGE(GRAPH_FAILED, "out_anchor size is:%zu, malloc shared_ptr failed.", out_anchors.size());
  332. return GRAPH_FAILED;
  333. }
  334. in_data_anchors_.push_back(anchor);
  335. (void)out_anchors.at(0)->LinkTo(in_data_anchors_.back());
  336. return GRAPH_SUCCESS;
  337. }
  338. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY ComputeGraphPtr Node::GetOwnerComputeGraph() const {
  339. return owner_graph_.lock();
  340. }
  341. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY graphStatus Node::SetOwnerComputeGraph(const ComputeGraphPtr &graph) {
  342. if (graph == nullptr) {
  343. return GRAPH_PARAM_INVALID;
  344. }
  345. owner_graph_ = graph;
  346. return GRAPH_SUCCESS;
  347. }
  348. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<InDataAnchorPtr> Node::GetAllInDataAnchors() const {
  349. return Vistor<InDataAnchorPtr>(shared_from_this(), in_data_anchors_);
  350. }
  351. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<OutDataAnchorPtr> Node::GetAllOutDataAnchors() const {
  352. return Vistor<OutDataAnchorPtr>(shared_from_this(), out_data_anchors_);
  353. }
  354. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY uint32_t Node::GetAllInDataAnchorsSize() const {
  355. return in_data_anchors_.size();
  356. }
  357. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY uint32_t Node::GetAllOutDataAnchorsSize() const {
  358. return out_data_anchors_.size();
  359. }
  360. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<AnchorPtr> Node::GetAllInAnchors() const {
  361. std::vector<AnchorPtr> vec;
  362. // Push back in_data_anchors_
  363. for (const auto &in_anchor_iter : Vistor<InDataAnchorPtr>(shared_from_this(), in_data_anchors_)) {
  364. auto in_anchor = Anchor::DynamicAnchorCast<Anchor>(in_anchor_iter);
  365. if (in_anchor != nullptr) {
  366. vec.push_back(in_anchor);
  367. }
  368. }
  369. // Push back in_control_anchor_
  370. if ((in_control_anchor_->GetPeerOutControlAnchors().size() > 0) ||
  371. (in_control_anchor_->GetPeerOutDataAnchors().size() > 0)) {
  372. auto in_anchor = Anchor::DynamicAnchorCast<Anchor>(in_control_anchor_);
  373. if (in_anchor != nullptr) {
  374. vec.push_back(in_anchor);
  375. }
  376. }
  377. return Node::Vistor<AnchorPtr>(shared_from_this(), vec);
  378. }
  379. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<AnchorPtr> Node::GetAllOutAnchors() const {
  380. std::vector<AnchorPtr> vec;
  381. // Push back out_data_anchors_
  382. for (const auto &out_anchor_iter : Vistor<OutDataAnchorPtr>(shared_from_this(), out_data_anchors_)) {
  383. auto out_anchor = Anchor::DynamicAnchorCast<Anchor>(out_anchor_iter);
  384. if (out_anchor != nullptr) {
  385. vec.push_back(out_anchor);
  386. }
  387. }
  388. // Push back out_control_anchor_
  389. if (out_control_anchor_->GetPeerInControlAnchors().size() > 0 ||
  390. out_control_anchor_->GetPeerInDataAnchors().size() > 0) {
  391. auto out_anchor = Anchor::DynamicAnchorCast<Anchor>(out_control_anchor_);
  392. if (out_anchor != nullptr) {
  393. vec.push_back(out_anchor);
  394. }
  395. }
  396. return Node::Vistor<AnchorPtr>(shared_from_this(), vec);
  397. }
  398. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY InDataAnchorPtr Node::GetInDataAnchor(int idx) const {
  399. if (idx < 0 || idx >= static_cast<int>(in_data_anchors_.size())) {
  400. ErrorManager::GetInstance().ATCReportErrMessage(
  401. "E19019", {"opname", "index", "anchorname", "optype"},
  402. {GetName().c_str(), std::to_string(idx), "in_data_anchor", GetType().c_str()});
  403. GELOGE(GRAPH_FAILED, "Op[%s] doesn't have index[%d]'s in_data_anchor which optype is %s.", GetName().c_str(), idx,
  404. GetType().c_str());
  405. return nullptr;
  406. } else {
  407. return in_data_anchors_[idx];
  408. }
  409. }
  410. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY AnchorPtr Node::GetInAnchor(int idx) const {
  411. // Idx can't be less than -1 or >= in_data_anchors_.size(), -1 means index of control anchor_
  412. if (idx < -1 || idx >= static_cast<int>(in_data_anchors_.size())) {
  413. GELOGW("Op[%s] doesn't have index[%d]'s in_anchor which optype is %s.", GetName().c_str(), idx, GetType().c_str());
  414. return nullptr;
  415. } else {
  416. // Return control anchor
  417. if (idx == -1) {
  418. auto in_anchor = Anchor::DynamicAnchorCast<Anchor>(in_control_anchor_);
  419. return in_anchor;
  420. }
  421. // Return data anchor
  422. return in_data_anchors_[idx];
  423. }
  424. }
  425. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY AnchorPtr Node::GetOutAnchor(int idx) const {
  426. // Idx can't be less than -1 or >= out_data_anchors_.size(), -1 means index of control anchor_
  427. if (idx < -1 || idx >= static_cast<int>(out_data_anchors_.size())) {
  428. ErrorManager::GetInstance().ATCReportErrMessage("E19019", {"opname", "index", "anchorname", "optype"},
  429. {
  430. GetName().c_str(),
  431. std::to_string(idx),
  432. "out_anchor",
  433. GetType().c_str(),
  434. });
  435. GELOGE(GRAPH_FAILED, "Op[%s] doesn't have index[%d]'s out_anchor which optype is %s.", GetName().c_str(), idx,
  436. GetType().c_str());
  437. return nullptr;
  438. } else {
  439. // Return control anchor
  440. if (idx == -1) {
  441. auto out_anchor = Anchor::DynamicAnchorCast<Anchor>(out_control_anchor_);
  442. return out_anchor;
  443. }
  444. // Return data anchor
  445. return out_data_anchors_[idx];
  446. }
  447. }
  448. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY OutDataAnchorPtr Node::GetOutDataAnchor(int idx) const {
  449. if (idx < 0 || idx >= static_cast<int>(out_data_anchors_.size())) {
  450. ErrorManager::GetInstance().ATCReportErrMessage(
  451. "E19019", {"opname", "index", "anchorname", "optype"},
  452. {GetName().c_str(), std::to_string(idx), "out_data_anchor", GetType().c_str()});
  453. GELOGE(GRAPH_FAILED, "Op[%s] doesn't have index[%d]'s out_data_anchor which optype is %s.", GetName().c_str(), idx,
  454. GetType().c_str());
  455. return nullptr;
  456. } else {
  457. return out_data_anchors_[idx];
  458. }
  459. }
  460. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY InControlAnchorPtr Node::GetInControlAnchor() const {
  461. return in_control_anchor_;
  462. }
  463. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY OutControlAnchorPtr Node::GetOutControlAnchor() const {
  464. return out_control_anchor_;
  465. }
  466. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetInNodes() const {
  467. std::vector<NodePtr> vec;
  468. for (const auto &in_anchor : in_data_anchors_) {
  469. GE_CHK_BOOL_EXEC((in_anchor != nullptr), continue, "in_data_anchor is nullptr");
  470. auto out_anchor = in_anchor->GetPeerOutAnchor();
  471. if (out_anchor == nullptr) {
  472. continue;
  473. }
  474. auto node = out_anchor->GetOwnerNode();
  475. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  476. vec.push_back(node);
  477. }
  478. if (in_control_anchor_ != nullptr) {
  479. if (in_control_anchor_->IsPeerOutAnchorsEmpty()) {
  480. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  481. }
  482. auto peer_out_anchors = in_control_anchor_->GetPeerOutDataAnchors();
  483. for (const auto &out_anchor : peer_out_anchors) {
  484. GE_CHK_BOOL_EXEC(out_anchor != nullptr, continue, "in_control_anchor_ peer out data anchors is nullptr");
  485. auto node = out_anchor->GetOwnerNode();
  486. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  487. vec.push_back(node);
  488. }
  489. auto peer_out_control_anchors = in_control_anchor_->GetPeerOutControlAnchors();
  490. for (const auto &out_control_anchor : peer_out_control_anchors) {
  491. GE_CHK_BOOL_EXEC(out_control_anchor != nullptr, continue,
  492. "in_control_anchor_ peer out control anchors is nullptr");
  493. auto node = out_control_anchor->GetOwnerNode();
  494. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  495. vec.push_back(node);
  496. }
  497. }
  498. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  499. }
  500. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY bool Node::IsAllInNodesSeen(
  501. std::unordered_set<Node *> &nodes_seen) const {
  502. for (const auto &in_anchor : in_data_anchors_) {
  503. GE_CHK_BOOL_EXEC((in_anchor != nullptr), continue, "in_data_anchor is nullptr");
  504. auto out_anchor = in_anchor->GetPeerOutAnchor();
  505. if (out_anchor == nullptr) {
  506. continue;
  507. }
  508. auto node = out_anchor->GetOwnerNode();
  509. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  510. if ((node->GetType() == NEXTITERATION) || (node->GetType() == REFNEXTITERATION)) {
  511. continue;
  512. }
  513. if (nodes_seen.count(node.get()) == 0) {
  514. return false;
  515. }
  516. }
  517. if (in_control_anchor_ != nullptr) {
  518. if (in_control_anchor_->IsPeerOutAnchorsEmpty()) {
  519. return true;
  520. }
  521. auto peer_out_control_anchors = in_control_anchor_->GetPeerOutControlAnchors();
  522. for (const auto &out_control_anchor : peer_out_control_anchors) {
  523. GE_CHK_BOOL_EXEC(out_control_anchor != nullptr, continue, "out_control_anchor is nullptr");
  524. auto node = out_control_anchor->GetOwnerNode();
  525. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  526. if ((node->GetType() == NEXTITERATION) || (node->GetType() == REFNEXTITERATION)) {
  527. continue;
  528. }
  529. if (nodes_seen.count(node.get()) == 0) {
  530. return false;
  531. }
  532. }
  533. }
  534. return true;
  535. }
  536. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetInDataNodes() const {
  537. std::vector<NodePtr> vec;
  538. for (const auto &in_anchor : in_data_anchors_) {
  539. GE_CHK_BOOL_EXEC((in_anchor != nullptr), continue, "in_data_anchor is nullptr");
  540. auto anchor_ptr = in_anchor->GetPeerOutAnchor();
  541. if (anchor_ptr == nullptr) {
  542. continue;
  543. }
  544. auto node = anchor_ptr->GetOwnerNode();
  545. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  546. vec.push_back(node);
  547. }
  548. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  549. }
  550. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetInControlNodes() const {
  551. std::vector<NodePtr> vec;
  552. if (in_control_anchor_ != nullptr) {
  553. for (const auto &in_anchor : in_control_anchor_->GetPeerOutControlAnchors()) {
  554. GE_CHK_BOOL_EXEC(in_anchor != nullptr, continue, "GetPeerOutControlAnchors is nullptr");
  555. auto node = in_anchor->GetOwnerNode();
  556. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  557. vec.push_back(node);
  558. }
  559. }
  560. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  561. }
  562. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetOutNodes() const {
  563. std::vector<NodePtr> vec;
  564. for (const auto &out_anchor : out_data_anchors_) {
  565. GE_CHK_BOOL_EXEC((out_anchor != nullptr), continue, "out_data_anchors_ is nullptr");
  566. for (const auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  567. GE_CHK_BOOL_EXEC((peer_in_anchor != nullptr), continue, "GetPeerInDataAnchors is nullptr");
  568. auto node = peer_in_anchor->GetOwnerNode();
  569. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  570. vec.push_back(node);
  571. }
  572. }
  573. if (out_control_anchor_ != nullptr) {
  574. auto peer_in_control_anchors = out_control_anchor_->GetPeerInControlAnchors();
  575. for (const auto &in_control_anchor : peer_in_control_anchors) {
  576. GE_CHK_BOOL_EXEC(in_control_anchor != nullptr, continue,
  577. "out_control_anchor_ peer in control anchors is nullptr");
  578. auto node = in_control_anchor->GetOwnerNode();
  579. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  580. vec.push_back(node);
  581. }
  582. }
  583. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  584. }
  585. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetInAllNodes() const {
  586. std::vector<NodePtr> vec;
  587. for (const auto &in_node : GetInDataNodes()) {
  588. vec.push_back(in_node);
  589. }
  590. for (const auto &in_control_node : GetInControlNodes()) {
  591. vec.push_back(in_control_node);
  592. }
  593. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  594. }
  595. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetOutDataNodes() const {
  596. std::vector<NodePtr> vec;
  597. for (const auto &out_anchor : out_data_anchors_) {
  598. GE_CHK_BOOL_EXEC((out_anchor != nullptr), continue, "out_data_anchors_ is nullptr");
  599. for (const auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  600. GE_CHK_BOOL_EXEC((in_anchor != nullptr), continue, "GetPeerInDataAnchors is nullptr");
  601. auto node = in_anchor->GetOwnerNode();
  602. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  603. vec.push_back(node);
  604. }
  605. }
  606. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  607. }
  608. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY uint32_t Node::GetOutDataNodesSize() const {
  609. uint32_t out_nums = 0;
  610. for (const auto &out_anchor : out_data_anchors_) {
  611. GE_CHK_BOOL_EXEC((out_anchor != nullptr), continue, "out_data_anchors_ is nullptr");
  612. out_nums += out_anchor->GetPeerInDataNodesSize();
  613. }
  614. return out_nums;
  615. }
  616. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetOutControlNodes() const {
  617. std::vector<NodePtr> vec;
  618. for (const auto &out_anchor : out_data_anchors_) {
  619. GE_CHK_BOOL_EXEC((out_anchor != nullptr), continue, "out_data_anchors_ is nullptr");
  620. for (const auto &in_anchor : out_anchor->GetPeerInControlAnchors()) {
  621. GE_CHK_BOOL_EXEC((in_anchor != nullptr), continue, "GetPeerInControlAnchors is nullptr");
  622. auto node = in_anchor->GetOwnerNode();
  623. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  624. vec.push_back(node);
  625. }
  626. }
  627. if (out_control_anchor_ != nullptr) {
  628. for (const auto &in_anchor : out_control_anchor_->GetPeerAnchors()) {
  629. GE_CHK_BOOL_EXEC(in_anchor != nullptr, continue, "GetPeerInControlAnchors is nullptr");
  630. auto node = in_anchor->GetOwnerNode();
  631. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  632. vec.push_back(node);
  633. }
  634. }
  635. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  636. }
  637. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetOutAllNodes() const {
  638. std::vector<NodePtr> vec;
  639. for (const auto &out_anchor : out_data_anchors_) {
  640. GE_CHK_BOOL_EXEC((out_anchor != nullptr), { continue; }, "out_data_anchors_ is nullptr");
  641. for (const auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  642. GE_CHK_BOOL_EXEC((in_anchor != nullptr), { continue; }, "GetPeerInDataAnchors is nullptr");
  643. auto node = in_anchor->GetOwnerNode();
  644. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  645. vec.push_back(node);
  646. }
  647. for (const auto &in_anchor : out_anchor->GetPeerInControlAnchors()) {
  648. GE_CHK_BOOL_EXEC(in_anchor != nullptr, continue, "GetPeerInControlAnchors is nullptr");
  649. auto node = in_anchor->GetOwnerNode();
  650. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  651. vec.push_back(node);
  652. }
  653. }
  654. if (out_control_anchor_ != nullptr) {
  655. for (const auto &in_anchor : out_control_anchor_->GetPeerAnchors()) {
  656. GE_CHK_BOOL_EXEC(in_anchor != nullptr, continue, "GetPeerInControlAnchors is nullptr");
  657. auto node = in_anchor->GetOwnerNode();
  658. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  659. vec.push_back(node);
  660. }
  661. }
  662. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  663. }
  664. graphStatus Node::InferShapeAndType() const {
  665. Operator op = ge::OpDescUtils::CreateOperatorFromNode(shared_from_this());
  666. graphStatus ret = ShapeRefiner::InferShapeAndType(shared_from_this(), op);
  667. return ret;
  668. }
  669. graphStatus Node::InferOriginFormat() const {
  670. Operator op = ge::OpDescUtils::CreateOperatorFromNode(shared_from_this());
  671. // Get infer func and execute
  672. GE_CHK_BOOL_EXEC(op_ != nullptr, return GRAPH_FAILED, "original OpDesc is nullptr");
  673. return op_->CallInferFormatFunc(op);
  674. }
  675. graphStatus Node::Verify() const {
  676. const string data_type = "Data";
  677. const string aipp_data_type = "AippData";
  678. const string const_type = "Const";
  679. const string variable_type = "Variable";
  680. bool is_unknown_graph = GetOwnerComputeGraph()->GetGraphUnknownFlag();
  681. GE_CHK_BOOL_EXEC(op_ != nullptr, return GRAPH_FAILED, "original OpDesc is nullptr");
  682. if (!is_unknown_graph) {
  683. for (const auto &in_anchor_ptr : GetAllInDataAnchors()) {
  684. GE_IF_BOOL_EXEC(in_anchor_ptr == nullptr, GELOGW("in anchor ptr is null"); continue);
  685. bool valid_anchor = op_->GetType() == data_type || op_->GetType() == aipp_data_type ||
  686. op_->GetType() == const_type || op_->GetType() == variable_type ||
  687. op_->IsOptionalInput(in_anchor_ptr->GetIdx()) || in_anchor_ptr->GetPeerAnchors().size() > 0;
  688. if (!valid_anchor) {
  689. ErrorManager::GetInstance().ATCReportErrMessage("E11019", {"opname", "index"},
  690. {GetName(), std::to_string(in_anchor_ptr->GetIdx())});
  691. GELOGE(GRAPH_FAILED, "operator %s's input %d is not linked.", GetName().c_str(), in_anchor_ptr->GetIdx());
  692. return GRAPH_FAILED;
  693. }
  694. }
  695. }
  696. string frameworkop_type = "FrameworkOp";
  697. bool need_update_name = op_->GetType() != frameworkop_type && !is_unknown_graph;
  698. if (need_update_name) {
  699. auto node_op = ge::OperatorFactoryImpl::CreateOperator("node_op", op_->GetType());
  700. if (node_op.IsEmpty()) {
  701. GELOGW("get op from OperatorFactory fail. opType: %s", op_->GetType().c_str());
  702. } else {
  703. GELOGD("get op from OperatorFactory success. opType: %s", op_->GetType().c_str());
  704. auto temp_op_desc = ge::OpDescUtils::GetOpDescFromOperator(node_op);
  705. if (temp_op_desc == nullptr) {
  706. GELOGE(GRAPH_FAILED, "temp op desc is null");
  707. return GRAPH_FAILED;
  708. }
  709. if (!op_->UpdateInputName(temp_op_desc->GetAllInputName())) {
  710. GELOGW("Verify UpdateInputName failed");
  711. }
  712. if (!op_->UpdateOutputName(temp_op_desc->GetAllOutputName())) {
  713. GELOGW("Verify UpdateOutputName failed");
  714. }
  715. }
  716. node_op.BreakConnect();
  717. }
  718. GE_IF_BOOL_EXEC(is_unknown_graph, return GRAPH_SUCCESS;);
  719. if (op_->CommonVerify() == GRAPH_SUCCESS) {
  720. Operator op_proxy = ge::OpDescUtils::CreateOperatorFromNode(shared_from_this());
  721. auto verify_func = op_->GetVerifyFunc();
  722. if (verify_func == nullptr) {
  723. verify_func = OperatorFactoryImpl::GetVerifyFunc(GetType());
  724. }
  725. if (verify_func != nullptr) {
  726. return (graphStatus)verify_func(op_proxy);
  727. }
  728. return GRAPH_SUCCESS;
  729. } else {
  730. GELOGE(GRAPH_FAILED, "%s Verify failed.", op_->GetType().c_str());
  731. return GRAPH_FAILED;
  732. }
  733. }
  734. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY OpDescPtr Node::GetOpDesc() const { return op_; }
  735. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY graphStatus Node::UpdateOpDesc(const OpDescPtr &op_desc) {
  736. GE_CHK_BOOL_EXEC(op_ != nullptr, return GRAPH_FAILED, "original OpDesc is nullptr");
  737. GE_CHK_BOOL_EXEC(op_desc != nullptr, return GRAPH_PARAM_INVALID, "Param OpDesc is nullptr");
  738. GE_CHK_BOOL_EXEC(op_->GetInputsSize() == op_desc->GetInputsSize(), return GRAPH_PARAM_INVALID,
  739. "Inputs count expected to be same, orginial OpDesc %zu, Param OpDesc %zu", op_->GetInputsSize(),
  740. op_desc->GetInputsSize());
  741. GE_CHK_BOOL_EXEC(op_->GetOutputsSize() == op_desc->GetOutputsSize(), return GRAPH_PARAM_INVALID,
  742. "Outputs count expected to be same, orginial OpDesc %zu, Param OpDesc %zu", op_->GetOutputsSize(),
  743. op_desc->GetOutputsSize());
  744. op_ = op_desc;
  745. return GRAPH_SUCCESS;
  746. }
  747. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<std::pair<NodePtr, OutDataAnchorPtr>>
  748. Node::GetInDataNodesAndAnchors() const {
  749. std::vector<std::pair<NodePtr, OutDataAnchorPtr>> vec;
  750. for (const auto &p : in_data_anchors_) {
  751. if (p == nullptr) {
  752. GELOGW("indata anchor is nullptr, node %s:%s", GetType().c_str(), GetName().c_str());
  753. continue;
  754. }
  755. auto anchor_ptr = p->GetPeerOutAnchor();
  756. if (anchor_ptr == nullptr) {
  757. continue;
  758. }
  759. auto node = anchor_ptr->GetOwnerNode();
  760. if (node == nullptr) {
  761. GELOGW("src node is nullptr, node %s:%s", GetType().c_str(), GetName().c_str());
  762. continue;
  763. }
  764. vec.push_back(std::make_pair(node, anchor_ptr));
  765. }
  766. return Node::Vistor<std::pair<NodePtr, OutDataAnchorPtr>>(shared_from_this(), vec);
  767. }
  768. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<std::pair<NodePtr, InDataAnchorPtr>>
  769. Node::GetOutDataNodesAndAnchors() const {
  770. std::vector<std::pair<NodePtr, InDataAnchorPtr>> vec;
  771. for (const auto &p : out_data_anchors_) {
  772. if (p == nullptr) {
  773. GELOGW("out data anchor is nullptr, node %s:%s", GetType().c_str(), GetName().c_str());
  774. continue;
  775. }
  776. for (const auto &in_anchor : p->GetPeerInDataAnchors()) {
  777. if (in_anchor == nullptr) {
  778. GELOGW("dst in data anchor is nullptr, node %s:%s", GetType().c_str(), GetName().c_str());
  779. continue;
  780. }
  781. auto node = in_anchor->GetOwnerNode();
  782. if (node == nullptr) {
  783. GELOGW("dst node is nullptr, node %s:%s", GetType().c_str(), GetName().c_str());
  784. continue;
  785. }
  786. vec.push_back(std::make_pair(node, in_anchor));
  787. }
  788. }
  789. return Node::Vistor<std::pair<NodePtr, InDataAnchorPtr>>(shared_from_this(), vec);
  790. }
  791. } // namespace ge

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