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_deter_pipeline_executor.h"
12 :
13 : namespace hccl {
14 :
15 0 : CollReduceScatterDeterPipelineExecutor::CollReduceScatterDeterPipelineExecutor(
16 : const HcclDispatcher dispatcher,
17 0 : std::unique_ptr<TopoMatcher> &topoMatcher)
18 0 : : CollReduceScatterExecutor(dispatcher, topoMatcher)
19 : {
20 0 : scratchMemFlag_ = (workflowMode_ != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
21 0 : DMAReduceFlag_ = (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
22 0 : }
23 :
24 0 : void CollReduceScatterDeterPipelineExecutor::ParseParam(const OpParam& param)
25 : {
26 0 : tag_ = param.tag;
27 0 : curOffset_ = 0;
28 0 : totalSize_ = topoAttr_.userRankSize * param.DataDes.count * SIZE_TABLE[param.DataDes.dataType];
29 0 : }
30 :
31 0 : HcclResult CollReduceScatterDeterPipelineExecutor::CalcScratchMemSize(u64& scratchMemSize)
32 : {
33 0 : scratchMemSize = scratchMemFlag_ ? totalSize_ + topoAttr_.userRankSize * HCCL_MIN_SLICE_ALIGN_910B : 0U;
34 0 : HCCL_INFO("[CollReduceScatterDeterPipelineExecutor][CalcScratchMemSize]tag[%s] scratchMemSize[%llu]",
35 : tag_.c_str(), scratchMemSize);
36 0 : return HCCL_SUCCESS;
37 : }
38 :
39 :
40 0 : HcclResult CollReduceScatterDeterPipelineExecutor::CalcStreamNum(u32& streamNum)
41 : {
42 0 : streamNum = topoAttr_.deviceNumPerAggregation + 3U; // (deviceNum - 1)机内 + 4Reduce + 1机间 - 1主流
43 0 : HCCL_INFO("[CollReduceScatterDeterPipelineExecutor][CalcStreamNum] tag[%s] streamNum[%u]",
44 : tag_.c_str(), streamNum);
45 0 : return HCCL_SUCCESS;
46 : }
47 :
48 0 : HcclResult CollReduceScatterDeterPipelineExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
49 : {
50 0 : TransportMemType inputType = TransportMemType::RESERVED;
51 0 : TransportMemType outputType = TransportMemType::RESERVED;
52 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
53 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
54 0 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
55 0 : return HCCL_SUCCESS;
56 : }
57 :
58 0 : HcclResult CollReduceScatterDeterPipelineExecutor::CalcLevel0CommInfo(TransportMemType inputType,
59 : TransportMemType outputType,
60 : std::vector<LevelNSubCommTransport>& opTransport)
61 : {
62 0 : CommParaInfo commParaInfo(COMM_LEVEL0, CommType::COMM_TAG_MESH);
63 0 : commParaInfo.meshSinglePlane = true;
64 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaInfo, opTransport[COMM_LEVEL0], inputType, outputType));
65 0 : return HCCL_SUCCESS;
66 0 : }
67 :
68 0 : HcclResult CollReduceScatterDeterPipelineExecutor::CalcLevel1CommInfo(TransportMemType inputType,
69 : TransportMemType outputType,
70 : std::vector<LevelNSubCommTransport>& opTransport)
71 : {
72 0 : CommParaInfo commParaInfo(COMM_LEVEL1, CommType::COMM_TAG_MESH);
73 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaInfo, opTransport[COMM_LEVEL1], inputType, outputType));
74 0 : return HCCL_SUCCESS;
75 0 : }
76 :
77 0 : HcclResult CollReduceScatterDeterPipelineExecutor::CalcTransportMemType(TransportMemType &inputType,
78 : TransportMemType &outputType)
79 : {
80 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
81 0 : inputType = TransportMemType::CCL_INPUT;
82 0 : outputType = TransportMemType::CCL_OUTPUT;
83 : } else {
84 0 : inputType = TransportMemType::PARAM_INPUT;
85 0 : outputType = TransportMemType::SCRATCH;
86 : }
87 0 : HCCL_INFO("[CollReduceScatterDeterPipelineExecutor][CalcTransportMemType] tag[%s] inputType[%d], "
88 : "outputType[%d]", tag_.c_str(), inputType, outputType);
89 0 : return HCCL_SUCCESS;
90 : }
91 :
92 0 : u64 CollReduceScatterDeterPipelineExecutor::CalcLoopMaxCount(const u32 unitSize)
93 : {
94 : // 中转内存单次最多能够接受的output count,这里每一块的地址不要求128byte对齐
95 0 : u64 maxCountPerLoop = ((inCCLbufferSize_ / topoAttr_.userRankSize) - HCCL_MIN_SLICE_ALIGN_910B) / unitSize;
96 0 : HCCL_INFO("[CollReduceScatterDeterPipelineExecutor][CalcLoopMaxCount] maxCountPerLoop[%llu]", maxCountPerLoop);
97 0 : return maxCountPerLoop;
98 : }
99 :
100 0 : HcclResult CollReduceScatterDeterPipelineExecutor::RunLoop(OpParam ¶m, AlgResourceResponse &algRes)
101 : {
102 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[CollReduceScatterDeterPipelineExecutor][RunLoop] tag[%s], userRank[%u] begins.",
103 : tag_.c_str(), topoAttr_.userRank);
104 :
105 0 : CHK_PRT_RET((param.reduceType == HCCL_REDUCE_PROD) || (param.DataDes.dataType == HCCL_DATA_TYPE_INT64),
106 : HCCL_ERROR("[CollReduceScatterDeterPipelineExecutor] unsupported reduceType[%u] or unsupported dataType[%u]",
107 : param.reduceType, param.DataDes.dataType), HCCL_E_INTERNAL);
108 :
109 0 : u32 unitSize = SIZE_TABLE[param.DataDes.dataType];
110 :
111 0 : u8 *curInputPtr = static_cast<u8 *>(param.inputPtr);
112 0 : u8 *curOutputPtr = static_cast<u8 *>(param.outputPtr);
113 0 : CHK_PTR_NULL(curInputPtr);
114 0 : CHK_PTR_NULL(curOutputPtr);
115 :
116 0 : u64 maxCountPerLoop = CalcLoopMaxCount(unitSize);
117 0 : HCCL_INFO("[CollReduceScatterDeterPipelineExecutor][RunLoop]tag[%s], userRankSize is [%u], maxCountPerLoop "
118 : "is [%llu].", tag_.c_str(), topoAttr_.userRankSize, maxCountPerLoop);
119 :
120 0 : auto autoSelectedAlgTypeLevel1 = static_cast<u32>(algType_.algoLevel1);
121 0 : u8 deterministic = topoMatcher_->GetExternalInputHcclDeterministic();
122 :
123 0 : for (u64 countLeft = param.DataDes.count, curCount = 0, curSize = 0; countLeft > 0;
124 0 : countLeft -= curCount) {
125 0 : curInputPtr += curSize;
126 0 : curOutputPtr += curSize;
127 :
128 0 : curCount = (countLeft > maxCountPerLoop) ? maxCountPerLoop : countLeft;
129 0 : curSize = curCount * unitSize;
130 :
131 0 : HCCL_CONFIG_DEBUG(HCCL_ALG, "[CollReduceScatterDeterPipelineExecutor][RunLoop]tag[%s], curOffset[%llu]," \
132 : "curInputPtr[%p], curOutputPtr[%p], curCount[%llu], dataType[%d].",
133 : tag_.c_str(), curOffset_, curInputPtr, curOutputPtr, curCount, param.DataDes.dataType);
134 :
135 0 : constexpr s64 HCCL_MEDIUM_COUNT_2_MB = 2 * 1024 * 1024;
136 0 : bool smallData = curSize < HCCL_MEDIUM_COUNT_2_MB ? 1 : 0;
137 0 : bool hugeData = IsHugeData(curSize);
138 0 : auto meta = HcclOpMetaInfo::GetOneForReduceScatter(autoSelectedAlgTypeLevel1, param.DataDes.dataType,
139 : ReduceType::INLINE_REDUCE, hugeData, smallData, CopyPattern::ZCOPY, false, deterministic, false);
140 0 : CHK_RET(InitTask(dispatcher_, const_cast<Stream&>(param.stream), meta.isEnableCache, meta.GetCacheKey(), true));
141 0 : ExecMem execMem;
142 0 : execMem.count = curCount;
143 0 : execMem.inputMem = algRes.cclInputMem;
144 0 : execMem.outputMem = algRes.cclOutputMem;
145 0 : execMem.scratchMem = algRes.cclOutputMem;
146 : // 使用当前Loop偏移到的地址作为当前的inputPtr和outputPtr
147 0 : execMem.inputPtr = curInputPtr;
148 0 : execMem.outputPtr = curOutputPtr;
149 :
150 0 : CHK_RET(KernelRun(param, execMem));
151 :
152 0 : CHK_RET(LaunchTaskExtend(dispatcher_, const_cast<Stream&>(param.stream),
153 : const_cast<std::vector<Stream> &>(algResResp_->slaveStreams)));
154 :
155 0 : curOffset_ += curSize;
156 0 : }
157 0 : HCCL_INFO("[CollReduceScatterDeterPipelineExecutor][RunLoop] tag[%s], userRank[%u] run loop success.",
158 : tag_.c_str(), topoAttr_.userRank);
159 :
160 0 : return HCCL_SUCCESS;
161 : }
162 :
163 0 : HcclResult CollReduceScatterDeterPipelineExecutor::PrepareDataSlice(const OpParam ¶m, const ExecMem &execMem,
164 : const SubCommInfo &level0CommInfo, const SubCommInfo &level1CommInfo, std::vector<Slice> &bufferSlices)
165 : {
166 0 : u32 unitSize = SIZE_TABLE[param.DataDes.dataType];
167 0 : bufferSlices.resize(topoAttr_.userRankSize);
168 0 : u64 totalOutputSize = param.DataDes.count * unitSize;
169 0 : u32 rankIdLevel0 = level0CommInfo.localRank;
170 0 : u32 rankSizeLevel0 = level0CommInfo.localRankSize;
171 0 : u32 rankIdLevel1 = level1CommInfo.localRank;
172 0 : u32 rankSizeLevel1 = level1CommInfo.localRankSize;
173 :
174 0 : for (u32 i = 0; i < rankSizeLevel1; i++) {
175 0 : u32 inputBlockIndex = (rankIdLevel1 + i) % rankSizeLevel1;
176 0 : u32 outputBlockIndex = (rankIdLevel1 + rankSizeLevel1 - i) % rankSizeLevel1;
177 0 : u32 inputSliceIndex = inputBlockIndex * rankSizeLevel0 + rankIdLevel0;
178 0 : u64 inputSliceOffset = totalOutputSize * inputSliceIndex + curOffset_;
179 0 : for (u32 j = 0; j < rankSizeLevel0; j++) {
180 0 : u32 outputSliceIndex = outputBlockIndex * rankSizeLevel0 + j;
181 0 : bufferSlices[outputSliceIndex].size = execMem.count * unitSize;
182 0 : u64 outputSliceOffset = (bufferSlices[outputSliceIndex].size + HCCL_MIN_SLICE_ALIGN_910B) * outputSliceIndex;
183 0 : u64 outputInSliceOffset = (HCCL_MIN_SLICE_ALIGN_910B + (inputSliceOffset % HCCL_MIN_SLICE_ALIGN_910B) -
184 0 : (outputSliceOffset % HCCL_MIN_SLICE_ALIGN_910B)) % HCCL_MIN_SLICE_ALIGN_910B;
185 0 : bufferSlices[outputSliceIndex].offset = outputSliceOffset + outputInSliceOffset;
186 0 : HCCL_DEBUG("[CollReduceScatterDeterPipelineExecutor][PrepareDataSlice]tag[%s], buffer slice i[%u], "
187 : "size[%llu], offset[%llu], outputInSliceOffset[%llu], inputSliceIndex[%u], inputSliceOffset[%llu], "
188 : "curOffset[%llu]", tag_.c_str(), outputSliceIndex, bufferSlices[outputSliceIndex].size,
189 : bufferSlices[outputSliceIndex].offset, outputInSliceOffset, inputSliceIndex, inputSliceOffset,
190 : curOffset_);
191 : }
192 : }
193 0 : return HCCL_SUCCESS;
194 : }
195 :
196 0 : HcclResult CollReduceScatterDeterPipelineExecutor::KernelRun(const OpParam ¶m, ExecMem &execMem)
197 : {
198 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[CollReduceScatterDeterPipelineExecutor][KernelRun] tag[%s], userRank[%u] starts.",
199 : tag_.c_str(), topoAttr_.userRank);
200 :
201 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
202 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
203 0 : u32 commIndex = level0CommInfo.localRank;
204 :
205 0 : CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
206 0 : SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
207 :
208 0 : std::vector<Slice> bufferSlices; // 数据分成ranksize份,每份的起始偏移和大小
209 0 : CHK_RET(PrepareDataSlice(param, execMem, level0CommInfo, level1CommInfo, bufferSlices));
210 :
211 0 : CHK_RET(ActiveSlaveStreams(param.stream));
212 :
213 0 : std::unique_ptr<AlgTemplateBase> tempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
214 0 : TemplateType::TEMPLATE_REDUCESCATTER_MULTI_DETERMINISTIC_PIPELINE, dispatcher_);
215 0 : CHK_SMART_PTR_NULL(tempAlg);
216 :
217 0 : HcomCollOpInfo opInfo = {"", execMem.inputPtr, execMem.outputPtr, param.DataDes.count, param.DataDes.dataType,
218 0 : param.root, param.reduceType};
219 :
220 0 : CHK_RET(tempAlg->Prepare(&opInfo, execMem.scratchMem, execMem.count, curOffset_, bufferSlices, level0CommInfo,
221 : level1CommInfo, const_cast<Stream&>(param.stream), algResResp_->slaveStreams, algResResp_->notifiesMain,
222 : algResResp_->notifiesAux));
223 0 : CHK_RET(tempAlg->RunAsync());
224 :
225 0 : HCCL_INFO("[CollReduceScatterDeterPipelineExecutor][KernelRun] tag[%s], userRank[%u] run success.",
226 : tag_.c_str(), topoAttr_.userRank);
227 0 : return HCCL_SUCCESS;
228 0 : }
229 :
230 : REGISTER_EXEC("ReduceScatterDeterPipelineExecutor", ReduceScatterDeterPipeline,
231 : CollReduceScatterDeterPipelineExecutor);
232 :
233 : }
234 :
|