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_broadcast_mesh_aiv_for_910_93_executor.h"
12 :
13 : namespace hccl {
14 :
15 : constexpr u32 BUFFER_IDX_ONE = 1;
16 :
17 0 : CollBroadcastMeshAivFor91093Executor::CollBroadcastMeshAivFor91093Executor(
18 0 : const HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher>& topoMatcher)
19 0 : : CollBroadcastExecutor(dispatcher, topoMatcher)
20 : {
21 0 : DMAReduceFlag_ = false;
22 0 : desc_.isAivMode = true;
23 0 : desc_.isAivCrossNode = true;
24 0 : }
25 :
26 0 : HcclResult CollBroadcastMeshAivFor91093Executor::CalcStreamNum(u32& streamNum)
27 : {
28 0 : streamNum = 0; // AIV通信不需要申请从流
29 0 : HCCL_INFO("[CollBroadcastMeshAivFor91093Executor][CalcStreamNum] tag[%s] streamNum[%u].", tag_.c_str(), streamNum);
30 0 : return HCCL_SUCCESS;
31 : }
32 :
33 0 : HcclResult CollBroadcastMeshAivFor91093Executor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
34 : {
35 0 : TransportMemType inputType = TransportMemType::CCL_INPUT;
36 0 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) {
37 0 : inputType = TransportMemType::SCRATCH;
38 : }
39 0 : TransportMemType outputType = TransportMemType::AIV_OUTPUT;
40 :
41 0 : HCCL_INFO(
42 : "[CollBroadcastMeshAivFor91093Executor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d].",
43 : tag_.c_str(), inputType, outputType);
44 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
45 0 : return HCCL_SUCCESS;
46 : }
47 :
48 0 : HcclResult CollBroadcastMeshAivFor91093Executor::CalcLevel0CommInfo(
49 : TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
50 : {
51 0 : CommParaInfo commCombinePara(COMM_COMBINE_ORDER, CommType::COMM_TAG_MESH);
52 0 : commCombinePara.meshSinglePlane = true;
53 0 : CHK_RET(CalcCommPlaneInfo(tag_, commCombinePara, opTransport[COMM_COMBINE_ORDER], inputType, outputType));
54 :
55 0 : LevelNSubCommTransport& commTransportLevel0 = opTransport[COMM_COMBINE_ORDER];
56 0 : for (u32 subCommIndex = 0; subCommIndex < commTransportLevel0.size(); subCommIndex++) {
57 0 : for (auto& transportRequest : commTransportLevel0[subCommIndex].transportRequests) {
58 0 : transportRequest.isUsedRdma = false;
59 : }
60 : }
61 0 : return HCCL_SUCCESS;
62 0 : }
63 0 : void CollBroadcastMeshAivFor91093Executor::ParseParam(const OpParam& param)
64 : {
65 0 : tag_ = param.tag;
66 0 : totalSize_ = param.DataDes.count * SIZE_TABLE[param.DataDes.dataType];
67 0 : }
68 :
69 0 : HcclResult CollBroadcastMeshAivFor91093Executor::CalcScratchMemSize(u64& scratchMemSize)
70 : {
71 0 : scratchMemSize = 0;
72 0 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) {
73 0 : scratchMemSize = totalSize_;
74 : }
75 0 : HCCL_INFO("[%s]tag[%s] scratchMemSize[%llu]", __func__, tag_.c_str(), scratchMemSize);
76 0 : return HCCL_SUCCESS;
77 : }
78 :
79 0 : HcclResult CollBroadcastMeshAivFor91093Executor::CalNumBlocks(
80 : u32& numBlocks, u32 rankSize, [[maybe_unused]] u64 dataSize, [[maybe_unused]] HcclCMDType cmdType)
81 : {
82 : // Step1. 计算最佳块维度
83 0 : const u32 bestNumBlock = std::min(rankSize - 1, MAX_NUM_BLOCKS);
84 0 : const u32 minNumBlock = (rankSize + MAX_TARGET_NUM - 1) / MAX_TARGET_NUM;
85 :
86 : // 参数校验
87 0 : if (numBlocks_ < NUM_BLOCKS_FACTOR_TWO) {
88 0 : HCCL_ERROR(
89 : "[CollBroadcastMeshAivFor91093Executor][CalNumBlocks] aivCore[%u] is invalid, at least need 2", numBlocks_);
90 0 : return HCCL_E_PARA;
91 : }
92 :
93 : // 选择合适的块数量
94 0 : numBlocks = numBlocks_ < bestNumBlock ? numBlocks_ : bestNumBlock;
95 :
96 : // 检查最小块数量限制
97 0 : if (numBlocks_ < minNumBlock) {
98 0 : HCCL_ERROR(
99 : "[CollBroadcastMeshAivFor91093Executor][CalNumBlocks] limit[%u], at least[%u]", numBlocks_, minNumBlock);
100 0 : return HCCL_E_PARA;
101 : }
102 :
103 : // 记录日志
104 0 : HCCL_INFO(
105 : "[CollBroadcastMeshAivFor91093Executor][CalNumBlocks] numBlock is set to [%u], limit[%u], best[%u]", numBlocks,
106 : numBlocks_, bestNumBlock);
107 0 : return HCCL_SUCCESS;
108 : }
109 :
110 0 : HcclResult CollBroadcastMeshAivFor91093Executor::PrepareCommInfoToDevice(AlgResourceResponse& algResource)
111 : {
112 0 : HCCL_INFO("[CollBroadcastMeshAivFor91093Executor][PrepareCommInfoToDevice]Broadcast aiv copy comm info to device.");
113 0 : CHK_RET(CopyAivCommInfoToDevice(COMM_COMBINE_ORDER, COMM_INDEX_0, algResource));
114 0 : return HCCL_SUCCESS;
115 : }
116 :
117 0 : HcclResult CollBroadcastMeshAivFor91093Executor::CopyAivCommInfoToDevice(
118 : const CommPlane levelIndex, const u32 subLevelIndex, AlgResourceResponse& algResource)
119 : {
120 0 : algResResp_ = &algResource;
121 0 : CHK_RET(CheckCommSize(levelIndex, subLevelIndex + 1));
122 0 : SubCommInfo commInfo = GetSubCommInfo(levelIndex, subLevelIndex);
123 0 : u32 localRank = commInfo.localRank;
124 0 : u32 localRankSize = commInfo.localRankSize;
125 :
126 0 : void* buffersInOut[MAX_RANK_SIZE_A3 * 2] = {};
127 0 : bool isOpbaseMode = GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE;
128 :
129 0 : for (u32 i = 0; i < localRankSize; i++) {
130 0 : u32 idx = (i << 1);
131 0 : if (i != localRank) {
132 0 : CHK_RET(commInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(buffersInOut[idx])));
133 0 : CHK_RET(commInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(buffersInOut[idx + 1])));
134 : } else {
135 0 : buffersInOut[idx] = isOpbaseMode ? algResource.cclInputMem.ptr() : algResource.scratchMem.ptr();
136 0 : buffersInOut[idx + 1] = algResource.aivOutputMem.ptr();
137 : }
138 : }
139 0 : constexpr u32 bufferNum = 2;
140 0 : CHK_RET(hrtMemSyncCopy(
141 : algResource.aivCommInfoMem.ptr(), sizeof(u64) * localRankSize * bufferNum, buffersInOut,
142 : sizeof(u64) * localRankSize * bufferNum, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
143 0 : return HCCL_SUCCESS;
144 0 : }
145 :
146 0 : HcclResult CollBroadcastMeshAivFor91093Executor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
147 : {
148 0 : HcclUs startut = TIME_NOW();
149 0 : tag_ = param.tag;
150 0 : algResResp_ = &algRes;
151 :
152 0 : ExecMem execMem;
153 0 : execMem.count = param.DataDes.count;
154 0 : execMem.inputPtr = param.inputPtr;
155 0 : execMem.outputPtr = param.inputPtr; // broadcast使用一块内存
156 :
157 : execMem.inputMem
158 0 : = (workflowMode_ != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE ? algRes.scratchMem : algRes.cclInputMem);
159 0 : execMem.outputMem = algRes.aivOutputMem;
160 :
161 0 : HcclResult ret = KernelRun(param, execMem);
162 :
163 0 : CHK_PRT_RET(
164 : ret != HCCL_SUCCESS,
165 : HCCL_ERROR(
166 : "[CollBroadcastMeshAivFor91093Executor][Orchestrate]errNo[0x%016llx] tag[%s] excutor kernel run failed",
167 : HCCL_ERROR_CODE(ret), param.tag.c_str()),
168 : ret);
169 :
170 0 : HCCL_INFO(
171 : "tag[%s], Broadcast executor orchestrate success, take time [%lld]us", param.tag.c_str(),
172 : DURATION_US(TIME_NOW() - startut));
173 0 : return HCCL_SUCCESS;
174 0 : }
175 :
176 0 : HcclResult CollBroadcastMeshAivFor91093Executor::KernelRun(const OpParam& param, ExecMem& execMem)
177 : {
178 0 : HCCL_INFO("[CollBroadcastMeshAivFor91093Executor][KernelRun]broadcast aiv enter.");
179 :
180 0 : CHK_RET(CheckCommSize(COMM_COMBINE_ORDER, COMM_INDEX_0 + 1));
181 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_COMBINE_ORDER, COMM_INDEX_0);
182 :
183 : void* buffersIn[MAX_RANK_SIZE];
184 : void* buffersOut[MAX_RANK_SIZE];
185 :
186 0 : u32 rootRank = 0;
187 0 : HcclResult ret = GetRankByUserRank(COMM_COMBINE_ORDER, COMM_INDEX_0, param.root, rootRank);
188 0 : CHK_PRT_RET(
189 : ret != HCCL_SUCCESS,
190 : HCCL_ERROR(
191 : "[BroadCastOperator][CollBroadcastMeshAivFor91093Executor]invalid root[%u] to get userrank", param.root),
192 : ret);
193 :
194 0 : u32 localRank = level0CommInfo.localRank;
195 0 : u32 localRankSize = level0CommInfo.localRankSize;
196 0 : HCCL_DEBUG(
197 : "[CollBroadcastMeshAivFor91093Executor][KernelRun] userRank [%u] localRank [%u]", topoAttr_.userRank,
198 : localRank);
199 :
200 0 : buffersIn[0] = execMem.inputMem.ptr();
201 0 : buffersOut[0] = execMem.outputMem.ptr();
202 0 : buffersOut[BUFFER_IDX_ONE] = algResResp_->aivCommInfoMem.ptr();
203 :
204 0 : bool isOpbase = (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
205 0 : AivOpArgs opArgs{HcclCMDType::HCCL_CMD_BROADCAST, execMem.inputPtr, execMem.outputPtr, execMem.count,
206 0 : param.DataDes.dataType, param.reduceType, rootRank, isOpbase};
207 : AivTopoArgs topoArgs{localRank, localRankSize, MAX_RANK_SIZE, 0,
208 0 : topoAttr_.serverNum, topoAttr_.deviceType, algoAttr_.identifier};
209 :
210 : u32 numBlocks;
211 0 : CHK_RET(CalNumBlocks(numBlocks, localRankSize));
212 0 : numBlocks_ = numBlocks;
213 :
214 0 : AivResourceArgs resourceArgs{param.tag, param.stream.ptr(), buffersIn, buffersOut, execMem.inputMem.size(),
215 0 : numBlocks_, param.aivTag};
216 0 : AivAlgArgs algArgs{};
217 0 : algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
218 0 : algArgs.execTimeOutSet = true;
219 0 : algArgs.argsType = KernelArgsType::ARGS_TYPE_SIMPLE;
220 :
221 0 : struct AivProfilingInfo aivProfilingInfo;
222 0 : aivProfilingInfo.counter = opCounter_;
223 0 : ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo);
224 0 : CHK_PRT_RET(
225 : ret != HCCL_SUCCESS,
226 : HCCL_ERROR("[CollBroadcastMeshAivFor91093Executor][KernelRun]broadcast aiv failed, return[%d]", ret), ret);
227 :
228 0 : ExtraArgs extraArgs;
229 0 : CHK_RET(SetOpCache(opArgs, topoArgs, resourceArgs, algArgs, extraArgs, aivProfilingInfo, true));
230 :
231 0 : HCCL_INFO("[CollBroadcastMeshAivFor91093Executor][KernelRun]broadcast aiv run success.");
232 0 : return HCCL_SUCCESS;
233 0 : }
234 :
235 : REGISTER_EXEC("BroadcastMeshAivFor91093Executor", BroadcastMeshAivFor91093, CollBroadcastMeshAivFor91093Executor);
236 :
237 : } // namespace hccl
|