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 "aiv_mc2_compont.h"
12 : #include "hccl_aiv_utils.h"
13 : #include <algorithm>
14 : #include <unordered_set>
15 : #include "coll_service_device_mode.h"
16 : #include "aiv_ins.h"
17 : #include "local_rma_buf_manager.h"
18 :
19 : namespace Hccl {
20 :
21 : constexpr u32 AIV_FAKE_OP_TPYE = 1;
22 :
23 245 : AivMc2Compont::~AivMc2Compont() {}
24 :
25 6 : void AivMc2Compont::AllocCommResource(void* mc2Tiling, void** commContext)
26 : {
27 6 : if (mc2Tiling == nullptr) {
28 3 : HCCL_ERROR("[AivMc2Compont::%s] mc2Tiling is nullptr", __func__);
29 1 : return;
30 : }
31 5 : auto tilingVersion = *static_cast<uint32_t*>(mc2Tiling);
32 15 : HCCL_INFO("[AivMc2Compont:%s] Tiling version [%u]", __func__, tilingVersion);
33 5 : if (tilingVersion != UNKNOWN_TILING_V1 && tilingVersion != UNKNOWN_TILING_V2) {
34 4 : THROW<NotSupportException>(StringFormat("Tiling version not support, version[%u]", tilingVersion));
35 : }
36 :
37 3 : if (comm->GetRankSize() == 1) {
38 1 : THROW<NotSupportException>(StringFormat("Comm[%s] rank size is 1, Mc2 not support", comm->GetId().c_str()));
39 : }
40 :
41 2 : if (tilingVersion == UNKNOWN_TILING_V1) {
42 1 : AivMC2AllocCommRes(reinterpret_cast<Mc2Tiling*>(mc2Tiling));
43 : } else {
44 1 : AivMC2AllocCommResV2(reinterpret_cast<Mc2InitTilingInner*>(mc2Tiling));
45 : }
46 :
47 2 : GenerateCommContext(commContext);
48 : }
49 :
50 3 : void AddCclBuffer(HcclCombinOpParam& combinOpParam, uint64_t bufferSize, uint64_t bufferAddr, RankId rankId)
51 : {
52 3 : combinOpParam.winSize = bufferSize;
53 3 : auto winInSize = bufferSize / 2;
54 3 : combinOpParam.windowsIn[rankId] = bufferAddr;
55 3 : combinOpParam.windowsOut[rankId] = bufferAddr + winInSize;
56 :
57 9 : HCCL_RUN_INFO(
58 : "hcclCombinOpParam info: bufferSize = [%llu], windowsIn[%d] = [%llu], windowsOut[%d] = [%llu]",
59 : combinOpParam.winSize, rankId, combinOpParam.windowsIn[rankId], rankId, combinOpParam.windowsOut[rankId]);
60 3 : }
61 :
62 6 : void AivMc2Compont::GenerateCommContext(void** commContext)
63 : {
64 6 : if (combinOpParamBuffer != nullptr) {
65 1 : *commContext = reinterpret_cast<void*>(combinOpParamBuffer->GetAddr());
66 1 : return;
67 : }
68 :
69 5 : workspaceBuffer = std::make_shared<DevBuffer>(MC2_WORKSPACE_SIZE);
70 :
71 5 : HcclCombinOpParam combinOpParam{};
72 5 : combinOpParam.workSpace = static_cast<uint64_t>(workspaceBuffer->GetAddr());
73 5 : combinOpParam.workSpaceSize = MC2_WORKSPACE_SIZE;
74 5 : combinOpParam.rankId = comm->GetMyRank();
75 5 : combinOpParam.rankDim = comm->GetRankSize();
76 :
77 15 : HCCL_RUN_INFO(
78 : "hcclCombinOpParam info: workSpace = [%llu], rankId = [%u], rankDim = [%u]", combinOpParam.workSpace,
79 : combinOpParam.rankId, combinOpParam.rankDim);
80 :
81 5 : auto collService = dynamic_cast<CollServiceDeviceMode*>(comm->GetCollService());
82 5 : auto protocol = collService->GetAivInsPreprocessor()->GetProtocol();
83 5 : if (protocol == 1) { // urma
84 1 : GenerateAivUrmaCommContext(combinOpParam);
85 4 : } else if (protocol == 0) { // ubmemory
86 4 : GenerateAivMemoryCommContext(combinOpParam);
87 : } else {
88 0 : THROW<InvalidParamsException>(StringFormat("protocol[%u] not supported", protocol));
89 : }
90 3 : auto paramSize = sizeof(HcclCombinOpParam);
91 3 : combinOpParamBuffer = std::make_shared<DevBuffer>(paramSize);
92 6 : HrtMemcpy(
93 3 : reinterpret_cast<void*>(combinOpParamBuffer->GetAddr()), paramSize, static_cast<void*>(&combinOpParam),
94 : paramSize, RT_MEMCPY_HOST_TO_DEVICE);
95 3 : *commContext = reinterpret_cast<void*>(combinOpParamBuffer->GetAddr());
96 : }
97 :
98 4 : void AivMc2Compont::GenerateAivMemoryCommContext(HcclCombinOpParam& combinOpParam)
99 : {
100 12 : HCCL_INFO("[AivMc2Compont][GenerateAivMemoryCommContext] ubmemory");
101 : // add cclbuffer info
102 4 : if (comm->GetCclBuffer() == nullptr) {
103 2 : THROW<Hccl::InternalException>(StringFormat("Cannot get CCL Buffer to fill window!"));
104 : }
105 :
106 : // winIn winOut 放到 commContext
107 3 : uint64_t commCclBufferSize = static_cast<uint64_t>(comm->GetCclBuffer()->GetSize());
108 3 : uint64_t commCclBufferAddr = static_cast<uint64_t>(comm->GetCclBuffer()->GetAddr());
109 3 : AddCclBuffer(combinOpParam, commCclBufferSize, commCclBufferAddr, comm->GetMyRank());
110 :
111 : // 获取ubmemoryTranMgr,遍历所有ubmemoryTran,拿到CclBuffer,一分为2,放到windowsIn、windowsOut
112 3 : auto rankId2RmtIpcRmaBufList = comm->GetUbMemoryTransportMgr()->GetRmtRankId2RmtIpcRmaBufList();
113 3 : for (auto& rankId2RmtIpcRmaBuf : rankId2RmtIpcRmaBufList) {
114 0 : auto rmtRank = rankId2RmtIpcRmaBuf.first;
115 0 : auto rmtMemBuffer = rankId2RmtIpcRmaBuf.second;
116 0 : uint64_t rmtBufferSize = static_cast<uint64_t>(rmtMemBuffer->GetSize());
117 0 : uint64_t rmtBufferAddr = static_cast<uint64_t>(rmtMemBuffer->GetAddr());
118 0 : AddCclBuffer(combinOpParam, rmtBufferSize, rmtBufferAddr, rmtRank);
119 : }
120 3 : }
121 :
122 1 : void AivMc2Compont::GenerateAivUrmaCommContext(HcclCombinOpParam& combinOpParam) const
123 : {
124 3 : HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext] aiv urma");
125 1 : auto collService = dynamic_cast<CollServiceDeviceMode*>(comm->GetCollService());
126 1 : auto links = comm->GetFullMeshLinks();
127 1 : if (links.empty()) {
128 2 : THROW<InvalidParamsException>(StringFormat("AivMc2Compont::GenerateAivUrmaCommContext links is empty"));
129 : }
130 0 : auto localBuffer = comm->GetLocalRmaBufManager().Get(
131 0 : GetAivUrmaBufferTag(comm->GetId()), links[0].GetLocalPort(), BufferType::SCRATCH);
132 0 : CHECK_NULLPTR(localBuffer, "[AivMc2Compont::GenerateAivUrmaCommContext] localBuffer is nullptr!");
133 0 : uint64_t localBufferSize = static_cast<uint64_t>(localBuffer->GetBuf()->GetSize());
134 0 : uint64_t localBufferAddr = static_cast<uint64_t>(localBuffer->GetBuf()->GetAddr());
135 0 : AddCclBuffer(combinOpParam, localBufferSize, localBufferAddr, comm->GetMyRank());
136 0 : for (auto& link : links) {
137 0 : auto rmtBuffer = comm->GetMemTransportManager()->GetUrmaDirectTransport(link)->GetRmtRmaBuffer(2);
138 0 : CHECK_NULLPTR(rmtBuffer, "[AivMc2Compont::GenerateAivUrmaCommContext] rmtBuffer is nullptr!");
139 0 : uint64_t rmtBufferSize = static_cast<uint64_t>(rmtBuffer->GetSize());
140 0 : uint64_t rmtBufferAddr = static_cast<uint64_t>(rmtBuffer->GetAddr());
141 0 : AddCclBuffer(combinOpParam, rmtBufferSize, rmtBufferAddr, link.GetRemoteRankId());
142 : }
143 :
144 0 : auto wqs = collService->GetAivInsPreprocessor()->GetWqs();
145 0 : for (size_t i = 0; i < wqs.size(); i++) {
146 0 : combinOpParam.wq[links[i].GetRemoteRankId()] = wqs[i];
147 : }
148 0 : HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext] wq info:");
149 0 : for (auto& wq : wqs) {
150 0 : HCCL_INFO(
151 : "[AivMc2Compont][GenerateAivUrmaCommContext]jettyId[%u], sqVA[%llx], wqeSize[%u], sqDepth[%u], "
152 : "headAddr[%llx], tailAddr[%llx], dbAddr[%llx], tp_id[%u]",
153 : wq.jettyId, wq.sqVA, wq.wqeSize, wq.sqDepth, wq.headAddr, wq.tailAddr, wq.dbAddr, wq.tp_id);
154 0 : for (size_t i = 0; i < 16; i++) { // 遍历rmtEid
155 0 : HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext]rmtEid[%llu][%u]", i, wq.rmtEid[i]);
156 : }
157 0 : HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext]rmtObjId[%u]", wq.rmtObjId);
158 : }
159 :
160 0 : auto cqs = collService->GetAivInsPreprocessor()->GetCqs();
161 0 : for (size_t i = 0; i < cqs.size(); i++) {
162 0 : combinOpParam.cq[links[i].GetRemoteRankId()] = cqs[i];
163 : }
164 0 : HCCL_INFO("[AivMc2Compont][GenerateAivUrmaCommContext] cq info:");
165 0 : for (auto& cq : cqs) {
166 0 : HCCL_INFO(
167 : "[AivMc2Compont][GenerateAivUrmaCommContext]jfcId[%u], cqVA[%llx], cqeSize[%u], cqDepth[%u], "
168 : "headAddr[%llx], "
169 : "tailAddr[%llx], dbAddr[%llx]",
170 : cq.jfcId, cq.cqVA, cq.cqeSize, cq.cqDepth, cq.headAddr, cq.tailAddr, cq.dbAddr);
171 : }
172 1 : }
173 :
174 1 : void AivMc2Compont::AivMC2AllocCommRes(Mc2Tiling* mc2TilingPtr) const
175 : {
176 3 : HCCL_INFO("AivMC2AllocCommRes start");
177 1 : auto insQueue = make_shared<InsQueue>();
178 :
179 1 : auto collService = dynamic_cast<CollServiceDeviceMode*>(comm->GetCollService());
180 1 : auto aivLinks = comm->GetFullMeshLinks();
181 :
182 1 : for (auto& link : aivLinks) {
183 0 : HCCL_INFO("[AivMc2Compont][AivMC2AllocCommRes] aivLink[%s]", link.Describe().c_str());
184 : }
185 :
186 1 : Mc2CommConfig* commConfigPtr = reinterpret_cast<Mc2CommConfig*>(
187 : reinterpret_cast<uint8_t*>(mc2TilingPtr) + sizeof(uint32_t) + sizeof(uint32_t) + sizeof(Mc2ServerCfg));
188 1 : const auto& commConfig = *(commConfigPtr);
189 1 : FillCollOperator(commConfig);
190 :
191 1 : AivOpArgs aivArgs{};
192 :
193 1 : std::unique_ptr<Instruction> aivIns = std::make_unique<AivInstruction>(aivLinks, aivArgs);
194 :
195 1 : insQueue->Append(std::move(aivIns));
196 :
197 1 : comm->GetSocketManager().BatchCreateSockets(aivLinks);
198 :
199 : // 对insQueue中ccuIns进行预处理 创建transport
200 1 : collService->GetAivInsPreprocessor()->Preprocess(insQueue);
201 3 : HCCL_INFO("AivMC2AllocCommRes success");
202 1 : }
203 :
204 1 : void AivMc2Compont::AivMC2AllocCommResV2(Mc2InitTilingInner* mc2TilingPtr) const
205 : {
206 3 : HCCL_INFO("AivMC2AllocCommRes start");
207 1 : auto insQueue = make_shared<InsQueue>();
208 :
209 1 : auto collService = dynamic_cast<CollServiceDeviceMode*>(comm->GetCollService());
210 1 : if (collService == nullptr) {
211 0 : HCCL_ERROR("[AivMc2Compont::%s] dynamic_cast<CollServiceDeviceMode*> failed", __func__);
212 0 : return;
213 : }
214 1 : auto aivLinks = comm->GetFullMeshLinks();
215 :
216 1 : for (auto& link : aivLinks) {
217 0 : HCCL_INFO("[AivMc2Compont][AivMC2AllocCommRes] aivLink[%s]", link.Describe().c_str());
218 : }
219 :
220 1 : const auto offset = mc2TilingPtr->offset[0];
221 1 : const auto& commConfig
222 1 : = *(reinterpret_cast<const Mc2CcTilingInner*>(reinterpret_cast<const uint8_t*>(mc2TilingPtr) + offset));
223 3 : HCCL_RUN_INFO(
224 : "[AivMc2Compont][AivMC2AllocCommRes] Mc2CcTilingInner skipLocalRankCopy[%u], skipBufferWindowCopy[%u], "
225 : "stepSize[%u], version[%u], protocol[%u], communicationEngine[%u], srcDataType[%u], dstDataType[%u], "
226 : "algConfig[%s], opType[%u], reduceType[%u]",
227 : commConfig.skipLocalRankCopy, commConfig.skipBufferWindowCopy, commConfig.stepSize, commConfig.version,
228 : commConfig.protocol, commConfig.communicationEngine, commConfig.srcDataType, commConfig.dstDataType,
229 : commConfig.algConfig, commConfig.opType, commConfig.reduceType);
230 3 : HCCL_RUN_INFO("[AivMc2Compont][AivMC2AllocCommRes] groupName[%s]", commConfig.groupName);
231 1 : FillCollOperatorV2(commConfig);
232 :
233 1 : AivOpArgs aivArgs{};
234 :
235 1 : std::unique_ptr<Instruction> aivIns = std::make_unique<AivInstruction>(aivLinks, aivArgs);
236 :
237 1 : insQueue->Append(std::move(aivIns));
238 :
239 1 : comm->GetSocketManager().BatchCreateSockets(aivLinks);
240 :
241 : // 对insQueue中ccuIns进行预处理 创建transport
242 1 : collService->GetAivInsPreprocessor()->SetProtocol(commConfig.protocol);
243 1 : collService->GetAivInsPreprocessor()->Preprocess(insQueue);
244 3 : HCCL_INFO("AivMC2AllocCommResV2 success");
245 1 : }
246 :
247 : // 为保证后续流程统一,构造一个虚拟算子
248 1 : void AivMc2Compont::FillCollOperator(const Mc2CommConfig& config) const
249 : {
250 1 : uint32_t dataCount = 1024;
251 1 : CollOpParams opParams;
252 1 : opParams.opType = MC2OpType(static_cast<AicpuComType>(config.opType));
253 1 : opParams.reduceOp = MC2ReduceType(static_cast<HcclReduceOp>(config.reduceType));
254 1 : opParams.dataType = MC2DataType(static_cast<HcclDataType>(config.dataType));
255 1 : opParams.outputDataType = MC2DataType(static_cast<HcclDataType>(config.outputDataType));
256 1 : opParams.count = dataCount;
257 1 : std::string opTag = comm->GetId();
258 1 : comm->CovertToCurrentCollOperator(opTag, opParams, OpMode::OPBASE);
259 1 : }
260 :
261 1 : void AivMc2Compont::FillCollOperatorV2(const Mc2CcTilingInner& config) const
262 : {
263 1 : uint32_t dataCount = 1024;
264 1 : CollOpParams opParams;
265 1 : opParams.opType = MC2OpType(static_cast<AicpuComType>(AIV_FAKE_OP_TPYE));
266 1 : opParams.reduceOp = MC2ReduceType(static_cast<HcclReduceOp>(config.reduceType));
267 1 : opParams.dataType = MC2DataType(static_cast<HcclDataType>(config.srcDataType));
268 1 : opParams.outputDataType = MC2DataType(static_cast<HcclDataType>(config.dstDataType));
269 1 : opParams.count = dataCount;
270 1 : std::string opTag = comm->GetId();
271 :
272 1 : comm->CovertToCurrentCollOperator(opTag, opParams, OpMode::OPBASE);
273 1 : }
274 :
275 : } // namespace Hccl
|