1 /** 2 * Copyright 2024 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_CONTROLFLOW_CONDITION_GATHER_ACTOR_H_ 18 #define MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_CONTROLFLOW_CONDITION_GATHER_ACTOR_H_ 19 20 #include <set> 21 #include <vector> 22 #include <string> 23 #include <memory> 24 #include "runtime/graph_scheduler/actor/actor_common.h" 25 #include "runtime/graph_scheduler/actor/kernel_actor.h" 26 27 namespace mindspore { 28 namespace runtime { 29 using mindspore::device::DeviceContext; 30 using mindspore::session::KernelWithIndex; 31 32 // Condition gather actor is used to collect the output of different branch from condition switch actor. 33 class ConditionGatherActor : public KernelActor { 34 public: 35 ConditionGatherActor(const std::string &name, const CNodePtr &kernel, const DeviceContext *device_context, 36 const AID &memory_manager_aid, const AID *debug_aid, const AID *recorder_aid, 37 GraphExecutionStrategy strategy, const std::set<size_t> &modifiable_ref_input_indexes, 38 const std::set<size_t> &modifiable_ref_output_indexes, 39 const KernelTransformType &type = KernelTransformType::kConditionGatherActor); 40 ~ConditionGatherActor() override; 41 // Receive the branch name from condition switch actor. 42 void RunBranchName(const std::string &branch_name, OpContext<DeviceTensor> *const context); 43 44 protected: 45 void Init() override; 46 void FetchInput(OpContext<DeviceTensor> *const context); 47 void Run(OpContext<DeviceTensor> *const context) override; 48 49 private: 50 friend class InlineControlFlowScheduler; 51 // Output num of each branch. 52 size_t branch_output_num_{0}; 53 // The order of each branch name. 54 std::vector<std::string> branch_names_; 55 // The current execute branch between switch and gather actor. 56 std::string current_branch_name_; 57 // Input data and control num for each branch. 58 mindspore::HashMap<std::string, size_t> branch_name_to_id_; 59 mindspore::HashMap<std::string, size_t> branch_name_to_input_data_num_; 60 mindspore::HashMap<std::string, size_t> branch_name_to_input_control_num_; 61 std::vector<device::DeviceAddressPtr> need_clean_ptr_device_addresses_; 62 }; 63 64 using ConditionGatherActorPtr = std::shared_ptr<ConditionGatherActor>; 65 } // namespace runtime 66 } // namespace mindspore 67 68 #endif // MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_CONTROLFLOW_CONDITION_GATHER_ACTOR_H_ 69