• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022-2023 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 #ifndef MINDSPORE_CCSRC_BACKEND_COMMON_SESSION_EXEC_ORDER_BUILDER_H
17 #define MINDSPORE_CCSRC_BACKEND_COMMON_SESSION_EXEC_ORDER_BUILDER_H
18 
19 #include <vector>
20 #include <stack>
21 #include <queue>
22 #include <deque>
23 #include <set>
24 #include "ir/anf.h"
25 #include "ir/func_graph.h"
26 #include "utils/hash_map.h"
27 
28 namespace mindspore::session {
29 
30 using NodeUser = mindspore::HashMap<AnfNodePtr, std::vector<AnfNodePtr>>;
31 class ExecOrderBuilder {
32  public:
33   ExecOrderBuilder() = default;
34 
35   ~ExecOrderBuilder();
36 
37   void Build(FuncGraph *graph, std::vector<CNodePtr> *execution_order, NodeUser *node_user);
38 
39  private:
40   void ClearLinkInfo();
41 
42   void BuildLinkInfo();
43 
44   void FindIndependentNodes();
45 
46   bool CanVisitInput(bool visit_with_refcount, const AnfNodePtr &input, SeenNum seen);
47 
48   void Build();
49 
50   void EnqueueReadyNodes(const AnfNodePtr &node, std::deque<AnfNodePtr> *visit_queue, bool comm_first = true);
51 
52   bool PrintLoopNodesIfExist(const AnfNodePtr &node, std::set<AnfNodePtr> *visited_nodes,
53                              mindspore::HashMap<AnfNodePtr, AnfNodePtr> *next_nodes);
54 
55   void CheckLoop();
56 
57   bool IsTrivialNode(const AnfNodePtr &node);
58   void GetTrivialInputNode(const AnfNodePtr &node, SeenNum seen);
59 
60   // If PyNative graph has is_pynative_kernel_graph_ true, means no control flow and no loop need to be checked
61   bool is_pynative_kernel_graph_{false};
62   std::vector<CNodePtr> *execution_order_{nullptr};
63   FuncGraph *graph_{nullptr};
64   std::stack<AnfNodePtr> independent_nodes_;
65   mindspore::HashMap<AnfNodePtr, size_t> node_input_num_;
66   mindspore::HashMap<AnfNodePtr, size_t> node_output_num_;
67   NodeUser node_input_edges_;
68   NodeUser *node_output_edges_{nullptr};
69   mindspore::HashMap<AnfNodePtr, bool> trivial_nodes_;
70 };
71 }  // namespace mindspore::session
72 #endif  // MINDSPORE_CCSRC_BACKEND_COMMON_SESSION_EXEC_ORDER_BUILDER_H
73