• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019 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 "common/backend_common_test.h"
17 
18 #include <vector>
19 #include <string>
20 #include <memory>
21 
22 #include "utils/log_adapter.h"
23 #include "frontend/operator/ops.h"
24 #include "debug/anf_ir_dump.h"
25 #include "backend/session/ascend_session.h"
26 #include "pipeline/jit/resource.h"
27 #include "pipeline/jit/action.h"
28 #include "ir/anf.h"
29 #include "ir/manager.h"
30 
31 namespace mindspore {
32 namespace {
GetCNodeList(const FuncGraphPtr & func_graph)33 std::vector<AnfNodePtr> GetCNodeList(const FuncGraphPtr &func_graph) {
34   std::vector<AnfNodePtr> nodes = TopoSort(func_graph->get_return());
35   std::vector<AnfNodePtr> lst;
36   for (auto &node : nodes) {
37     MS_LOG(INFO) << "nodes: " << node->DebugString(10);
38     if (node->isa<CNode>() && IsValueNode<Primitive>(node->cast<CNodePtr>()->input(0)) &&
39         !IsPrimitiveCNode(node, prim::kPrimReturn)) {
40       MS_LOG(INFO) << "push in anf_node list: " << node->DebugString(10);
41       lst.push_back(node);
42     }
43   }
44   return lst;
45 }
46 }  // namespace
47 
CheckEqualGraph(const FuncGraphPtr & a,const FuncGraphPtr & b)48 bool BackendCommon::CheckEqualGraph(const FuncGraphPtr &a, const FuncGraphPtr &b) {
49   FuncGraphPairMapEquiv equiv_graph_;
50   NodeMapEquiv equiv_node_;
51   return Isomorphic(a, b, &equiv_graph_, &equiv_node_);
52 }
53 
GetKernelGraph(const FuncGraphPtr & func_graph,const AbstractBasePtrList & args_spec_list,bool need_infer)54 std::shared_ptr<session::KernelGraph> BackendCommon::GetKernelGraph(const FuncGraphPtr &func_graph,
55                                                                     const AbstractBasePtrList &args_spec_list,
56                                                                     bool need_infer) {
57   FuncGraphPtr inferred_graph = func_graph;
58   if (need_infer) {
59     inferred_graph = GetFuncGraph(func_graph, args_spec_list);
60   }
61   AnfNodePtrList applies = GetCNodeList(inferred_graph);
62   AnfNodePtrList ins = inferred_graph->parameters();
63   AnfNodePtrList outs = {inferred_graph->get_return()->input(1)};
64   auto session = std::make_shared<session::AscendSession>();
65   session->Init(0);
66   auto kernel_graph = session->ConstructKernelGraph(applies, outs);
67   kernel_graph->SetExecOrderByDefault();
68   return kernel_graph;
69 }
70 
GetFuncGraph(const FuncGraphPtr & func_graph,const AbstractBasePtrList & args_spec_list)71 FuncGraphPtr BackendCommon::GetFuncGraph(const FuncGraphPtr &func_graph, const AbstractBasePtrList &args_spec_list) {
72   if (func_graph->manager() == nullptr) {
73     std::vector<FuncGraphPtr> graphs{func_graph};
74     FuncGraphManagerPtr manager = std::make_shared<FuncGraphManager>(graphs);
75     manager->AddFuncGraph(func_graph);
76   }
77   // Renormalize func_graph to infer and set shape and type information.
78   pipeline::ResourcePtr resource_ = std::make_shared<pipeline::Resource>();
79   return pipeline::Renormalize(resource_, func_graph, args_spec_list);
80 }
81 }  // namespace mindspore
82