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_reduce_scatter_order_preserved_for_910_93_executor.h"
12 :
13 : namespace hccl {
14 :
15 0 : CollReduceScatterOrderPreservedFor91093Executor::CollReduceScatterOrderPreservedFor91093Executor(const HcclDispatcher dispatcher,
16 0 : std::unique_ptr<TopoMatcher> &topoMatcher)
17 0 : : CollReduceScatterExecutor(dispatcher, topoMatcher)
18 : {
19 0 : DMAReduceFlag_ = true;
20 0 : desc_.deterministic = DETERMINISTIC_STRICT;
21 0 : }
22 :
23 0 : void CollReduceScatterOrderPreservedFor91093Executor::ParseParam(const OpParam& param)
24 : {
25 0 : tag_ = param.tag;
26 0 : aicpuUnfoldMode_ = param.aicpuUnfoldMode;
27 :
28 : // 是否需要scratch memory(图模式没有cclbuffer,需要额外申请scratchMem)
29 0 : scratchMemFlag_ = (workflowMode_ != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
30 :
31 0 : u64 sizePerRank = param.DataDes.count * SIZE_TABLE[param.DataDes.dataType];
32 0 : totalSize_ = topoAttr_.userRankSize * sizePerRank;
33 0 : }
34 :
35 0 : HcclResult CollReduceScatterOrderPreservedFor91093Executor::CalcScratchMemSize(u64& scratchMemSize)
36 : {
37 0 : scratchMemSize = scratchMemFlag_ ? totalSize_ : 0U;
38 0 : HCCL_INFO("[%s]tag[%s] scratchMemSize[%llu]", __func__, tag_.c_str(), scratchMemSize);
39 0 : return HCCL_SUCCESS;
40 : }
41 :
42 0 : u32 CollReduceScatterOrderPreservedFor91093Executor::CalReduceStreamNum(const u32& localRankSize) const
43 : {
44 0 : return (1 << static_cast<int>(std::floor(log2(localRankSize))));
45 : }
46 :
47 0 : HcclResult CollReduceScatterOrderPreservedFor91093Executor::CalcStreamNum(u32& streamNum)
48 : {
49 : // 获取超节点内rank数
50 0 : u32 devNumInlocalPod = 0;
51 0 : u32 rankIdxInPod = 0;
52 0 : CHK_RET(topoMatcher_->GetLocalSuperPodRankSize(topoAttr_.userRank, devNumInlocalPod, rankIdxInPod));
53 :
54 : // 单卡节点场景,L1(超节点内)不需要流,仅计算L2流数
55 0 : if (devNumInlocalPod == 1) {
56 0 : u32 level2StreamNum = std::min(CalReduceStreamNum(topoAttr_.superPodNum) - 1, DEVICE_FOUR);
57 0 : streamNum = level2StreamNum;
58 0 : HCCL_INFO("[%s]tag[%s] single rank per module, level2StreamNum[%u], streamNum[%u]",
59 : __func__, tag_.c_str(), level2StreamNum, streamNum);
60 0 : return HCCL_SUCCESS;
61 : }
62 :
63 : // all2allStreamNum条流给alltoall
64 0 : u32 all2allStreamNum = std::min(devNumInlocalPod, DEVICE_EIGHT);
65 : // reduceStreamNum主流分给alltoall,从流给LocalReduce使用
66 0 : u32 reduceStreamNum = std::min(CalReduceStreamNum(devNumInlocalPod) - 1, DEVICE_FOUR);
67 : // level2StreamNum超节点间reducescatter
68 0 : u32 level2StreamNum = std::min(CalReduceStreamNum(topoAttr_.superPodNum) - 1, DEVICE_FOUR);
69 : // 总流数上限:7(alltoall使用,提前的本地拷贝任务不需要并行)+ 4(LocalReduce使用)
70 0 : streamNum = std::max(all2allStreamNum + reduceStreamNum - 1, level2StreamNum);
71 :
72 0 : HCCL_INFO("[%s]tag[%s] all2allStreamNum[%u], reduceStreamNum[%u], level2StreamNum[%u], streamNum[%u]", __func__, tag_.c_str(),
73 : all2allStreamNum, reduceStreamNum, level2StreamNum, streamNum);
74 0 : return HCCL_SUCCESS;
75 : }
76 :
77 0 : HcclResult CollReduceScatterOrderPreservedFor91093Executor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
78 : {
79 0 : TransportMemType inputType = TransportMemType::RESERVED;
80 0 : TransportMemType outputType = TransportMemType::RESERVED;
81 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
82 0 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
83 0 : CHK_RET(CalcLevel2CommInfo(inputType, outputType, opTransport));
84 0 : return HCCL_SUCCESS;
85 : }
86 :
87 0 : HcclResult CollReduceScatterOrderPreservedFor91093Executor::CalcTransportMemType(TransportMemType &inputType,
88 : TransportMemType &outputType) const
89 : {
90 : // scratchMemFlag_ 对应图模式场景(图模式没有cclbuffer), PARAM_INPUT -> userInput
91 0 : inputType = scratchMemFlag_ ? TransportMemType::PARAM_INPUT : TransportMemType::CCL_INPUT;
92 0 : outputType = scratchMemFlag_ ? TransportMemType::SCRATCH : TransportMemType::CCL_OUTPUT;
93 0 : HCCL_INFO("[%s]tag[%s] inputType[%d], outputType[%d]", __func__, tag_.c_str(), inputType, outputType);
94 0 : return HCCL_SUCCESS;
95 : }
96 :
97 0 : HcclResult CollReduceScatterOrderPreservedFor91093Executor::CalcLevel1CommInfo(TransportMemType inputType,
98 : TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
99 : {
100 0 : CommParaInfo commParaLevel1(COMM_COMBINE_L1, CommType::COMM_TAG_MESH);
101 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel1, opTransport[COMM_COMBINE_L1], inputType, outputType));
102 0 : return HCCL_SUCCESS;
103 0 : }
104 :
105 0 : HcclResult CollReduceScatterOrderPreservedFor91093Executor::CalcLevel2CommInfo(TransportMemType inputType,
106 : TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
107 : {
108 0 : if (topoAttr_.superPodNum > 1) {
109 0 : CommParaInfo commParaLevel2(COMM_LEVEL2, CommType::COMM_TAG_MESH);
110 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel2, opTransport[COMM_LEVEL2], inputType, outputType));
111 0 : }
112 0 : return HCCL_SUCCESS;
113 : }
114 :
115 0 : bool CollReduceScatterOrderPreservedFor91093Executor::IsSmallData(const u64 totalSize, const u64 curSize)
116 : {
117 : (void) curSize;
118 : // 子图复用的阈值(opmeta全一致时,ffts子图复用)
119 0 : return totalSize <= HCCL_SMALL_COUNT_32_KB;
120 : }
121 :
122 0 : HcclResult CollReduceScatterOrderPreservedFor91093Executor::RunReduceScatterLevel1SingleRank(const OpParam ¶m,
123 : ExecMem &execMem, const SubCommInfo &level1CommInfo) const
124 : {
125 : (void) level1CommInfo;
126 0 : HCCL_INFO("[%s] single rank per module, skip L1 AllToAll and LocalReduce, tag[%s]",
127 : __func__, tag_.c_str());
128 :
129 0 : u64 unitSize = SIZE_TABLE[param.DataDes.dataType];
130 0 : u64 curSize = execMem.count * unitSize;
131 0 : DeviceMem bufferMem = scratchMemFlag_ ? execMem.scratchMem : execMem.inputMem;
132 0 : DeviceMem dstMem;
133 0 : DeviceMem srcMem;
134 0 : for (u32 i = 0; i < topoAttr_.userRankSize; i++) {
135 : // 拷贝input上每个slice的数据到中转内存,源端每个slice的size固定为output的size
136 0 : dstMem = bufferMem.range(curSize * i, curSize);
137 0 : srcMem = DeviceMem::create(static_cast<u8 *>(execMem.inputPtr) + param.DataDes.count * unitSize * i, curSize);
138 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstMem, srcMem, const_cast<Stream&>(param.stream)));
139 : }
140 0 : return HCCL_SUCCESS;
141 0 : }
142 :
143 0 : HcclResult CollReduceScatterOrderPreservedFor91093Executor::RunReduceScatterLevel1(const OpParam ¶m, ExecMem &execMem,
144 : SubCommInfo &level1CommInfo)
145 : {
146 0 : if (level1CommInfo.localRankSize == 1) {
147 0 : all2allOffset_ = topoAttr_.superPodNum > 1 ? 1 : 0;
148 0 : CHK_RET(RunReduceScatterLevel1SingleRank(param, execMem, level1CommInfo));
149 0 : return HCCL_SUCCESS;
150 : }
151 :
152 0 : CHK_RET(ActiveSlaveStreams(param.stream));
153 :
154 : // 切分数据(ReduceScatter分组,记录每组的起始偏移和大小)
155 0 : GroupSlicesInfo groupSlicesInfoLevel0;
156 0 : u64 size = execMem.count * SIZE_TABLE[param.DataDes.dataType];
157 0 : for (u32 groupId = 0; groupId < topoAttr_.superPodNum; groupId++) {
158 0 : MemBlockInfo memInfo;
159 0 : for (u32 dataId = 0; dataId < level1CommInfo.localRankSize; dataId ++) {
160 0 : u64 offset = (dataId + groupId * level1CommInfo.localRankSize) * size;
161 0 : u64 userMemInOffset = param.DataDes.count * SIZE_TABLE[param.DataDes.dataType] *
162 0 : (dataId + groupId * level1CommInfo.localRankSize);
163 0 : memInfo.size.push_back(size);
164 0 : memInfo.userInputOffsets.push_back(userMemInOffset);
165 0 : memInfo.inputOffsets.push_back(offset);
166 0 : memInfo.outputOffsets.push_back(offset);
167 : }
168 0 : groupSlicesInfoLevel0.push_back(memInfo);
169 0 : }
170 :
171 0 : all2allOffset_ = topoAttr_.superPodNum > 1 ? 1 : 0; // 多机场景需要偏移1(给L1预留计算位,减少拷贝次数)
172 0 : std::unique_ptr<AlgTemplateBase> level1TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
173 0 : TemplateType::TEMPLATE_REDUCESCATTER_PLANT_LOCAL_REDUCE, dispatcher_);
174 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[%s] Run TEMPLATE_REDUCESCATTER_PLANT_LOCAL_REDUCE in COMM_COMBINE_L1", __func__);
175 0 : CHK_SMART_PTR_NULL(level1TempAlg);
176 :
177 : // execMem.scratchMem在单算子模式下为cclout,图模式为scrach,因此output传入scrach即可
178 0 : CHK_RET(level1TempAlg->Prepare(execMem.inputPtr, execMem.inputMem, execMem.scratchMem, param.stream,
179 : algResResp_->slaveStreams, algResResp_->notifiesMain, algResResp_->notifiesAux,
180 : groupSlicesInfoLevel0, param.reduceType, all2allOffset_, param.DataDes.dataType, false, false, true));
181 0 : CHK_RET(level1TempAlg->RegisterProfiler(
182 : (level1CommInfo.localRankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + level1CommInfo.localRank,
183 : PROF_STAGE_2, HCCL_EXEC_STEP_NOT_SET, param.stream));
184 0 : CHK_RET(RunTemplate(level1TempAlg, level1CommInfo));
185 :
186 0 : return HCCL_SUCCESS;
187 0 : }
188 :
189 0 : HcclResult CollReduceScatterOrderPreservedFor91093Executor::RunReduceScatterLevel2(const OpParam ¶m, ExecMem &execMem,
190 : SubCommInfo &level1CommInfo)
191 : {
192 0 : u32 commIndex = level1CommInfo.localRank;
193 0 : CHK_RET(CheckCommSize(COMM_LEVEL2, COMM_INDEX_0 + 1));
194 0 : SubCommInfo level2CommInfo = GetSubCommInfo(COMM_LEVEL2, COMM_INDEX_0);
195 :
196 : // 切分数据,记录每组的起始偏移和大小(仅1组)
197 0 : u64 size = execMem.count * SIZE_TABLE[param.DataDes.dataType];
198 0 : MemBlockInfo memInfo;
199 0 : u32 level0Ranksize = level1CommInfo.localRankSize;
200 0 : u32 inputBaseIndex = (all2allOffset_ + commIndex) % level0Ranksize; // 多机场景需要偏移1(给L1预留计算位,减少拷贝次数)
201 0 : for (u32 dataId = 0; dataId < level2CommInfo.localRankSize; dataId ++) {
202 0 : u64 inputIndex = inputBaseIndex + dataId * level0Ranksize;
203 0 : memInfo.inputOffsets.push_back(inputIndex * size);
204 0 : u64 outputIndex = commIndex + dataId * level0Ranksize;
205 0 : memInfo.outputOffsets.push_back(outputIndex * size);
206 0 : memInfo.userInputOffsets.push_back(outputIndex * size);
207 0 : memInfo.size.push_back(size);
208 : }
209 :
210 0 : std::unique_ptr<AlgTemplateBase> level2TempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
211 0 : TemplateType::TEMPLATE_REDUCESCATTER_PLANT_LOCAL_REDUCE_COMBINE, dispatcher_);
212 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[%s] Run TEMPLATE_REDUCESCATTER_PLANT_LOCAL_REDUCE_COMBINE in COMM_LEVEL2", __func__);
213 0 : CHK_SMART_PTR_NULL(level2TempAlg);
214 :
215 0 : u32 level0LastRank = level0Ranksize - 1;
216 0 : bool isUseCclIn = (level0Ranksize == 1) || (commIndex == level0LastRank - 1);
217 0 : bool borrowSpace = level0Ranksize == 1;
218 0 : CHK_RET(level2TempAlg->Prepare(execMem.inputMem, execMem.scratchMem,
219 : param.stream, algResResp_->slaveStreams, algResResp_->notifiesMain, algResResp_->notifiesAux,
220 : memInfo, param.reduceType, param.DataDes.dataType, isUseCclIn,
221 : commIndex == level0LastRank, borrowSpace));
222 0 : CHK_RET(level2TempAlg->RegisterProfiler((level0Ranksize << PROF_RANKSIZE_OFFSET_OF_PLANEID) +
223 : level1CommInfo.localRank, PROF_STAGE_2, HCCL_EXEC_STEP_NOT_SET, param.stream));
224 0 : CHK_RET(RunTemplate(level2TempAlg, level2CommInfo));
225 0 : return HCCL_SUCCESS;
226 0 : }
227 :
228 0 : HcclResult CollReduceScatterOrderPreservedFor91093Executor::KernelRun(const OpParam ¶m, ExecMem &execMem)
229 : {
230 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[%s]CollReduceScatterOrderPreservedFor91093Executor starts, tag[%s]", __func__, tag_.c_str());
231 0 : CHK_RET(CheckCommSize(COMM_COMBINE_L1, COMM_INDEX_0 + 1));
232 0 : SubCommInfo level1CommInfo = GetSubCommInfo(COMM_COMBINE_L1, COMM_INDEX_0);
233 :
234 : // L1 节点内 reduce scatter
235 0 : CHK_RET(RunReduceScatterLevel1(param, execMem, level1CommInfo));
236 : // L2 节点间 reduce scatter
237 0 : if (topoAttr_.superPodNum > 1) {
238 0 : CHK_RET(RunReduceScatterLevel2(param, execMem, level1CommInfo));
239 : }
240 :
241 : // 非HD算法 execMem.scratchMem最后拷贝至UserOut
242 0 : u64 dataSize = execMem.count * SIZE_TABLE[param.DataDes.dataType];
243 0 : DeviceMem srcMem = execMem.scratchMem.range(dataSize * topoAttr_.userRank, dataSize);
244 0 : DeviceMem dstMem = DeviceMem::create(execMem.outputPtr, dataSize);
245 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstMem, srcMem, const_cast<Stream&>(param.stream)));
246 :
247 0 : HCCL_INFO("[%s]order preserved ReduceScatter run success, tag[%s]", __func__, tag_.c_str());
248 0 : return HCCL_SUCCESS;
249 0 : }
250 :
251 : REGISTER_EXEC("ReduceScatterOrderPreservedFor91093Executor", ReduceScatterOrderPreservedFor91093,
252 : CollReduceScatterOrderPreservedFor91093Executor);
253 : }
|