Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "log.h"
12 : #include "mc2_selector.h"
13 : #include "selector_registry.h"
14 : #include "coll_operator.h"
15 : #include "coll_alg_params.h"
16 :
17 : namespace Hccl {
18 : const std::map<OpType, std::string> MC2_CCU_1D_DEFAULT_ALG_MAP = {
19 : {OpType::ALLGATHER, "CcuAllGatherMesh1D"}, {OpType::REDUCESCATTER, "CcuReduceScatterMesh1D"},
20 : {OpType::ALLREDUCE, "CcuAllReduceMesh1D"}, {OpType::REDUCE, "CcuReduceMesh1D"},
21 : {OpType::ALLTOALL, "CcuAlltoAllMesh1D"}, {OpType::ALLTOALLV, "CcuAlltoAllVMesh1D"},
22 : {OpType::HALFALLTOALLV, "CcuHalfAll2AllVMesh1D"},
23 : };
24 :
25 : const std::map<OpType, std::string> MC2_CCU_SCHED_1D_DEFAULT_ALG_MAP = {
26 : {OpType::ALLGATHER, "CcuAllGatherMeshMem2Mem1D"}, {OpType::REDUCESCATTER, "CcuReduceScatterMeshMem2Mem1D"},
27 : {OpType::ALLREDUCE, "CcuAllReduceMeshMem2Mem1D"}, {OpType::ALLTOALL, "CcuAlltoAllMesh1D"},
28 : {OpType::ALLTOALLV, "CcuAlltoAllVMesh1D"}, {OpType::HALFALLTOALLV, "CcuHalfAll2AllVMesh1D"},
29 : };
30 :
31 : const std::map<OpType, std::string> MC2_CCU_2D_DEFAULT_ALG_MAP = {
32 : {OpType::ALLGATHER, "CcuAllGatherMesh2D"}, {OpType::REDUCESCATTER, "CcuReduceScatterMesh2D"},
33 : {OpType::ALLREDUCE, "CcuAllReduceMesh2DOneShot"}, {OpType::REDUCE, "CcuReduceMesh2D"},
34 : {OpType::ALLTOALL, "CcuAlltoAllMesh2D"},
35 : };
36 :
37 : const std::map<OpType, std::string> MC2_AICPU_1D_DEFAULT_ALG_MAP = {
38 : {OpType::ALLGATHER, "InsAllGatherMesh"},
39 : {OpType::REDUCESCATTER, "InsReduceScatterNHR"},
40 : {OpType::ALLREDUCE, "InsAllReduceNHR"},
41 : {OpType::REDUCE, "InsReduceNHR"},
42 : {OpType::ALLTOALL, "InsAlltoAllMesh"},
43 : {OpType::ALLTOALLV, "InsAlltoAllvMesh"},
44 : {OpType::BATCHSENDRECV, "InsBatchSendRecv"},
45 : {OpType::BROADCAST, "InsBroadcastNHR"},
46 : {OpType::SCATTER, "InsScatterNHR"},
47 : {OpType::SEND, "InsSend"},
48 : {OpType::RECV, "InsRecv"},
49 : };
50 :
51 0 : AlgorithmType Mc2Selector::GetAlgorithmTypeForMC2CCU(const std::string& name)
52 : {
53 0 : auto it = algorithmMap_.find(name);
54 0 : if (it == algorithmMap_.end()) {
55 0 : THROW<InvalidParamsException>(StringFormat("Unknown algorithm name: [%s] ", name.c_str()));
56 : }
57 0 : return it->second;
58 : }
59 :
60 : const std::map<OpType, std::string> MC2_AICPU_2D_DEFAULT_ALG_MAP = {};
61 :
62 0 : SelectorStatus Mc2Selector::SelectDefaultCcuMsAlgo(
63 : const CollAlgOperator& op, const CollAlgParams& params, std::string& primQueueGenName) const
64 : {
65 : (void)params;
66 0 : TopoInfo topoInfo;
67 0 : CalcTopoShape(topoInfo);
68 0 : std::map<OpType, string> algMap;
69 0 : if (topoInfo.level0Shape == Level0Shape::MESH_1D) {
70 0 : algMap = MC2_CCU_1D_DEFAULT_ALG_MAP;
71 0 : } else if (topoInfo.level0Shape == Level0Shape::MESH_2D) {
72 0 : algMap = MC2_CCU_2D_DEFAULT_ALG_MAP;
73 : } else {
74 0 : HCCL_ERROR("[Algo][Mc2Selector][SelectDefaultCcuMsAlgo] only support 1D mesh and 2D mesh algo.");
75 0 : return SelectorStatus::NOT_MATCH;
76 : }
77 0 : auto it = algMap.find(op.opType);
78 0 : if (it != algMap.end()) {
79 0 : primQueueGenName = it->second;
80 : } else {
81 0 : HCCL_ERROR(
82 : "[Algo][Mc2Selector][SelectDefaultCcuMsAlgo] op.opType[%s] Level0Shape[%d] does not have any default mc2 "
83 : "algo.",
84 : op.opType.Describe().c_str(), topoInfo.level0Shape);
85 0 : return SelectorStatus::NOT_MATCH;
86 : }
87 0 : return SelectorStatus::MATCH;
88 0 : }
89 :
90 0 : SelectorStatus Mc2Selector::SelectDefaultCcuSchedAlgo(
91 : const CollAlgOperator& op, const CollAlgParams& params, std::string& primQueueGenName) const
92 : {
93 : (void)params;
94 0 : TopoInfo topoInfo;
95 0 : CalcTopoShape(topoInfo);
96 0 : std::map<OpType, string> algMap;
97 0 : if (topoInfo.level0Shape == Level0Shape::MESH_1D || topoInfo.level0Shape == Level0Shape::MESH_1D_CLOS) {
98 0 : algMap = MC2_CCU_SCHED_1D_DEFAULT_ALG_MAP;
99 : } else {
100 0 : HCCL_ERROR("[Algo][Mc2Selector][SelectDefaultCcuSchedAlgo] only support 1D mesh algo.");
101 0 : return SelectorStatus::NOT_MATCH;
102 : }
103 0 : auto it = algMap.find(op.opType);
104 0 : if (it != algMap.end()) {
105 0 : primQueueGenName = it->second;
106 : } else {
107 0 : HCCL_ERROR(
108 : "[Algo][Mc2Selector][SelectDefaultCcuSchedAlgo] op.opType[%s] Level0Shape[%d] does not have any default "
109 : "mc2 algo.",
110 : op.opType.Describe().c_str(), topoInfo.level0Shape);
111 0 : return SelectorStatus::NOT_MATCH;
112 : }
113 0 : return SelectorStatus::MATCH;
114 0 : }
115 :
116 0 : SelectorStatus Mc2Selector::SelectDefaultAicpuAlgo(
117 : const CollAlgOperator& op, const CollAlgParams& params, std::string& primQueueGenName) const
118 : {
119 : (void)params;
120 0 : TopoInfo topoInfo;
121 0 : CalcTopoShape(topoInfo);
122 0 : std::map<OpType, string> algMap;
123 0 : if (topoInfo.level0Shape == Level0Shape::MESH_1D) {
124 0 : algMap = MC2_AICPU_1D_DEFAULT_ALG_MAP;
125 0 : } else if (topoInfo.level0Shape == Level0Shape::MESH_2D) {
126 0 : algMap = MC2_AICPU_2D_DEFAULT_ALG_MAP;
127 : } else {
128 0 : HCCL_ERROR("[Algo][Mc2Selector][SelectDefaultAicpuAlgo] only support 1D mesh and 2D mesh algo.");
129 0 : return SelectorStatus::NOT_MATCH;
130 : }
131 0 : auto it = algMap.find(op.opType);
132 0 : if (it != algMap.end()) {
133 0 : primQueueGenName = it->second;
134 : } else {
135 0 : HCCL_ERROR(
136 : "[Algo][Mc2Selector][SelectDefaultAicpuAlgo] op.opType[%s] Level0Shape[%d] does not have any default mc2 "
137 : "algo.",
138 : op.opType.Describe().c_str(), topoInfo.level0Shape);
139 0 : return SelectorStatus::NOT_MATCH;
140 : }
141 0 : return SelectorStatus::MATCH;
142 0 : }
143 :
144 : SelectorStatus
145 0 : Mc2Selector::SelectCcuMsAlgo(const CollAlgOperator& op, CollAlgParams& params, std::string& primQueueGenName) const
146 : {
147 : // 校验 algConfig 是否为空
148 0 : if (params.algConfig.empty()) {
149 0 : HCCL_INFO("[Algo][Mc2Selector][SelectCcuMsAlgo] algConfig is [%s].", params.algConfig.c_str());
150 : // 没有配置算法类型,返回默认算法
151 0 : HCCL_INFO("[Algo][Mc2Selector][SelectCcuMsAlgo] MC2 CCU MS does not support algConfig yet.");
152 : }
153 :
154 : // 当前 ccu 模式只有默认算法选择,不支持配置 algConfig
155 0 : return SelectDefaultCcuMsAlgo(op, params, primQueueGenName);
156 : }
157 :
158 : SelectorStatus
159 0 : Mc2Selector::SelectCcuSchedAlgo(const CollAlgOperator& op, CollAlgParams& params, std::string& primQueueGenName) const
160 : {
161 : // 校验 algConfig 是否为空
162 0 : if (params.algConfig.empty()) {
163 0 : HCCL_INFO("[Algo][Mc2Selector][SelectCcuSchedAlgo] algConfig is [%s].", params.algConfig.c_str());
164 : // 没有配置算法类型,返回默认算法
165 0 : HCCL_INFO("[Algo][Mc2Selector][SelectCcuSchedAlgo] MC2 CCU Sched does not support algConfig yet.");
166 : }
167 :
168 : // 当前 ccu 模式只有默认算法选择,不支持配置 algConfig
169 0 : return SelectDefaultCcuSchedAlgo(op, params, primQueueGenName);
170 : }
171 :
172 : SelectorStatus
173 0 : Mc2Selector::SelectAicpuAlgo(const CollAlgOperator& op, CollAlgParams& params, std::string& primQueueGenName) const
174 : {
175 : // 校验 algConfig 是否为空
176 0 : if (params.algConfig.empty()) {
177 0 : HCCL_INFO("[Algo][Mc2Selector][SelectAicpuAlgo] algConfig is [%s].", params.algConfig.c_str());
178 : // 没有配置算法类型,返回默认算法
179 0 : HCCL_INFO("[Algo][Mc2Selector][SelectAicpuAlgo] MC2 AICPU does not support algConfig yet.");
180 : }
181 :
182 : // 当前 ccu 模式只有默认算法选择,不支持配置 algConfig
183 0 : return SelectDefaultAicpuAlgo(op, params, primQueueGenName);
184 : }
185 :
186 0 : SelectorStatus Mc2Selector::Select(const CollAlgOperator& op, CollAlgParams& params, std::string& primQueueGenName)
187 : {
188 0 : if (rankGraph_ == nullptr) {
189 0 : HCCL_ERROR("[Algo][Mc2Selector] rankGraph_ is nullptr.");
190 0 : return SelectorStatus::NOT_MATCH;
191 : }
192 :
193 0 : if (params.opExecuteConfig.accState == AcceleratorState::CCU_MS) {
194 0 : return SelectCcuMsAlgo(op, params, primQueueGenName);
195 0 : } else if (params.opExecuteConfig.accState == AcceleratorState::CCU_SCHED) {
196 0 : return SelectCcuSchedAlgo(op, params, primQueueGenName);
197 0 : } else if (params.opExecuteConfig.accState == AcceleratorState::AICPU_TS) {
198 0 : return SelectAicpuAlgo(op, params, primQueueGenName);
199 : } else {
200 : // 当前 MC2 场景不支持回退,当遇到不支持的 AcceleratorState 时直接报错
201 0 : HCCL_ERROR(
202 : "[Algo][Mc2Selector] AcceleratorState[%s] is not supported, match failed",
203 : params.opExecuteConfig.accState.Describe().c_str());
204 0 : return SelectorStatus::NOT_MATCH;
205 : }
206 : }
207 :
208 : REGISTER_SELECTOR(18, Mc2Selector);
209 : } // namespace Hccl
|