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_gather_mix_executor.h"
12 :
13 : namespace hccl {
14 0 : CollAllGatherMixExecutor::CollAllGatherMixExecutor(const HcclDispatcher dispatcher,
15 0 : std::unique_ptr<TopoMatcher> &topoMatcher)
16 0 : : CollAllGatherExecutor(dispatcher, topoMatcher)
17 : {
18 0 : DMAReduceFlag_ = workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE;
19 0 : }
20 :
21 0 : HcclResult CollAllGatherMixExecutor::CalcStreamNum(u32& streamNum)
22 : {
23 0 : u32 totalStreamNum = 0;
24 0 : if (topoAttr_.deviceType == DevType::DEV_TYPE_910B) { // mesh
25 0 : totalStreamNum = topoAttr_.deviceNumPerAggregation;
26 0 : } else if (topoAttr_.deviceType == DevType::DEV_TYPE_910_93) { // dbring
27 0 : totalStreamNum = (topoType_ == TopoType::TOPO_TYPE_NP_DOUBLE_RING ?
28 : LEVEL0_PLANE_NUM_IN_NPRING_DOUBLE : LEVEL0_PLANE_NUM_IN_NPRING_SINGLE);
29 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
30 0 : totalStreamNum *= STREAM_NUM_FOR_DMAREDUCE_ONE_RING;
31 : }
32 : }
33 :
34 0 : streamNum = totalStreamNum - 1U;
35 0 : HCCL_INFO("[CollAllGatherMixExecutor][CalcStreamNum] tag[%s] streamNum[%u]",
36 : tag_.c_str(), streamNum);
37 0 : return HCCL_SUCCESS;
38 : }
39 :
40 0 : HcclResult CollAllGatherMixExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
41 : {
42 0 : TransportMemType inputType = TransportMemType::RESERVED;
43 0 : TransportMemType outputType = TransportMemType::RESERVED;
44 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
45 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
46 0 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
47 :
48 : // mix在server间使用NHR通信域,并在多机A+X场景下当未设置使用RDMA时,默认使用RDMA
49 0 : std::vector<SingleSubCommTransport> &commTransportLevel1 = opTransport[COMM_LEVEL1];
50 0 : for (u32 ringIndex = 0; ringIndex < commTransportLevel1.size(); ringIndex++) {
51 0 : for (auto &transportRequest : commTransportLevel1[ringIndex].transportRequests) {
52 0 : transportRequest.isUsedRdma = true;
53 : }
54 : }
55 0 : return HCCL_SUCCESS;
56 : }
57 :
58 0 : HcclResult CollAllGatherMixExecutor::CalcTransportMemType(TransportMemType &inputType, TransportMemType &outputType)
59 : {
60 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
61 0 : inputType = TransportMemType::CCL_INPUT;
62 0 : outputType = TransportMemType::CCL_OUTPUT;
63 : } else {
64 0 : inputType = TransportMemType::PARAM_INPUT;
65 0 : outputType = TransportMemType::PARAM_OUTPUT;
66 : }
67 0 : HCCL_INFO("[CollAllGatherMixExecutor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d]",
68 : tag_.c_str(), inputType, outputType);
69 0 : return HCCL_SUCCESS;
70 : }
71 :
72 0 : HcclResult CollAllGatherMixExecutor::CalcLevel0CommInfo(TransportMemType inputType, TransportMemType outputType,
73 : std::vector<LevelNSubCommTransport>& opTransport)
74 : {
75 0 : if (topoAttr_.deviceType == DevType::DEV_TYPE_910B) {
76 0 : CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_MESH);
77 0 : commParaLevel0.meshSinglePlane =
78 0 : topoMatcher_->GetExternalInputHcclDeterministic() == DETERMINISTIC_DISABLE &&
79 0 : (workflowMode_ != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
80 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
81 0 : } else if (topoAttr_.deviceType == DevType::DEV_TYPE_910_93) {
82 0 : CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_RING_INNER);
83 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
84 0 : }
85 :
86 0 : return HCCL_SUCCESS;
87 : }
88 :
89 0 : u64 CollAllGatherMixExecutor::CalcLoopMaxCount(const u64 cclBuffSize, const u32 unitSize)
90 : {
91 0 : u64 maxCountPerLoop = cclBuffSize / (topoAttr_.userRankSize * unitSize);
92 0 : return maxCountPerLoop;
93 : }
94 :
95 0 : HcclResult CollAllGatherMixExecutor::KernelRun(const OpParam ¶m, ExecMem &execMem)
96 : {
97 0 : HCCL_CONFIG_INFO(HCCL_ALG,"[CollAllGatherMixExecutor][KernelRun] The AllGatherMixExecutor starts.");
98 0 : u32 perDataSize = SIZE_TABLE[param.DataDes.dataType];
99 :
100 : // 获取子通信域信息
101 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
102 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
103 0 : u32 level0RankSize = level0CommInfo.localRankSize;
104 0 : u32 commIndex = level0CommInfo.localRank;
105 0 : CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
106 0 : SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
107 0 : u32 serverIndex = level1CommInfo.localRank;
108 0 : u32 level1RankSize = level1CommInfo.localRankSize;
109 0 : HCCL_DEBUG("inputSize=%llu, level0RankSize=%u, commIndex=%u, level1RankSize=%u, serverIndex=%u",
110 : execMem.inputMem.size(), level0RankSize, commIndex, level1RankSize, serverIndex);
111 :
112 0 : u64 inputMemSize = execMem.inputMem.size();
113 0 : u64 baseOffset = serverIndex * inputMemSize * level0RankSize;
114 0 : u64 level0Offset = commIndex * inputMemSize;
115 0 : DeviceMem dstMem = execMem.outputMem.range(baseOffset + level0Offset, inputMemSize);
116 0 : CHK_SMART_PTR_NULL(dstMem);
117 :
118 0 : HcomCollOpInfo opInfo = {"", execMem.inputPtr, execMem.outputPtr, param.DataDes.count, param.DataDes.dataType, 0,
119 0 : HCCL_REDUCE_RESERVED};
120 0 : HcomCollOpInfo *opInfoPtr = nullptr;
121 : // 图模式opinfo不为空,但需要将数据从ccl input拷贝到ccl output上
122 0 : HcclResult ret = HCCL_SUCCESS;
123 0 : if (!DMAReduceFlag_) {
124 0 : ret = HcclD2DMemcpyAsync(dispatcher_, dstMem, execMem.inputMem, const_cast<Stream&>(param.stream));
125 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
126 : HCCL_ERROR("[CollAllGatherMixExecutor][KernelRun]AllGather mix memcpy Failed, Offset[%llu], Size[%llu]",
127 : baseOffset + level0Offset, inputMemSize), ret);
128 : } else {
129 0 : opInfoPtr = &opInfo;
130 : // 先做server间算法,带有消减拷贝场景数据需要从user input取,拷贝到ccl output上
131 0 : DeviceMem srcMem = DeviceMem::create(static_cast<u8 *>(execMem.inputPtr), inputMemSize);
132 0 : ret = HcclD2DMemcpyAsync(dispatcher_, dstMem, srcMem, const_cast<Stream&>(param.stream));
133 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
134 : HCCL_ERROR("[CollAllGatherMixExecutor][KernelRun]AllGather mix user memcpy "
135 : "Failed, Offset[%llu], Size[%llu]", baseOffset + level0Offset, inputMemSize), ret);
136 0 : }
137 :
138 : // 第一步,AI server间AllGather
139 0 : std::unique_ptr<AlgTemplateBase> level1Executor;
140 0 : if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_RING) {
141 0 : level1Executor = AlgTemplateRegistry::Instance().GetAlgTemplate(
142 0 : TemplateType::TEMPLATE_ALL_GATHER_RING, dispatcher_);
143 0 : HCCL_INFO("[CollAllGatherMixExecutor][KernelRun]AllGather mix: using ring algo inter-server.");
144 0 : } else if (algType_.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR) {
145 0 : level1Executor = AlgTemplateRegistry::Instance().GetAlgTemplate(
146 0 : TemplateType::TEMPLATE_ALL_GATHER_NHR, dispatcher_);
147 0 : HCCL_INFO("[CollAllGatherMixExecutor][KernelRun]AllGather mix: using nhr algo inter-server.");
148 : } else {
149 0 : HCCL_ERROR("[CollAllGatherMixExecutor][KernelRun]AllGather mix: algType_[%u] is not supported.", algType_.algoLevel1);
150 0 : return HCCL_E_NOT_SUPPORT;
151 : }
152 0 : CHK_SMART_PTR_NULL(level1Executor);
153 :
154 : // 计算slice
155 0 : std::vector<Slice> level1DataSegsSlice;
156 0 : for (u32 i = 0; i < level1RankSize; i++) {
157 0 : Slice level1Slice;
158 0 : level1Slice.size = inputMemSize;
159 0 : level1Slice.offset = (i * level0RankSize + commIndex) * inputMemSize;
160 0 : level1DataSegsSlice.push_back(level1Slice);
161 : }
162 0 : CHK_RET(level1Executor->Prepare(execMem.outputMem, execMem.outputMem, execMem.inputMem, execMem.count,
163 : param.DataDes.dataType, param.stream, HCCL_REDUCE_RESERVED, INVALID_VALUE_RANKID, level1DataSegsSlice, 0));
164 :
165 0 : CHK_RET(level1Executor->RegisterProfiler((
166 : level1RankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + serverIndex,
167 : PROF_STAGE_0, HCCL_EXEC_STEP_NOT_SET, param.stream));
168 :
169 0 : CHK_RET(RunTemplate(level1Executor, level1CommInfo));
170 0 : HCCL_INFO("[CollAllGatherMixExecutor][KernelRun]AllGather mix level1 AllGather run success");
171 :
172 : // 第二步,节点内做AllGather mesh/dbring
173 0 : if (topoAttr_.deviceType == DevType::DEV_TYPE_910_93) {
174 0 : std::vector<Slice> dataSegsSlice; // 数据分成ranksize份,每份的起始偏移和大小
175 0 : CHK_RET(PrepareAllgatherSlice(level0RankSize, inputMemSize, dataSegsSlice));
176 0 : std::vector<std::vector<Slice>> multRingsSliceZero; // 数据基于该rank上环0的偏移
177 : // 多环数据切分
178 0 : if (topoType_ == TopoType::TOPO_TYPE_NP_DOUBLE_RING) {
179 0 : multRingsSliceZero = PrepareMultiRingSlice(dataSegsSlice, param.tag, false, topoAttr_.nicList);
180 : } else {
181 0 : multRingsSliceZero.push_back(dataSegsSlice);
182 : }
183 0 : std::vector<std::vector<Slice>> multRingsSlice;
184 0 : CHK_RET(CalculateLevel1AllgatherSlice(inputMemSize, level0RankSize, level1RankSize,
185 : multRingsSliceZero, multRingsSlice));
186 :
187 0 : std::vector<std::vector<Slice>> multRingsUserMemSlice;
188 0 : if (!DMAReduceFlag_) {
189 0 : multRingsUserMemSlice = multRingsSlice;
190 : } else {
191 0 : for (u32 ringIndex = 0; ringIndex < multRingsSlice.size(); ringIndex++) {
192 0 : std::vector<Slice> userMemSlice;
193 0 : for (auto &cclSlice : multRingsSlice[ringIndex]) {
194 0 : Slice tmpSlice;
195 0 : tmpSlice.size = cclSlice.size;
196 0 : tmpSlice.offset = (cclSlice.offset / inputMemSize) * opInfo.count * perDataSize +
197 0 : multRingsSliceZero[ringIndex][0].offset;
198 0 : userMemSlice.push_back(tmpSlice);
199 : }
200 0 : multRingsUserMemSlice.push_back(userMemSlice);
201 0 : }
202 : }
203 0 : CHK_RET(ActiveSlaveStreams(param.stream));
204 0 : if (DMAReduceFlag_) {
205 : // allgather输入放在CCL buffer上,通过设置nullptr指示要从CCL buffer获取输入
206 0 : opInfo.inputAddr = nullptr;
207 : }
208 0 : CHK_RET(MultiRingAllGather(param.tag, execMem.inputMem, execMem.outputMem, execMem.count,
209 : param.DataDes.dataType, multRingsSlice, param.stream, PROF_STAGE_1, 0, opInfoPtr, multRingsUserMemSlice));
210 0 : } else if (topoAttr_.deviceType == DevType::DEV_TYPE_910B) {
211 0 : CHK_RET(ActiveSlaveStreams(param.stream));
212 :
213 0 : std::unique_ptr<AlgTemplateBase> level0Executor = AlgTemplateRegistry::Instance().GetAlgTemplate(
214 0 : TemplateType::TEMPLATE_ALL_GATHER_MESH_MIX, dispatcher_);
215 0 : CHK_SMART_PTR_NULL(level0Executor);
216 0 : CHK_RET(level0Executor->Prepare(algResResp_->slaveStreams, algResResp_->notifiesMain, algResResp_->notifiesAux,
217 : 0, opInfoPtr, serverIndex, level1RankSize));
218 0 : std::vector<Slice> emptySlices;
219 0 : CHK_RET(level0Executor->Prepare(execMem.outputMem, execMem.outputMem, execMem.inputMem,
220 : execMem.count, param.DataDes.dataType, param.stream, HCCL_REDUCE_RESERVED,
221 : LEVEL0_BRIDGE_RANK_ID, emptySlices, 0));
222 0 : CHK_RET(level0Executor->RegisterProfiler((level0RankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + commIndex,
223 : PROF_STAGE_1, HCCL_EXEC_STEP_NOT_SET, param.stream));
224 0 : CHK_RET(RunTemplate(level0Executor, level0CommInfo));
225 0 : }
226 :
227 0 : HCCL_INFO("[CollAllGatherMixExecutor][KernelRun]AllGather mix run success");
228 0 : return HCCL_SUCCESS;
229 0 : }
230 :
231 : REGISTER_EXEC("AllGatherMixExecutor", AllGatherMix, CollAllGatherMixExecutor);
232 : } // namespace hccl
|