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