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 "coll_all_reduce_mix_executor.h"
12 :
13 : namespace hccl {
14 :
15 0 : CollAllReduceMixExecutor::CollAllReduceMixExecutor(const HcclDispatcher dispatcher,
16 0 : std::unique_ptr<TopoMatcher> &topoMatcher)
17 0 : : CollAllReduceExecutor(dispatcher, topoMatcher)
18 : {
19 0 : DMAReduceFlag_ = workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE &&
20 0 : topoAttr_.deviceType == DevType::DEV_TYPE_910_93;
21 0 : }
22 :
23 0 : void CollAllReduceMixExecutor::ParseParam(const OpParam& param)
24 : {
25 0 : tag_ = param.tag;
26 0 : bool isInlineReduce = IsSupportSDMAReduce(param.inputPtr, param.outputPtr,
27 0 : param.DataDes.dataType, param.reduceType);
28 0 : meshSinglePlane_ = (topoAttr_.deviceType == DevType::DEV_TYPE_910B) &&
29 0 : topoMatcher_->GetExternalInputHcclDeterministic() == DETERMINISTIC_DISABLE &&
30 0 : isInlineReduce && (workflowMode_ != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
31 0 : }
32 :
33 0 : HcclResult CollAllReduceMixExecutor::CalcStreamNum(u32& streamNum)
34 : {
35 0 : u32 totalStreamNum = 0;
36 :
37 0 : if (topoAttr_.deviceType == DevType::DEV_TYPE_910B) {
38 0 : totalStreamNum = topoAttr_.deviceNumPerAggregation;
39 0 : } else if (topoAttr_.deviceType == DevType::DEV_TYPE_910_93) {
40 0 : totalStreamNum = (topoType_ == TopoType::TOPO_TYPE_NP_DOUBLE_RING ? LEVEL0_PLANE_NUM_IN_NPRING_DOUBLE :
41 : LEVEL0_PLANE_NUM_IN_NPRING_SINGLE);
42 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
43 0 : totalStreamNum *= STREAM_NUM_FOR_DMAREDUCE_ONE_RING;
44 : }
45 : }
46 :
47 0 : streamNum = totalStreamNum - 1;
48 0 : HCCL_INFO("[CollAllReduceMixExecutor][CalcStreamNum] tag[%s] streamNum[%u].", tag_.c_str(), streamNum);
49 0 : return HCCL_SUCCESS;
50 : }
51 :
52 0 : HcclResult CollAllReduceMixExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
53 : {
54 0 : TransportMemType inputType = TransportMemType::RESERVED;
55 0 : TransportMemType outputType = TransportMemType::RESERVED;
56 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
57 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
58 0 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
59 :
60 : // mix在server间使用NHR通信域,并在多机A+X场景下当未设置使用RDMA时,默认使用RDMA
61 0 : std::vector<SingleSubCommTransport> &commTransportLevel1 = opTransport[COMM_LEVEL1];
62 0 : for (u32 ringIndex = 0; ringIndex < commTransportLevel1.size(); ringIndex++) {
63 0 : for (auto &transportRequest : commTransportLevel1[ringIndex].transportRequests) {
64 0 : transportRequest.isUsedRdma = true;
65 : }
66 : }
67 0 : return HCCL_SUCCESS;
68 : }
69 :
70 0 : HcclResult CollAllReduceMixExecutor::CalcTransportMemType(TransportMemType &inputType,
71 : TransportMemType &outputType)
72 : {
73 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
74 0 : inputType = TransportMemType::CCL_INPUT;
75 0 : outputType = TransportMemType::CCL_OUTPUT;
76 : } else {
77 0 : inputType = TransportMemType::PARAM_INPUT;
78 0 : outputType = TransportMemType::PARAM_OUTPUT;
79 : }
80 0 : HCCL_INFO("[CollAllReduceMixExecutor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d].",
81 : tag_.c_str(), inputType, outputType);
82 0 : return HCCL_SUCCESS;
83 : }
84 :
85 0 : HcclResult CollAllReduceMixExecutor::CalcLevel0CommInfo(TransportMemType inputType,
86 : TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
87 : {
88 0 : if (topoAttr_.deviceType == DevType::DEV_TYPE_910B) {
89 0 : CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_MESH);
90 0 : commParaLevel0.meshSinglePlane = meshSinglePlane_;
91 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
92 0 : } else if (topoAttr_.deviceType == DevType::DEV_TYPE_910_93) {
93 0 : CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_RING_INNER);
94 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
95 0 : }
96 0 : HCCL_DEBUG("[CollAllReduceMixExecutor][CalcLevel0CommInfo]Calculate for Level0CommInfo success");
97 0 : return HCCL_SUCCESS;
98 : }
99 :
100 0 : bool CollAllReduceMixExecutor::IsSmallData(const u64 totalSize, const u64 curSize)
101 : {
102 0 : bool smallData = IsAllReduceSmallData(curSize);
103 0 : return smallData;
104 : }
105 :
106 0 : bool CollAllReduceMixExecutor::IsHugeData(const u64 curSize)
107 : {
108 0 : bool hugeData = curSize / topoAttr_.deviceNumPerAggregation / HCCL_INTERNODE_MAX_DATA_RATE > RDMA_SEND_MAX_SIZE ||
109 : curSize > SDMA_SEND_MAX_SIZE;
110 0 : return hugeData;
111 : }
112 :
113 0 : HcclResult CollAllReduceMixExecutor::KernelRun(const OpParam ¶m, ExecMem &execMem)
114 : {
115 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[CollAllReduceMixExecutor][Run]The CollAllReduceMixExecutor starts.");
116 0 : u32 perDataSize = 0;
117 0 : CHK_RET(SalGetDataTypeSize(param.DataDes.dataType, perDataSize));
118 :
119 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
120 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
121 0 : u32 sliceNum = level0CommInfo.localRankSize;
122 :
123 0 : std::vector<Slice> dataSegsSlice; // 数据分成ranksize份,每份的起始偏移和大小
124 : // 根据数据量计算每个环上数据的偏移和大小
125 0 : CHK_RET(AlgTemplateBase::PrepareSliceData(execMem.count, perDataSize, sliceNum, 0, dataSegsSlice));
126 0 : std::vector<std::vector<Slice> > multRingsSliceZero; // 910_93数据基于该rank上环0的偏移
127 :
128 : /* 三步算法step1:外层 - 节点内 reduce-scatter */
129 0 : CHK_RET(ActiveSlaveStreams(param.stream));
130 :
131 0 : if (topoAttr_.deviceType == DevType::DEV_TYPE_910_93) {
132 : // 多环数据切分
133 0 : if (topoType_ == TopoType::TOPO_TYPE_NP_DOUBLE_RING) {
134 0 : multRingsSliceZero = PrepareMultiRingSlice(dataSegsSlice, param.tag, false, topoAttr_.nicList);
135 : } else {
136 0 : multRingsSliceZero.push_back(dataSegsSlice);
137 : }
138 :
139 : // 第一步的reducescatter输出放在CCL buffer上,通过设置nullptr指示不做最后一步的DMA削减动作
140 0 : HcomCollOpInfo reduceScatterOpInfo = {
141 0 : "", execMem.inputPtr, nullptr, execMem.count, param.DataDes.dataType, param.root, param.reduceType
142 0 : };
143 0 : HcomCollOpInfo reduceScatterGraphModeOpInfo = {
144 0 : "", execMem.inputMem.ptr(), nullptr, execMem.count, param.DataDes.dataType, param.root, param.reduceType
145 0 : };
146 0 : HcomCollOpInfo *reduceScatterOpInfoPtr = nullptr;
147 0 : if (topoType_ == TopoType::TOPO_TYPE_NP_DOUBLE_RING) {
148 0 : reduceScatterOpInfoPtr = &reduceScatterGraphModeOpInfo;
149 : }
150 0 : if (DMAReduceFlag_) {
151 0 : reduceScatterOpInfoPtr = &reduceScatterOpInfo;
152 : }
153 0 : const std::vector<std::vector<Slice>> multRingsUserMemSliceDefault = std::vector<std::vector<Slice>>(0);
154 0 : CHK_RET(MultiRingReduceScatter(param.tag, execMem.inputMem, execMem.outputMem, execMem.count,
155 : param.DataDes.dataType, param.reduceType, multRingsSliceZero, param.stream,
156 : PROF_STAGE_0, 0, reduceScatterOpInfoPtr, multRingsUserMemSliceDefault));
157 0 : } else if (topoAttr_.deviceType == DevType::DEV_TYPE_910B) {
158 0 : if (topoMatcher_->GetExternalInputHcclDeterministic() == DETERMINISTIC_DISABLE &&
159 0 : (param.DataDes.dataType != HCCL_DATA_TYPE_INT64) &&
160 0 : (topoAttr_.deviceType == DevType::DEV_TYPE_910B && param.reduceType != HCCL_REDUCE_PROD)) {
161 0 : CHK_RET(MultiStreamReduceScatterMeshAtomic(param.tag, execMem.inputMem, execMem.outputMem, execMem.count,
162 : param.DataDes.dataType, param.reduceType, dataSegsSlice, const_cast<Stream&>(param.stream), COMM_LEVEL0));
163 : } else {
164 0 : std::vector<std::vector<Slice> > multiStreamSlice; // 每个stream使用的数据基于用户buffer的偏移
165 0 : CHK_RET(AlgTemplateBase::PrepareSliceMeshStreams(dataSegsSlice, sliceNum - 1, multiStreamSlice));
166 0 : CHK_RET(MultiStreamReduceScatterMesh(param.tag, execMem.inputMem, execMem.outputMem, execMem.count,
167 : param.DataDes.dataType, param.reduceType, multiStreamSlice,
168 : const_cast<Stream&>(param.stream), COMM_LEVEL0));
169 0 : }
170 : }
171 :
172 0 : HCCL_INFO("[CollAllReduceMixExecutor][KernelRun]AllReduce mix stage0 run success");
173 :
174 : /* 三步算法step2: 内层 - 节点间 allreduce */
175 0 : u32 commIndex = level0CommInfo.localRank;
176 0 : CHK_PRT_RET(commIndex >= dataSegsSlice.size(),
177 : HCCL_ERROR("[CollAllReduceMixExecutor][Run]commIndex[%u] >= dataSegsSlice size[%zu]", commIndex,
178 : dataSegsSlice.size()), HCCL_E_INTERNAL);
179 :
180 0 : DeviceMem allreduceInput = execMem.inputMem.range(dataSegsSlice[commIndex].offset, dataSegsSlice[commIndex].size);
181 0 : CHK_SMART_PTR_NULL(allreduceInput);
182 0 : DeviceMem allreduceOutput = execMem.outputMem.range(dataSegsSlice[commIndex].offset, dataSegsSlice[commIndex].size);
183 0 : CHK_SMART_PTR_NULL(allreduceOutput);
184 :
185 0 : CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
186 0 : SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
187 :
188 0 : u64 reduceAttr = GetReduceAttr(allreduceInput, allreduceOutput, param.DataDes.dataType, param.reduceType);
189 :
190 0 : std::unique_ptr<AlgTemplateBase> level1Executor;
191 0 : if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_RING) {
192 0 : level1Executor = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_REDUCE_RING,
193 0 : dispatcher_);
194 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[%s] Run TEMPLATE_ALL_REDUCE_RING in COMM_LEVEL1", __func__);
195 0 : CHK_SMART_PTR_NULL(level1Executor);
196 0 : CHK_RET(level1Executor->Prepare(reduceAttr));
197 0 : } else if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR) {
198 0 : u64 curSize = execMem.count * perDataSize; // 单位 byte
199 0 : HCCL_DEBUG("[CollAllReduceMixExecutor][KernelRun] curSize[%llu] deviceNumPerAggregation[%u] commLevel0Size[%u]",
200 : curSize, topoAttr_.deviceNumPerAggregation, level0CommInfo.localRankSize);
201 0 : if (curSize / topoAttr_.deviceNumPerAggregation <= NHR_ALLREDUCE_SMALL_SIZE) {
202 0 : level1Executor = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_REDUCE_NHR_ONESHOT, dispatcher_);
203 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[%s] Run TEMPLATE_ALL_REDUCE_NHR_ONESHOT in COMM_LEVEL1", __func__);
204 : } else {
205 0 : level1Executor = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_REDUCE_NHR, dispatcher_);
206 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[%s] Run TEMPLATE_ALL_GATHER_NHR in COMM_LEVEL1", __func__);
207 : }
208 0 : CHK_SMART_PTR_NULL(level1Executor);
209 0 : CHK_RET(level1Executor->Prepare(reduceAttr));
210 : } else {
211 0 : HCCL_ERROR("[CollAllReduceMixExecutor][KernelRun]AllReduce mix: algType[%u] is not supported.", algType_.algoLevel1);
212 0 : return HCCL_E_NOT_SUPPORT;
213 : }
214 0 : CHK_SMART_PTR_NULL(level1Executor);
215 0 : u32 rankSize = level1CommInfo.localRankSize;
216 :
217 0 : u64 level1Count = dataSegsSlice[commIndex].size / perDataSize;
218 0 : CHK_RET(level1Executor->Prepare(allreduceInput, allreduceOutput, allreduceOutput, level1Count,
219 : param.DataDes.dataType, param.stream, param.reduceType, LEVEL0_BRIDGE_RANK_ID,
220 : std::vector<Slice>(0), dataSegsSlice[commIndex].offset));
221 0 : CHK_RET(level1Executor->RegisterProfiler((rankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + level1CommInfo.localRank,
222 : PROF_STAGE_1, HCCL_EXEC_STEP_NOT_SET, param.stream));
223 0 : CHK_RET(RunTemplate(level1Executor, level1CommInfo));
224 :
225 0 : HCCL_INFO("[CollAllReduceMixExecutor][KernelRun]AllReduce mix stage1 run success.");
226 :
227 : /* 三步算法step3:外层 - 节点内 allgather */
228 : // 第三步的allgather输入放在CCL buffer上,通过设置nullptr指示要从CCL buffer获取输入
229 0 : if (topoAttr_.deviceType == DevType::DEV_TYPE_910_93) {
230 0 : HcomCollOpInfo allgatherOpInfo = {
231 0 : "", nullptr, execMem.outputPtr, execMem.count, param.DataDes.dataType, param.root, param.reduceType
232 0 : };
233 0 : HcomCollOpInfo allgatherOpInfoGraphModeOpInfo = {
234 0 : "", nullptr, execMem.outputMem.ptr(), execMem.count, param.DataDes.dataType, param.root, param.reduceType
235 0 : };
236 0 : HcomCollOpInfo *allgatherOpInfoPtr = nullptr;
237 0 : if (topoType_ == TopoType::TOPO_TYPE_NP_DOUBLE_RING) {
238 0 : allgatherOpInfoPtr = &allgatherOpInfoGraphModeOpInfo;
239 : }
240 0 : if (DMAReduceFlag_) {
241 0 : allgatherOpInfoPtr = &allgatherOpInfo;
242 : }
243 0 : CHK_RET(MultiRingAllGather(param.tag, execMem.inputMem, execMem.outputMem, level1Count,
244 : param.DataDes.dataType, multRingsSliceZero, param.stream,
245 : PROF_STAGE_2, 0, allgatherOpInfoPtr));
246 0 : } else if (topoAttr_.deviceType == DevType::DEV_TYPE_910B) {
247 0 : std::unique_ptr<AlgTemplateBase> level0Executor = AlgTemplateRegistry::Instance().GetAlgTemplate(
248 0 : TemplateType::TEMPLATE_ALL_GATHER_MESH_ATOMIC, dispatcher_);
249 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[%s] Run TEMPLATE_ALL_GATHER_MESH_ATOMIC in COMM_LEVEL0", __func__);
250 0 : CHK_SMART_PTR_NULL(level0Executor);
251 0 : CHK_RET(level0Executor->Prepare(algResResp_->slaveStreams, algResResp_->notifiesMain, algResResp_->notifiesAux, topoAttr_.userRank,
252 : nullptr, level0CommInfo.localRank, level0CommInfo.localRankSize));
253 :
254 0 : u32 rankSize = level0CommInfo.localRankSize;
255 0 : CHK_RET(level0Executor->Prepare(execMem.outputMem, execMem.outputMem, execMem.inputMem, execMem.count,
256 : param.DataDes.dataType, param.stream, param.reduceType,
257 : LEVEL0_BRIDGE_RANK_ID, dataSegsSlice, 0));
258 :
259 0 : CHK_RET(level0Executor->RegisterProfiler(
260 : (rankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + level0CommInfo.localRank,
261 : PROF_STAGE_2, HCCL_EXEC_STEP_NOT_SET, param.stream));
262 :
263 0 : CHK_RET(RunTemplate(level0Executor, level0CommInfo));
264 0 : }
265 :
266 0 : HCCL_INFO("[CollAllReduceMixExecutor][KernelRun]AllReduce mix stage2 run success.");
267 0 : return HCCL_SUCCESS;
268 0 : }
269 :
270 : REGISTER_EXEC("AllReduceMixExecutor", AllReduceMix, CollAllReduceMixExecutor);
271 :
272 : } // namespace hccl
|