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