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 <ios>
12 : #include <iostream>
13 :
14 : #include "log.h"
15 :
16 : #include "ccu_rank_group.h"
17 : #include "ccu_ctx_creator_registry.h"
18 : #include "ccu_context_all_gather_nhr1d_mem2mem.h"
19 : #include "ccu_temp_all_gather_nhr_1D_mem2mem.h"
20 : #include "ccu_ins_group.h"
21 :
22 : namespace Hccl {
23 :
24 : static CcuInstRegister<CcuContextAllGatherNHR1D>
25 : g_registrarAllGatherMesh1DNHR(CcuInstType::CCU_ALLGATHER_NHR_1D_MEM2MEM);
26 :
27 0 : CcuTempAllGatherNHRMem2Mem1D::CcuTempAllGatherNHRMem2Mem1D(const RankId virtualRank, const u32 tempRankSize,
28 : const std::vector<std::vector<RankId>> &tempVTopo,
29 0 : const std::map<RankId, u32> &tempVirtRankMap)
30 0 : : CcuAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
31 : {
32 0 : }
33 :
34 0 : CcuTempAllGatherNHRMem2Mem1D::~CcuTempAllGatherNHRMem2Mem1D()
35 : {
36 0 : }
37 :
38 0 : u32 CcuTempAllGatherNHRMem2Mem1D::CalcScratchMultiple(BufferType inBuffType, BufferType outBuffType)
39 : {
40 : (void)inBuffType;
41 : (void)outBuffType;
42 0 : return 0;
43 : }
44 :
45 0 : HcclResult CcuTempAllGatherNHRMem2Mem1D::CalcRes(AlgTempResReq &tempResReq)
46 : {
47 0 : tempResReq.queNum = 1;
48 0 : tempResReq.streamNum = tempResReq.queNum;
49 0 : HCCL_DEBUG("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
50 0 : u32 linkNum = 1;
51 0 : linkNumBtwPeers_ = linkNum;
52 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
53 0 : return HcclResult::HCCL_SUCCESS;
54 : }
55 :
56 0 : uint64_t CcuTempAllGatherNHRMem2Mem1D::GetMaxSliceSize() const
57 : {
58 0 : return UB_MAX_DATA_SIZE;
59 : }
60 :
61 0 : uint32_t CcuTempAllGatherNHRMem2Mem1D::virtRankId2RankId(const uint32_t virtRankId)
62 : {
63 0 : for (auto iter = tempVirtRankMap_.begin(); iter != tempVirtRankMap_.end(); iter++) {
64 0 : if (iter->second == virtRankId) {
65 0 : return iter->first;
66 : }
67 : }
68 0 : return 0;
69 : }
70 :
71 0 : HcclResult CcuTempAllGatherNHRMem2Mem1D::GenExtIns(const TempFuncs &tempFuncs, TemplateDataParams &tempAlgParams,
72 : const ResLinks &tempLinks, std::vector<InsQuePtr> &tempInsQues)
73 : {
74 0 : HCCL_INFO("[CcuTempAllGatherNHRMem2Mem1D] Template Run start.");
75 0 : CHK_PRT_RET(tempInsQues.empty(),
76 : HCCL_ERROR("[CcuTempAllGatherNHRMem2Mem1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
77 0 : CHK_PTR_NULL(tempInsQues[0]);
78 0 : std::vector<uint64_t> dimSize;
79 0 : dimSize.push_back(tempRankSize_);
80 0 : opMode_ = tempFuncs.opMode;
81 0 : u32 rankIdx = tempVirtRankMap_[myRank_];
82 0 : uint32_t axisSize = tempLinks.begin()->second.size();
83 0 : uint64_t dataCount = (tempAlgParams.sliceSize / DataTypeSizeGet(dataType_));
84 0 : uint64_t die0Size = dataCount / axisSize * DataTypeSizeGet(dataType_);
85 0 : uint64_t die1Size = tempAlgParams.sliceSize - die0Size;
86 :
87 0 : uint64_t inputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.inBuffType) + tempAlgParams.buffInfo.inBuffBaseOff;
88 0 : uint64_t outputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.outBuffType) + tempAlgParams.buffInfo.outBuffBaseOff;
89 0 : uint64_t repeatNum = tempAlgParams.repeatNum;
90 0 : uint64_t inputSliceStride = tempAlgParams.inputSliceStride;
91 0 : uint64_t outputSliceStride = tempAlgParams.outputSliceStride;
92 0 : uint64_t inputRepeatStride = tempAlgParams.inputRepeatStride;
93 0 : uint64_t outputRepeatStride = tempAlgParams.outputRepeatStride;
94 : uint64_t token;
95 0 : CHK_RET(GetToken(op_, token));
96 0 : HCCL_INFO("[CcuTempAllGatherNHRMem2Mem1D] dimSize[%llu], die0Size[%llu], die1Size[%llu], inputAddr[%llu],"
97 : "outputAddr[%llu], repeatNum[%llu], inputSliceStride[%llu], outputSliceStride[%llu],"
98 : "inputRepeatStride[%llu], outputRepeatStride[%llu]",
99 : dimSize[0], die0Size, die1Size, inputAddr, outputAddr, repeatNum, inputSliceStride, outputSliceStride,
100 : inputRepeatStride, outputRepeatStride);
101 :
102 0 : if (dataCount == 0) {
103 0 : HCCL_INFO("[CcuTempAllGatherNHRMem2Mem1D] DataCount == 0, Template Run Ends.");
104 0 : return HCCL_SUCCESS;
105 : }
106 :
107 0 : if (axisSize > 1 || die1Size == 0) {
108 0 : axisSize = 1;
109 : }
110 :
111 0 : std::vector<LinkData> linksDie0;
112 0 : std::vector<LinkData> linksDie1;
113 0 : RankGroup allgatherRankGroup;
114 0 : std::map<u32, u32> indexMap;
115 0 : std::vector<NHRStepInfo> stepInfoVector;
116 0 : u32 nSteps = GetNHRStepNum(tempRankSize_);
117 0 : for (u32 step = 0; step < nSteps; step++) {
118 0 : NHRStepInfo stepInfo;
119 0 : CHK_RET(GetStepInfo(step, nSteps, stepInfo));
120 0 : stepInfoVector.push_back(stepInfo);
121 0 : if (indexMap.count(stepInfo.fromRank) == 0 && stepInfo.rxSliceIdxs.size() > 0) {
122 0 : u32 fromRankIdx = virtRankId2RankId(stepInfo.fromRank);
123 0 : indexMap[stepInfo.fromRank] = linksDie0.size();
124 0 : linksDie0.push_back(tempLinks.at(fromRankIdx)[0]);
125 0 : if (axisSize > 1) {
126 0 : linksDie1.push_back(tempLinks.at(fromRankIdx)[1]);
127 : }
128 0 : allgatherRankGroup.AddRank(fromRankIdx);
129 : }
130 0 : if (indexMap.count(stepInfo.toRank) == 0 && stepInfo.txSliceIdxs.size() > 0) {
131 0 : u32 toRankIdx = virtRankId2RankId(stepInfo.toRank);
132 0 : indexMap[stepInfo.toRank] = linksDie0.size();
133 0 : linksDie0.push_back(tempLinks.at(toRankIdx)[0]);
134 0 : if (axisSize > 1) {
135 0 : linksDie1.push_back(tempLinks.at(toRankIdx)[1]);
136 : }
137 0 : allgatherRankGroup.AddRank(toRankIdx);
138 : }
139 0 : }
140 0 : allgatherRankGroup.AddRank(myRank_);
141 :
142 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
143 0 : for (uint32_t axisId = 0; axisId < axisSize; axisId++) { // 2D算法,需要下发 2 条通信指令
144 0 : CcuInstructionAllGatherNHR1D ccuInstruction;
145 0 : uint64_t isInputOutputEqual = (inputAddr == outputAddr) ? 1 : 0;
146 0 : if ((axisId == 0 && die0Size == 0) || (axisId == 1 && die1Size == 0)) {
147 0 : continue;
148 : }
149 0 : ccuInstruction.Init(rankIdx, inputAddr, outputAddr, axisId, axisSize, die0Size, die1Size, repeatNum,
150 : inputSliceStride, outputSliceStride, inputRepeatStride, outputRepeatStride, stepInfoVector,
151 0 : indexMap, token, isInputOutputEqual, op_, tempVTopo_);
152 0 : ccuInstruction.SetLinks(axisId == 0 ? linksDie0 : linksDie1);
153 0 : ccuInstruction.SetRankGroup(allgatherRankGroup);
154 0 : ccuInstruction.SetCntCkeNum(5); // 每个transport用5个CKE
155 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionAllGatherNHR1D>(ccuInstruction)));
156 0 : }
157 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
158 0 : HCCL_INFO("[CcuTempAllGatherNHRMem2Mem1D] Template Run for all steps Ends.");
159 0 : return HcclResult::HCCL_SUCCESS;
160 0 : }
161 :
162 0 : HcclResult CcuTempAllGatherNHRMem2Mem1D::GetStepInfo(u32 step, u32 nSteps, NHRStepInfo &stepInfo)
163 : {
164 0 : u32 rankIdx = tempVirtRankMap_[myRank_];
165 0 : stepInfo.txSliceIdxs.clear();
166 0 : stepInfo.rxSliceIdxs.clear();
167 0 : stepInfo.step = step;
168 0 : stepInfo.myRank = rankIdx;
169 :
170 : // AllGatherNHR计算通信对象
171 0 : u32 deltaRank = 1 << (nSteps - 1 - step);
172 0 : u32 recvFrom = (rankIdx + tempRankSize_ - deltaRank) % tempRankSize_;
173 0 : u32 sendTo = (rankIdx + deltaRank) % tempRankSize_;
174 :
175 : // AllGatherNHR数据份数和数据编号增量
176 0 : u32 nSlices = (tempRankSize_ - 1 + (1 << (nSteps - 1 - step))) / (1 << (nSteps - step));
177 0 : u32 deltaSliceIndex = 1 << (nSteps - step);
178 0 : u32 txSliceIdx = rankIdx;
179 0 : u32 rxSliceIdx = (rankIdx - (1 << (nSteps - 1 - step)) + tempRankSize_) % tempRankSize_;
180 :
181 0 : stepInfo.nSlices = nSlices;
182 0 : stepInfo.toRank = sendTo;
183 0 : stepInfo.fromRank = recvFrom;
184 0 : for (u32 i = 0; i < nSlices; i++) {
185 0 : stepInfo.txSliceIdxs.push_back(txSliceIdx);
186 0 : stepInfo.rxSliceIdxs.push_back(rxSliceIdx);
187 0 : HCCL_DEBUG("[AllGatherNHR][GetStepInfo] i[%u] txSliceIdx[%u] rxSliceIdx[%u]", i, txSliceIdx, rxSliceIdx);
188 0 : txSliceIdx = (txSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
189 0 : rxSliceIdx = (rxSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
190 : }
191 0 : return HcclResult::HCCL_SUCCESS;
192 : }
193 : } // namespace Hccl
|