1 /** 2 * Copyright 2021-2022 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_ACTOR_SET_H_ 18 #define MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_SET_H_ 19 20 #if defined(__linux__) && defined(WITH_BACKEND) 21 #define ENABLE_RPC_ACTOR 22 #endif 23 24 #include <vector> 25 #include <string> 26 #include <memory> 27 #include <utility> 28 #include <unordered_map> 29 #include <map> 30 #include <set> 31 #include "runtime/graph_scheduler/actor/abstract_actor.h" 32 #include "runtime/graph_scheduler/actor/data_prepare_actor.h" 33 #include "runtime/graph_scheduler/actor/data_source_actor.h" 34 #include "runtime/graph_scheduler/actor/loop_count_actor.h" 35 #include "runtime/graph_scheduler/actor/kernel_actor.h" 36 #include "runtime/graph_scheduler/actor/kernel_infer_actor.h" 37 #include "runtime/graph_scheduler/actor/kernel_resize_actor.h" 38 #include "runtime/graph_scheduler/actor/custom_actor.h" 39 #include "runtime/graph_scheduler/actor/super_kernel_actor.h" 40 #include "runtime/graph_scheduler/actor/any_type_kernel_actor.h" 41 #include "runtime/graph_scheduler/actor/output_actor.h" 42 #include "runtime/graph_scheduler/actor/copy_actor.h" 43 #include "runtime/graph_scheduler/actor/fusion/fusion_actor.h" 44 #include "runtime/graph_scheduler/actor/control_flow/switch_actor.h" 45 #include "runtime/graph_scheduler/actor/control_flow/gather_actor.h" 46 #include "runtime/graph_scheduler/actor/control_flow/entrance_actor.h" 47 #include "runtime/graph_scheduler/actor/control_flow/exit_actor.h" 48 #include "runtime/graph_scheduler/actor/control_flow/stack_actor.h" 49 #include "runtime/graph_scheduler/actor/control_flow/condition_switch_actor.h" 50 #include "runtime/graph_scheduler/actor/control_flow/condition_gather_actor.h" 51 #include "runtime/graph_scheduler/actor/memory/memory_swap_actor.h" 52 53 #ifdef ENABLE_RPC_ACTOR 54 #include "runtime/graph_scheduler/actor/rpc/send_actor.h" 55 #include "runtime/graph_scheduler/actor/rpc/recv_actor.h" 56 #endif 57 58 namespace mindspore { 59 namespace runtime { 60 using ActorInfo = std::string; 61 62 // Control actor set is a series of actors used to implement control flow: 63 // switch actor judges which branch to output according to the input index; 64 // gather actor is used to collect the actual parameters required by the subgraph call Entrance actor is used 65 // as the entrance of the subgraph to receive the actual parameters and branch id sent by gather, and send them 66 // to the kernel actor; 67 // exit actor is used as the output of the subgraph to collect the calculation results of the subgraph and return 68 // to the caller according to the branch id; 69 // The stack actor collects the output of the kernel actor and exit actor in the untail recursion, and the output 70 // of the kernel actor needs to be saved by the stack. 71 struct ControlActorSet { 72 std::vector<SwitchActorPtr> switch_actors_; 73 std::vector<GatherActorPtr> gather_actors_; 74 std::vector<EntranceActorPtr> entrance_actors_; 75 std::vector<ExitActorPtr> exit_actors_; 76 std::vector<StackActorPtr> stack_actors_; 77 }; 78 using ControlActorSetPtr = std::shared_ptr<ControlActorSet>; 79 80 #ifdef ENABLE_RPC_ACTOR 81 // Rpc actor set is a series of actors implemented to communicate with other processes. In distributed execution mode, 82 // the graph could be considered as partitioned to different processes, which is connected by these rpc actors. Send 83 // actors are in charge of sending data to other processes. Recv actors are in charge of receiving data from other 84 // processes. 85 struct RpcActorSet { 86 std::vector<SendActorPtr> send_actors_; 87 std::vector<RecvActorPtr> recv_actors_; 88 }; 89 using RpcActorSetPtr = std::shared_ptr<RpcActorSet>; 90 using RpcActorSetWeakPtr = std::weak_ptr<RpcActorSet>; 91 #endif 92 93 // The actor set generated by graph transformer is the execution unit of actor runtime. 94 // It includes data source actor, kernel actor, switch actor, copy actor, loop count actor and output actor. 95 // The data prepare actor is used to prepare data for device tensor store and host tensor queue to represent the begin 96 // of one step. 97 // The data source actor is used to obtain data and process them into device tensors, and send them to kernel actor. 98 // The kernel actor is used to receive the device tensors to luanch kernel. 99 // The Super kernel actor is used to represent the sink executing of graph which is the combination of kernels. 100 // The no input kernel actor means that this actor has no input arrow and needs to be triggered externally. 101 // The copy actor is used to convert the device tensor between the different device kernel. 102 // The loop count actor is used to receive the control of tail kernel actor to represent the end of one step 103 // and decide whether to loop execution by loop count. 104 // The output actor is used to receive the output result of actor which represents the graph output. 105 struct ActorSet { ActorSetActorSet106 explicit ActorSet(const ActorInfo &name) : name_(name) {} 107 ~ActorSet() = default; 108 109 DataPrepareActorPtr data_prepare_actor_{nullptr}; 110 std::vector<DataSourceActorPtr> data_source_actors_; 111 std::vector<KernelActorPtr> kernel_actors_; 112 std::vector<KernelInferActorPtr> kernel_infer_actors_; 113 std::vector<KernelResizeActorPtr> kernel_resize_actors_; 114 std::vector<CustomActorPtr> custom_actors_; 115 std::vector<SuperKernelActorPtr> super_kernel_actors_; 116 std::vector<AnyTypeKernelActorPtr> any_type_kernel_actors_; 117 // No input kernel actors need be triggered specifically. 118 std::vector<AbstractActorPtr> no_input_kernel_actors_; 119 std::vector<MemoryAwareActorPtr> memory_actors_; 120 std::vector<CopyActorPtr> copy_actors_; 121 std::vector<FusionActorPtr> fusion_actors_; 122 std::vector<std::vector<MemSwapActorPtr>> swap_actors_; 123 LoopCountActorPtr loop_count_actor_{nullptr}; 124 OutputActorPtr output_actor_{nullptr}; 125 ControlActorSetPtr control_actors_{nullptr}; 126 #ifdef ENABLE_RPC_ACTOR 127 RpcActorSetPtr rpc_actors_{nullptr}; 128 #endif 129 std::vector<AbstractActorPtr> all_actors_; 130 ActorInfo name_; 131 // The related statistics information of multi thread and single thread to decide whether use the multi thread. 132 bool is_multi_thread_execution_{true}; 133 size_t execution_count_{0}; 134 double multi_thread_execution_time_{0}; 135 double single_thread_execution_time_{0}; 136 // Record the execution state. 137 bool is_execution_failed_{false}; 138 bool has_dynamic_shape_{false}; 139 bool has_kernel_need_user_data_{false}; 140 bool enable_multi_stream_{false}; 141 }; 142 using ActorSetPtr = std::shared_ptr<ActorSet>; 143 144 // The operation of the map of kActorNameToActor. 145 void InsertActor(AbstractActor *actor); 146 AbstractActor *FetchActor(const std::string &actor_name); 147 AbstractActor *FetchActor(KernelTransformType kernel_type, const std::string &actor_set_name, 148 const AnfNodePtr &node = nullptr, const KernelGraphPtr &graph = nullptr); 149 void EraseActor(const std::string &actor_name); 150 void ClearAllActors(); 151 } // namespace runtime 152 } // namespace mindspore 153 154 #endif // MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_SET_H_ 155