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