• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SWITCH_ACTOR_H_
18 #define MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_CONTROLFLOW_CONDITION_SWITCH_ACTOR_H_
19 
20 #include <set>
21 #include <string>
22 #include <vector>
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 switch actor is used to execute the branch according to the input condition in kernel graph.
33 class ConditionSwitchActor : public KernelActor {
34  public:
35   ConditionSwitchActor(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::kConditionSwitchActor);
40   ~ConditionSwitchActor() override = default;
41 
42  protected:
43   void Init() override;
44   void Run(OpContext<DeviceTensor> *const context) override;
45   void FetchInput(OpContext<DeviceTensor> *const context);
46   void SendOutput(OpContext<DeviceTensor> *const context, size_t index);
47 
48  private:
49   friend class InlineControlFlowScheduler;
50   // Collect memory free list, as the ref counts of different branches are superimposed on the output,
51   // so the excess reference counts of other branches need to be subtracted in advance.
52   void CollectMemoryFreeList(size_t index);
53 
54   // Graph name of each branch,
55   std::vector<std::string> branch_names_;
56   // Ref count of each branch.
57   std::vector<std::vector<size_t>> branch_origin_ref_count_;
58   // Branch of data arrow and control arrow.
59   std::vector<size_t> output_data_branch_indexes_;
60   std::vector<size_t> output_control_branch_indexes_;
61 
62   // Cache output data by output index to modify the output data effectively.
63   std::vector<std::vector<OpData<DeviceTensor> *>> output_data_by_output_index_;
64 
65   // Switch needs to send current branch name to the corresponding gather actor to check its inputs.
66   AID *gather_aid_{nullptr};
67 };
68 
69 using ConditionSwitchActorPtr = std::shared_ptr<ConditionSwitchActor>;
70 }  // namespace runtime
71 }  // namespace mindspore
72 
73 #endif  // MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_CONTROLFLOW_CONDITION_SWITCH_ACTOR_H_
74