• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 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 #ifndef MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_CONTROL_NODE_SCHEDULER_H_
18 #define MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_CONTROL_NODE_SCHEDULER_H_
19 
20 #include <vector>
21 #include <string>
22 #include <memory>
23 #include <utility>
24 #include <unordered_map>
25 #include <map>
26 #include <set>
27 #include <algorithm>
28 #include <queue>
29 #include "runtime/graph_scheduler/actor/actor_set.h"
30 #include "runtime/graph_scheduler/graph_compiler.h"
31 
32 namespace mindspore {
33 namespace runtime {
34 class ControlNodeScheduler {
35  public:
36   ControlNodeScheduler() = default;
37   ~ControlNodeScheduler() = default;
38   DISABLE_COPY_AND_ASSIGN(ControlNodeScheduler);
39 
40   // Transform the control nodes to control actors.
41   ControlActorSetPtr Build(const GraphCompilerInfo &graph_compiler_info, const AID &memory_manager_aid);
42   // Link control actors.
43   void Link(ActorSet *const actor_set, const GraphCompilerInfo &graph_compiler_info) const;
44 
45   void BuildDataSourceActorForControlNode(const GraphCompilerInfo &graph_compiler_info,
46                                           const HostTensorQueuePtr &host_queue,
47                                           const HostQueueDSActorPtr &host_queue_ds_actor, const AID &memory_manager_aid,
48                                           std::vector<DataSourceActorPtr> *data_source_actors) const;
49 
50   // The control flow actor will generate some data in the loop body execution, so need clear on the end of execution.
51   void ClearActorData(const ControlActorSet *control_actor_set) const;
52   void Optimize(const ActorSetPtr &actor_set, const GraphCompilerInfo &graph_compiler_info) const;
53 
54  private:
55   // Interface to create control actors.
56   std::vector<SwitchActorPtr> BuildSwitchActor(const GraphCompilerInfo &graph_compiler_info) const;
57   std::vector<GatherActorPtr> BuildGatherActor(const GraphCompilerInfo &graph_compiler_info) const;
58   std::vector<EntranceActorPtr> BuildEntranceActor(const GraphCompilerInfo &graph_compiler_info) const;
59   std::vector<ExitActorPtr> BuildExitActor(const GraphCompilerInfo &graph_compiler_info) const;
60   std::vector<StackActorPtr> BuildStackActor(const GraphCompilerInfo &graph_compiler_info) const;
61   void BuildStackActorForControlNode(const GraphCompilerInfo &graph_compiler_info,
62                                      std::vector<StackActorPtr> *const stack_actors) const;
63   // Interface to link control actors.
64   void LinkControlArrowForControlActor(ActorSet *const actor_set, const GraphCompilerInfo &graph_compiler_info) const;
65   void LinkControlArrowForEntranceActor(ActorSet *const actor_set, const GraphCompilerInfo &graph_compiler_info) const;
66   void LinkBranchIDArrowForControlActor(ControlActorSet *const control_actor_set) const;
67   // Link all arrows between control actors.
68   void LinkArrowForControlActor(ControlActorSet *const control_actor_set,
69                                 const GraphCompilerInfo &graph_compiler_info) const;
70   void LinkArrowbyFormalParameter(ControlActor *const to_actor, const KernelWithIndex &from_node_with_index,
71                                   const KernelWithIndex &to_node_with_index,
72                                   const GraphCompilerInfo &graph_compiler_info) const;
73   void LinkArrowByCallNode(const AnfNodePtr &call_node, ControlActor *const to_actor,
74                            const KernelWithIndex &from_node_with_index, const KernelWithIndex &to_node_with_index,
75                            const GraphCompilerInfo &graph_compiler_info) const;
76   void LinkArrowByKernel(const AnfNodePtr &kernel, ControlActor *const to_actor,
77                          const KernelWithIndex &from_node_with_index, const KernelWithIndex &to_node_with_index,
78                          const GraphCompilerInfo &graph_compiler_info) const;
79   void LinkArrowByParameter(const AnfNodePtr &parameter, ControlActor *const to_actor,
80                             const KernelWithIndex &from_node_with_index, const KernelWithIndex &to_node_with_index,
81                             const ControlNodeParserPtr &parser) const;
82   void LinkArrowByValueNode(const AnfNodePtr &value_node, ControlActor *const to_actor, size_t from_index,
83                             size_t to_index) const;
84   // Link arrow from stack actor to control actor.
85   void LinkArrowFromStackActor(StackActor *stack_actor, ControlActor *to_actor,
86                                const GraphCompilerInfo &graph_compiler_info) const;
87 
88   // Link data arrow between control actor and actor in frame, including kernel actor, output actor, data source actor.
89   void LinkDataArrowForKernelActor(const GraphCompilerInfo &graph_compiler_info) const;
90   void LinkDataArrowForCustomActor(const ActorSet *actor_set, const GraphCompilerInfo &graph_compiler_info) const;
91   void LinkDataArrowByKernelGraph(const KernelGraphPtr &graph, ControlActor *const entrance_actor,
92                                   const ControlNodeParserPtr &parser) const;
93   void LinkDataArrowByKernelGraphInSinkMode(const KernelGraphPtr &graph, ControlActor *const from_actor,
94                                             const ControlNodeParserPtr &parser) const;
95   void LinkArrowForRootGraphEntranceActor(const GraphCompilerInfo &graph_compiler_info) const;
96   void LinkControlArrowForLoopCountActor(const ActorSet *actor_set, const GraphCompilerInfo &graph_compiler_info) const;
97   void LinkDataArrowForOutputActor(ActorSet *const actor_set, const GraphCompilerInfo &graph_compiler_info) const;
98   void LinkControlArrowForKernelActor(ActorSet *const actor_set, const GraphCompilerInfo &graph_compiler_info) const;
99   void LinkOutputControlArrowForActor(ActorSet *const actor_set, const GraphCompilerInfo &graph_compiler_info) const;
100   void LinkControlArrowForCustomActor(ActorSet *const actor_set, const GraphCompilerInfo &graph_compiler_info) const;
101   void LinkControlArrowByKernelGraphGroup(const GraphCompilerInfo &graph_compiler_info) const;
102   void LinkControlArrowByAutoMonad(ControlActor *to_actor, const AnfNodePtr &from_node,
103                                    const ControlNodeParserPtr &parser) const;
104 
105   // Add time summary info for counting the execution time between two actors.
106   void SetTimeSummaryForControlActor(const GraphCompilerInfo &graph_compiler_info) const;
107   bool IsNoInputActor(const ControlActor *control_actor) const;
108   void CollectDynamicLenIndexForArgment(const GraphCompilerInfo &graph_compiler_info) const;
109   // The id of memory manager actor.
110   AID memory_manager_aid_;
111 };
112 }  // namespace runtime
113 }  // namespace mindspore
114 
115 #endif  // MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_CONTROL_NODE_SCHEDULER_H_
116