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.

anchor.cc 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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/anchor.h"
  17. #include <algorithm>
  18. #include <cstring>
  19. #include "debug/ge_util.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "graph/node.h"
  22. namespace ge {
  23. Anchor::Anchor(const NodePtr &owner_node, int idx) : owner_node_(owner_node), idx_(idx) {}
  24. bool Anchor::IsTypeOf(TYPE type) const { return strcmp(Anchor::TypeOf<Anchor>(), type) == 0; }
  25. size_t Anchor::GetPeerAnchorsSize() const {
  26. return peer_anchors_.size();
  27. }
  28. Anchor::Vistor<AnchorPtr> Anchor::GetPeerAnchors() const {
  29. vector<AnchorPtr> ret;
  30. for (const auto &anchor : peer_anchors_) {
  31. ret.push_back(anchor.lock());
  32. }
  33. return Anchor::Vistor<AnchorPtr>(shared_from_this(), ret);
  34. }
  35. AnchorPtr Anchor::GetFirstPeerAnchor() const {
  36. if (peer_anchors_.empty()) {
  37. return nullptr;
  38. } else {
  39. return Anchor::DynamicAnchorCast<Anchor>(peer_anchors_.begin()->lock());
  40. }
  41. }
  42. NodePtr Anchor::GetOwnerNode() const { return owner_node_.lock(); }
  43. void Anchor::UnlinkAll() noexcept {
  44. if (!peer_anchors_.empty()) {
  45. do {
  46. auto peer_anchor_ptr = peer_anchors_.begin()->lock();
  47. if (Unlink(peer_anchor_ptr) != GRAPH_SUCCESS) {
  48. GELOGW("unlink peer_anchor_ptr failed.");
  49. }
  50. } while (!peer_anchors_.empty());
  51. }
  52. }
  53. graphStatus Anchor::Unlink(const AnchorPtr &peer) {
  54. if (peer == nullptr) {
  55. GELOGE(GRAPH_FAILED, "peer anchor is invalid.");
  56. return GRAPH_FAILED;
  57. }
  58. auto it = std::find_if(peer_anchors_.begin(), peer_anchors_.end(), [peer](const std::weak_ptr<Anchor> &an) {
  59. auto anchor = an.lock();
  60. return peer->Equal(anchor);
  61. });
  62. GE_IF_BOOL_EXEC(it == peer_anchors_.end(), GELOGW("this anchor is not connected to peer"); return GRAPH_FAILED);
  63. auto it_peer =
  64. std::find_if(peer->peer_anchors_.begin(), peer->peer_anchors_.end(), [this](const std::weak_ptr<Anchor> &an) {
  65. auto anchor = an.lock();
  66. return Equal(anchor);
  67. });
  68. GE_CHK_BOOL_RET_STATUS(it_peer != peer->peer_anchors_.end(), GRAPH_FAILED, "peer is not connected to this anchor");
  69. (void)peer_anchors_.erase(it);
  70. (void)peer->peer_anchors_.erase(it_peer);
  71. return GRAPH_SUCCESS;
  72. }
  73. graphStatus Anchor::ReplacePeer(const AnchorPtr &old_peer, const AnchorPtr &first_peer, const AnchorPtr &second_peer) {
  74. GE_CHK_BOOL_RET_STATUS(old_peer != nullptr, GRAPH_FAILED, "this old peer anchor is nullptr");
  75. GE_CHK_BOOL_RET_STATUS(first_peer != nullptr, GRAPH_FAILED, "this first peer anchor is nullptr");
  76. GE_CHK_BOOL_RET_STATUS(second_peer != nullptr, GRAPH_FAILED, "this second peer anchor is nullptr");
  77. auto this_it = std::find_if(peer_anchors_.begin(), peer_anchors_.end(), [old_peer](const std::weak_ptr<Anchor> &an) {
  78. auto anchor = an.lock();
  79. return old_peer->Equal(anchor);
  80. });
  81. GE_CHK_BOOL_RET_STATUS(this_it != peer_anchors_.end(), GRAPH_FAILED, "this anchor is not connected to old_peer");
  82. auto old_it = std::find_if(old_peer->peer_anchors_.begin(), old_peer->peer_anchors_.end(),
  83. [this](const std::weak_ptr<Anchor> &an) {
  84. auto anchor = an.lock();
  85. return Equal(anchor);
  86. });
  87. GE_CHK_BOOL_RET_STATUS(old_it != old_peer->peer_anchors_.end(), GRAPH_FAILED,
  88. "old_peer is not connected to this anchor");
  89. *this_it = first_peer;
  90. first_peer->peer_anchors_.push_back(shared_from_this());
  91. *old_it = second_peer;
  92. second_peer->peer_anchors_.push_back(old_peer);
  93. return GRAPH_SUCCESS;
  94. }
  95. bool Anchor::IsLinkedWith(const AnchorPtr &peer) {
  96. auto it = std::find_if(peer_anchors_.begin(), peer_anchors_.end(), [peer](const std::weak_ptr<Anchor> &an) {
  97. auto anchor = an.lock();
  98. GE_CHK_BOOL_RET_STATUS(peer != nullptr, false, "this old peer anchor is nullptr");
  99. return peer->Equal(anchor);
  100. });
  101. return (it != peer_anchors_.end());
  102. }
  103. int Anchor::GetIdx() const { return idx_; }
  104. void Anchor::SetIdx(int index) { idx_ = index; }
  105. DataAnchor::DataAnchor(const NodePtr &owner_node, int idx) : Anchor(owner_node, idx) {}
  106. bool DataAnchor::IsTypeOf(TYPE type) const {
  107. if (strcmp(Anchor::TypeOf<DataAnchor>(), type) == 0) {
  108. return true;
  109. }
  110. return Anchor::IsTypeOf(type);
  111. }
  112. InDataAnchor::InDataAnchor(const NodePtr &owner_node, int idx) : DataAnchor(owner_node, idx) {}
  113. OutDataAnchorPtr InDataAnchor::GetPeerOutAnchor() const {
  114. if (peer_anchors_.empty()) {
  115. return nullptr;
  116. } else {
  117. return Anchor::DynamicAnchorCast<OutDataAnchor>(peer_anchors_.begin()->lock());
  118. }
  119. }
  120. graphStatus InDataAnchor::LinkFrom(const OutDataAnchorPtr &src) {
  121. // InDataAnchor must be only linkfrom once
  122. if (src == nullptr || !peer_anchors_.empty()) {
  123. GELOGE(GRAPH_FAILED, "src anchor is invalid or the peerAnchors is not empty.");
  124. return GRAPH_FAILED;
  125. }
  126. peer_anchors_.push_back(src);
  127. src->peer_anchors_.push_back(shared_from_this());
  128. return GRAPH_SUCCESS;
  129. }
  130. bool InDataAnchor::Equal(AnchorPtr anchor) const {
  131. auto in_data_anchor = Anchor::DynamicAnchorCast<InDataAnchor>(anchor);
  132. if (in_data_anchor != nullptr) {
  133. if (GetOwnerNode() == in_data_anchor->GetOwnerNode() && GetIdx() == in_data_anchor->GetIdx()) {
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. bool InDataAnchor::IsTypeOf(TYPE type) const {
  140. if (strcmp(Anchor::TypeOf<InDataAnchor>(), type) == 0) {
  141. return true;
  142. }
  143. return DataAnchor::IsTypeOf(type);
  144. }
  145. OutDataAnchor::OutDataAnchor(const NodePtr &owner_node, int idx) : DataAnchor(owner_node, idx) {}
  146. OutDataAnchor::Vistor<InDataAnchorPtr> OutDataAnchor::GetPeerInDataAnchors() const {
  147. vector<InDataAnchorPtr> ret;
  148. for (const auto &anchor : peer_anchors_) {
  149. auto in_data_anchor = Anchor::DynamicAnchorCast<InDataAnchor>(anchor.lock());
  150. if (in_data_anchor != nullptr) {
  151. ret.push_back(in_data_anchor);
  152. }
  153. }
  154. return OutDataAnchor::Vistor<InDataAnchorPtr>(shared_from_this(), ret);
  155. }
  156. uint32_t OutDataAnchor::GetPeerInDataNodesSize() const {
  157. uint32_t out_nums = 0;
  158. for (const auto &anchor : peer_anchors_) {
  159. auto in_data_anchor = Anchor::DynamicAnchorCast<InDataAnchor>(anchor.lock());
  160. if (in_data_anchor != nullptr && in_data_anchor->GetOwnerNode() != nullptr) {
  161. out_nums++;
  162. }
  163. }
  164. return out_nums;
  165. }
  166. OutDataAnchor::Vistor<InControlAnchorPtr> OutDataAnchor::GetPeerInControlAnchors() const {
  167. vector<InControlAnchorPtr> ret;
  168. for (const auto &anchor : peer_anchors_) {
  169. auto in_control_anchor = Anchor::DynamicAnchorCast<InControlAnchor>(anchor.lock());
  170. if (in_control_anchor != nullptr) {
  171. ret.push_back(in_control_anchor);
  172. }
  173. }
  174. return OutDataAnchor::Vistor<InControlAnchorPtr>(shared_from_this(), ret);
  175. }
  176. graphStatus OutDataAnchor::LinkTo(const InDataAnchorPtr &dest) {
  177. if (dest == nullptr || !dest->peer_anchors_.empty()) {
  178. GELOGE(GRAPH_FAILED, "dest anchor is invalid or the peerAnchors is not empty.");
  179. return GRAPH_FAILED;
  180. }
  181. peer_anchors_.push_back(dest);
  182. dest->peer_anchors_.push_back(shared_from_this());
  183. return GRAPH_SUCCESS;
  184. }
  185. graphStatus OutDataAnchor::LinkTo(const InControlAnchorPtr &dest) {
  186. if (dest == nullptr) {
  187. GELOGE(GRAPH_FAILED, "dest anchor is invalid.");
  188. return GRAPH_FAILED;
  189. }
  190. peer_anchors_.push_back(dest);
  191. dest->peer_anchors_.push_back(shared_from_this());
  192. return GRAPH_SUCCESS;
  193. }
  194. graphStatus OutControlAnchor::LinkTo(const InDataAnchorPtr &dest) {
  195. if (dest == nullptr) {
  196. GELOGE(GRAPH_FAILED, "dest anchor is invalid.");
  197. return GRAPH_FAILED;
  198. }
  199. peer_anchors_.push_back(dest);
  200. dest->peer_anchors_.push_back(shared_from_this());
  201. return GRAPH_SUCCESS;
  202. }
  203. bool OutDataAnchor::Equal(AnchorPtr anchor) const {
  204. CHECK_FALSE_EXEC(anchor != nullptr, return false);
  205. auto out_data_anchor = Anchor::DynamicAnchorCast<OutDataAnchor>(anchor);
  206. if (out_data_anchor != nullptr) {
  207. if (GetOwnerNode() == out_data_anchor->GetOwnerNode() && GetIdx() == out_data_anchor->GetIdx()) {
  208. return true;
  209. }
  210. }
  211. return false;
  212. }
  213. bool OutDataAnchor::IsTypeOf(TYPE type) const {
  214. if (strcmp(Anchor::TypeOf<OutDataAnchor>(), type) == 0) {
  215. return true;
  216. }
  217. return DataAnchor::IsTypeOf(type);
  218. }
  219. ControlAnchor::ControlAnchor(const NodePtr &owner_node) : Anchor(owner_node, -1) {}
  220. ControlAnchor::ControlAnchor(const NodePtr &owner_node, int idx) : Anchor(owner_node, idx) {}
  221. bool ControlAnchor::IsTypeOf(TYPE type) const {
  222. if (strcmp(Anchor::TypeOf<ControlAnchor>(), type) == 0) {
  223. return true;
  224. }
  225. return Anchor::IsTypeOf(type);
  226. }
  227. InControlAnchor::InControlAnchor(const NodePtr &owner_node) : ControlAnchor(owner_node) {}
  228. InControlAnchor::InControlAnchor(const NodePtr &owner_node, int idx) : ControlAnchor(owner_node, idx) {}
  229. InControlAnchor::Vistor<OutControlAnchorPtr> InControlAnchor::GetPeerOutControlAnchors() const {
  230. vector<OutControlAnchorPtr> ret;
  231. for (const auto &anchor : peer_anchors_) {
  232. auto out_control_anchor = Anchor::DynamicAnchorCast<OutControlAnchor>(anchor.lock());
  233. if (out_control_anchor != nullptr) {
  234. ret.push_back(out_control_anchor);
  235. }
  236. }
  237. return InControlAnchor::Vistor<OutControlAnchorPtr>(shared_from_this(), ret);
  238. }
  239. InControlAnchor::Vistor<OutDataAnchorPtr> InControlAnchor::GetPeerOutDataAnchors() const {
  240. vector<OutDataAnchorPtr> ret;
  241. for (const auto &anchor : peer_anchors_) {
  242. auto out_data_anchor = Anchor::DynamicAnchorCast<OutDataAnchor>(anchor.lock());
  243. if (out_data_anchor != nullptr) {
  244. ret.push_back(out_data_anchor);
  245. }
  246. }
  247. return InControlAnchor::Vistor<OutDataAnchorPtr>(shared_from_this(), ret);
  248. }
  249. graphStatus InControlAnchor::LinkFrom(const OutControlAnchorPtr &src) {
  250. if (src == nullptr) {
  251. GELOGE(GRAPH_FAILED, "src anchor is invalid.");
  252. return GRAPH_FAILED;
  253. }
  254. peer_anchors_.push_back(src);
  255. src->peer_anchors_.push_back(shared_from_this());
  256. return GRAPH_SUCCESS;
  257. }
  258. bool InControlAnchor::Equal(AnchorPtr anchor) const {
  259. CHECK_FALSE_EXEC(anchor != nullptr, return false);
  260. auto in_control_anchor = Anchor::DynamicAnchorCast<InControlAnchor>(anchor);
  261. if (in_control_anchor != nullptr) {
  262. if (GetOwnerNode() == in_control_anchor->GetOwnerNode()) {
  263. return true;
  264. }
  265. }
  266. return false;
  267. }
  268. bool InControlAnchor::IsTypeOf(TYPE type) const {
  269. if (strcmp(Anchor::TypeOf<InControlAnchor>(), type) == 0) {
  270. return true;
  271. }
  272. return ControlAnchor::IsTypeOf(type);
  273. }
  274. OutControlAnchor::OutControlAnchor(const NodePtr &owner_node) : ControlAnchor(owner_node) {}
  275. OutControlAnchor::OutControlAnchor(const NodePtr &owner_node, int idx) : ControlAnchor(owner_node, idx) {}
  276. OutControlAnchor::Vistor<InControlAnchorPtr> OutControlAnchor::GetPeerInControlAnchors() const {
  277. vector<InControlAnchorPtr> ret;
  278. for (const auto &anchor : peer_anchors_) {
  279. auto in_control_anchor = Anchor::DynamicAnchorCast<InControlAnchor>(anchor.lock());
  280. if (in_control_anchor != nullptr) {
  281. ret.push_back(in_control_anchor);
  282. }
  283. }
  284. return OutControlAnchor::Vistor<InControlAnchorPtr>(shared_from_this(), ret);
  285. }
  286. OutControlAnchor::Vistor<InDataAnchorPtr> OutControlAnchor::GetPeerInDataAnchors() const {
  287. vector<InDataAnchorPtr> ret;
  288. for (const auto &anchor : peer_anchors_) {
  289. auto in_data_anchor = Anchor::DynamicAnchorCast<InDataAnchor>(anchor.lock());
  290. if (in_data_anchor != nullptr) {
  291. ret.push_back(in_data_anchor);
  292. }
  293. }
  294. return OutControlAnchor::Vistor<InDataAnchorPtr>(shared_from_this(), ret);
  295. }
  296. graphStatus OutControlAnchor::LinkTo(const InControlAnchorPtr &dest) {
  297. if (dest == nullptr) {
  298. GELOGE(GRAPH_FAILED, "dest anchor is invalid.");
  299. return GRAPH_FAILED;
  300. }
  301. peer_anchors_.push_back(dest);
  302. dest->peer_anchors_.push_back(shared_from_this());
  303. return GRAPH_SUCCESS;
  304. }
  305. bool OutControlAnchor::Equal(AnchorPtr anchor) const {
  306. auto out_control_anchor = Anchor::DynamicAnchorCast<OutControlAnchor>(anchor);
  307. if (out_control_anchor != nullptr) {
  308. if (GetOwnerNode() == out_control_anchor->GetOwnerNode()) {
  309. return true;
  310. }
  311. }
  312. return false;
  313. }
  314. bool OutControlAnchor::IsTypeOf(TYPE type) const {
  315. if (strcmp(Anchor::TypeOf<OutControlAnchor>(), type) == 0) {
  316. return true;
  317. }
  318. return ControlAnchor::IsTypeOf(type);
  319. }
  320. } // namespace ge

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