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 : #include "log.h"
14 : #include "ccu_rank_group.h"
15 : #include "ccu_ctx_creator_registry.h"
16 : #include "ccu_context_scatter_nhr1d_mem2mem.h"
17 : #include "ccu_temp_scatter_nhr_1D_mem2mem.h"
18 : #include "ccu_ins_group.h"
19 :
20 : namespace Hccl {
21 :
22 : static CcuInstRegister<CcuContextScatterNHR1DMem2Mem> g_registrarScatter(CcuInstType::CCU_SCATTER_NHR_1D_MEM2MEM);
23 :
24 0 : CcuTempScatterNHRMem2Mem1D::CcuTempScatterNHRMem2Mem1D(const RankId virtualRank, const u32 tempRankSize,
25 : const std::vector<std::vector<RankId>> &tempVTopo,
26 0 : const std::map<RankId, u32> &tempVirtRankMap)
27 0 : : CcuAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
28 : {
29 0 : }
30 :
31 0 : CcuTempScatterNHRMem2Mem1D::~CcuTempScatterNHRMem2Mem1D()
32 : {
33 0 : }
34 :
35 0 : HcclResult CcuTempScatterNHRMem2Mem1D::CalcRes(AlgTempResReq &tempResReq)
36 : {
37 0 : tempResReq.queNum = 1;
38 0 : tempResReq.streamNum = tempResReq.queNum;
39 0 : HCCL_INFO("[CalcRes] tempResReq.queNum[%u]", tempResReq.queNum);
40 0 : u32 linkNum = 1;
41 0 : linkNumBtwPeers_ = linkNum;
42 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
43 0 : return HcclResult::HCCL_SUCCESS;
44 : }
45 :
46 0 : uint64_t CcuTempScatterNHRMem2Mem1D::GetMaxSliceSize() const
47 : {
48 0 : return UB_MAX_DATA_SIZE;
49 : }
50 :
51 0 : uint32_t CcuTempScatterNHRMem2Mem1D::virtRankId2RankId(const uint32_t virtRankId)
52 : {
53 0 : for (auto iter = tempVirtRankMap_.begin(); iter != tempVirtRankMap_.end(); iter++) {
54 0 : if (iter->second == virtRankId) {
55 0 : return iter->first;
56 : }
57 : }
58 0 : return 0;
59 : }
60 :
61 0 : u32 CcuTempScatterNHRMem2Mem1D::CalcScratchMultiple(BufferType input, BufferType output)
62 : {
63 : (void)input;
64 : (void)output;
65 0 : return tempRankSize_;
66 : }
67 :
68 0 : HcclResult CcuTempScatterNHRMem2Mem1D::GenExtIns(const TempFuncs &tempFuncs, TemplateDataParams &tempAlgParams,
69 : const ResLinks &tempLinks, std::vector<InsQuePtr> &tempInsQues)
70 : {
71 0 : CHK_PRT_RET(tempInsQues.empty(),
72 : HCCL_ERROR("[CcuInstructionScatterNHR1D] empty queue"), HcclResult::HCCL_E_INTERNAL);
73 0 : CHK_PTR_NULL(tempInsQues[0]);
74 0 : opMode_ = tempFuncs.opMode;
75 0 : uint64_t rootId = tempVirtRankMap_[rootId_];
76 0 : CcuInstructionScatterNHR1D ccuInsScatterNHR1D;
77 0 : std::vector<uint64_t> dimSize;
78 0 : dimSize.push_back(tempRankSize_);
79 0 : DataType dataType_ = op_.dataType;
80 0 : uint32_t axisSize = tempLinks.begin()->second.size();
81 0 : uint32_t myVirtRankId = tempVirtRankMap_[myRank_];
82 0 : uint64_t sliceSize = tempAlgParams.sliceSize;
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 0 : uint64_t inputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.inBuffType) + tempAlgParams.buffInfo.inBuffBaseOff;
87 0 : uint64_t outputAddr = BufferTypeToAddr(tempAlgParams.buffInfo.outBuffType) + tempAlgParams.buffInfo.outBuffBaseOff;
88 0 : uint64_t isOutputScratch = (tempAlgParams.buffInfo.outBuffType == BufferType::SCRATCH) ? 1 : 0;
89 : uint64_t scratchAddr
90 0 : = BufferTypeToAddr(tempAlgParams.buffInfo.scratBuffType) + tempAlgParams.buffInfo.scratchBuffBaseOff;
91 : uint64_t token;
92 0 : CHK_RET(GetToken(op_, token));
93 0 : uint64_t inputSliceStride = tempAlgParams.inputSliceStride;
94 0 : uint64_t outputSliceStride = tempAlgParams.outputSliceStride;
95 0 : uint64_t inputRepeatStride = tempAlgParams.inputRepeatStride;
96 0 : uint64_t outputRepeatStride = tempAlgParams.outputRepeatStride;
97 0 : uint64_t repeatNum = tempAlgParams.repeatNum;
98 0 : uint64_t repeatNumVar = UINT64_MAX - repeatNum;
99 :
100 0 : HCCL_INFO(
101 : "[CcuInstructionScatterNHR1D] dimSize[%llu], die0Size[%llu], die1Size[%llu], inputAddr[%llu], outputAddr[%llu],"
102 : "scratchAddr[%llu], repeatNum[%llu]",
103 : dimSize[0], die0Size, die1Size, inputAddr, outputAddr, scratchAddr, repeatNum);
104 :
105 0 : if (DataCount == 0) {
106 0 : HCCL_INFO("[CcuTempScatterNHRMem2Mem1D] DataCount == 0, Template Run Ends.");
107 0 : return HCCL_SUCCESS;
108 : }
109 :
110 0 : if (axisSize > 1 || die1Size == 0) {
111 0 : axisSize = 1;
112 : }
113 :
114 0 : std::vector<LinkData> linksDie0;
115 0 : std::vector<LinkData> linksDie1;
116 0 : RankGroup scatterRankGroup;
117 0 : std::map<u32, u32> indexMap;
118 0 : std::vector<NHRStepInfo> stepInfoVector;
119 0 : u32 nSteps = GetNHRStepNum(tempRankSize_);
120 0 : for (u32 step = 0; step < nSteps; step++) {
121 0 : NHRStepInfo stepInfo;
122 0 : CHK_RET(GetScatterStepInfo(step, nSteps, stepInfo));
123 0 : stepInfoVector.push_back(stepInfo);
124 0 : if (indexMap.count(stepInfo.fromRank) == 0 && stepInfo.rxSliceIdxs.size() > 0) {
125 0 : u32 fromRankIdx = virtRankId2RankId(stepInfo.fromRank);
126 0 : indexMap[stepInfo.fromRank] = linksDie0.size();
127 0 : linksDie0.push_back(tempLinks.at(fromRankIdx)[0]);
128 0 : if (axisSize > 1) {
129 0 : linksDie1.push_back(tempLinks.at(fromRankIdx)[1]);
130 : }
131 0 : scatterRankGroup.AddRank(fromRankIdx);
132 : }
133 0 : if (indexMap.count(stepInfo.toRank) == 0 && stepInfo.txSliceIdxs.size() > 0) {
134 0 : u32 toRankIdx = virtRankId2RankId(stepInfo.toRank);
135 0 : indexMap[stepInfo.toRank] = linksDie0.size();
136 0 : linksDie0.push_back(tempLinks.at(toRankIdx)[0]);
137 0 : if (axisSize > 1) {
138 0 : linksDie1.push_back(tempLinks.at(toRankIdx)[1]);
139 : }
140 0 : scatterRankGroup.AddRank(toRankIdx);
141 : }
142 0 : }
143 0 : scatterRankGroup.AddRank(myRank_);
144 :
145 0 : std::unique_ptr<CcuInsGroup> insGroupPtr = std::make_unique<CcuInsGroup>();
146 0 : for (uint32_t axisId = 0; axisId < axisSize; axisId++) { // 2个die上各一个mission
147 0 : CcuInstructionScatterNHR1D ccuInstruction;
148 0 : ccuInstruction.Init(myVirtRankId, rootId, inputAddr, outputAddr, scratchAddr, axisId, axisSize, sliceSize,
149 : die0Size, die1Size, inputSliceStride, outputSliceStride, inputRepeatStride,
150 0 : outputRepeatStride, repeatNum, repeatNumVar, stepInfoVector, indexMap, token, op_,
151 0 : tempVTopo_, isOutputScratch);
152 0 : HCCL_INFO(
153 : "[CcuTempScatterNHRMem2Mem1D] Run Init: myVirtRankId[%u], myRank_[%d], repeatNum[%u], inputAddr[%llu], "
154 : "outputAddr[%llu], inputSliceStride[%llu], outputSliceStride[%llu], "
155 : "inputRepeatStride[%llu], outputRepeatStride[%llu], die0Size[%llu], die1Size[%llu], "
156 : "repeatNumVar[%llu]",
157 : myVirtRankId, myRank_, repeatNum, inputAddr, outputAddr, inputSliceStride, outputSliceStride,
158 : inputRepeatStride, outputRepeatStride, die0Size, die1Size, repeatNumVar);
159 0 : ccuInstruction.SetLinks(axisId == 0 ? linksDie0 : linksDie1);
160 0 : ccuInstruction.SetRankGroup(scatterRankGroup);
161 0 : ccuInstruction.SetCntCkeNum(5); // 每个transport用5个CKE
162 0 : insGroupPtr->Append(std::move(std::make_unique<CcuInstructionScatterNHR1D>(ccuInstruction)));
163 0 : }
164 0 : tempInsQues[0]->Append(std::move(insGroupPtr)); // 只有一条流
165 0 : HCCL_INFO("[CcuTempScatterNHRMem2Mem1D] Template Run for all steps Ends.");
166 0 : return HcclResult::HCCL_SUCCESS;
167 0 : }
168 :
169 0 : HcclResult CcuTempScatterNHRMem2Mem1D::GetScatterStepInfo(u32 step, u32 nSteps, NHRStepInfo &stepInfo)
170 : {
171 0 : u32 virtRankIdx = tempVirtRankMap_[myRank_];
172 0 : stepInfo.txSliceIdxs.clear();
173 0 : stepInfo.rxSliceIdxs.clear();
174 0 : stepInfo.nSlices = 0;
175 0 : stepInfo.toRank = tempRankSize_;
176 0 : stepInfo.fromRank = tempRankSize_;
177 0 : stepInfo.step = step;
178 0 : stepInfo.myRank = virtRankIdx;
179 0 : uint32_t rootId = tempVirtRankMap_[rootId_];
180 0 : u32 deltaRoot = (rootId + tempRankSize_ - virtRankIdx) % tempRankSize_;
181 0 : u32 deltaRankPair = 1 << step;
182 : // 数据份数和数据编号增量
183 0 : u32 nSlices = (tempRankSize_ - 1 + (1 << step)) / (1 << (step + 1)); // 向上取整设计了下的
184 0 : u32 deltaSliceIndex = 1 << (step + 1);
185 : // 是否为2的幂
186 0 : u32 nRanks = 0;
187 0 : bool isPowerOfTwo = (tempRankSize_ & (tempRankSize_ - 1)) == 0;
188 0 : if (!isPowerOfTwo && step == nSteps - 1) {
189 0 : nRanks = tempRankSize_ - deltaRankPair;
190 : } else {
191 0 : nRanks = deltaRankPair;
192 : }
193 :
194 0 : if (deltaRoot < nRanks) { // 需要发
195 0 : u32 sendTo = (virtRankIdx + tempRankSize_ - deltaRankPair) % tempRankSize_;
196 0 : u32 txSliceIdx = sendTo;
197 0 : for (u32 i = 0; i < nSlices; i++) {
198 0 : u32 targetTxSliceIdx = txSliceIdx;
199 0 : stepInfo.txSliceIdxs.push_back(targetTxSliceIdx);
200 0 : txSliceIdx = (txSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
201 : }
202 0 : stepInfo.toRank = sendTo;
203 0 : stepInfo.nSlices = nSlices;
204 0 : } else if (deltaRoot >= deltaRankPair && deltaRoot < nRanks + deltaRankPair) { // 需要收
205 0 : u32 recvFrom = (virtRankIdx + deltaRankPair) % tempRankSize_;
206 0 : u32 rxSliceIdx = virtRankIdx;
207 0 : for (u32 i = 0; i < nSlices; i++) {
208 0 : u32 targetRxSliceIdx = rxSliceIdx;
209 0 : stepInfo.rxSliceIdxs.push_back(targetRxSliceIdx);
210 0 : rxSliceIdx = (rxSliceIdx + tempRankSize_ - deltaSliceIndex) % tempRankSize_;
211 : }
212 0 : stepInfo.fromRank = recvFrom;
213 0 : stepInfo.nSlices = nSlices;
214 : }
215 0 : return HcclResult::HCCL_SUCCESS;
216 : }
217 : } // namespace Hccl
|