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_mid_count_for_910_93_executor.h"
12 :
13 : namespace hccl {
14 0 : CollAllGatherMidCountFor91093Executor::CollAllGatherMidCountFor91093Executor(const HcclDispatcher dispatcher,
15 0 : std::unique_ptr<TopoMatcher> &topoMatcher)
16 0 : : CollAllGatherExecutor(dispatcher, topoMatcher)
17 : {
18 0 : DMAReduceFlag_ = true;
19 0 : desc_.level1SupportedAlgos = {
20 : AlgTypeLevel1::ALG_LEVEL1_NHR,
21 0 : };
22 0 : desc_.level2SupportedAlgos = {
23 : AlgTypeLevel2::ALG_LEVEL2_NHR,
24 0 : };
25 0 : }
26 :
27 0 : HcclResult CollAllGatherMidCountFor91093Executor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
28 : {
29 0 : TransportMemType inputType = TransportMemType::RESERVED;
30 0 : TransportMemType outputType = TransportMemType::RESERVED;
31 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
32 0 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
33 0 : CHK_RET(CalcLevel2CommInfo(inputType, outputType, opTransport));
34 0 : return HCCL_SUCCESS;
35 : }
36 :
37 0 : HcclResult CollAllGatherMidCountFor91093Executor::CalcTransportMemType(TransportMemType &inputType,
38 : TransportMemType &outputType) const
39 : {
40 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
41 0 : inputType = TransportMemType::CCL_INPUT;
42 0 : outputType = TransportMemType::CCL_OUTPUT;
43 : } else {
44 0 : HCCL_ERROR("AllGatherMidCountFor91093Executor do not support offload mode");
45 0 : return HCCL_E_UNAVAIL;
46 : }
47 0 : HCCL_INFO("[CollAllGatherMidCountFor91093Executor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d]",
48 : tag_.c_str(), inputType, outputType);
49 0 : return HCCL_SUCCESS;
50 : }
51 :
52 0 : HcclResult CollAllGatherMidCountFor91093Executor::CalcLevel1CommInfo(TransportMemType inputType,
53 : TransportMemType outputType,
54 : std::vector<LevelNSubCommTransport>& opTransport)
55 : {
56 0 : CommParaInfo commParaCombineL1(COMM_COMBINE_L1, CommType::COMM_TAG_WHOLE_NHR);
57 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaCombineL1, opTransport[COMM_COMBINE_L1], inputType, outputType));
58 0 : return HCCL_SUCCESS;
59 0 : }
60 :
61 0 : HcclResult CollAllGatherMidCountFor91093Executor::CalcLevel2CommInfo(TransportMemType inputType, TransportMemType outputType,
62 : std::vector<LevelNSubCommTransport>& opTransport)
63 : {
64 0 : CommParaInfo commParaLevel2(COMM_LEVEL2, CommType::COMM_TAG_NONUNIFORM_HIERARCHICAL_RING);
65 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel2, opTransport[COMM_LEVEL2], inputType, outputType));
66 0 : return HCCL_SUCCESS;
67 0 : }
68 :
69 0 : u64 CollAllGatherMidCountFor91093Executor::CalcLoopMaxCount(const u64 cclBuffSize, const u32 unitSize)
70 : {
71 0 : u64 maxCountPerLoop = cclBuffSize / topoAttr_.userRankSize / HCCL_MIN_SLICE_ALIGN_910_93 / unitSize * HCCL_MIN_SLICE_ALIGN_910_93;
72 0 : if (cclBuffSize != 0 && maxCountPerLoop == 0) {
73 0 : maxCountPerLoop = cclBuffSize / topoAttr_.userRankSize / unitSize;
74 0 : HCCL_INFO("[CollAllGatherMidCountFor91093Executor][CalcLoopMaxCount]" \
75 : "using default maxCountPerLoop[%llu] as CCLBuffSize / unitSize", maxCountPerLoop);
76 : }
77 0 : if (maxCountPerLoop == 0) {
78 0 : HCCL_ERROR("[CollAllGatherMidCountFor91093Executor][CalcLoopMaxCount] cclbuffer size is too small");
79 : }
80 0 : return maxCountPerLoop;
81 : }
82 :
83 0 : u64 CollAllGatherMidCountFor91093Executor::CalcDstMemOffset(const OpParam ¶m, u64 inputMemSize) const
84 : {
85 : (void) param;
86 0 : return topoAttr_.userRank * inputMemSize;
87 : }
88 :
89 0 : HcclResult CollAllGatherMidCountFor91093Executor::PrepareL2DataSlices(const OpParam ¶m,
90 : const SubCommInfo &level1CommInfo, const SubCommInfo &level2CommInfo, u64 inputMemSize,
91 : std::vector<Slice> &dataSlices) const
92 : {
93 : (void) param;
94 0 : const u32 level1RankSize = level1CommInfo.localRankSize;
95 0 : const u32 level1RankIndex = level1CommInfo.localRank;
96 0 : const u32 level2RankSize = level2CommInfo.localRankSize;
97 :
98 0 : std::vector<Slice> level2DataSlices;
99 0 : for (u32 i = 0; i < level2RankSize; i++) {
100 0 : Slice sliceTemp;
101 0 : sliceTemp.size = inputMemSize;
102 0 : sliceTemp.offset = inputMemSize * (i * level1RankSize + level1RankIndex);
103 0 : level2DataSlices.push_back(sliceTemp);
104 : }
105 :
106 0 : dataSlices = std::move(level2DataSlices);
107 0 : return HCCL_SUCCESS;
108 0 : }
109 :
110 0 : HcclResult CollAllGatherMidCountFor91093Executor::RunLevel2ByNHR(const OpParam ¶m, ExecMem &execMem,
111 : SubCommInfo &level1CommInfo, SubCommInfo &level2CommInfo) const
112 : {
113 0 : const u32 level2RankSize = level2CommInfo.localRankSize;
114 0 : const u32 multiSuperPodMode = 1;
115 0 : if (level2RankSize > multiSuperPodMode) {
116 0 : u32 unitSize = 0;
117 0 : const HcclDataType dataType = param.GetDataType();
118 0 : CHK_RET(SalGetDataTypeSize(dataType, unitSize));
119 0 : u64 inputMemSize = execMem.count * unitSize;
120 :
121 0 : std::unique_ptr<AlgTemplateBase> level2AGExecutor = AlgTemplateRegistry::Instance().GetAlgTemplate(
122 0 : TemplateType::TEMPLATE_ALL_GATHER_NHR, dispatcher_);
123 0 : CHK_SMART_PTR_NULL(level2AGExecutor);
124 :
125 0 : std::vector<Slice> dataSlices;
126 0 : CHK_RET(PrepareL2DataSlices(param, level1CommInfo, level2CommInfo, inputMemSize, dataSlices));
127 0 : CHK_RET(level2AGExecutor->Prepare(execMem.outputMem, execMem.outputMem, execMem.inputMem, execMem.count,
128 : param.GetDataType(), param.stream, HCCL_REDUCE_RESERVED, INVALID_VALUE_RANKID, dataSlices, 0));
129 :
130 0 : CHK_RET(level2AGExecutor->RegisterProfiler((
131 : level2RankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + level2CommInfo.localRank,
132 : PROF_STAGE_0, HCCL_EXEC_STEP_NOT_SET, param.stream));
133 :
134 0 : CHK_RET(RunTemplate(level2AGExecutor, level2CommInfo));
135 0 : }
136 0 : HCCL_INFO("MidCountAllGather run success in level2");
137 0 : return HCCL_SUCCESS;
138 : }
139 :
140 0 : HcclResult CollAllGatherMidCountFor91093Executor::PrepareL1DataSlices(const OpParam ¶m,
141 : const SubCommInfo &level1CommInfo, const SubCommInfo &level2CommInfo,
142 : u64 inputMemSize, u32 moduleId, std::vector<Slice> &dataSlices) const
143 : {
144 : (void) level2CommInfo;
145 0 : u32 unitSize = 0;
146 0 : CHK_RET(SalGetDataTypeSize(param.GetDataType(), unitSize));
147 0 : const u32 level1RankSize = level1CommInfo.localRankSize;
148 0 : std::vector<Slice> level1DataSlices;
149 0 : for (u32 i = 0; i < level1RankSize; i++) {
150 0 : Slice sliceTemp;
151 0 : sliceTemp.size = inputMemSize;
152 0 : sliceTemp.offset = inputMemSize * (level1RankSize * moduleId + i) ;
153 0 : level1DataSlices.push_back(sliceTemp);
154 : }
155 0 : dataSlices = std::move(level1DataSlices);
156 0 : return HCCL_SUCCESS;
157 0 : }
158 :
159 0 : HcclResult CollAllGatherMidCountFor91093Executor::RunLevel1ByNHR(const OpParam ¶m, ExecMem &execMem,
160 : SubCommInfo &level1CommInfo, SubCommInfo &level2CommInfo)
161 : {
162 0 : const u32 level1RankSize = level1CommInfo.localRankSize;
163 0 : const u32 level2RankSize = level2CommInfo.localRankSize;
164 0 : const u32 multiRankMode = 1;
165 0 : if (level1RankSize <= multiRankMode) {
166 0 : return HCCL_SUCCESS;
167 : }
168 :
169 0 : u32 unitSize = 0;
170 0 : const HcclDataType dataType = param.GetDataType();
171 0 : CHK_RET(SalGetDataTypeSize(dataType, unitSize));
172 0 : u64 inputMemSize = execMem.count * unitSize;
173 :
174 0 : for (u32 moduleId = 0; moduleId < level2RankSize; moduleId++){
175 0 : std::vector<Slice> dataSlices;
176 0 : CHK_RET(PrepareL1DataSlices(param, level1CommInfo, level2CommInfo, inputMemSize, moduleId, dataSlices));
177 :
178 : // 计算slice, 不同超节点相同slice
179 0 : std::unique_ptr<AlgTemplateBase> level1AGExecutor = AlgTemplateRegistry::Instance().GetAlgTemplate(
180 0 : TemplateType::TEMPLATE_ALL_GATHER_NHR, dispatcher_);
181 0 : CHK_SMART_PTR_NULL(level1AGExecutor);
182 :
183 0 : CHK_RET(level1AGExecutor->Prepare(execMem.outputMem, execMem.outputMem, execMem.inputMem, execMem.count,
184 : param.DataDes.dataType, param.stream, HCCL_REDUCE_RESERVED, INVALID_VALUE_RANKID, dataSlices, 0));
185 :
186 0 : CHK_RET(level1AGExecutor->RegisterProfiler((
187 : level1RankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + level2CommInfo.localRank,
188 : PROF_STAGE_1, HCCL_EXEC_STEP_NOT_SET, param.stream));
189 :
190 0 : CHK_RET(RunTemplate(level1AGExecutor, level1CommInfo));
191 0 : }
192 0 : HCCL_INFO("MidCountAllGather run success in level1");
193 0 : return HCCL_SUCCESS;
194 : }
195 :
196 0 : HcclResult CollAllGatherMidCountFor91093Executor::KernelRun(const OpParam ¶m, ExecMem &execMem)
197 : {
198 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[%s] The MidCountFor91093Executor starts", __func__);
199 :
200 0 : SubCommInfo level1CommInfo;
201 0 : SubCommInfo level2CommInfo;
202 0 : CHK_RET(CheckCommSize(COMM_COMBINE_L1, COMM_INDEX_0 + 1));
203 0 : CHK_RET(CheckCommSize(COMM_LEVEL2, COMM_INDEX_0 + 1));
204 0 : level1CommInfo = GetSubCommInfo(COMM_COMBINE_L1, COMM_INDEX_0);
205 0 : level2CommInfo = GetSubCommInfo(COMM_LEVEL2, COMM_INDEX_0);
206 :
207 0 : u32 unitSize = 0;
208 0 : const HcclDataType dataType = param.GetDataType();
209 0 : CHK_RET(SalGetDataTypeSize(dataType, unitSize));
210 :
211 0 : u64 inputMemSize = execMem.count * unitSize;
212 0 : const u32 SINGLE_RANK_FLAG = 1;
213 :
214 0 : if (topoAttr_.userRankSize == SINGLE_RANK_FLAG) {
215 0 : DeviceMem srcMem = DeviceMem::create(static_cast<u8 *>(execMem.inputPtr), inputMemSize);
216 0 : DeviceMem dstMem = DeviceMem::create(static_cast<u8 *>(execMem.outputPtr), inputMemSize);
217 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstMem, srcMem, const_cast<Stream&>(param.stream)));
218 0 : return HCCL_SUCCESS;
219 0 : }
220 :
221 : //先做server间算法,带有消减拷贝场景数据需要从user input取,拷贝到ccl output上
222 0 : if (DMAReduceFlag_) {
223 0 : u64 dstMemOffset = CalcDstMemOffset(param, inputMemSize);
224 0 : DeviceMem srcMem = DeviceMem::create(static_cast<u8 *>(execMem.inputPtr), inputMemSize);
225 0 : DeviceMem dstMem = execMem.outputMem.range(dstMemOffset, inputMemSize);
226 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstMem, srcMem, const_cast<Stream&>(param.stream)));
227 0 : }
228 :
229 0 : CHK_RET(RunLevel2ByNHR(param, execMem, level1CommInfo, level2CommInfo));
230 0 : CHK_RET(RunLevel1ByNHR(param, execMem, level1CommInfo, level2CommInfo));
231 :
232 0 : if (DMAReduceFlag_) {
233 0 : for (u32 i = 0; i < topoAttr_.userRankSize; i++) {
234 0 : DeviceMem srcMem = DeviceMem::create(static_cast<u8 *>(execMem.outputMem.ptr())+ inputMemSize * i, inputMemSize);
235 0 : DeviceMem dstMem = DeviceMem::create(static_cast<u8 *>(execMem.outputPtr) + param.DataDes.count * unitSize * i, inputMemSize);
236 0 : CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstMem, srcMem, const_cast<Stream&>(param.stream)));
237 0 : }
238 : }
239 0 : HCCL_INFO("MidCountAllGather run success.");
240 0 : return HCCL_SUCCESS;
241 0 : }
242 : REGISTER_EXEC("AllGatherMidCountFor91093Executor", AllGatherMidCountFor91093, CollAllGatherMidCountFor91093Executor);
243 : } // namespace hccl
|