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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /**
  2. * Copyright 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_->GetAllInputsSize();
  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. if (index < GetAllInDataAnchors().size()) {
  288. (void) out_anchors.at(0)->LinkTo(in_data_anchors_[index]);
  289. } else {
  290. std::shared_ptr<InDataAnchor>
  291. anchor = ComGraphMakeShared<InDataAnchor>(shared_from_this(), in_data_anchors_.size());
  292. if (anchor == nullptr) {
  293. GELOGE(GRAPH_FAILED, "out_anchor size is:%zu, malloc shared_ptr failed.", out_anchors.size());
  294. return GRAPH_FAILED;
  295. }
  296. in_data_anchors_.push_back(anchor);
  297. (void) out_anchors.at(0)->LinkTo(in_data_anchors_.back());
  298. }
  299. return GRAPH_SUCCESS;
  300. }
  301. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY graphStatus Node::AddLinkFromForParse(const NodePtr &input_node) {
  302. // This function is used for ParseWeights.
  303. GE_CHECK_NOTNULL(input_node);
  304. // Input_node ---> this
  305. auto out_anchors = input_node->GetAllOutDataAnchors();
  306. if (out_anchors.size() != 1) {
  307. GELOGE(GRAPH_PARAM_INVALID, "out_anchor size is:%zu, only support 1", out_anchors.size());
  308. return GRAPH_PARAM_INVALID;
  309. }
  310. std::shared_ptr<InDataAnchor> anchor = ComGraphMakeShared<InDataAnchor>(shared_from_this(), in_data_anchors_.size());
  311. if (anchor == nullptr) {
  312. GELOGE(GRAPH_FAILED, "out_anchor size is:%zu, make anchor failed", out_anchors.size());
  313. return GRAPH_FAILED;
  314. }
  315. in_data_anchors_.push_back(anchor);
  316. (void)out_anchors.at(0)->LinkTo(in_data_anchors_.back());
  317. return GRAPH_SUCCESS;
  318. }
  319. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY graphStatus Node::AddLinkFrom(const string &name, NodePtr input_node) {
  320. GE_CHECK_NOTNULL(input_node);
  321. // Input_node ---> this
  322. auto out_anchors = input_node->GetAllOutDataAnchors();
  323. if (out_anchors.size() != 1) {
  324. GELOGE(GRAPH_PARAM_INVALID, "out_anchor size is:%zu, only support 1", out_anchors.size());
  325. return GRAPH_PARAM_INVALID;
  326. }
  327. GE_CHECK_NOTNULL(op_);
  328. auto input_op_desc = input_node->GetOpDesc();
  329. GE_CHECK_NOTNULL(input_op_desc);
  330. auto index = op_->GetInputIndexByName(name);
  331. if (index != -1) {
  332. if (index >= static_cast<int>(in_data_anchors_.size())) {
  333. GELOGE(GRAPH_FAILED, "op %s get input name %s 's index %d is illegal.",
  334. op_->GetName().c_str(), name.c_str(), index);
  335. return GRAPH_FAILED;
  336. }
  337. (void) out_anchors.at(0)->LinkTo(in_data_anchors_[index]);
  338. } else {
  339. std::shared_ptr<InDataAnchor>
  340. anchor = ComGraphMakeShared<InDataAnchor>(shared_from_this(), in_data_anchors_.size());
  341. if (anchor == nullptr) {
  342. GELOGE(GRAPH_FAILED, "in_data_anchors_size is:%zu, malloc shared_ptr failed.", in_data_anchors_.size());
  343. return GRAPH_FAILED;
  344. }
  345. in_data_anchors_.push_back(anchor);
  346. (void) out_anchors.at(0)->LinkTo(in_data_anchors_.back());
  347. }
  348. if (op_->AddInputDesc(name, input_op_desc->GetOutputDesc(0)) != GRAPH_SUCCESS) {
  349. GELOGE(GRAPH_FAILED, "add input desc failed.");
  350. return GRAPH_FAILED;
  351. }
  352. return GRAPH_SUCCESS;
  353. }
  354. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY ComputeGraphPtr Node::GetOwnerComputeGraph() const {
  355. return owner_graph_.lock();
  356. }
  357. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY graphStatus Node::SetOwnerComputeGraph(const ComputeGraphPtr &graph) {
  358. if (graph == nullptr) {
  359. return GRAPH_PARAM_INVALID;
  360. }
  361. owner_graph_ = graph;
  362. return GRAPH_SUCCESS;
  363. }
  364. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY graphStatus Node::SetAnyOwnerComputeGraph(const ComputeGraphPtr &graph) {
  365. owner_graph_ = graph;
  366. return GRAPH_SUCCESS;
  367. }
  368. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<InDataAnchorPtr> Node::GetAllInDataAnchors() const {
  369. return Vistor<InDataAnchorPtr>(shared_from_this(), in_data_anchors_);
  370. }
  371. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<OutDataAnchorPtr> Node::GetAllOutDataAnchors() const {
  372. return Vistor<OutDataAnchorPtr>(shared_from_this(), out_data_anchors_);
  373. }
  374. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY uint32_t Node::GetAllInDataAnchorsSize() const {
  375. return in_data_anchors_.size();
  376. }
  377. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY uint32_t Node::GetAllOutDataAnchorsSize() const {
  378. return out_data_anchors_.size();
  379. }
  380. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<AnchorPtr> Node::GetAllInAnchors() const {
  381. std::vector<AnchorPtr> vec;
  382. // Push back in_data_anchors_
  383. for (const auto &in_anchor_iter : Vistor<InDataAnchorPtr>(shared_from_this(), in_data_anchors_)) {
  384. auto in_anchor = Anchor::DynamicAnchorCast<Anchor>(in_anchor_iter);
  385. if (in_anchor != nullptr) {
  386. vec.push_back(in_anchor);
  387. }
  388. }
  389. // Push back in_control_anchor_
  390. if ((in_control_anchor_->GetPeerOutControlAnchors().size() > 0) ||
  391. (in_control_anchor_->GetPeerOutDataAnchors().size() > 0)) {
  392. auto in_anchor = Anchor::DynamicAnchorCast<Anchor>(in_control_anchor_);
  393. if (in_anchor != nullptr) {
  394. vec.push_back(in_anchor);
  395. }
  396. }
  397. return Node::Vistor<AnchorPtr>(shared_from_this(), vec);
  398. }
  399. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<AnchorPtr> Node::GetAllOutAnchors() const {
  400. std::vector<AnchorPtr> vec;
  401. // Push back out_data_anchors_
  402. for (const auto &out_anchor_iter : Vistor<OutDataAnchorPtr>(shared_from_this(), out_data_anchors_)) {
  403. auto out_anchor = Anchor::DynamicAnchorCast<Anchor>(out_anchor_iter);
  404. if (out_anchor != nullptr) {
  405. vec.push_back(out_anchor);
  406. }
  407. }
  408. // Push back out_control_anchor_
  409. if (out_control_anchor_->GetPeerInControlAnchors().size() > 0 ||
  410. out_control_anchor_->GetPeerInDataAnchors().size() > 0) {
  411. auto out_anchor = Anchor::DynamicAnchorCast<Anchor>(out_control_anchor_);
  412. if (out_anchor != nullptr) {
  413. vec.push_back(out_anchor);
  414. }
  415. }
  416. return Node::Vistor<AnchorPtr>(shared_from_this(), vec);
  417. }
  418. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY InDataAnchorPtr Node::GetInDataAnchor(int idx) const {
  419. if (idx < 0 || idx >= static_cast<int>(in_data_anchors_.size())) {
  420. ErrorManager::GetInstance().ATCReportErrMessage(
  421. "E19019", {"opname", "index", "anchorname", "optype"},
  422. {GetName().c_str(), std::to_string(idx), "in_data_anchor", GetType().c_str()});
  423. GELOGE(GRAPH_FAILED,
  424. "Op[%s] doesn't have index[%d]'s in_data_anchor which optype is %s.",
  425. GetName().c_str(), idx, GetType().c_str());
  426. return nullptr;
  427. } else {
  428. return in_data_anchors_[idx];
  429. }
  430. }
  431. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY AnchorPtr Node::GetInAnchor(int idx) const {
  432. // Idx can't be less than -1 or >= in_data_anchors_.size(), -1 means index of control anchor_
  433. if (idx < -1 || idx >= static_cast<int>(in_data_anchors_.size())) {
  434. GELOGW("Op[%s] doesn't have index[%d]'s in_anchor which optype is %s.", GetName().c_str(), idx, GetType().c_str());
  435. return nullptr;
  436. } else {
  437. // Return control anchor
  438. if (idx == -1) {
  439. auto in_anchor = Anchor::DynamicAnchorCast<Anchor>(in_control_anchor_);
  440. return in_anchor;
  441. }
  442. // Return data anchor
  443. return in_data_anchors_[idx];
  444. }
  445. }
  446. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY AnchorPtr Node::GetOutAnchor(int idx) const {
  447. // Idx can't be less than -1 or >= out_data_anchors_.size(), -1 means index of control anchor_
  448. if (idx < -1 || idx >= static_cast<int>(out_data_anchors_.size())) {
  449. ErrorManager::GetInstance().ATCReportErrMessage(
  450. "E19019", {"opname", "index", "anchorname", "optype"},
  451. {GetName().c_str(), std::to_string(idx), "out_anchor", GetType().c_str(), });
  452. GELOGE(GRAPH_FAILED,
  453. "Op[%s] doesn't have index[%d]'s out_anchor which optype is %s.", GetName().c_str(), idx, GetType().c_str());
  454. return nullptr;
  455. } else {
  456. // Return control anchor
  457. if (idx == -1) {
  458. auto out_anchor = Anchor::DynamicAnchorCast<Anchor>(out_control_anchor_);
  459. return out_anchor;
  460. }
  461. // Return data anchor
  462. return out_data_anchors_[idx];
  463. }
  464. }
  465. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY OutDataAnchorPtr Node::GetOutDataAnchor(int idx) const {
  466. if (idx < 0 || idx >= static_cast<int>(out_data_anchors_.size())) {
  467. ErrorManager::GetInstance().ATCReportErrMessage(
  468. "E19019", {"opname", "index", "anchorname", "optype"},
  469. {GetName().c_str(), std::to_string(idx), "out_data_anchor", GetType().c_str()});
  470. GELOGE(GRAPH_FAILED,
  471. "Op[%s] doesn't have index[%d]'s out_data_anchor which optype is %s.",
  472. GetName().c_str(), idx, GetType().c_str());
  473. return nullptr;
  474. } else {
  475. return out_data_anchors_[idx];
  476. }
  477. }
  478. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY InControlAnchorPtr Node::GetInControlAnchor() const {
  479. return in_control_anchor_;
  480. }
  481. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY OutControlAnchorPtr Node::GetOutControlAnchor() const {
  482. return out_control_anchor_;
  483. }
  484. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetInNodes() const {
  485. std::vector<NodePtr> vec;
  486. for (const auto &in_anchor : in_data_anchors_) {
  487. GE_CHK_BOOL_EXEC((in_anchor != nullptr), continue, "in_data_anchor is nullptr");
  488. auto out_anchor = in_anchor->GetPeerOutAnchor();
  489. if (out_anchor == nullptr) {
  490. continue;
  491. }
  492. auto node = out_anchor->GetOwnerNode();
  493. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  494. vec.push_back(node);
  495. }
  496. if (in_control_anchor_ != nullptr) {
  497. if (in_control_anchor_->IsPeerOutAnchorsEmpty()) {
  498. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  499. }
  500. auto peer_out_anchors = in_control_anchor_->GetPeerOutDataAnchors();
  501. for (const auto &out_anchor : peer_out_anchors) {
  502. GE_CHK_BOOL_EXEC(out_anchor != nullptr, continue, "in_control_anchor_ peer out data anchors is nullptr");
  503. auto node = out_anchor->GetOwnerNode();
  504. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  505. vec.push_back(node);
  506. }
  507. auto peer_out_control_anchors = in_control_anchor_->GetPeerOutControlAnchors();
  508. for (const auto &out_control_anchor : peer_out_control_anchors) {
  509. GE_CHK_BOOL_EXEC(out_control_anchor != nullptr, continue,
  510. "in_control_anchor_ peer out control anchors is nullptr");
  511. auto node = out_control_anchor->GetOwnerNode();
  512. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  513. vec.push_back(node);
  514. }
  515. }
  516. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  517. }
  518. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY bool Node::IsAllInNodesSeen(
  519. std::unordered_set<Node *> &nodes_seen) const {
  520. for (const auto &in_anchor : in_data_anchors_) {
  521. GE_CHK_BOOL_EXEC((in_anchor != nullptr), continue, "in_data_anchor is nullptr");
  522. auto out_anchor = in_anchor->GetPeerOutAnchor();
  523. if (out_anchor == nullptr) {
  524. continue;
  525. }
  526. auto node = out_anchor->GetOwnerNode();
  527. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  528. if ((node->GetType() == NEXTITERATION) || (node->GetType() == REFNEXTITERATION)) {
  529. continue;
  530. }
  531. if (nodes_seen.count(node.get()) == 0) {
  532. return false;
  533. }
  534. }
  535. if (in_control_anchor_ != nullptr) {
  536. if (in_control_anchor_->IsPeerOutAnchorsEmpty()) {
  537. return true;
  538. }
  539. auto peer_out_control_anchors = in_control_anchor_->GetPeerOutControlAnchors();
  540. for (const auto &out_control_anchor : peer_out_control_anchors) {
  541. GE_CHK_BOOL_EXEC(out_control_anchor != nullptr, continue, "out_control_anchor is nullptr");
  542. auto node = out_control_anchor->GetOwnerNode();
  543. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  544. if ((node->GetType() == NEXTITERATION) || (node->GetType() == REFNEXTITERATION)) {
  545. continue;
  546. }
  547. if (nodes_seen.count(node.get()) == 0) {
  548. return false;
  549. }
  550. }
  551. }
  552. return true;
  553. }
  554. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetInDataNodes() const {
  555. std::vector<NodePtr> vec;
  556. for (const auto &in_anchor : in_data_anchors_) {
  557. GE_CHK_BOOL_EXEC((in_anchor != nullptr), continue, "in_data_anchor is nullptr");
  558. auto anchor_ptr = in_anchor->GetPeerOutAnchor();
  559. if (anchor_ptr == nullptr) {
  560. continue;
  561. }
  562. auto node = anchor_ptr->GetOwnerNode();
  563. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  564. vec.push_back(node);
  565. }
  566. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  567. }
  568. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetInControlNodes() const {
  569. std::vector<NodePtr> vec;
  570. if (in_control_anchor_ != nullptr) {
  571. for (const auto &in_anchor : in_control_anchor_->GetPeerOutControlAnchors()) {
  572. GE_CHK_BOOL_EXEC(in_anchor != nullptr, continue, "GetPeerOutControlAnchors is nullptr");
  573. auto node = in_anchor->GetOwnerNode();
  574. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  575. vec.push_back(node);
  576. }
  577. }
  578. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  579. }
  580. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetOutNodes() const {
  581. std::vector<NodePtr> vec;
  582. for (const auto &out_anchor : out_data_anchors_) {
  583. GE_CHK_BOOL_EXEC((out_anchor != nullptr), continue, "out_data_anchors_ is nullptr");
  584. for (const auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  585. GE_CHK_BOOL_EXEC((peer_in_anchor != nullptr), continue, "GetPeerInDataAnchors is nullptr");
  586. auto node = peer_in_anchor->GetOwnerNode();
  587. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  588. vec.push_back(node);
  589. }
  590. }
  591. if (out_control_anchor_ != nullptr) {
  592. auto peer_in_control_anchors = out_control_anchor_->GetPeerInControlAnchors();
  593. for (const auto &in_control_anchor : peer_in_control_anchors) {
  594. GE_CHK_BOOL_EXEC(in_control_anchor != nullptr, continue,
  595. "out_control_anchor_ peer in control anchors is nullptr");
  596. auto node = in_control_anchor->GetOwnerNode();
  597. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  598. vec.push_back(node);
  599. }
  600. }
  601. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  602. }
  603. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetInAllNodes() const {
  604. std::vector<NodePtr> vec;
  605. for (const auto &in_node : GetInDataNodes()) {
  606. vec.push_back(in_node);
  607. }
  608. for (const auto &in_control_node : GetInControlNodes()) {
  609. vec.push_back(in_control_node);
  610. }
  611. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  612. }
  613. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetOutDataNodes() const {
  614. std::vector<NodePtr> vec;
  615. for (const auto &out_anchor : out_data_anchors_) {
  616. GE_CHK_BOOL_EXEC((out_anchor != nullptr), continue, "out_data_anchors_ is nullptr");
  617. for (const auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  618. GE_CHK_BOOL_EXEC((in_anchor != nullptr), continue, "GetPeerInDataAnchors is nullptr");
  619. auto node = in_anchor->GetOwnerNode();
  620. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  621. vec.push_back(node);
  622. }
  623. }
  624. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  625. }
  626. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY uint32_t Node::GetOutDataNodesSize() const {
  627. uint32_t out_nums = 0;
  628. for (const auto &out_anchor : out_data_anchors_) {
  629. GE_CHK_BOOL_EXEC((out_anchor != nullptr), continue, "out_data_anchors_ is nullptr");
  630. out_nums += out_anchor->GetPeerInDataNodesSize();
  631. }
  632. return out_nums;
  633. }
  634. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetOutControlNodes() const {
  635. std::vector<NodePtr> vec;
  636. for (const auto &out_anchor : out_data_anchors_) {
  637. GE_CHK_BOOL_EXEC((out_anchor != nullptr), continue, "out_data_anchors_ is nullptr");
  638. for (const auto &in_anchor : out_anchor->GetPeerInControlAnchors()) {
  639. GE_CHK_BOOL_EXEC((in_anchor != nullptr), continue, "GetPeerInControlAnchors is nullptr");
  640. auto node = in_anchor->GetOwnerNode();
  641. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  642. vec.push_back(node);
  643. }
  644. }
  645. if (out_control_anchor_ != nullptr) {
  646. for (const auto &in_anchor : out_control_anchor_->GetPeerAnchors()) {
  647. GE_CHK_BOOL_EXEC(in_anchor != nullptr, continue, "GetPeerInControlAnchors is nullptr");
  648. auto node = in_anchor->GetOwnerNode();
  649. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  650. vec.push_back(node);
  651. }
  652. }
  653. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  654. }
  655. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<NodePtr> Node::GetOutAllNodes() const {
  656. std::vector<NodePtr> vec;
  657. for (const auto &out_anchor : out_data_anchors_) {
  658. GE_CHK_BOOL_EXEC((out_anchor != nullptr), { continue; }, "out_data_anchors_ is nullptr");
  659. for (const auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  660. GE_CHK_BOOL_EXEC((in_anchor != nullptr), { continue; }, "GetPeerInDataAnchors is nullptr");
  661. auto node = in_anchor->GetOwnerNode();
  662. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  663. vec.push_back(node);
  664. }
  665. for (const auto &in_anchor : out_anchor->GetPeerInControlAnchors()) {
  666. GE_CHK_BOOL_EXEC(in_anchor != nullptr, continue, "GetPeerInControlAnchors is nullptr");
  667. auto node = in_anchor->GetOwnerNode();
  668. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  669. vec.push_back(node);
  670. }
  671. }
  672. if (out_control_anchor_ != nullptr) {
  673. for (const auto &in_anchor : out_control_anchor_->GetPeerAnchors()) {
  674. GE_CHK_BOOL_EXEC(in_anchor != nullptr, continue, "GetPeerInControlAnchors is nullptr");
  675. auto node = in_anchor->GetOwnerNode();
  676. GE_CHK_BOOL_EXEC(node != nullptr, continue, "GetOwnerNode is nullptr");
  677. vec.push_back(node);
  678. }
  679. }
  680. return Node::Vistor<NodePtr>(shared_from_this(), vec);
  681. }
  682. graphStatus Node::InferShapeAndType() const {
  683. Operator op = ge::OpDescUtils::CreateOperatorFromNode(shared_from_this());
  684. graphStatus ret = ShapeRefiner::InferShapeAndType(shared_from_this(), op);
  685. return ret;
  686. }
  687. graphStatus Node::InferOriginFormat() const {
  688. Operator op = ge::OpDescUtils::CreateOperatorFromNode(shared_from_this());
  689. // Get infer func and execute
  690. GE_CHK_BOOL_EXEC(op_ != nullptr, return GRAPH_FAILED, "original OpDesc is nullptr");
  691. return op_->CallInferFormatFunc(op);
  692. }
  693. graphStatus Node::Verify() const {
  694. const string data_type = "Data";
  695. const string aipp_data_type = "AippData";
  696. const string const_type = "Const";
  697. const string const_type_train = "Constant";
  698. const string variable_type = "Variable";
  699. bool is_unknown_graph = GetOwnerComputeGraph()->GetGraphUnknownFlag();
  700. GE_CHK_BOOL_EXEC(op_ != nullptr, return GRAPH_FAILED, "original OpDesc is nullptr");
  701. if (!is_unknown_graph) {
  702. for (const auto &in_anchor_ptr : GetAllInDataAnchors()) {
  703. GE_IF_BOOL_EXEC(in_anchor_ptr == nullptr, GELOGW("in anchor ptr is null");
  704. continue);
  705. bool valid_anchor = op_->GetType() == data_type || op_->GetType() == aipp_data_type ||
  706. op_->GetType() == const_type || op_->GetType() == variable_type || op_->GetType() == const_type_train ||
  707. op_->IsOptionalInput(in_anchor_ptr->GetIdx()) || op_->MutableInputDesc(in_anchor_ptr->GetIdx()) == nullptr ||
  708. in_anchor_ptr->GetPeerAnchors().size() > 0;
  709. if (!valid_anchor) {
  710. ErrorManager::GetInstance().ATCReportErrMessage("E11019", {"opname", "index"},
  711. {GetName(), std::to_string(in_anchor_ptr->GetIdx())});
  712. GELOGE(GRAPH_FAILED, "operator %s's input %d is not linked.", GetName().c_str(), in_anchor_ptr->GetIdx());
  713. return GRAPH_FAILED;
  714. }
  715. }
  716. }
  717. string frameworkop_type = "FrameworkOp";
  718. bool need_update_name = op_->GetType() != frameworkop_type && !is_unknown_graph;
  719. if (need_update_name) {
  720. auto node_op = ge::OperatorFactoryImpl::CreateOperator("node_op", op_->GetType());
  721. if (node_op.IsEmpty()) {
  722. GELOGW("get op from OperatorFactory fail. opType: %s", op_->GetType().c_str());
  723. } else {
  724. GELOGD("get op from OperatorFactory success. opType: %s", op_->GetType().c_str());
  725. auto temp_op_desc = ge::OpDescUtils::GetOpDescFromOperator(node_op);
  726. if (temp_op_desc == nullptr) {
  727. GELOGE(GRAPH_FAILED, "temp op desc is null");
  728. return GRAPH_FAILED;
  729. }
  730. if (!op_->UpdateInputName(temp_op_desc->GetAllInputName())) {
  731. GELOGW("Verify UpdateInputName failed");
  732. }
  733. if (!op_->UpdateOutputName(temp_op_desc->GetAllOutputName())) {
  734. GELOGW("Verify UpdateOutputName failed");
  735. }
  736. }
  737. node_op.BreakConnect();
  738. }
  739. GE_IF_BOOL_EXEC(is_unknown_graph, return GRAPH_SUCCESS;);
  740. if (op_->CommonVerify() == GRAPH_SUCCESS) {
  741. Operator op_proxy = ge::OpDescUtils::CreateOperatorFromNode(shared_from_this());
  742. auto verify_func = op_->GetVerifyFunc();
  743. if (verify_func == nullptr) {
  744. verify_func = OperatorFactoryImpl::GetVerifyFunc(GetType());
  745. }
  746. if (verify_func != nullptr) {
  747. return (graphStatus)verify_func(op_proxy);
  748. }
  749. return GRAPH_SUCCESS;
  750. } else {
  751. GELOGE(GRAPH_FAILED, "%s Verify failed.", op_->GetType().c_str());
  752. return GRAPH_FAILED;
  753. }
  754. }
  755. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY OpDescPtr Node::GetOpDesc() const { return op_; }
  756. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY graphStatus Node::UpdateOpDesc(const OpDescPtr &op_desc) {
  757. GE_CHK_BOOL_EXEC(op_ != nullptr, return GRAPH_FAILED, "original OpDesc is nullptr");
  758. GE_CHK_BOOL_EXEC(op_desc != nullptr, return GRAPH_PARAM_INVALID, "Param OpDesc is nullptr");
  759. GE_CHK_BOOL_EXEC(op_->GetInputsSize() == op_desc->GetInputsSize(), return GRAPH_PARAM_INVALID,
  760. "Inputs count expected to be same, orginial OpDesc %zu, Param OpDesc %zu", op_->GetInputsSize(),
  761. op_desc->GetInputsSize());
  762. GE_CHK_BOOL_EXEC(op_->GetOutputsSize() == op_desc->GetOutputsSize(), return GRAPH_PARAM_INVALID,
  763. "Outputs count expected to be same, orginial OpDesc %zu, Param OpDesc %zu", op_->GetOutputsSize(),
  764. op_desc->GetOutputsSize());
  765. op_ = op_desc;
  766. return GRAPH_SUCCESS;
  767. }
  768. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<std::pair<NodePtr, OutDataAnchorPtr>>
  769. Node::GetInDataNodesAndAnchors() const {
  770. std::vector<std::pair<NodePtr, OutDataAnchorPtr>> vec;
  771. for (const auto &p : in_data_anchors_) {
  772. if (p == nullptr) {
  773. GELOGW("indata anchor is nullptr, node %s:%s", GetType().c_str(), GetName().c_str());
  774. continue;
  775. }
  776. auto anchor_ptr = p->GetPeerOutAnchor();
  777. if (anchor_ptr == nullptr) {
  778. continue;
  779. }
  780. auto node = anchor_ptr->GetOwnerNode();
  781. if (node == nullptr) {
  782. GELOGW("src node is nullptr, node %s:%s", GetType().c_str(), GetName().c_str());
  783. continue;
  784. }
  785. vec.push_back(std::make_pair(node, anchor_ptr));
  786. }
  787. return Node::Vistor<std::pair<NodePtr, OutDataAnchorPtr>>(shared_from_this(), vec);
  788. }
  789. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Node::Vistor<std::pair<NodePtr, InDataAnchorPtr>>
  790. Node::GetOutDataNodesAndAnchors() const {
  791. std::vector<std::pair<NodePtr, InDataAnchorPtr>> vec;
  792. for (const auto &p : out_data_anchors_) {
  793. if (p == nullptr) {
  794. GELOGW("out data anchor is nullptr, node %s:%s", GetType().c_str(), GetName().c_str());
  795. continue;
  796. }
  797. for (const auto &in_anchor : p->GetPeerInDataAnchors()) {
  798. if (in_anchor == nullptr) {
  799. GELOGW("dst in data anchor is nullptr, node %s:%s", GetType().c_str(), GetName().c_str());
  800. continue;
  801. }
  802. auto node = in_anchor->GetOwnerNode();
  803. if (node == nullptr) {
  804. GELOGW("dst node is nullptr, node %s:%s", GetType().c_str(), GetName().c_str());
  805. continue;
  806. }
  807. vec.push_back(std::make_pair(node, in_anchor));
  808. }
  809. }
  810. return Node::Vistor<std::pair<NodePtr, InDataAnchorPtr>>(shared_from_this(), vec);
  811. }
  812. } // namespace ge

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