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 :
12 : #include "coll_all_to_all_v_continuous_pipeline_executor.h"
13 :
14 : namespace hccl {
15 :
16 0 : CollAlltoAllVContinuousPipeline::CollAlltoAllVContinuousPipeline(const HcclDispatcher dispatcher,
17 0 : std::unique_ptr<TopoMatcher> &topoMatcher)
18 0 : : CollAlltoAllExecutor(dispatcher, topoMatcher)
19 : {
20 0 : }
21 :
22 : template<typename T>
23 0 : static void PrintListHelper(const T* listPtr, const size_t length, const std::string &prefix) {
24 0 : std::ostringstream oss;
25 0 : oss << "[ ";
26 0 : for (size_t i = 0; i < length; ++i) {
27 0 : oss << listPtr[i] << " ";
28 : }
29 0 : oss << "]";
30 0 : HCCL_DEBUG("%s: %s", prefix.c_str(), oss.str().c_str());
31 0 : }
32 :
33 0 : static void PrintCountsAndDispls(const u32 length, const OpParam ¶m)
34 : {
35 : // 打印counts和displs
36 0 : const u64 *sendCountsPtr = static_cast<const u64 *>(param.All2AllDataDes.sendCounts);
37 0 : const u64 *sendDisplsPtr = static_cast<const u64 *>(param.All2AllDataDes.sdispls);
38 :
39 0 : PrintListHelper(sendCountsPtr, length, "[PrintCountsAndDispls] sendCounts");
40 0 : PrintListHelper(sendDisplsPtr, length, "[PrintCountsAndDispls] sendDispls");
41 0 : if (param.All2AllDataDes.recvCounts != nullptr) {
42 0 : const u64 *recvCountsPtr = static_cast<const u64 *>(param.All2AllDataDes.recvCounts);
43 0 : const u64 *recvDisplsPtr = static_cast<const u64 *>(param.All2AllDataDes.rdispls);
44 0 : PrintListHelper(recvCountsPtr, length, "[PrintCountsAndDispls] recvCounts");
45 0 : PrintListHelper(recvDisplsPtr, length, "[PrintCountsAndDispls] recvDispls");
46 : }
47 0 : }
48 :
49 0 : HcclResult CollAlltoAllVContinuousPipeline::CalcStreamNum(u32& streamNum)
50 : {
51 0 : const u32 level0StreamNum = topoAttr_.deviceNumPerAggregation - 1;
52 0 : const u32 level1StreamNum = 1; // 先固定1条
53 :
54 0 : streamNum = level0StreamNum + level1StreamNum;
55 0 : HCCL_INFO("[CollAlltoAllVContinuousPipeline]tag[%s] level0StreamNum[%u], level1StreamNum[%u], streamNum[%u]",
56 : tag_.c_str(), level0StreamNum, level1StreamNum, streamNum);
57 0 : return HCCL_SUCCESS;
58 : }
59 :
60 0 : HcclResult CollAlltoAllVContinuousPipeline::CalcTransportMemType(
61 : TransportMemType &inputType, TransportMemType &outputType)
62 : {
63 0 : inputType = TransportMemType::CCL_INPUT;
64 0 : outputType = TransportMemType::CCL_OUTPUT;
65 :
66 0 : HCCL_INFO("[CollAlltoAllVContinuousPipeline][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d]",
67 : tag_.c_str(), inputType, outputType);
68 0 : return HCCL_SUCCESS;
69 : }
70 :
71 0 : HcclResult CollAlltoAllVContinuousPipeline::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
72 : {
73 0 : TransportMemType inputType = TransportMemType::RESERVED;
74 0 : TransportMemType outputType = TransportMemType::RESERVED;
75 :
76 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
77 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
78 0 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
79 0 : return HCCL_SUCCESS;
80 : }
81 :
82 0 : HcclResult CollAlltoAllVContinuousPipeline::CalcLevel0CommInfo(TransportMemType inputType,
83 : TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
84 : {
85 : // level0 - Mesh建链
86 0 : CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_MESH);
87 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
88 0 : return HCCL_SUCCESS;
89 0 : }
90 :
91 0 : HcclResult CollAlltoAllVContinuousPipeline::CalcLevel1CommInfo(TransportMemType inputType,
92 : TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
93 : {
94 : // level1 - Mesh建链
95 0 : CommParaInfo commParaLevel1(COMM_LEVEL1, CommType::COMM_TAG_MESH);
96 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel1, opTransport[COMM_LEVEL1], inputType, outputType));
97 0 : return HCCL_SUCCESS;
98 0 : }
99 :
100 0 : HcclResult CollAlltoAllVContinuousPipeline::FillLocalSendRecvInfo(const OpParam ¶m, SendRecvInfo &info)
101 : {
102 0 : const bool hasRecvInfo = param.All2AllDataDes.recvCounts != nullptr;
103 0 : const u32 userRankSize = topoAttr_.userRankSize;
104 :
105 0 : info.sendCounts.resize(userRankSize);
106 0 : info.sendDispls.resize(userRankSize);
107 :
108 0 : if (hasRecvInfo) {
109 0 : info.recvCounts.resize(userRankSize);
110 0 : info.recvDispls.resize(userRankSize);
111 : }
112 :
113 0 : for (u32 i = 0; i < userRankSize; ++i) {
114 0 : info.sendCounts[i] = *(static_cast<const u64 *>(param.All2AllDataDes.sendCounts) + i);
115 0 : info.sendDispls[i] = *(static_cast<const u64 *>(param.All2AllDataDes.sdispls) + i);
116 :
117 0 : if (hasRecvInfo) {
118 0 : info.recvCounts[i] = *(static_cast<const u64 *>(param.All2AllDataDes.recvCounts) + i);
119 0 : info.recvDispls[i] = *(static_cast<const u64 *>(param.All2AllDataDes.rdispls) + i);
120 : }
121 : }
122 :
123 0 : if (UNLIKELY(HcclCheckLogLevel(DLOG_DEBUG))) {
124 0 : PrintCountsAndDispls(userRankSize, param);
125 : }
126 :
127 0 : return HCCL_SUCCESS;
128 : }
129 :
130 0 : HcclResult CollAlltoAllVContinuousPipeline::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
131 : {
132 0 : HcclUs startut = TIME_NOW();
133 0 : HcclResult ret = HCCL_SUCCESS;
134 0 : tag_ = param.tag;
135 0 : algResResp_ = &algRes;
136 0 : AlltoAllVParam_ = param;
137 :
138 0 : ExecMem execMem;
139 0 : execMem.count = 0;
140 0 : execMem.inputPtr = param.inputPtr;
141 0 : execMem.outputPtr = param.outputPtr;
142 0 : execMem.inputMem = algRes.cclInputMem;
143 0 : execMem.outputMem = algRes.cclOutputMem;
144 0 : ret = KernelRun(param, execMem);
145 :
146 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
147 : HCCL_ERROR("[CollAlltoAllVContinuousPipeline][Orchestrate]errNo[0x%016llx]executor run failed",
148 : HCCL_ERROR_CODE(ret)), ret);
149 :
150 0 : HCCL_INFO("tag[%s], CollAlltoAllVContinuousPipeline tempAlg orchestrate success, take time [%lld]us.",
151 : param.tag.c_str(), DURATION_US(TIME_NOW() - startut));
152 0 : return HCCL_SUCCESS;
153 0 : }
154 :
155 0 : HcclResult CollAlltoAllVContinuousPipeline::KernelRun(const OpParam ¶m, ExecMem &execMem)
156 : {
157 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[CollAlltoAllVContinuousPipeline][KernelRun] AllToAllV npu direct start.");
158 :
159 : // 获取通信域
160 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
161 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
162 :
163 0 : const u32 commIndex = level0CommInfo.localRank;
164 0 : CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
165 0 : SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
166 :
167 : // 执行
168 0 : std::unique_ptr<AlgTemplateBase> tempAlg = AlgTemplateRegistry::Instance().GetAlgTemplate(
169 0 : TemplateType::TEMPLATE_ALL_2_ALL_V_CONTINUOUS_PIPELINE, dispatcher_);
170 :
171 0 : CHK_SMART_PTR_NULL(tempAlg);
172 :
173 0 : SendRecvInfo sendRecvInfo;
174 0 : CHK_RET(FillLocalSendRecvInfo(param, sendRecvInfo));
175 :
176 0 : A2aPipelineMemory a2aPipelineMemory;
177 0 : a2aPipelineMemory.userInput = algResResp_->paramInputMem;
178 0 : a2aPipelineMemory.userOutput = algResResp_->paramOutputMem;
179 0 : a2aPipelineMemory.cclInBuffer = execMem.inputMem;
180 0 : a2aPipelineMemory.cclOutBuffer = execMem.outputMem;
181 :
182 0 : HCCL_INFO("[CollAlltoAllVContinuousPipeline] Memory info[addr, size]: userInput[%p, %llu], userOutput[%p, %llu], "
183 : "cclInBuffer[%p, %llu] ,cclOutBuffer[%p, %llu].",
184 : a2aPipelineMemory.userInput.ptr(), a2aPipelineMemory.userInput.size(),
185 : a2aPipelineMemory.userOutput.ptr(), a2aPipelineMemory.userOutput.size(),
186 : a2aPipelineMemory.cclInBuffer.ptr(), a2aPipelineMemory.cclInBuffer.size(),
187 : a2aPipelineMemory.cclOutBuffer.ptr(), a2aPipelineMemory.cclOutBuffer.size()
188 : );
189 :
190 : #ifndef OPEN_HCCL_TEST
191 0 : std::vector<SendRecvInfo> sendRecvInfoList{sendRecvInfo};
192 : #else
193 : // 适配算法检查器,传入全局的info
194 : std::vector<SendRecvInfo> sendRecvInfoList = allMeshAggregationSendRecvInfo_;
195 : #endif
196 :
197 0 : CHK_RET(tempAlg->Prepare(topoAttr_.userRank,
198 : a2aPipelineMemory,
199 : level0CommInfo,
200 : level1CommInfo,
201 : param.stream,
202 : algResResp_->slaveStreams,
203 : algResResp_->notifiesMain,
204 : algResResp_->notifiesAux,
205 : sendRecvInfoList,
206 : param.All2AllDataDes.sendType,
207 : workflowMode_));
208 :
209 0 : CHK_RET(tempAlg->RunAsync());
210 :
211 0 : HCCL_INFO("[CollAlltoAllVContinuousPipeline] executor run success.");
212 0 : return HCCL_SUCCESS;
213 0 : }
214 :
215 : REGISTER_EXEC("RunAlltoAllVContinuousPipeline", AlltoAllVContinuousPipeline, CollAlltoAllVContinuousPipeline);
216 : } // namespace hccl
|