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

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

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