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