• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include <regex>
18 #include "frontend/parallel/graph_util/graph_info.h"
19 #include "include/common/debug/anf_ir_dump.h"
20 #include "include/common/debug/draw.h"
21 #include "utils/ms_context.h"
22 #include "ir/graph_utils.h"
23 #include "pipeline/jit/ps/pipeline.h"
24 #include "frontend/parallel/ops_info/ops_utils.h"
25 
26 namespace mindspore {
27 namespace parallel {
FindPrimtive(const FuncGraphPtr & graph,const std::string & name)28 std::vector<PrimitivePtr> FindPrimtive(const FuncGraphPtr &graph, const std::string &name) {
29   AnfNodePtr ret = graph->get_return();
30   MS_EXCEPTION_IF_NULL(ret);
31   std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
32   std::vector<PrimitivePtr> prim_list;
33   for (auto &node : all_nodes) {
34     if (!IsValueNode<Primitive>(node)) {
35       continue;
36     }
37     ValueNodePtr prim_node_anf = node->cast<ValueNodePtr>();
38     MS_EXCEPTION_IF_NULL(prim_node_anf);
39     PrimitivePtr node_prim = prim_node_anf->value()->cast<PrimitivePtr>();
40     MS_EXCEPTION_IF_NULL(node_prim);
41     if (node_prim->name() == name) {
42       (void)prim_list.emplace_back(node_prim);
43     }
44   }
45   return prim_list;
46 }
47 
DumpGraph(const FuncGraphPtr & root,const std::string & name)48 void DumpGraph(const FuncGraphPtr &root, const std::string &name) {
49 #ifdef ENABLE_DUMP_IR
50   auto context = MsContext::GetInstance();
51   MS_EXCEPTION_IF_NULL(context);
52   if (context->CanDump(kIntroductory)) {
53     static const auto switch_order = (common::GetEnv("MS_DEV_SAVE_GRAPHS_SORT_MODE") == "1");
54     if (switch_order) {
55       ExportIR(name + ".ir", root);
56     } else {
57       DumpIR(name + ".ir", root);
58     }
59     if (context->CanDump(kFully)) {
60       draw::Draw(name + ".dot", root);
61     }
62   }
63 #endif
64 }
65 
66 // Return true if the cnode is in a for-loop and loop_index indicates the i-th loop;
67 // otherwise return false
GetLoopIndexFromCNode(const CNodePtr & cnode,size_t * loop_index)68 bool GetLoopIndexFromCNode(const CNodePtr &cnode, size_t *loop_index) {
69   std::regex pattern(CELLLIST_KEYWORD_PATTERN);
70   std::smatch result;
71   const auto &cnode_fullname = cnode->fullname_with_scope();
72   if (std::regex_search(cnode_fullname, result, pattern)) {
73     if (result.length() < 2) {
74       MS_LOG(EXCEPTION) << "Wrong format of fullname_with_scope: " << cnode_fullname;
75     }
76     *loop_index = IntToSize(std::atoi(result[1].str().c_str()));
77     return true;
78   }
79   return false;
80 }
81 
SetOpsNumToExecutor(size_t num_ops)82 void SetOpsNumToExecutor(size_t num_ops) {
83   auto executor = pipeline::GraphExecutorPy::GetInstance();
84   executor->SetNumOpsInfo(num_ops);
85 }
86 }  // namespace parallel
87 }  // namespace mindspore
88