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_item.cc 17 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 "node_item.h"
  17. #include <sstream>
  18. #include "common/debug/log.h"
  19. #include "graph/common/omg_util.h"
  20. #include "graph/compute_graph.h"
  21. #include "graph/debug/ge_attr_define.h"
  22. #include "graph/utils/node_utils.h"
  23. #include "hybrid/executor/worker/shape_inference_engine.h"
  24. #include "hybrid/node_executor/node_executor.h"
  25. namespace ge {
  26. namespace hybrid {
  27. namespace {
  28. const char *const kAttrNameOriginalFusionGraph = "_original_fusion_graph";
  29. const char *const kNodeTypeRetVal = "_RetVal";
  30. const std::set<std::string> kControlOpTypes{
  31. IF, STATELESSIF, CASE, WHILE, STATELESSWHILE
  32. };
  33. const std::set<std::string> kControlFlowOpTypes{
  34. STREAMACTIVE, STREAMSWITCH, STREAMSWITCHN, NEXTITERATION, REFNEXTITERATION, EXIT, REFEXIT,
  35. LABELGOTO, LABELGOTOEX, LABELSWITCH, LABELSWITCHBYINDEX
  36. };
  37. const std::set<std::string> kMergeOpTypes{
  38. MERGE, REFMERGE, STREAMMERGE
  39. };
  40. Status ParseInputMapping(Node &node, OpDesc &op_desc, FusedSubgraph &fused_subgraph) {
  41. uint32_t parent_index = 0;
  42. if (!AttrUtils::GetInt(op_desc, ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  43. GELOGE(FAILED, "[Invoke][GetInt][%s] Failed to get attr [%s]",
  44. op_desc.GetName().c_str(), ATTR_NAME_PARENT_NODE_INDEX.c_str());
  45. REPORT_CALL_ERROR("E19999", "[%s] Failed to get attr [%s]",
  46. op_desc.GetName().c_str(), ATTR_NAME_PARENT_NODE_INDEX.c_str());
  47. return FAILED;
  48. }
  49. for (auto &node_and_anchor : node.GetOutDataNodesAndAnchors()) {
  50. auto dst_op_desc = node_and_anchor.first->GetOpDesc();
  51. GE_CHECK_NOTNULL(dst_op_desc);
  52. auto in_idx = node_and_anchor.second->GetIdx();
  53. auto tensor_desc = dst_op_desc->MutableInputDesc(in_idx);
  54. fused_subgraph.input_mapping[static_cast<int>(parent_index)].emplace_back(tensor_desc);
  55. GELOGD("Input[%u] mapped to [%s:%u]", parent_index, dst_op_desc->GetName().c_str(), in_idx);
  56. }
  57. return SUCCESS;
  58. }
  59. Status ParseOutputMapping(const OpDescPtr &op_desc, FusedSubgraph &fused_subgraph) {
  60. uint32_t parent_index = 0;
  61. if (!AttrUtils::GetInt(op_desc, ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  62. GELOGE(FAILED, "[Invoke][GetInt][%s] Failed to get attr [%s]",
  63. op_desc->GetName().c_str(), ATTR_NAME_PARENT_NODE_INDEX.c_str());
  64. REPORT_CALL_ERROR("E19999", "[%s] Failed to get attr [%s].",
  65. op_desc->GetName().c_str(), ATTR_NAME_PARENT_NODE_INDEX.c_str());
  66. return FAILED;
  67. }
  68. fused_subgraph.output_mapping.emplace(static_cast<int>(parent_index), op_desc);
  69. return SUCCESS;
  70. }
  71. Status ParseFusedSubgraph(NodeItem &node_item) {
  72. if (!node_item.op_desc->HasAttr(kAttrNameOriginalFusionGraph)) {
  73. return SUCCESS;
  74. }
  75. GELOGI("[%s] Start to parse fused subgraph.", node_item.node_name.c_str());
  76. auto fused_subgraph = std::unique_ptr<FusedSubgraph>(new(std::nothrow)FusedSubgraph());
  77. GE_CHECK_NOTNULL(fused_subgraph);
  78. ComputeGraphPtr fused_graph;
  79. (void) AttrUtils::GetGraph(*node_item.op_desc, kAttrNameOriginalFusionGraph, fused_graph);
  80. GE_CHECK_NOTNULL(fused_graph);
  81. fused_graph->SetGraphUnknownFlag(true);
  82. fused_subgraph->graph = fused_graph;
  83. GE_CHK_GRAPH_STATUS_RET(fused_graph->TopologicalSorting());
  84. for (auto &node : fused_graph->GetAllNodes()) {
  85. GE_CHECK_NOTNULL(node);
  86. auto op_desc = node->GetOpDesc();
  87. GE_CHECK_NOTNULL(op_desc);
  88. std::string node_type;
  89. GE_CHK_STATUS_RET(GetOriginalType(node, node_type));
  90. if (node_type == DATA) {
  91. GE_CHK_GRAPH_STATUS_RET(ParseInputMapping(*node, *op_desc, *fused_subgraph));
  92. } else if (node_type == kNodeTypeRetVal) {
  93. GE_CHK_GRAPH_STATUS_RET(ParseOutputMapping(op_desc, *fused_subgraph));
  94. } else {
  95. fused_subgraph->nodes.emplace_back(node);
  96. }
  97. }
  98. node_item.fused_subgraph = std::move(fused_subgraph);
  99. GELOGI("[%s] Done parsing fused subgraph successfully.", node_item.NodeName().c_str());
  100. return SUCCESS;
  101. }
  102. } // namespace
  103. bool IsControlFlowV2Op(const std::string &op_type) {
  104. return kControlOpTypes.count(op_type) > 0;
  105. }
  106. NodeItem::NodeItem(NodePtr node) : node(std::move(node)) {
  107. this->op_desc = this->node->GetOpDesc().get();
  108. this->node_name = this->node->GetName();
  109. this->node_type = this->node->GetType();
  110. }
  111. Status NodeItem::Create(const NodePtr &node, std::unique_ptr<NodeItem> &node_item) {
  112. GE_CHECK_NOTNULL(node);
  113. GE_CHECK_NOTNULL(node->GetOpDesc());
  114. std::unique_ptr<NodeItem> instance(new(std::nothrow)NodeItem(node));
  115. GE_CHECK_NOTNULL(instance);
  116. GE_CHK_STATUS_RET(instance->Init(), "[Invoke][Init]Failed to init NodeItem [%s] .", node->GetName().c_str());
  117. node_item = std::move(instance);
  118. return SUCCESS;
  119. }
  120. void NodeItem::ResolveOptionalInputs() {
  121. if (op_desc->GetAllInputsSize() != op_desc->GetInputsSize()) {
  122. has_optional_inputs = true;
  123. for (size_t i = 0; i < op_desc->GetAllInputsSize(); ++i) {
  124. const auto &input_desc = op_desc->MutableInputDesc(i);
  125. if (input_desc == nullptr) {
  126. GELOGD("[%s] Input[%zu] is optional and invalid", NodeName().c_str(), i);
  127. } else {
  128. input_desc_indices_.emplace_back(static_cast<uint32_t>(i));
  129. }
  130. }
  131. }
  132. }
  133. Status NodeItem::InitInputsAndOutputs() {
  134. GE_CHECK_LE(op_desc->GetInputsSize(), INT32_MAX);
  135. GE_CHECK_LE(op_desc->GetOutputsSize(), INT32_MAX);
  136. num_inputs = static_cast<int>(op_desc->GetInputsSize());
  137. num_outputs = static_cast<int>(op_desc->GetOutputsSize());
  138. if (AttrUtils::GetInt(op_desc, ::ge::ATTR_STAGE_LEVEL, group)) {
  139. GELOGD("[%s] Got stage level from op_desc = %d", op_desc->GetName().c_str(), group);
  140. } else {
  141. if (node->GetOwnerComputeGraph() != nullptr) {
  142. if (AttrUtils::GetInt(node->GetOwnerComputeGraph(), ::ge::ATTR_STAGE_LEVEL, group)) {
  143. GELOGD("[%s] Got stage level from parent graph = %d", op_desc->GetName().c_str(), group);
  144. } else {
  145. auto parent_node = node->GetOwnerComputeGraph()->GetParentNode();
  146. if ((parent_node != nullptr) && (AttrUtils::GetInt(parent_node->GetOpDesc(), ::ge::ATTR_STAGE_LEVEL, group))) {
  147. GELOGD("[%s] Got stage level from parent node = %d", op_desc->GetName().c_str(), group);
  148. } else {
  149. GELOGD("[%s] Node do not set stage level", op_desc->GetName().c_str());
  150. }
  151. }
  152. }
  153. }
  154. ResolveOptionalInputs();
  155. return SUCCESS;
  156. }
  157. Status NodeItem::ResolveDynamicState() {
  158. (void) AttrUtils::GetBool(op_desc, ATTR_NAME_FORCE_UNKNOWN_SHAPE, is_dynamic);
  159. GELOGD("Node name is %s, dynamic state is %d.", this->node_name.c_str(), is_dynamic);
  160. if (!is_dynamic) {
  161. GE_CHK_STATUS_RET(NodeUtils::GetNodeUnknownShapeStatus(*node, is_dynamic),
  162. "[Invoke][GetNodeUnknownShapeStatus][%s] Failed to get shape status.",
  163. node->GetName().c_str());
  164. }
  165. return SUCCESS;
  166. }
  167. Status NodeItem::ResolveStaticInputsAndOutputs() {
  168. for (int i = 0; i < num_inputs; ++i) {
  169. // Data has unconnected input but set by framework
  170. if (node_type != DATA) {
  171. int origin_index = i;
  172. if (has_optional_inputs) {
  173. origin_index = input_desc_indices_[i];
  174. }
  175. auto in_data_anchor = node->GetInDataAnchor(origin_index);
  176. GE_CHECK_NOTNULL(in_data_anchor);
  177. // If no node was connected to the current input anchor
  178. // increase num_static_input_shapes in case dead wait in ShapeInferenceState::AwaitShapesReady
  179. if (in_data_anchor->GetPeerOutAnchor() == nullptr ||
  180. in_data_anchor->GetPeerOutAnchor()->GetOwnerNode() == nullptr) {
  181. num_static_input_shapes++;
  182. is_input_shape_static_.push_back(true);
  183. GELOGW("[%s] Peer node of input[%d] is empty", NodeName().c_str(), i);
  184. continue;
  185. }
  186. }
  187. const auto &input_desc = MutableInputDesc(i);
  188. GE_CHECK_NOTNULL(input_desc);
  189. if (input_desc->MutableShape().IsUnknownShape()) {
  190. is_input_shape_static_.push_back(false);
  191. } else {
  192. num_static_input_shapes++;
  193. is_input_shape_static_.push_back(true);
  194. GELOGD("[%s] The shape of input[%d] is static. shape = [%s]",
  195. NodeName().c_str(), i, input_desc->MutableShape().ToString().c_str());
  196. }
  197. }
  198. for (int i = 0; i < num_outputs; ++i) {
  199. const auto &output_desc = op_desc->MutableOutputDesc(i);
  200. GE_CHECK_NOTNULL(output_desc);
  201. if (output_desc->MutableShape().IsUnknownShape()) {
  202. is_output_shape_static = false;
  203. break;
  204. }
  205. }
  206. if (is_output_shape_static) {
  207. GE_CHK_STATUS_RET_NOLOG(ShapeInferenceEngine::CalcOutputTensorSizes(*this));
  208. }
  209. return SUCCESS;
  210. }
  211. void NodeItem::ResolveUnknownShapeType() {
  212. if (IsControlFlowV2Op() || (is_dynamic && node_type == PARTITIONEDCALL)) {
  213. shape_inference_type = DEPEND_COMPUTE;
  214. } else {
  215. int32_t unknown_shape_type_val = 0;
  216. (void) AttrUtils::GetInt(op_desc, ::ge::ATTR_NAME_UNKNOWN_SHAPE_TYPE, unknown_shape_type_val);
  217. shape_inference_type = static_cast<UnknowShapeOpType>(unknown_shape_type_val);
  218. }
  219. }
  220. Status NodeItem::Init() {
  221. is_ctrl_flow_v2_op_ = ge::hybrid::IsControlFlowV2Op(node_type);
  222. is_ctrl_flow_op_ = kControlFlowOpTypes.count(node_type) > 0;
  223. is_merge_op_ = kMergeOpTypes.count(node_type) > 0;
  224. is_root_node_ = node->GetInAllNodes().empty();
  225. GE_CHK_STATUS_RET_NOLOG(InitInputsAndOutputs());
  226. GE_CHK_STATUS_RET_NOLOG(ResolveDynamicState());
  227. ResolveUnknownShapeType();
  228. if (is_dynamic) {
  229. GE_CHK_STATUS_RET_NOLOG(ResolveStaticInputsAndOutputs());
  230. GE_CHK_STATUS_RET(ParseFusedSubgraph(*this),
  231. "[Invoke][ParseFusedSubgraph][%s] Failed to parse fused subgraph", node_name.c_str());
  232. }
  233. copy_mu_ = MakeShared<std::mutex>();
  234. GE_CHECK_NOTNULL(copy_mu_);
  235. return SUCCESS;
  236. }
  237. bool NodeItem::IsHcclOp() const {
  238. return NodeExecutorManager::GetInstance().ResolveExecutorType(*node) == NodeExecutorManager::ExecutorType::HCCL;
  239. }
  240. std::string NodeItem::DebugString() const {
  241. std::stringstream ss;
  242. ss << "Node: ";
  243. ss << "id = " << node_id;
  244. ss << ", name = [" << node->GetName();
  245. ss << "], type = " << node->GetType();
  246. ss << ", is_dynamic = " << (is_dynamic ? "True" : "False");
  247. ss << ", is_output_static = " << (is_output_shape_static ? "True" : "False");
  248. ss << ", unknown_shape_op_type = " << shape_inference_type;
  249. ss << ", stage = " << group;
  250. ss << ", input_start = " << input_start;
  251. ss << ", num_inputs = " << num_inputs;
  252. ss << ", output_start = " << output_start;
  253. ss << ", num_outputs = " << num_outputs;
  254. ss << ", dependent_nodes = [";
  255. for (const auto &dep_node : dependents_for_shape_inference) {
  256. ss << dep_node->GetName() << ", ";
  257. }
  258. ss << "]";
  259. int index = 0;
  260. for (auto &items : outputs) {
  261. ss << ", output[" << index++ << "]: ";
  262. for (auto &item : items) {
  263. ss << "(" << item.second->NodeName() << ":" << item.first << "), ";
  264. }
  265. }
  266. return ss.str();
  267. }
  268. void NodeItem::SetToDynamic() {
  269. num_static_input_shapes = 0;
  270. is_dynamic = true;
  271. for (size_t i = 0; i < is_input_shape_static_.size(); ++i) {
  272. is_input_shape_static_[i] = false;
  273. }
  274. if (kernel_task != nullptr && !kernel_task->IsSupportDynamicShape()) {
  275. GELOGD("[%s] Dynamic shape is not supported, clear node task.", node_name.c_str());
  276. kernel_task = nullptr;
  277. }
  278. }
  279. GeTensorDescPtr NodeItem::DoGetInputDesc(int index) const {
  280. if (!has_optional_inputs) {
  281. return op_desc->MutableInputDesc(static_cast<uint32_t>(index));
  282. }
  283. if (index < 0 || index >= num_inputs) {
  284. GELOGE(PARAM_INVALID, "[Check][Param:index][%s] Invalid input index, num inputs = %d, index = %d",
  285. node_name.c_str(), num_inputs, index);
  286. REPORT_INNER_ERROR("E19999", "Invalid input index, node:%s num inputs = %d, index = %d",
  287. node_name.c_str(), num_inputs, index);
  288. return nullptr;
  289. }
  290. return op_desc->MutableInputDesc(input_desc_indices_[index]);
  291. }
  292. GeTensorDescPtr NodeItem::MutableInputDesc(int index) const {
  293. std::lock_guard<std::mutex> lk(mu_);
  294. return DoGetInputDesc(index);
  295. }
  296. Status NodeItem::GetInputDesc(int index, GeTensorDesc &tensor_desc) const {
  297. std::lock_guard<std::mutex> lk(mu_);
  298. auto input_desc = DoGetInputDesc(index);
  299. GE_CHECK_NOTNULL(input_desc);
  300. tensor_desc = *input_desc;
  301. return SUCCESS;
  302. }
  303. Status NodeItem::GetOutputDesc(int index, GeTensorDesc &tensor_desc) const {
  304. std::lock_guard<std::mutex> lk(mu_);
  305. auto output_desc = op_desc->MutableOutputDesc(static_cast<uint32_t>(index));
  306. GE_CHECK_NOTNULL(output_desc);
  307. tensor_desc = *output_desc;
  308. return SUCCESS;
  309. }
  310. GeTensorDescPtr NodeItem::MutableOutputDesc(int index) const {
  311. std::lock_guard<std::mutex> lk(mu_);
  312. return op_desc->MutableOutputDesc(static_cast<uint32_t>(index));
  313. }
  314. Status NodeItem::UpdateInputDesc(int index, const GeTensorDesc &tensor_desc) {
  315. std::lock_guard<std::mutex> lk(mu_);
  316. auto input_desc = DoGetInputDesc(index);
  317. GE_CHECK_NOTNULL(input_desc);
  318. *input_desc = tensor_desc;
  319. return SUCCESS;
  320. }
  321. Status NodeItem::GetCanonicalInputIndex(uint32_t index, int &canonical_index) const {
  322. if (!has_optional_inputs) {
  323. canonical_index = index;
  324. return SUCCESS;
  325. }
  326. auto iter = std::find(input_desc_indices_.begin(), input_desc_indices_.end(), index);
  327. if (iter == input_desc_indices_.end()) {
  328. GELOGE(INTERNAL_ERROR,
  329. "[Check][Param:index]input index:%u not in input_desc_indices_, check Invalid, node:%s",
  330. index, node_name.c_str());
  331. REPORT_INNER_ERROR("E19999", "input index:%u not in input_desc_indices_, check Invalid, node:%s",
  332. index, node_name.c_str());
  333. return INTERNAL_ERROR;
  334. }
  335. canonical_index = static_cast<int>(iter - input_desc_indices_.begin());
  336. GELOGD("[%s] Canonicalize input index from [%u] to [%d]", node_name.c_str(), index, canonical_index);
  337. return SUCCESS;
  338. }
  339. bool NodeItem::IsInputShapeStatic(int index) const {
  340. if (!is_dynamic) {
  341. return true;
  342. }
  343. if (static_cast<size_t>(index) >= is_input_shape_static_.size()) {
  344. GELOGE(PARAM_INVALID, "[Check][Param:index]Input index(%d) out of range: [0, %zu)",
  345. index, is_input_shape_static_.size());
  346. REPORT_INNER_ERROR("E19999", "Input index(%d) out of range: [0, %zu).", index, is_input_shape_static_.size());
  347. return false;
  348. }
  349. return is_input_shape_static_[index];
  350. }
  351. void NodeItem::SetDataSend(NodeItem *node_item, int anchor_index) {
  352. data_send_.emplace(node_item);
  353. node_item->data_recv_[this] = anchor_index;
  354. if (is_root_node_) {
  355. node_item->root_data_.emplace(this);
  356. }
  357. GELOGI("Node[%s] will control node[%s]", NodeName().c_str(), node_item->NodeName().c_str());
  358. }
  359. void NodeItem::SetCtrlSend(NodeItem *node_item, uint32_t switch_index) {
  360. if (switch_index < switch_groups_.size()) {
  361. std::vector<const NodeItem *> &switch_group = switch_groups_[switch_index];
  362. switch_group.emplace_back(node_item);
  363. } else {
  364. ctrl_send_.insert(node_item);
  365. }
  366. node_item->ctrl_recv_.emplace(this);
  367. if (is_root_node_) {
  368. node_item->root_ctrl_.emplace(this);
  369. }
  370. GELOGI("Node[%s] will control node[%s]", NodeName().c_str(), node_item->NodeName().c_str());
  371. }
  372. void NodeItem::SetMergeCtrl(NodeItem *node_item, uint32_t merge_index) {
  373. if (merge_index >= switch_groups_.size()) {
  374. GELOGE(FAILED, "[%s] group size: %zu, merge index: %u", NodeName().c_str(), switch_groups_.size(), merge_index);
  375. return;
  376. }
  377. // this is StreamMerge node, node_item is StreamActive node.
  378. std::vector<const NodeItem *> &switch_group = switch_groups_[merge_index];
  379. switch_group.emplace_back(node_item);
  380. node_item->ctrl_send_.emplace(this);
  381. GELOGI("Node[%s] will control node[%s]", node_item->NodeName().c_str(), NodeName().c_str());
  382. }
  383. size_t NodeItem::GetMergeCtrl(uint32_t merge_index) const {
  384. return (merge_index < switch_groups_.size()) ? switch_groups_[merge_index].size() : 0;
  385. }
  386. OptionalMutexGuard::OptionalMutexGuard(std::mutex *mutex, const string &name) : mu_(mutex), name_(name) {
  387. if (mu_ != nullptr) {
  388. GELOGD("lock for %s", name_.c_str());
  389. mu_->lock();
  390. }
  391. }
  392. OptionalMutexGuard::~OptionalMutexGuard() {
  393. if (mu_ != nullptr) {
  394. GELOGD("unlock for %s", name_.c_str());
  395. mu_->unlock();
  396. mu_ = nullptr;
  397. }
  398. }
  399. } // namespace hybrid
  400. } // namespace ge

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